forked from RBVI/ChimeraX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clean_app.py
60 lines (52 loc) · 1.9 KB
/
clean_app.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
#
# vi:set expandtab shiftwidth=4:
#
# Post-install cleanup up of application directory
#
import sys
import os
import shutil
def clean_app(chimerax_root):
"""Clean application tree for distribution"""
remove_pycache(chimerax_root)
# TODO: figure this out
# removing scripts makes multiple "make installs" without
# a "make clean" inbetween fail and removes scripts that
# would be needed for developers to make documentation
# remove_scripts(chimerax_root)
def remove_pycache(chimerax_root):
"""remove unwanted __pycache__ directories"""
# cleanup -- remove __pycache__ directories
cache_dirs = []
for root, dirs, files in os.walk(chimerax_root):
if "__pycache__" in dirs:
cache_dirs.append(os.path.join(root, "__pycache__"))
for d in cache_dirs:
shutil.rmtree(d)
def remove_scripts(chimerax_root):
"""remove script's whose interpreter is not a system binary
(eg., Python scripts with paths to "nonexisting" python)
"""
# cleanup -- remove python shell scripts
if sys.platform.startswith('win'):
shutil.rmtree(f'{chimerax_root}/bin/Scripts')
else:
for fn in os.listdir(f'{chimerax_root}/bin'):
filename = f'{chimerax_root}/bin/{fn}'
if not os.path.isfile(filename):
continue
with open(filename, 'rb') as f:
header = f.read(64)
if header[0:2] != b'#!':
continue
program = header[2:].lstrip()
if program.startswith(b'/bin/') or \
program.startswith(b'/usr/bin/'):
continue
os.remove(filename)
if __name__ == "__main__":
bin_dir = os.path.dirname(sys.executable)
if os.path.basename(bin_dir) != 'bin':
print("Must be called as CHIMERAX/bin/python", file=sys.__stderr__)
raise SystemExit(1)
clean_app(os.path.dirname(bin_dir))