Debian Codegraph @

util/make-site-package-dist.py

raw
1 #!/usr/bin/env python3
2 """Collect modules from site-packages used by DisplayCAL."""
3
4 import glob
5 import os
6 import shutil
7 import subprocess as sp
8 import sys
9 from distutils.sysconfig import get_python_lib
10
11 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
13 from DisplayCAL.meta import WX_MINVERSION
14
15 # Search for a suitable wx version
16 wx_minversion_str = ".".join(str(n) for n in WX_MINVERSION[:2])
17 wxversions_candidates = []
18 for pth in sys.path:
19 if not pth:
20 pth = "."
21 if os.path.isdir(pth):
22 for name in glob.glob(os.path.join(pth, "wx*")):
23 base = os.path.basename(name)
24 if os.path.isdir(name) and (
25 (
26 os.path.isdir(os.path.join(name, "wx"))
27 and base[3:] >= wx_minversion_str
28 )
29 or base == "wx"
30 ):
31 wxversions_candidates.append(name)
32 if not wxversions_candidates:
33 print("No wxPython versions >= %s found" % wx_minversion_str)
34 choice = 1
35 if len(wxversions_candidates) > 1:
36 print("Several wxPython versions >= %s found." % wx_minversion_str)
37 print("")
38 for i, pth in enumerate(wxversions_candidates):
39 print("%2i: %s" % (i + 1, os.path.basename(pth)))
40 print("")
41 while choice:
42 choice = input(
43 "Please select (1..%i) and press <ENTER>,\n"
44 "or press just <ENTER> to abort: " % len(wxversions_candidates)
45 )
46 if not choice:
47 sys.exit()
48 try:
49 choice = int(choice)
50 except ValueError:
51 print(
52 "Error: Only integers in range %i to %i allowed."
53 % (1, len(wxversions_candidates))
54 )
55 else:
56 break
57 wx_pth = wxversions_candidates[choice - 1]
58 print("Your choice: %s" % wx_pth)
59 if input("Press <ENTER> to continue, X <ENTER> to abort: ").upper() == "X":
60 sys.exit()
61 elif wxversions_candidates:
62 wx_pth = wxversions_candidates[0]
63 else:
64 wx_pth = "wx"
65
66 # Packages to collect
67 pkgs = {
68 "numpy": ["numpy"],
69 "pygame": ["pygame"],
70 "wx": [wx_pth],
71 "netifaces": ["netifaces"],
72 "google.protobuf": ["google.protobuf"],
73 "pychromecast": ["pychromecast"],
74 "requests": ["requests"],
75 "zeroconf": ["zeroconf"],
76 }
77 if os.path.isdir(os.path.join(wx_pth, "wx")):
78 # Not Phoenix
79 sys.path.insert(0, wx_pth)
80 pkgs["wx"].append("wx_version")
81 if sys.platform == "win32":
82 pkgs["wmi"] = ["wmi"]
83 if sys.platform == "darwin":
84 pkgs["pyglet"] = ["pyglet"]
85 pkgs["pygame"].extend(
86 [
87 "/Library/Frameworks/SDL_image.framework",
88 "/Library/Frameworks/SDL_mixer.framework",
89 "/Library/Frameworks/SDL_ttf.framework",
90 "/Library/Frameworks/SDL.framework",
91 ]
92 )
93 pkgs["wx"].extend(
94 glob.glob(
95 os.path.normpath(
96 os.path.join(os.path.dirname(wx_pth), "..", "..", "libwx_*.dylib")
97 )
98 )
99 )
100
101 dylibs = []
102
103
104 def copy(src, dst):
105 if os.path.islink(src):
106 os.symlink(os.readlink(src), dst)
107 else:
108 shutil.copy(src, dst)
109 name, ext = os.path.splitext(os.path.basename(dst))
110 if sys.platform == "darwin":
111 # Fixup loader path
112 if ext == ".so":
113 for dylib in dylibs:
114 args = [
115 "install_name_tool",
116 "-change",
117 dylib,
118 "@loader_path/../../" + os.path.basename(dylib),
119 dst,
120 ]
121 print(sp.list2cmdline(args))
122 sp.call(args)
123 elif ext == ".dylib":
124 args = [
125 "install_name_tool",
126 "-id",
127 "@loader_path/../../" + os.path.basename(src),
128 dst,
129 ]
130 print(sp.list2cmdline(args))
131 sp.call(args)
132
133
134 # Collect packages
135 python_lib = get_python_lib(True)
136 for pkg_name in pkgs:
137 data = pkgs[pkg_name]
138 print("Checking for package: %s" % pkg_name)
139 dylibs = filter(lambda entry: entry.endswith(".dylib"), data)
140 fromlist = pkg_name.split(".")
141 try:
142 pkg = __import__(pkg_name, fromlist=fromlist)
143 except ImportError as exception:
144 print(exception)
145 continue
146 version = getattr(
147 pkg, "__version__", getattr(pkg, "version", getattr(pkg, "VERSION", "UNKNOWN"))
148 )
149 if isinstance(version, tuple):
150 version = ".".join(str(item) for item in version)
151 dist_dir = os.path.join(
152 "dist",
153 "%s-%s-%s-py%i.%i"
154 % (pkg_name, version, sys.platform, sys.version_info[0], sys.version_info[1]),
155 )
156 print("Destination: %s" % dist_dir)
157 if not os.path.isdir(dist_dir):
158 os.makedirs(dist_dir)
159 for entry in data:
160 pth = entry
161 if not os.path.isabs(pth):
162 pth = os.path.join(python_lib, pth)
163 if pth.lower().endswith(".pth"):
164 with open(pth) as pthfile:
165 pth = os.path.join(os.path.dirname(pth), pthfile.read().strip())
166 if not os.path.exists(pth):
167 print(" Checking for module: %s" % pth)
168 fromlist = entry.split(".")
169 try:
170 module = __import__(entry, fromlist=fromlist)
171 except ImportError as exception:
172 print(exception)
173 continue
174 filename, ext = os.path.splitext(module.__file__)
175 if os.path.basename(filename) == "__init__":
176 pth = os.path.dirname(filename)
177 if not os.path.isdir(pth):
178 while not os.path.isfile(pth):
179 pth = os.path.dirname(pth)
180 print(" Checking for %s" % pth)
181 if not os.path.isfile(pth):
182 print(" Warning: Module not found: %s" % entry)
183 continue
184 else:
185 if ext in (".pyc", ".pyo"):
186 ext = ".py"
187 pth = "%s%s" % (filename, ext)
188 if os.path.isdir(pth):
189 print(" Collecting package: %s" % pth)
190 dirname = os.path.dirname(pth)
191 for dirpath, _dirnames, filenames in os.walk(pth):
192 for filename in filenames:
193 src = os.path.join(dirpath, filename)
194 if filename in ("unins000.exe", "unins000.dat"):
195 print(" Skipping %s" % src)
196 continue
197 name, ext = os.path.splitext(filename)
198 if ext in (".pyc", ".pyo"):
199 continue
200 dst_dir = os.path.join(dist_dir, os.path.relpath(dirpath, dirname))
201 if not os.path.isdir(dst_dir):
202 os.makedirs(dst_dir)
203 dst = os.path.join(dst_dir, filename)
204 if not os.path.isfile(dst):
205 print(" Collecting file: %s" % src)
206 copy(src, dst)
207 else:
208 dst = os.path.join(dist_dir, os.path.basename(pth))
209 if not os.path.isfile(dst):
210 print(" Collecting file: %s" % pth)
211 copy(pth, dst)