This repository has been archived by the owner on Jun 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hw01.py
78 lines (60 loc) · 2.26 KB
/
hw01.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from abc import ABC, abstractmethod
from logging import INFO, basicConfig, error, info, warning
from pathlib import Path
from shutil import copy
from sys import argv
from threading import Semaphore, Thread
class Worker(ABC, Thread):
_args: list[Path]
def __init__(self,
source: Path,
target: Path,
semaphore: Semaphore) -> None:
super().__init__(name=f'{self.__class__.__name__} of {source}',
args=(source, target))
self._semaphore = semaphore
self.start()
@abstractmethod
def _action(self) -> None:
...
def run(self) -> None:
with self._semaphore:
self._action()
class Copyist(Worker):
def _action(self) -> None:
info(f'Copying the file to folder "{self._args[1]}"...')
self._args[1].mkdir(parents=True, exist_ok=True)
copy(*self._args)
class Scanner(Worker):
def _action(self) -> None:
for source in self._args[0].iterdir():
if source.is_dir():
Scanner(source, self._args[1], self._semaphore)
else:
target = self._args[1] / source.suffix[1:] / source.name
if target.exists():
warning(f'Skip file "{source.name}".')
else:
Copyist(source, target.parent, self._semaphore)
def main() -> None:
basicConfig(format='%(threadName)s\n%(levelname)s: %(message)s\n',
level=INFO)
if len(argv) < 2:
error('Specify the source folder as the first argument of the script.')
else:
source = Path(argv[1])
if source.exists():
if source.is_dir():
target = Path(argv[2] if len(argv) > 2 else 'dist')
if target.exists() and not target.is_dir():
error(f'The target "{target}" is not a folder.')
else:
info(f'Copying files from folder "{source}" to '
f'"{target}"...')
Scanner(source, target, Semaphore(10))
else:
error(f'The source "{source}" is not a folder.')
else:
error(f'The source folder "{source}" not found.')
if __name__ == '__main__':
main()