forked from LuffyTheFox/blender-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_whl.py
executable file
·129 lines (88 loc) · 3.55 KB
/
install_whl.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
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
#!/usr/bin/env python3.5
"""This Python script installs a new version of BAM here."""
import pathlib
my_dir = pathlib.Path(__file__).absolute().parent
def main():
import argparse
parser = argparse.ArgumentParser(description="This script installs a new version of BAM here.")
parser.add_argument('wheelfile', type=pathlib.Path,
help='Location of the wheel file to install.')
args = parser.parse_args()
install(args.wheelfile.expanduser())
def install(wheelfile: pathlib.Path):
import json
import os
import re
assert_is_zipfile(wheelfile)
wipe_preexisting()
print('Installing %s' % wheelfile)
target = my_dir / 'blender_bam-unpacked.whl'
print('Creating target directory %s' % target)
target.mkdir(parents=True)
extract(wheelfile, target)
copy_files(target)
version = find_version(target)
print('This is BAM version %s' % (version, ))
update_init_file(version)
print('Done installing %s' % wheelfile.name)
def assert_is_zipfile(wheelfile: pathlib.Path):
import zipfile
# In Python 3.6 conversion to str is not necessary any more:
if not zipfile.is_zipfile(str(wheelfile)):
log.error('%s is not a valid ZIP file!' % wheelfile)
raise SystemExit()
def wipe_preexisting():
import shutil
for existing in sorted(my_dir.glob('blender_bam-*.whl')):
if existing.is_dir():
print('Wiping pre-existing directory %s' % existing)
# In Python 3.6 conversion to str is not necessary any more:
shutil.rmtree(str(existing))
else:
print('Wiping pre-existing file %s' % existing)
existing.unlink()
def extract(wheelfile: pathlib.Path, target: pathlib.Path):
import os
import zipfile
# In Python 3.6 conversion to str is not necessary any more:
os.chdir(str(target))
print('Extracting wheel')
# In Python 3.6 conversion to str is not necessary any more:
with zipfile.ZipFile(str(wheelfile)) as whlzip:
whlzip.extractall()
os.chdir(str(my_dir))
def copy_files(target: pathlib.Path):
import shutil
print('Copying some files from wheel to other locations')
# In Python 3.6 conversion to str is not necessary any more:
shutil.copy(str(target / 'bam' / 'blend' / 'blendfile_path_walker.py'), './blend')
shutil.copy(str(target / 'bam' / 'blend' / 'blendfile.py'), './blend')
shutil.copy(str(target / 'bam' / 'utils' / 'system.py'), './utils')
def find_version(target: pathlib.Path):
import json
import shutil
print('Obtaining version number from wheel.')
distinfo = next(target.glob('*.dist-info'))
with (distinfo / 'metadata.json').open() as infofile:
metadata = json.load(infofile)
print('Wiping dist-info directory.')
shutil.rmtree(str(distinfo))
# "1.2.3" -> (1, 2, 3)
str_ver = metadata['version']
return tuple(int(x) for x in str_ver.split('.'))
def update_init_file(version: tuple):
import os
import re
print('Updating __init__.py to have the correct version.')
version_line_re = re.compile(r'^\s+[\'"]version[\'"]: (\([0-9,]+\)),')
with open('__init__.py', 'r') as infile, \
open('__init__.py~whl~installer~', 'w') as outfile:
for line in infile:
if version_line_re.match(line):
outfile.write(" 'version': %s,%s" % (version, os.linesep))
else:
outfile.write(line)
os.unlink('__init__.py')
os.rename('__init__.py~whl~installer~', '__init__.py')
if __name__ == '__main__':
main()