-
Notifications
You must be signed in to change notification settings - Fork 26
/
update_module_builder.py
98 lines (79 loc) · 3 KB
/
update_module_builder.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
"""
This script downloads and updates the module builder.
"""
from __future__ import print_function
import os
import sys
import zipfile
import shutil
if sys.version_info.major >= 3: # we are running Python 3.x
from io import BytesIO as StringIO
from urllib.request import urlopen
else:
# Import cStringIO in case it's available, since it's faster
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from urllib import urlopen
def download_submodule(author, module_name, dest_path, ignore_list):
""" Downloads a submodule from the given author and module name, and extracts
all files which are not on the ignore_list to the dest_path.
Example: download_submodule("tobspr", "RenderPipeline", ".", ["README.md", "LICENSE"])
"""
# Make directory, if it does not exist yet
if not os.path.isdir(dest_path):
os.makedirs(dest_path)
# Construct download url
source_url = "https://github.com/" + author + "/" + module_name + "/archive/master.zip"
prefix = module_name + "-master"
print("Fetching:", source_url)
# Download the zip
try:
usock = urlopen(source_url)
zip_data = usock.read()
usock.close()
except Exception as msg:
print("ERROR: Could not fetch module", module_name, "! Reason:", msg, file=sys.stderr)
sys.exit(2)
# Extract the zip
zip_ptr = StringIO(zip_data)
try:
zip_handle = zipfile.ZipFile(zip_ptr)
except zipfile.BadZipfile:
print("ERROR: Invalid zip file!", file=sys.stderr)
sys.exit(3)
if zip_handle.testzip() is not None:
print("ERROR: Invalid zip file checksums!", file=sys.stderr)
sys.exit(1)
num_files, num_dirs = 0, 0
for fname in zip_handle.namelist():
rel_name = fname.replace(prefix, "").replace("\\", "/").lstrip("/")
if not rel_name:
continue
is_file = not rel_name.endswith("/")
rel_name = dest_path.rstrip("/\\") + "/" + rel_name
# Files
if is_file:
for ignore in ignore_list:
if ignore in rel_name:
break
else:
with zip_handle.open(fname, "r") as source, open(rel_name, "wb") as dest:
shutil.copyfileobj(source, dest)
num_files += 1
# Directories
else:
if not os.path.isdir(rel_name):
os.makedirs(rel_name)
num_dirs += 1
print("Extracted", num_files, "files and", num_dirs, "directories")
if __name__ == "__main__":
ignore = ("__init__.py LICENSE README.md config.ini source/config_module.cpp "
"source/config_module.h .travis.yml").split()
curr_dir = os.path.dirname(os.path.realpath(__file__)); os.chdir(curr_dir);
download_submodule("tobspr", "P3DModuleBuilder", curr_dir, ignore)
with open("scripts/__init__.py", "w") as handle: pass
try: os.remove(".gitignore")
except: pass
os.rename("prefab.gitignore", ".gitignore")