-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
51 lines (38 loc) · 1.46 KB
/
compiler.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
__author__ = 'Khiem Doan'
__github__ = 'https://github.com/khiemdoan'
__email__ = '[email protected]'
from argparse import ArgumentParser
from pathlib import Path
from py_compile import compile
from shutil import copyfile
def compile_file(src_file: Path, dest_file: Path) -> None:
dest_file.parent.mkdir(parents=True, exist_ok=True)
try:
if src_file.suffix.lower() == '.py':
print(f'{src_file} -> {dest_file}c')
compile(src_file, f'{dest_file}c')
else:
print(f'{src_file} -> {dest_file}')
copyfile(src_file, dest_file)
except Exception:
return
def main() -> None:
"""Script main program."""
parser = ArgumentParser(description='Utilities to compile *.py to *.pyc.')
parser.add_argument('src', type=str, help='source file or directory to compile')
parser.add_argument('dest', type=str, help='destination file or directory')
args = parser.parse_args()
print(args)
src_path = Path(args.src)
dest_path = Path(args.dest)
if src_path.is_file():
dest_path = Path(args.dest)
compile_file(src_path, dest_path)
if src_path.is_dir():
dest_path.mkdir(parents=True, exist_ok=True)
for src_file_path in src_path.glob('**/*'):
relative = src_file_path.relative_to(src_path)
dest_file_path = dest_path / relative
compile_file(src_file_path, dest_file_path)
if __name__ == '__main__':
main()