Actually bootstrap our shiv environment.
()
| 187 | |
| 188 | |
| 189 | def bootstrap(): # pragma: no cover |
| 190 | """Actually bootstrap our shiv environment.""" |
| 191 | |
| 192 | # get a handle of the currently executing zip file |
| 193 | with current_zipfile() as archive: |
| 194 | |
| 195 | # create an environment object (a combination of env vars and json metadata) |
| 196 | env = Environment.from_json(archive.read("environment.json").decode()) |
| 197 | |
| 198 | # get a site-packages directory (from env var or via build id) |
| 199 | site_packages = cache_path(archive, env.root, env.build_id) / "site-packages" |
| 200 | |
| 201 | # determine if first run or forcing extract |
| 202 | if not site_packages.exists() or env.force_extract: |
| 203 | extract_site_packages( |
| 204 | archive, |
| 205 | site_packages.parent, |
| 206 | env.compile_pyc, |
| 207 | env.compile_workers, |
| 208 | env.force_extract, |
| 209 | ) |
| 210 | |
| 211 | # get sys.path's length |
| 212 | length = len(sys.path) |
| 213 | |
| 214 | # Find the first instance of an existing site-packages on sys.path |
| 215 | index = get_first_sitedir_index() or length |
| 216 | |
| 217 | # copy sys.path to determine diff |
| 218 | sys_path_before = sys.path.copy() |
| 219 | |
| 220 | # append site-packages using the stdlib blessed way of extending path |
| 221 | # so as to handle .pth files correctly |
| 222 | site.addsitedir(site_packages) |
| 223 | |
| 224 | # reorder to place our site-packages before any others found |
| 225 | sys.path = sys.path[:index] + sys.path[length:] + sys.path[index:length] |
| 226 | |
| 227 | # Prepend the sys.path if environment variable is set |
| 228 | prepend_pythonpath(env) |
| 229 | |
| 230 | # determine newly added paths |
| 231 | new_paths = [p for p in sys.path if p not in sys_path_before] |
| 232 | |
| 233 | # check if source files have been modified, if required |
| 234 | if env.no_modify: |
| 235 | ensure_no_modify(site_packages, env.hashes) |
| 236 | |
| 237 | # add any new paths to the environment, if requested |
| 238 | if env.extend_pythonpath: |
| 239 | extend_python_path(os.environ, new_paths) |
| 240 | |
| 241 | # if a preamble script was provided, run it |
| 242 | if env.preamble: |
| 243 | |
| 244 | # path to the preamble |
| 245 | preamble_bin = site_packages / "bin" / env.preamble |
| 246 |
no test coverage detected