-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyoxidizer.bzl
150 lines (116 loc) · 5.94 KB
/
pyoxidizer.bzl
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# This file defines how PyOxidizer application building and packaging is
# performed. See PyOxidizer's documentation at
# https://pyoxidizer.readthedocs.io/en/stable/ for details of this
# configuration file format.
# Configuration files consist of functions which define build "targets."
# This function creates a Python executable and installs it in a destination
# directory.
def make_exe():
# Obtain the default PythonDistribution for our build target. We link
# this distribution into our produced executable and extract the Python
# standard library from it.
dist = default_python_distribution()
# This function creates a `PythonPackagingPolicy` instance, which
# influences how executables are built and how resources are added to
# the executable. You can customize the default behavior by assigning
# to attributes and calling functions.
policy = dist.make_python_packaging_policy()
# Enable support for non-classified "file" resources to be added to
# resource collections.
# policy.allow_files = True
# Control support for loading Python extensions and other shared libraries
# from memory. This is only supported on Windows and is ignored on other
# platforms.
# policy.allow_in_memory_shared_library_loading = True
# Control whether to generate Python bytecode at various optimization
# levels. The default optimization level used by Python is 0.
# policy.bytecode_optimize_level_zero = True
# policy.bytecode_optimize_level_one = True
policy.bytecode_optimize_level_two = True
# Resources are loaded from "in-memory" or "filesystem-relative" paths.
# The locations to attempt to add resources to are defined by the
# `resources_location` and `resources_location_fallback` attributes.
# The former is the first/primary location to try and the latter is
# an optional fallback.
# Use in-memory location for adding resources by default.
policy.resources_location = "in-memory"
# Use filesystem-relative location for adding resources by default.
# policy.resources_location = "filesystem-relative:prefix"
# Attempt to add resources relative to the built binary when
# `resources_location` fails.
policy.resources_location_fallback = "filesystem-relative:lib"
# Configure policy values to handle files as files and not attempt
# to classify files as specific types.
policy.set_resource_handling_mode("files")
# The configuration of the embedded Python interpreter can be modified
# by setting attributes on the instance. Some of these are
# documented below.
python_config = dist.make_python_interpreter_config()
# Make the embedded interpreter behave like a `python` process.
# python_config.config_profile = "python"
# Set initial value for `sys.path`. If the string `$ORIGIN` exists in
# a value, it will be expanded to the directory of the built executable.
python_config.module_search_paths = ["$ORIGIN/lib", "$ORIGIN/lib/site-packages"]
# Run a Python module as __main__ when the interpreter starts.
python_config.run_module = "portable_rez"
python_config.parse_argv = True
# Produce a PythonExecutable from a Python distribution, embedded
# resources, and other options. The returned object represents the
# standalone executable that will be built.
exe = dist.to_python_executable(
name="portable_rez",
# If no argument passed, the default `PythonPackagingPolicy` for the
# distribution is used.
packaging_policy=policy,
# If no argument passed, the default `PythonInterpreterConfig` is used.
config=python_config,
)
# Invoke `pip install` using a requirements file and add the collected resources
# to our binary.
for resource in exe.pip_install([CWD]):
resource.add_location = "filesystem-relative:lib/site-packages"
exe.add_python_resource(resource)
return exe
def make_embedded_resources(exe):
return exe.to_embedded_resources()
def make_install(exe):
# Create an object that represents our installed application file layout.
files = FileManifest()
# Add the generated executable to our install layout in the root directory.
files.add_python_resource(".", exe)
return files
# Dynamically enable automatic code signing.
def register_code_signers():
# You will need to run with `pyoxidizer build --var ENABLE_CODE_SIGNING 1` for
# this if block to be evaluated.
if not VARS.get("ENABLE_CODE_SIGNING"):
return
# Use a code signing certificate in a .pfx/.p12 file, prompting the
# user for its path and password to open.
# pfx_path = prompt_input("path to code signing certificate file")
# pfx_password = prompt_password(
# "password for code signing certificate file",
# confirm = True
# )
# signer = code_signer_from_pfx_file(pfx_path, pfx_password)
# Use a code signing certificate in the Windows certificate store, specified
# by its SHA-1 thumbprint. (This allows you to use YubiKeys and other
# hardware tokens if they speak to the Windows certificate APIs.)
# sha1_thumbprint = prompt_input(
# "SHA-1 thumbprint of code signing certificate in Windows store"
# )
# signer = code_signer_from_windows_store_sha1_thumbprint(sha1_thumbprint)
# Choose a code signing certificate automatically from the Windows
# certificate store.
# signer = code_signer_from_windows_store_auto()
# Activate your signer so it gets called automatically.
# signer.activate()
# Call our function to set up automatic code signers.
register_code_signers()
# Tell PyOxidizer about the build targets defined above.
register_target("exe", make_exe)
register_target("resources", make_embedded_resources, depends=["exe"], default_build_script=True)
register_target("install", make_install, depends=["exe"], default=True)
# Resolve whatever targets the invoker of this configuration file is requesting
# be resolved.
resolve_targets()