-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_for_deploy.py
66 lines (56 loc) · 2.22 KB
/
prepare_for_deploy.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
from yapf.yapflib.yapf_api import FormatFile
from zipfile import ZipFile
import glob
from pathlib import Path
import shutil
import click
PATTERN = "*.py"
def get_ignored_objects(directory):
if not Path(directory / ".gitignore").exists():
return None
ignored_files = set()
ignored_files.add(Path(directory / ".git"))
ignored_files.add(Path(directory / ".gitignore"))
with open(Path(directory / ".gitignore")) as gitignore:
for line in gitignore:
line = line.strip()
if not line.startswith("#"):
for object_to_ignore in glob.glob(line):
obj_to_ignore_path = Path(directory / object_to_ignore)
ignored_files.add(obj_to_ignore_path)
if obj_to_ignore_path.is_dir():
for file in obj_to_ignore_path.iterdir():
ignored_files.add(file)
return ignored_files
def delete_directory(directory):
for obj in directory.iterdir():
if obj.is_dir():
delete_directory(obj)
else:
obj.unlink()
directory.rmdir()
@click.command()
@click.argument("directory", required=False, default='.')
@click.argument("archive_path", required=False, default='.')
def make_zip_with_file_for_deploy(directory,
archive_path):
directory = Path(directory)
archive_path = Path(archive_path)
ignored_objects = get_ignored_objects(directory)
py_files = set(directory.glob(PATTERN))
tmp_folder = Path(directory / "for_deploy")
tmp_folder.mkdir()
for obj in directory.iterdir():
if obj in py_files:
reformatted_code, encoding, changed = FormatFile(str(obj))
with open(Path(tmp_folder / obj.name), 'w') as py_file:
for line in reformatted_code.split('\n'):
py_file.write(line)
elif obj != tmp_folder and obj not in ignored_objects:
shutil.copyfile(obj, Path(tmp_folder / obj.name))
with ZipFile(Path(archive_path / "archive.zip"), 'w') as archive:
for obj in tmp_folder.iterdir():
archive.write(obj)
delete_directory(tmp_folder)
if __name__ == '__main__':
make_zip_with_file_for_deploy()