ducktools-env 0.1.0a1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ducktools/env/__init__.py +43 -0
- ducktools/env/__main__.py +114 -0
- ducktools/env/_version.py +2 -0
- ducktools/env/bundle/__init__.py +127 -0
- ducktools/env/bundle/bundle_main.py +72 -0
- ducktools/env/catalogue.py +438 -0
- ducktools/env/config.py +80 -0
- ducktools/env/environment_specs.py +190 -0
- ducktools/env/exceptions.py +56 -0
- ducktools/env/manager.py +76 -0
- ducktools/env/platform_paths.py +169 -0
- ducktools/env/run/__init__.py +38 -0
- ducktools/env/scripts/bootstrap.py +102 -0
- ducktools/env/scripts/clear_cache.py +29 -0
- ducktools/env/scripts/create_zipapp.py +218 -0
- ducktools/env/scripts/get_pip.py +155 -0
- ducktools/env/scripts/zipapp_main.py +28 -0
- ducktools_env-0.1.0a1.dist-info/LICENSE +21 -0
- ducktools_env-0.1.0a1.dist-info/METADATA +150 -0
- ducktools_env-0.1.0a1.dist-info/RECORD +22 -0
- ducktools_env-0.1.0a1.dist-info/WHEEL +5 -0
- ducktools_env-0.1.0a1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# ducktools.env
|
|
2
|
+
# MIT License
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2024 David C Ellis
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
|
|
24
|
+
import sys
|
|
25
|
+
|
|
26
|
+
from ._version import __version__, __version_tuple__
|
|
27
|
+
|
|
28
|
+
MINIMUM_PYTHON = (3, 8)
|
|
29
|
+
MINIMUM_PYTHON_STR = ".".join(str(v) for v in MINIMUM_PYTHON)
|
|
30
|
+
|
|
31
|
+
bootstrap_requires = [
|
|
32
|
+
"ducktools-lazyimporter>=0.5.1",
|
|
33
|
+
"packaging>=23.2",
|
|
34
|
+
"zipp>=3.16",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
if sys.version_info < MINIMUM_PYTHON:
|
|
38
|
+
v = sys.version_info
|
|
39
|
+
major, minor = MINIMUM_PYTHON
|
|
40
|
+
raise ImportError(
|
|
41
|
+
f"Python {v.major}.{v.minor} is not supported by ducktools-env. "
|
|
42
|
+
f"Python {major}.{minor} is the minimum required version."
|
|
43
|
+
)
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
# ducktools.env
|
|
2
|
+
# MIT License
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2024 David C Ellis
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
import sys
|
|
24
|
+
|
|
25
|
+
import argparse
|
|
26
|
+
from ducktools.lazyimporter import LazyImporter, FromImport, MultiFromImport
|
|
27
|
+
|
|
28
|
+
_laz = LazyImporter(
|
|
29
|
+
[
|
|
30
|
+
FromImport("ducktools.env", "__version__"),
|
|
31
|
+
FromImport("ducktools.env.run", "run_script"),
|
|
32
|
+
FromImport("ducktools.env.scripts.clear_cache", "clear_cache"),
|
|
33
|
+
FromImport("ducktools.env.bundle", "create_bundle"),
|
|
34
|
+
MultiFromImport(
|
|
35
|
+
"ducktools.env.scripts.create_zipapp",
|
|
36
|
+
["build_env_folder", "build_zipapp"]
|
|
37
|
+
)
|
|
38
|
+
]
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def main():
|
|
43
|
+
parser = argparse.ArgumentParser(
|
|
44
|
+
prog="ducktools-env",
|
|
45
|
+
description="Script runner and bundler for scripts with inline dependencies",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
parser.add_argument("-V", "--version", action="version", version=_laz.__version__)
|
|
49
|
+
|
|
50
|
+
subparsers = parser.add_subparsers(dest="command", required=True)
|
|
51
|
+
|
|
52
|
+
run_parser = subparsers.add_parser(
|
|
53
|
+
"run",
|
|
54
|
+
help="Launch the provided python script with inline dependencies",
|
|
55
|
+
prefix_chars="+/",
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
bundle_parser = subparsers.add_parser(
|
|
59
|
+
"bundle",
|
|
60
|
+
help="Bundle the provided python script with inline dependencies into a python zipapp",
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
clear_cache_parser = subparsers.add_parser(
|
|
64
|
+
"clear_cache",
|
|
65
|
+
help="Completely clear the ducktools/env cache folder"
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
create_zipapp_parser = subparsers.add_parser(
|
|
69
|
+
"rebuild_env",
|
|
70
|
+
help="Recreate the ducktools-env library cache from the installed package"
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
create_zipapp_parser.add_argument(
|
|
74
|
+
"--zipapp",
|
|
75
|
+
action="store_true",
|
|
76
|
+
help="Also create the portable ducktools.pyz zipapp",
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
run_parser.add_argument("script_filename", help="Path to the script to run")
|
|
80
|
+
bundle_parser.add_argument("script_filename", help="Path to the script to bundle into a zipapp")
|
|
81
|
+
|
|
82
|
+
args, extras = parser.parse_known_args()
|
|
83
|
+
|
|
84
|
+
if args.command == "run":
|
|
85
|
+
_laz.run_script(args.script_filename, extras)
|
|
86
|
+
elif args.command == "bundle":
|
|
87
|
+
if extras:
|
|
88
|
+
arg_text = ' '.join(extras)
|
|
89
|
+
sys.stderr.write(f"Unrecognised arguments: {arg_text}")
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
_laz.create_bundle(args.script_filename)
|
|
93
|
+
elif args.command == "clear_cache":
|
|
94
|
+
if extras:
|
|
95
|
+
arg_text = ' '.join(extras)
|
|
96
|
+
sys.stderr.write(f"Unrecognised arguments: {arg_text}")
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
_laz.clear_cache()
|
|
100
|
+
elif args.command == "rebuild_env":
|
|
101
|
+
if extras:
|
|
102
|
+
arg_text = ' '.join(extras)
|
|
103
|
+
sys.stderr.write(f"Unrecognised arguments: {arg_text}")
|
|
104
|
+
return
|
|
105
|
+
_laz.build_env_folder()
|
|
106
|
+
if args.zipapp:
|
|
107
|
+
_laz.build_zipapp()
|
|
108
|
+
else:
|
|
109
|
+
# Should be unreachable
|
|
110
|
+
raise ValueError("Invalid command")
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
if __name__ == "__main__":
|
|
114
|
+
main()
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
# ducktools.env
|
|
2
|
+
# MIT License
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2024 David C Ellis
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
|
|
24
|
+
import os.path
|
|
25
|
+
import shutil
|
|
26
|
+
import subprocess
|
|
27
|
+
import sys
|
|
28
|
+
import zipapp
|
|
29
|
+
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
import importlib_resources
|
|
33
|
+
|
|
34
|
+
from .. import MINIMUM_PYTHON_STR, bootstrap_requires
|
|
35
|
+
from ..platform_paths import default_paths
|
|
36
|
+
from ..scripts.get_pip import retrieve_pip
|
|
37
|
+
from ..scripts.create_zipapp import build_env_folder
|
|
38
|
+
from ..exceptions import ScriptNameClash
|
|
39
|
+
|
|
40
|
+
invalid_script_names = {
|
|
41
|
+
"__main__.py",
|
|
42
|
+
"_bootstrap.py",
|
|
43
|
+
"_platform_paths.py",
|
|
44
|
+
"_vendor.py",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def create_bundle(script_file, *, paths=default_paths):
|
|
49
|
+
if script_file.endswith(".pyz") or script_file.endswith(".pyzw"):
|
|
50
|
+
sys.stderr.write(
|
|
51
|
+
"Bundles must be created from .py scripts not .pyz[w] archives\n"
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
if script_file in invalid_script_names:
|
|
55
|
+
raise ScriptNameClash(
|
|
56
|
+
f"Script {script_file!r} can't be bundled as the name clashes with "
|
|
57
|
+
f"a script or library required for unbundling"
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
build_folder = paths.build_folder()
|
|
61
|
+
|
|
62
|
+
print(f"Building bundle in {build_folder!r}")
|
|
63
|
+
|
|
64
|
+
if not os.path.exists(paths.pip_zipapp):
|
|
65
|
+
retrieve_pip()
|
|
66
|
+
|
|
67
|
+
if not os.path.exists(paths.env_folder):
|
|
68
|
+
build_env_folder()
|
|
69
|
+
|
|
70
|
+
print("Copying libraries into build folder")
|
|
71
|
+
# Copy pip and ducktools zipapps into folder
|
|
72
|
+
shutil.copytree(paths.manager_folder, build_folder, dirs_exist_ok=True)
|
|
73
|
+
|
|
74
|
+
resources = importlib_resources.files("ducktools.env")
|
|
75
|
+
|
|
76
|
+
with importlib_resources.as_file(resources) as env_folder:
|
|
77
|
+
platform_paths_path = env_folder / "platform_paths.py"
|
|
78
|
+
bootstrap_path = env_folder / "scripts" / "bootstrap.py"
|
|
79
|
+
main_zipapp_path = env_folder / "bundle" / "bundle_main.py"
|
|
80
|
+
|
|
81
|
+
shutil.copy(platform_paths_path, os.path.join(build_folder, "_platform_paths.py"))
|
|
82
|
+
shutil.copy(bootstrap_path, os.path.join(build_folder, "_bootstrap.py"))
|
|
83
|
+
shutil.copy(main_zipapp_path, os.path.join(build_folder, "__main__.py"))
|
|
84
|
+
|
|
85
|
+
print("Installing required unpacking libraries")
|
|
86
|
+
vendor_folder = os.path.join(build_folder, "_vendor")
|
|
87
|
+
|
|
88
|
+
pip_command = [
|
|
89
|
+
sys.executable,
|
|
90
|
+
paths.pip_zipapp,
|
|
91
|
+
"--disable-pip-version-check",
|
|
92
|
+
"install",
|
|
93
|
+
*bootstrap_requires,
|
|
94
|
+
"--python-version",
|
|
95
|
+
MINIMUM_PYTHON_STR,
|
|
96
|
+
"--only-binary=:all:",
|
|
97
|
+
"--no-compile",
|
|
98
|
+
"--target",
|
|
99
|
+
vendor_folder
|
|
100
|
+
]
|
|
101
|
+
|
|
102
|
+
subprocess.run(pip_command)
|
|
103
|
+
|
|
104
|
+
freeze_command = [
|
|
105
|
+
sys.executable,
|
|
106
|
+
paths.pip_zipapp,
|
|
107
|
+
"freeze",
|
|
108
|
+
"--path",
|
|
109
|
+
vendor_folder,
|
|
110
|
+
]
|
|
111
|
+
|
|
112
|
+
freeze = subprocess.run(freeze_command, capture_output=True, text=True)
|
|
113
|
+
|
|
114
|
+
(Path(vendor_folder) / "requirements.txt").write_text(freeze.stdout)
|
|
115
|
+
|
|
116
|
+
print("Copying script to build folder and bundling")
|
|
117
|
+
shutil.copy(script_file, build_folder)
|
|
118
|
+
|
|
119
|
+
archive_path = Path(script_file).with_suffix(".pyz")
|
|
120
|
+
|
|
121
|
+
zipapp.create_archive(
|
|
122
|
+
source=build_folder,
|
|
123
|
+
target=Path(script_file).with_suffix(".pyz"),
|
|
124
|
+
interpreter="/usr/bin/env python",
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
print(f"Bundled {script_file!r} as '{archive_path}'")
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# ducktools.env
|
|
2
|
+
# MIT License
|
|
3
|
+
#
|
|
4
|
+
# Copyright (c) 2024 David C Ellis
|
|
5
|
+
#
|
|
6
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
# of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
# in the Software without restriction, including without limitation the rights
|
|
9
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
# copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
# furnished to do so, subject to the following conditions:
|
|
12
|
+
#
|
|
13
|
+
# The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
# copies or substantial portions of the Software.
|
|
15
|
+
#
|
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
# SOFTWARE.
|
|
23
|
+
|
|
24
|
+
# This becomes the bundler bootstrap python script
|
|
25
|
+
import sys
|
|
26
|
+
import zipfile
|
|
27
|
+
|
|
28
|
+
from pathlib import Path
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# Included in bundle
|
|
32
|
+
from _bootstrap import update_libraries, launch_script # noqa
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def main():
|
|
36
|
+
# Get updated ducktools and pip
|
|
37
|
+
update_libraries()
|
|
38
|
+
|
|
39
|
+
# Get the path to this zipfile and the folder is is being run from
|
|
40
|
+
zip_path = sys.argv[0]
|
|
41
|
+
|
|
42
|
+
script_path = Path(zip_path).with_suffix(".py")
|
|
43
|
+
script_name = script_path.name
|
|
44
|
+
|
|
45
|
+
temp_path = script_path.with_suffix(".temp.py")
|
|
46
|
+
|
|
47
|
+
i = 0
|
|
48
|
+
while temp_path.exists():
|
|
49
|
+
# Keep adding .temp.py until the path doesn't exist - up to a point
|
|
50
|
+
temp_path = temp_path.with_suffix(".temp.py")
|
|
51
|
+
i += 1
|
|
52
|
+
if i > 5:
|
|
53
|
+
raise FileExistsError(
|
|
54
|
+
f"'{temp_path}' already exists, as did all the versions with fewer '.temp' segments"
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
working_dir = Path(zip_path).parent
|
|
58
|
+
script_path = str(working_dir / script_name)
|
|
59
|
+
|
|
60
|
+
try:
|
|
61
|
+
with zipfile.ZipFile(zip_path) as zf:
|
|
62
|
+
script_info = zf.getinfo(script_name)
|
|
63
|
+
script_info.filename = temp_path.name
|
|
64
|
+
# Extract the script file to the existing folder
|
|
65
|
+
zf.extract(script_info, path=working_dir)
|
|
66
|
+
launch_script(script_path, sys.argv[1:])
|
|
67
|
+
finally:
|
|
68
|
+
temp_path.unlink()
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if __name__ == "__main__":
|
|
72
|
+
main()
|