-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_sd.py
More file actions
143 lines (113 loc) · 3.78 KB
/
Copy pathpackage_sd.py
File metadata and controls
143 lines (113 loc) · 3.78 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python3
import argparse
import os
import shutil
import subprocess
import sys
import tempfile
from pathlib import Path
SEVEN_ZIP_ARGS = [
"-t7z",
"-ms=off",
"-m0=LZMA:d=14",
]
def find_7z(explicit_path=None):
if explicit_path:
candidate = Path(explicit_path).expanduser()
if candidate.is_file():
return str(candidate)
raise FileNotFoundError("7-Zip executable not found: {}".format(candidate))
for name in ("7z", "7zz", "7za", "7zr"):
found = shutil.which(name)
if found:
return found
if os.name == "nt":
for env_name in ("ProgramFiles", "ProgramFiles(x86)"):
program_files = os.environ.get(env_name)
if program_files:
candidate = Path(program_files) / "7-Zip" / "7z.exe"
if candidate.is_file():
return str(candidate)
raise FileNotFoundError(
"Could not find 7-Zip. Install 7-Zip, add 7z.exe to PATH, "
"or pass --seven-zip /path/to/7z."
)
def is_relative_to(path, parent):
try:
path.relative_to(parent)
return True
except ValueError:
return False
def should_package(item):
if item.name == "__pycache__":
return False
if item.is_file() and item.suffix == ".pyc":
return False
return True
def build_package(source_dir, output_file, seven_zip):
source_dir = Path(source_dir).resolve()
output_file = Path(output_file).resolve()
if not source_dir.is_dir():
raise NotADirectoryError("Source folder does not exist: {}".format(source_dir))
source_items = [item for item in source_dir.iterdir() if should_package(item)]
if not source_items:
raise ValueError("Source folder is empty: {}".format(source_dir))
if is_relative_to(output_file, source_dir):
raise ValueError("Output file must be outside the source folder.")
output_file.parent.mkdir(parents=True, exist_ok=True)
source_names = sorted(item.name for item in source_items)
with tempfile.TemporaryDirectory(prefix="sd-package-") as temp_dir:
temp_output = Path(temp_dir) / output_file.name
command = [
seven_zip,
"a",
"-y",
*SEVEN_ZIP_ARGS,
str(temp_output),
*source_names,
]
subprocess.run(command, cwd=str(source_dir), check=True)
if output_file.exists():
output_file.unlink()
shutil.move(str(temp_output), str(output_file))
return output_file
def parse_args(argv):
parser = argparse.ArgumentParser(
description=(
"Package the contents of an unpacked ArcGIS service definition folder "
"into a .sd file using 7-Zip."
)
)
parser.add_argument(
"source_dir",
help="Folder whose contents should become the root of the .sd archive.",
)
parser.add_argument(
"output_file",
nargs="?",
help="Output .sd path. Defaults to <source_dir>.sd next to the folder.",
)
parser.add_argument(
"--seven-zip",
dest="seven_zip",
help="Path to 7z.exe if it is not available on PATH.",
)
return parser.parse_args(argv)
def main(argv=None):
args = parse_args(argv or sys.argv[1:])
source_dir = Path(args.source_dir)
output_file = (
Path(args.output_file)
if args.output_file
else source_dir.parent / "{}.sd".format(source_dir.name)
)
try:
seven_zip = find_7z(args.seven_zip)
packaged = build_package(source_dir, output_file, seven_zip)
except Exception as exc:
print("ERROR: {}".format(exc), file=sys.stderr)
return 1
print("Created {}".format(packaged))
return 0
if __name__ == "__main__":
raise SystemExit(main())