/
copier.py
40 lines (30 loc) · 1.18 KB
/
copier.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from typing import NewType, Tuple
import stat
import cli_ui as ui
from path import Path
import tsrc.executor
Copy = NewType("Copy", Tuple[str, str])
class FileCopier(tsrc.executor.Task[Copy]):
def __init__(self, workspace_path: Path) -> None:
self.workspace_path = workspace_path
def on_start(self, *, num_items: int) -> None:
ui.info_1("Copying files")
def on_failure(self, *, num_errors: int) -> None:
ui.error("Failed to perform the following copies:")
def display_item(self, item: Copy) -> str:
src, dest = item
return "%s -> %s" % (src, dest)
def process(self, index: int, count: int, item: Copy) -> None:
src, dest = item
ui.info_count(index, count, src, "->", dest)
try:
src_path = self.workspace_path / src
dest_path = self.workspace_path / dest
if dest_path.exists():
# Re-set the write permissions on the file:
dest_path.chmod(stat.S_IWRITE)
src_path.copy(dest_path)
# Make sure perms are read only for everyone
dest_path.chmod(0o10444)
except Exception as e:
raise tsrc.Error(str(e))