##// END OF EJS Templates
pyoxidizer: add the user site to `sys.path` on macOS...
Matt Harbison -
r49031:e10f5dc7 default
parent child Browse files
Show More
@@ -1,321 +1,335 b''
1 # The following variables can be passed in as parameters:
1 # The following variables can be passed in as parameters:
2 #
2 #
3 # VERSION
3 # VERSION
4 # Version string of program being produced.
4 # Version string of program being produced.
5 #
5 #
6 # MSI_NAME
6 # MSI_NAME
7 # Root name of MSI installer.
7 # Root name of MSI installer.
8 #
8 #
9 # EXTRA_MSI_FEATURES
9 # EXTRA_MSI_FEATURES
10 # ; delimited string of extra features to advertise in the built MSA.
10 # ; delimited string of extra features to advertise in the built MSA.
11 #
11 #
12 # SIGNING_PFX_PATH
12 # SIGNING_PFX_PATH
13 # Path to code signing certificate to use.
13 # Path to code signing certificate to use.
14 #
14 #
15 # SIGNING_PFX_PASSWORD
15 # SIGNING_PFX_PASSWORD
16 # Password to code signing PFX file defined by SIGNING_PFX_PATH.
16 # Password to code signing PFX file defined by SIGNING_PFX_PATH.
17 #
17 #
18 # SIGNING_SUBJECT_NAME
18 # SIGNING_SUBJECT_NAME
19 # String fragment in code signing certificate subject name used to find
19 # String fragment in code signing certificate subject name used to find
20 # code signing certificate in Windows certificate store.
20 # code signing certificate in Windows certificate store.
21 #
21 #
22 # TIME_STAMP_SERVER_URL
22 # TIME_STAMP_SERVER_URL
23 # URL of time-stamp token authority (RFC 3161) servers to stamp code signatures.
23 # URL of time-stamp token authority (RFC 3161) servers to stamp code signatures.
24
24
25 ROOT = CWD + "/../.."
25 ROOT = CWD + "/../.."
26
26
27 VERSION = VARS.get("VERSION", "5.8")
27 VERSION = VARS.get("VERSION", "5.8")
28 MSI_NAME = VARS.get("MSI_NAME", "mercurial")
28 MSI_NAME = VARS.get("MSI_NAME", "mercurial")
29 EXTRA_MSI_FEATURES = VARS.get("EXTRA_MSI_FEATURES")
29 EXTRA_MSI_FEATURES = VARS.get("EXTRA_MSI_FEATURES")
30 SIGNING_PFX_PATH = VARS.get("SIGNING_PFX_PATH")
30 SIGNING_PFX_PATH = VARS.get("SIGNING_PFX_PATH")
31 SIGNING_PFX_PASSWORD = VARS.get("SIGNING_PFX_PASSWORD", "")
31 SIGNING_PFX_PASSWORD = VARS.get("SIGNING_PFX_PASSWORD", "")
32 SIGNING_SUBJECT_NAME = VARS.get("SIGNING_SUBJECT_NAME")
32 SIGNING_SUBJECT_NAME = VARS.get("SIGNING_SUBJECT_NAME")
33 TIME_STAMP_SERVER_URL = VARS.get("TIME_STAMP_SERVER_URL", "http://timestamp.digicert.com")
33 TIME_STAMP_SERVER_URL = VARS.get("TIME_STAMP_SERVER_URL", "http://timestamp.digicert.com")
34
34
35 IS_WINDOWS = "windows" in BUILD_TARGET_TRIPLE
35 IS_WINDOWS = "windows" in BUILD_TARGET_TRIPLE
36 IS_MACOS = "darwin" in BUILD_TARGET_TRIPLE
36 IS_MACOS = "darwin" in BUILD_TARGET_TRIPLE
37
37
38 # Code to run in Python interpreter.
38 # Code to run in Python interpreter.
39 RUN_CODE = """
39 RUN_CODE = """
40 import os
40 import os
41 import sys
41 import sys
42 extra_path = os.environ.get('PYTHONPATH')
42 extra_path = os.environ.get('PYTHONPATH')
43 if extra_path is not None:
43 if extra_path is not None:
44 # extensions and hooks expect a working python environment
44 # extensions and hooks expect a working python environment
45 # We do not prepend the values because the Mercurial library wants to be in
45 # We do not prepend the values because the Mercurial library wants to be in
46 # the front of the sys.path to avoid picking up other installations.
46 # the front of the sys.path to avoid picking up other installations.
47 sys.path.extend(extra_path.split(os.pathsep))
47 sys.path.extend(extra_path.split(os.pathsep))
48 # Add user site to sys.path to load extensions without the full path
48 # Add user site to sys.path to load extensions without the full path
49 if os.name == 'nt':
49 if os.name == 'nt':
50 vi = sys.version_info
50 vi = sys.version_info
51 appdata = os.environ.get('APPDATA')
51 appdata = os.environ.get('APPDATA')
52 if appdata:
52 if appdata:
53 sys.path.append(
53 sys.path.append(
54 os.path.join(
54 os.path.join(
55 appdata,
55 appdata,
56 'Python',
56 'Python',
57 'Python%d%d' % (vi[0], vi[1]),
57 'Python%d%d' % (vi[0], vi[1]),
58 'site-packages',
58 'site-packages',
59 )
59 )
60 )
60 )
61 elif sys.platform == "darwin":
62 vi = sys.version_info
63
64 def joinuser(*args):
65 return os.path.expanduser(os.path.join(*args))
66
67 # Note: site.py uses `sys._framework` instead of hardcoding "Python" as the
68 # 3rd arg, but that is set to an empty string in an oxidized binary. It
69 # has a fallback to ~/.local when `sys._framework` isn't set, but we want
70 # to match what the system python uses, so it sees pip installed stuff.
71 usersite = joinuser("~", "Library", "Python",
72 "%d.%d" % vi[:2], "lib/python/site-packages")
73
74 sys.path.append(usersite)
61 import hgdemandimport;
75 import hgdemandimport;
62 hgdemandimport.enable();
76 hgdemandimport.enable();
63 from mercurial import dispatch;
77 from mercurial import dispatch;
64 dispatch.run();
78 dispatch.run();
65 """
79 """
66
80
67 set_build_path(ROOT + "/build/pyoxidizer")
81 set_build_path(ROOT + "/build/pyoxidizer")
68
82
69 def make_distribution():
83 def make_distribution():
70 return default_python_distribution(python_version = "3.9")
84 return default_python_distribution(python_version = "3.9")
71
85
72 def resource_callback(policy, resource):
86 def resource_callback(policy, resource):
73 if not (IS_WINDOWS or IS_MACOS):
87 if not (IS_WINDOWS or IS_MACOS):
74 resource.add_location = "in-memory"
88 resource.add_location = "in-memory"
75 return
89 return
76
90
77 # We use a custom resource routing policy to influence where things are loaded
91 # We use a custom resource routing policy to influence where things are loaded
78 # from.
92 # from.
79 #
93 #
80 # For Python modules and resources, we load from memory if they are in
94 # For Python modules and resources, we load from memory if they are in
81 # the standard library and from the filesystem if not. This is because
95 # the standard library and from the filesystem if not. This is because
82 # parts of Mercurial and some 3rd party packages aren't yet compatible
96 # parts of Mercurial and some 3rd party packages aren't yet compatible
83 # with memory loading.
97 # with memory loading.
84 #
98 #
85 # For Python extension modules, we load from the filesystem because
99 # For Python extension modules, we load from the filesystem because
86 # this yields greatest compatibility.
100 # this yields greatest compatibility.
87 if type(resource) in ("PythonModuleSource", "PythonPackageResource", "PythonPackageDistributionResource"):
101 if type(resource) in ("PythonModuleSource", "PythonPackageResource", "PythonPackageDistributionResource"):
88 if resource.is_stdlib:
102 if resource.is_stdlib:
89 resource.add_location = "in-memory"
103 resource.add_location = "in-memory"
90 else:
104 else:
91 resource.add_location = "filesystem-relative:lib"
105 resource.add_location = "filesystem-relative:lib"
92
106
93 elif type(resource) == "PythonExtensionModule":
107 elif type(resource) == "PythonExtensionModule":
94 resource.add_location = "filesystem-relative:lib"
108 resource.add_location = "filesystem-relative:lib"
95
109
96 def make_exe(dist):
110 def make_exe(dist):
97 """Builds a Rust-wrapped Mercurial binary."""
111 """Builds a Rust-wrapped Mercurial binary."""
98 packaging_policy = dist.make_python_packaging_policy()
112 packaging_policy = dist.make_python_packaging_policy()
99
113
100 # Extension may depend on any Python functionality. Include all
114 # Extension may depend on any Python functionality. Include all
101 # extensions.
115 # extensions.
102 packaging_policy.extension_module_filter = "all"
116 packaging_policy.extension_module_filter = "all"
103 packaging_policy.resources_location = "in-memory"
117 packaging_policy.resources_location = "in-memory"
104 if IS_WINDOWS or IS_MACOS:
118 if IS_WINDOWS or IS_MACOS:
105 packaging_policy.resources_location_fallback = "filesystem-relative:lib"
119 packaging_policy.resources_location_fallback = "filesystem-relative:lib"
106 packaging_policy.register_resource_callback(resource_callback)
120 packaging_policy.register_resource_callback(resource_callback)
107
121
108 config = dist.make_python_interpreter_config()
122 config = dist.make_python_interpreter_config()
109 config.allocator_backend = "default"
123 config.allocator_backend = "default"
110 config.run_command = RUN_CODE
124 config.run_command = RUN_CODE
111
125
112 # We want to let the user load extensions from the file system
126 # We want to let the user load extensions from the file system
113 config.filesystem_importer = True
127 config.filesystem_importer = True
114
128
115 # We need this to make resourceutil happy, since it looks for sys.frozen.
129 # We need this to make resourceutil happy, since it looks for sys.frozen.
116 config.sys_frozen = True
130 config.sys_frozen = True
117 config.legacy_windows_stdio = True
131 config.legacy_windows_stdio = True
118
132
119 exe = dist.to_python_executable(
133 exe = dist.to_python_executable(
120 name = "hg",
134 name = "hg",
121 packaging_policy = packaging_policy,
135 packaging_policy = packaging_policy,
122 config = config,
136 config = config,
123 )
137 )
124
138
125 # Add Mercurial to resources.
139 # Add Mercurial to resources.
126 exe.add_python_resources(exe.pip_install(["--verbose", ROOT]))
140 exe.add_python_resources(exe.pip_install(["--verbose", ROOT]))
127
141
128 # On Windows, we install extra packages for convenience.
142 # On Windows, we install extra packages for convenience.
129 if IS_WINDOWS:
143 if IS_WINDOWS:
130 exe.add_python_resources(
144 exe.add_python_resources(
131 exe.pip_install(["-r", ROOT + "/contrib/packaging/requirements-windows-py3.txt"]),
145 exe.pip_install(["-r", ROOT + "/contrib/packaging/requirements-windows-py3.txt"]),
132 )
146 )
133 extra_packages = VARS.get("extra_py_packages", "")
147 extra_packages = VARS.get("extra_py_packages", "")
134 if extra_packages:
148 if extra_packages:
135 for extra in extra_packages.split(","):
149 for extra in extra_packages.split(","):
136 extra_src, pkgs = extra.split("=")
150 extra_src, pkgs = extra.split("=")
137 pkgs = pkgs.split(":")
151 pkgs = pkgs.split(":")
138 exe.add_python_resources(exe.read_package_root(extra_src, pkgs))
152 exe.add_python_resources(exe.read_package_root(extra_src, pkgs))
139
153
140 return exe
154 return exe
141
155
142 def make_manifest(dist, exe):
156 def make_manifest(dist, exe):
143 m = FileManifest()
157 m = FileManifest()
144 m.add_python_resource(".", exe)
158 m.add_python_resource(".", exe)
145
159
146 return m
160 return m
147
161
148
162
149 # This adjusts the InstallManifest produced from exe generation to provide
163 # This adjusts the InstallManifest produced from exe generation to provide
150 # additional files found in a Windows install layout.
164 # additional files found in a Windows install layout.
151 def make_windows_install_layout(manifest):
165 def make_windows_install_layout(manifest):
152 # Copy various files to new install locations. This can go away once
166 # Copy various files to new install locations. This can go away once
153 # we're using the importlib resource reader.
167 # we're using the importlib resource reader.
154 RECURSIVE_COPIES = {
168 RECURSIVE_COPIES = {
155 "lib/mercurial/locale/": "locale/",
169 "lib/mercurial/locale/": "locale/",
156 "lib/mercurial/templates/": "templates/",
170 "lib/mercurial/templates/": "templates/",
157 }
171 }
158 for (search, replace) in RECURSIVE_COPIES.items():
172 for (search, replace) in RECURSIVE_COPIES.items():
159 for path in manifest.paths():
173 for path in manifest.paths():
160 if path.startswith(search):
174 if path.startswith(search):
161 new_path = path.replace(search, replace)
175 new_path = path.replace(search, replace)
162 print("copy %s to %s" % (path, new_path))
176 print("copy %s to %s" % (path, new_path))
163 file = manifest.get_file(path)
177 file = manifest.get_file(path)
164 manifest.add_file(file, path = new_path)
178 manifest.add_file(file, path = new_path)
165
179
166 # Similar to above, but with filename pattern matching.
180 # Similar to above, but with filename pattern matching.
167 # lib/mercurial/helptext/**/*.txt -> helptext/
181 # lib/mercurial/helptext/**/*.txt -> helptext/
168 # lib/mercurial/defaultrc/*.rc -> defaultrc/
182 # lib/mercurial/defaultrc/*.rc -> defaultrc/
169 for path in manifest.paths():
183 for path in manifest.paths():
170 if path.startswith("lib/mercurial/helptext/") and path.endswith(".txt"):
184 if path.startswith("lib/mercurial/helptext/") and path.endswith(".txt"):
171 new_path = path[len("lib/mercurial/"):]
185 new_path = path[len("lib/mercurial/"):]
172 elif path.startswith("lib/mercurial/defaultrc/") and path.endswith(".rc"):
186 elif path.startswith("lib/mercurial/defaultrc/") and path.endswith(".rc"):
173 new_path = path[len("lib/mercurial/"):]
187 new_path = path[len("lib/mercurial/"):]
174 else:
188 else:
175 continue
189 continue
176
190
177 print("copying %s to %s" % (path, new_path))
191 print("copying %s to %s" % (path, new_path))
178 manifest.add_file(manifest.get_file(path), path = new_path)
192 manifest.add_file(manifest.get_file(path), path = new_path)
179
193
180 extra_install_files = VARS.get("extra_install_files", "")
194 extra_install_files = VARS.get("extra_install_files", "")
181 if extra_install_files:
195 if extra_install_files:
182 for extra in extra_install_files.split(","):
196 for extra in extra_install_files.split(","):
183 print("adding extra files from %s" % extra)
197 print("adding extra files from %s" % extra)
184 # TODO: I expected a ** glob to work, but it didn't.
198 # TODO: I expected a ** glob to work, but it didn't.
185 #
199 #
186 # TODO: I know this has forward-slash paths. As far as I can tell,
200 # TODO: I know this has forward-slash paths. As far as I can tell,
187 # backslashes don't ever match glob() expansions in
201 # backslashes don't ever match glob() expansions in
188 # tugger-starlark, even on Windows.
202 # tugger-starlark, even on Windows.
189 manifest.add_manifest(glob(include=[extra + "/*/*"], strip_prefix=extra+"/"))
203 manifest.add_manifest(glob(include=[extra + "/*/*"], strip_prefix=extra+"/"))
190
204
191 # We also install a handful of additional files.
205 # We also install a handful of additional files.
192 EXTRA_CONTRIB_FILES = [
206 EXTRA_CONTRIB_FILES = [
193 "bash_completion",
207 "bash_completion",
194 "hgweb.fcgi",
208 "hgweb.fcgi",
195 "hgweb.wsgi",
209 "hgweb.wsgi",
196 "logo-droplets.svg",
210 "logo-droplets.svg",
197 "mercurial.el",
211 "mercurial.el",
198 "mq.el",
212 "mq.el",
199 "tcsh_completion",
213 "tcsh_completion",
200 "tcsh_completion_build.sh",
214 "tcsh_completion_build.sh",
201 "xml.rnc",
215 "xml.rnc",
202 "zsh_completion",
216 "zsh_completion",
203 ]
217 ]
204
218
205 for f in EXTRA_CONTRIB_FILES:
219 for f in EXTRA_CONTRIB_FILES:
206 manifest.add_file(FileContent(path = ROOT + "/contrib/" + f), directory = "contrib")
220 manifest.add_file(FileContent(path = ROOT + "/contrib/" + f), directory = "contrib")
207
221
208 # Individual files with full source to destination path mapping.
222 # Individual files with full source to destination path mapping.
209 EXTRA_FILES = {
223 EXTRA_FILES = {
210 "contrib/hgk": "contrib/hgk.tcl",
224 "contrib/hgk": "contrib/hgk.tcl",
211 "contrib/win32/postinstall.txt": "ReleaseNotes.txt",
225 "contrib/win32/postinstall.txt": "ReleaseNotes.txt",
212 "contrib/win32/ReadMe.html": "ReadMe.html",
226 "contrib/win32/ReadMe.html": "ReadMe.html",
213 "doc/style.css": "doc/style.css",
227 "doc/style.css": "doc/style.css",
214 "COPYING": "Copying.txt",
228 "COPYING": "Copying.txt",
215 }
229 }
216
230
217 for source, dest in EXTRA_FILES.items():
231 for source, dest in EXTRA_FILES.items():
218 print("adding extra file %s" % dest)
232 print("adding extra file %s" % dest)
219 manifest.add_file(FileContent(path = ROOT + "/" + source), path = dest)
233 manifest.add_file(FileContent(path = ROOT + "/" + source), path = dest)
220
234
221 # And finally some wildcard matches.
235 # And finally some wildcard matches.
222 manifest.add_manifest(glob(
236 manifest.add_manifest(glob(
223 include = [ROOT + "/contrib/vim/*"],
237 include = [ROOT + "/contrib/vim/*"],
224 strip_prefix = ROOT + "/"
238 strip_prefix = ROOT + "/"
225 ))
239 ))
226 manifest.add_manifest(glob(
240 manifest.add_manifest(glob(
227 include = [ROOT + "/doc/*.html"],
241 include = [ROOT + "/doc/*.html"],
228 strip_prefix = ROOT + "/"
242 strip_prefix = ROOT + "/"
229 ))
243 ))
230
244
231 # But we don't ship hg-ssh on Windows, so exclude its documentation.
245 # But we don't ship hg-ssh on Windows, so exclude its documentation.
232 manifest.remove("doc/hg-ssh.8.html")
246 manifest.remove("doc/hg-ssh.8.html")
233
247
234 return manifest
248 return manifest
235
249
236
250
237 def make_msi(manifest):
251 def make_msi(manifest):
238 manifest = make_windows_install_layout(manifest)
252 manifest = make_windows_install_layout(manifest)
239
253
240 if "x86_64" in BUILD_TARGET_TRIPLE:
254 if "x86_64" in BUILD_TARGET_TRIPLE:
241 platform = "x64"
255 platform = "x64"
242 else:
256 else:
243 platform = "x86"
257 platform = "x86"
244
258
245 manifest.add_file(
259 manifest.add_file(
246 FileContent(path = ROOT + "/contrib/packaging/wix/COPYING.rtf"),
260 FileContent(path = ROOT + "/contrib/packaging/wix/COPYING.rtf"),
247 path = "COPYING.rtf",
261 path = "COPYING.rtf",
248 )
262 )
249 manifest.remove("Copying.txt")
263 manifest.remove("Copying.txt")
250 manifest.add_file(
264 manifest.add_file(
251 FileContent(path = ROOT + "/contrib/win32/mercurial.ini"),
265 FileContent(path = ROOT + "/contrib/win32/mercurial.ini"),
252 path = "defaultrc/mercurial.rc",
266 path = "defaultrc/mercurial.rc",
253 )
267 )
254 manifest.add_file(
268 manifest.add_file(
255 FileContent(filename = "editor.rc", content = "[ui]\neditor = notepad\n"),
269 FileContent(filename = "editor.rc", content = "[ui]\neditor = notepad\n"),
256 path = "defaultrc/editor.rc",
270 path = "defaultrc/editor.rc",
257 )
271 )
258
272
259 wix = WiXInstaller(
273 wix = WiXInstaller(
260 "hg",
274 "hg",
261 "%s-%s-%s.msi" % (MSI_NAME, VERSION, platform),
275 "%s-%s-%s.msi" % (MSI_NAME, VERSION, platform),
262 arch = platform,
276 arch = platform,
263 )
277 )
264
278
265 # Materialize files in the manifest to the install layout.
279 # Materialize files in the manifest to the install layout.
266 wix.add_install_files(manifest)
280 wix.add_install_files(manifest)
267
281
268 # From mercurial.wxs.
282 # From mercurial.wxs.
269 wix.install_files_root_directory_id = "INSTALLDIR"
283 wix.install_files_root_directory_id = "INSTALLDIR"
270
284
271 # Pull in our custom .wxs files.
285 # Pull in our custom .wxs files.
272 defines = {
286 defines = {
273 "PyOxidizer": "1",
287 "PyOxidizer": "1",
274 "Platform": platform,
288 "Platform": platform,
275 "Version": VERSION,
289 "Version": VERSION,
276 "Comments": "Installs Mercurial version %s" % VERSION,
290 "Comments": "Installs Mercurial version %s" % VERSION,
277 "PythonVersion": "3",
291 "PythonVersion": "3",
278 "MercurialHasLib": "1",
292 "MercurialHasLib": "1",
279 }
293 }
280
294
281 if EXTRA_MSI_FEATURES:
295 if EXTRA_MSI_FEATURES:
282 defines["MercurialExtraFeatures"] = EXTRA_MSI_FEATURES
296 defines["MercurialExtraFeatures"] = EXTRA_MSI_FEATURES
283
297
284 wix.add_wxs_file(
298 wix.add_wxs_file(
285 ROOT + "/contrib/packaging/wix/mercurial.wxs",
299 ROOT + "/contrib/packaging/wix/mercurial.wxs",
286 preprocessor_parameters=defines,
300 preprocessor_parameters=defines,
287 )
301 )
288
302
289 # Our .wxs references to other files. Pull those into the build environment.
303 # Our .wxs references to other files. Pull those into the build environment.
290 for f in ("defines.wxi", "guids.wxi", "COPYING.rtf"):
304 for f in ("defines.wxi", "guids.wxi", "COPYING.rtf"):
291 wix.add_build_file(f, ROOT + "/contrib/packaging/wix/" + f)
305 wix.add_build_file(f, ROOT + "/contrib/packaging/wix/" + f)
292
306
293 wix.add_build_file("mercurial.ico", ROOT + "/contrib/win32/mercurial.ico")
307 wix.add_build_file("mercurial.ico", ROOT + "/contrib/win32/mercurial.ico")
294
308
295 return wix
309 return wix
296
310
297
311
298 def register_code_signers():
312 def register_code_signers():
299 if not IS_WINDOWS:
313 if not IS_WINDOWS:
300 return
314 return
301
315
302 if SIGNING_PFX_PATH:
316 if SIGNING_PFX_PATH:
303 signer = code_signer_from_pfx_file(SIGNING_PFX_PATH, SIGNING_PFX_PASSWORD)
317 signer = code_signer_from_pfx_file(SIGNING_PFX_PATH, SIGNING_PFX_PASSWORD)
304 elif SIGNING_SUBJECT_NAME:
318 elif SIGNING_SUBJECT_NAME:
305 signer = code_signer_from_windows_store_subject(SIGNING_SUBJECT_NAME)
319 signer = code_signer_from_windows_store_subject(SIGNING_SUBJECT_NAME)
306 else:
320 else:
307 signer = None
321 signer = None
308
322
309 if signer:
323 if signer:
310 signer.set_time_stamp_server(TIME_STAMP_SERVER_URL)
324 signer.set_time_stamp_server(TIME_STAMP_SERVER_URL)
311 signer.activate()
325 signer.activate()
312
326
313
327
314 register_code_signers()
328 register_code_signers()
315
329
316 register_target("distribution", make_distribution)
330 register_target("distribution", make_distribution)
317 register_target("exe", make_exe, depends = ["distribution"])
331 register_target("exe", make_exe, depends = ["distribution"])
318 register_target("app", make_manifest, depends = ["distribution", "exe"], default = True)
332 register_target("app", make_manifest, depends = ["distribution", "exe"], default = True)
319 register_target("msi", make_msi, depends = ["app"])
333 register_target("msi", make_msi, depends = ["app"])
320
334
321 resolve_targets()
335 resolve_targets()
General Comments 0
You need to be logged in to leave comments. Login now