ducktools-env 0.1.0__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.
@@ -0,0 +1,46 @@
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
+ from ._version import __version__, __version_tuple__
25
+ from .check_outdated_python import python_version_outdated, MINIMUM_PYTHON_STR, MINIMUM_PYTHON
26
+
27
+ # Check the python version is up to date
28
+ if err_message := python_version_outdated():
29
+ raise ImportError(err_message) from None
30
+
31
+
32
+ PROJECT_NAME = "ducktools"
33
+
34
+ FOLDER_ENVVAR = "DUCKTOOLS_ENV_FOLDER"
35
+ LAUNCH_TYPE_ENVVAR = "DUCKTOOLS_ENV_LAUNCH_TYPE"
36
+ LAUNCH_PATH_ENVVAR = "DUCKTOOLS_ENV_LAUNCH_PATH"
37
+ LAUNCH_ENVIRONMENT_ENVVAR = "DUCKTOOLS_ENV_LAUNCH_ENVIRONMENT"
38
+ DATA_BUNDLE_ENVVAR = "DUCKTOOLS_ENV_BUNDLED_DATA"
39
+
40
+ DATA_BUNDLE_FOLDER = "bundledata"
41
+
42
+
43
+ bootstrap_requires = [
44
+ "ducktools-lazyimporter>=0.5.1",
45
+ "packaging>=23.2",
46
+ ]
@@ -0,0 +1,241 @@
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
+ import os
25
+
26
+ import argparse
27
+ from ducktools.lazyimporter import LazyImporter, FromImport
28
+
29
+ from ducktools.env import __version__, PROJECT_NAME
30
+
31
+ _laz = LazyImporter(
32
+ [
33
+ FromImport("ducktools.env.manager", "Manager"),
34
+ FromImport("ducktools.env.environment_specs", "EnvironmentSpec"),
35
+ ]
36
+ )
37
+
38
+
39
+ class FixedArgumentParser(argparse.ArgumentParser):
40
+ """
41
+ The builtin argument parser uses shutil to figure out the terminal width
42
+ to display help info. This one replaces the function that calls help info
43
+ and plugs in a value for width.
44
+
45
+ This prevents the unnecessary import.
46
+ """
47
+ def _get_formatter(self):
48
+ # Calculate width
49
+ try:
50
+ columns = int(os.environ['COLUMNS'])
51
+ except (KeyError, ValueError):
52
+ try:
53
+ size = os.get_terminal_size()
54
+ except (AttributeError, ValueError, OSError):
55
+ # get_terminal_size unsupported
56
+ columns = 80
57
+ else:
58
+ columns = size.columns
59
+
60
+ # noinspection PyArgumentList
61
+ return self.formatter_class(prog=self.prog, width=columns-2)
62
+
63
+
64
+ def main():
65
+ parser = FixedArgumentParser(
66
+ prog="ducktools-env",
67
+ description="Script runner and bundler for scripts with inline dependencies",
68
+ )
69
+
70
+ parser.add_argument("-V", "--version", action="version", version=__version__)
71
+
72
+ subparsers = parser.add_subparsers(dest="command", required=True)
73
+
74
+ # 'run' command and args
75
+ run_parser = subparsers.add_parser(
76
+ "run",
77
+ help="Launch the provided python script with inline dependencies",
78
+ )
79
+
80
+ run_parser.add_argument("script_filename", help="Path to the script to run")
81
+
82
+ run_lock_group = run_parser.add_mutually_exclusive_group()
83
+ run_lock_group.add_argument(
84
+ "--with-lock",
85
+ help="Include a lockfile to use when running the script",
86
+ action="store"
87
+ )
88
+ run_lock_group.add_argument(
89
+ "--generate-lock",
90
+ help="Generate a lockfile based on the dependencies in the script",
91
+ action="store_true",
92
+ )
93
+
94
+ # 'bundle' command and args
95
+ bundle_parser = subparsers.add_parser(
96
+ "bundle",
97
+ help="Bundle the provided python script with inline dependencies into a python zipapp",
98
+ )
99
+
100
+ bundle_parser.add_argument("script_filename", help="Path to the script to bundle into a zipapp")
101
+ bundle_parser.add_argument(
102
+ "-o", "--output",
103
+ help="Output to given filename",
104
+ action="store",
105
+ )
106
+
107
+ bundle_lock_group = bundle_parser.add_mutually_exclusive_group()
108
+ bundle_lock_group.add_argument(
109
+ "--with-lock",
110
+ help="Include a lockfile to use when running the script",
111
+ action="store"
112
+ )
113
+ bundle_lock_group.add_argument(
114
+ "--generate-lock",
115
+ help="Generate a lockfile to use when unbundling",
116
+ action="store_true"
117
+ )
118
+
119
+ # 'generate_lock' command and args
120
+ generate_lock_parser = subparsers.add_parser(
121
+ "generate_lock",
122
+ help="Generate a lockfile based on inline dependencies in a script"
123
+ )
124
+
125
+ generate_lock_parser.add_argument(
126
+ "script_filename",
127
+ help="Path to the script to use to generate a lockfile"
128
+ )
129
+
130
+ generate_lock_parser.add_argument(
131
+ "-o", "--output",
132
+ help="Output to given filename",
133
+ )
134
+
135
+ # 'clear_cache' command and args
136
+ clear_cache_parser = subparsers.add_parser(
137
+ "clear_cache",
138
+ help="clear the temporary environment cache folder",
139
+ )
140
+
141
+ clear_cache_parser.add_argument(
142
+ "--full",
143
+ action="store_true",
144
+ help="clear the full ducktools/env application folder",
145
+ )
146
+
147
+ # 'rebuild_env' command and args
148
+ create_zipapp_parser = subparsers.add_parser(
149
+ "rebuild_env",
150
+ help="Recreate the ducktools-env library cache from the installed package"
151
+ )
152
+
153
+ create_zipapp_parser.add_argument(
154
+ "--zipapp",
155
+ action="store_true",
156
+ help="Also create the portable ducktools.pyz zipapp",
157
+ )
158
+
159
+ args, extras = parser.parse_known_args()
160
+
161
+ # Finally create a manager
162
+ manager = _laz.Manager(PROJECT_NAME)
163
+
164
+ if args.command == "run":
165
+ spec = _laz.EnvironmentSpec.from_script(
166
+ script_path=args.script_filename
167
+ )
168
+ if args.generate_lock:
169
+ uv_path = manager.retrieve_uv(required=True)
170
+ lockdata = spec.generate_lockdata(uv_path=uv_path)
171
+ lock_path = f"{args.script_filename}.lock"
172
+ with open(lock_path, 'w') as f:
173
+ f.write(lockdata)
174
+ elif lock_path := args.with_lock:
175
+ with open(lock_path, 'r') as f:
176
+ lockdata = f.read()
177
+ spec.lockdata = lockdata
178
+
179
+ manager.run_direct_script(
180
+ spec=spec,
181
+ args=extras,
182
+ )
183
+ elif args.command == "bundle":
184
+ if extras:
185
+ arg_text = ' '.join(extras)
186
+ sys.stderr.write(f"Unrecognised arguments: {arg_text}")
187
+ return
188
+
189
+ spec = _laz.EnvironmentSpec.from_script(
190
+ script_path=args.script_filename
191
+ )
192
+
193
+ if args.generate_lock:
194
+ uv_path = manager.retrieve_uv(required=True)
195
+ spec.generate_lockdata(uv_path=uv_path)
196
+ elif lock_path := args.with_lock:
197
+ with open(lock_path, 'r') as f:
198
+ lockdata = f.read()
199
+ spec.lockdata = lockdata
200
+
201
+ manager.create_bundle(
202
+ spec=spec,
203
+ output_file=args.output,
204
+ )
205
+ elif args.command == "generate_lock":
206
+ uv_path = manager.retrieve_uv(required=True)
207
+ spec = _laz.EnvironmentSpec.from_script(
208
+ script_path=args.script_filename
209
+ )
210
+ lockdata = spec.generate_lockdata(uv_path=uv_path)
211
+
212
+ out_path = args.output if args.output else f"{args.script_filename}.lock"
213
+ with open(out_path, "w") as f:
214
+ f.write(lockdata)
215
+
216
+ elif args.command == "clear_cache":
217
+ if extras:
218
+ arg_text = ' '.join(extras)
219
+ sys.stderr.write(f"Unrecognised arguments: {arg_text}")
220
+ return
221
+
222
+ if args.full:
223
+ manager.clear_project_folder()
224
+ else:
225
+ manager.clear_temporary_cache()
226
+ elif args.command == "rebuild_env":
227
+ if extras:
228
+ arg_text = ' '.join(extras)
229
+ sys.stderr.write(f"Unrecognised arguments: {arg_text}")
230
+ return
231
+
232
+ manager.build_env_folder()
233
+ if args.zipapp:
234
+ manager.build_zipapp()
235
+ else:
236
+ # Should be unreachable
237
+ raise ValueError("Invalid command")
238
+
239
+
240
+ if __name__ == "__main__":
241
+ main()
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ __version_tuple__ = (0, 1, 0)
@@ -0,0 +1,119 @@
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
+ """
25
+ This script becomes the __main__.py script inside the ducktools.pyz zipapp.
26
+ """
27
+ from __future__ import annotations
28
+
29
+ import os.path
30
+ import sys
31
+ import zipfile
32
+
33
+ # This file is moved and these imports created when bundling
34
+ from _platform_paths import ManagedPaths # type: ignore
35
+
36
+ from _vendor.ducktools.lazyimporter import LazyImporter, FromImport, ModuleImport # type: ignore
37
+
38
+ # This bootstrap script exists without ducktools.env and so needs a copy of project_name
39
+ PROJECT_NAME = "ducktools"
40
+
41
+ _laz = LazyImporter(
42
+ [
43
+ FromImport("_vendor.packaging.version", "Version"),
44
+ ModuleImport("runpy"),
45
+ ModuleImport("shutil"),
46
+ ]
47
+ )
48
+
49
+ default_paths = ManagedPaths(PROJECT_NAME)
50
+
51
+
52
+ def is_outdated(installed_version: str | None, bundled_version: str) -> bool:
53
+ # Shortcut for no version installed
54
+ if installed_version is None:
55
+ return True
56
+
57
+ # Always consider dev versions outdated
58
+ if "dev" in installed_version:
59
+ return True
60
+
61
+ # Shortcut for identical version string
62
+ if installed_version == bundled_version:
63
+ return False
64
+
65
+ # Try to use tuples, fallback to packaging
66
+ try:
67
+ installed_info = tuple(int(segment) for segment in installed_version.split("."))
68
+ bundled_info = tuple(int(segment) for segment in bundled_version.split("."))
69
+ except (ValueError, TypeError):
70
+ installed_info = _laz.Version(installed_version)
71
+ bundled_info = _laz.Version(bundled_version)
72
+
73
+ return installed_info < bundled_info
74
+
75
+
76
+ def update_libraries():
77
+ archive_path = os.path.abspath(sys.argv[0])
78
+
79
+ # Compare library versions to those in cache
80
+ with zipfile.ZipFile(archive_path, "r") as zf:
81
+ bundled_ducktools_ver = zipfile.Path(zf, "ducktools-env.version").read_text()
82
+ bundled_pip_ver = zipfile.Path(zf, "pip.pyz.version").read_text()
83
+
84
+ # Copy ducktools if outdated
85
+ if is_outdated(default_paths.get_env_version(), bundled_ducktools_ver):
86
+ sys.stderr.write("Installed ducktools is older than bundled, replacing.\n")
87
+ extract_names = sorted(n for n in zf.namelist() if n.startswith("ducktools-env/"))
88
+ zf.extractall(default_paths.manager_folder, members=extract_names)
89
+ zf.extract("ducktools-env.version", default_paths.manager_folder)
90
+
91
+ # Copy pip if outdated
92
+ if is_outdated(default_paths.get_pip_version(), bundled_pip_ver):
93
+ sys.stderr.write("Installed pip is older than bundled, replacing.\n")
94
+ zf.extract("pip.pyz", default_paths.manager_folder)
95
+ zf.extract("pip.pyz.version", default_paths.manager_folder)
96
+
97
+
98
+ def launch_script(script_file, zipapp_path, args, lockdata=None):
99
+ sys.path.insert(0, default_paths.env_folder)
100
+ try:
101
+ from ducktools.env.manager import Manager
102
+ from ducktools.env.environment_specs import EnvironmentSpec
103
+ manager = Manager(PROJECT_NAME)
104
+ spec = EnvironmentSpec.from_script(
105
+ script_path=script_file,
106
+ lockdata=lockdata,
107
+ )
108
+
109
+ manager.run_bundled_script(
110
+ spec=spec,
111
+ zipapp_path=zipapp_path,
112
+ args=args,
113
+ )
114
+ finally:
115
+ sys.path.pop(0)
116
+
117
+
118
+ def launch_ducktools():
119
+ _laz.runpy.run_path(default_paths.env_folder, run_name="__main__")
@@ -0,0 +1,93 @@
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
+
27
+ from _check_outdated_python import python_version_outdated # type: ignore
28
+
29
+ message = python_version_outdated()
30
+ if message:
31
+ print("The Python version used to unpack this bundle is outdated.")
32
+ print(message)
33
+ if sys.platform in {"win32", "darwin"}:
34
+ print("You can get the latest Python from: https://www.python.org/downloads/")
35
+ input("Press return to close")
36
+ sys.exit()
37
+ del message
38
+
39
+
40
+ import zipfile
41
+ from pathlib import Path
42
+
43
+ # Included in bundle
44
+ from _bootstrap import update_libraries, launch_script # type: ignore
45
+
46
+
47
+ def main(script_name):
48
+ # Get updated ducktools and pip
49
+ update_libraries()
50
+
51
+ # Get the path to this zipfile and the folder is is being run from
52
+ zip_path = sys.argv[0]
53
+
54
+ script_dest = Path(zip_path).with_suffix(".py")
55
+
56
+ i = 0
57
+ while script_dest.exists():
58
+ # Keep adding .temp.py until the path doesn't exist - up to a point
59
+ script_dest = script_dest.with_suffix(".temp.py")
60
+ i += 1
61
+ if i > 5:
62
+ raise FileExistsError(
63
+ f"'{script_dest}' already exists, as did all the versions with fewer '.temp' segments"
64
+ )
65
+
66
+ working_dir = Path(zip_path).parent
67
+
68
+ try:
69
+ with zipfile.ZipFile(zip_path) as zf:
70
+ script_info = zf.getinfo(script_name)
71
+ script_info.filename = script_dest.name
72
+
73
+ # Get lockfile if it exists
74
+ lock_name = f"{script_name}.lock"
75
+ try:
76
+ lockdata = zipfile.Path(zf, lock_name).read_text()
77
+ except FileNotFoundError:
78
+ # No lockfile
79
+ lockdata = None
80
+
81
+ # Extract the script file to the existing folder
82
+ zf.extract(script_info, path=working_dir)
83
+
84
+ launch_script(
85
+ script_file=str(script_dest),
86
+ zipapp_path=zip_path,
87
+ args=sys.argv[1:],
88
+ lockdata=lockdata,
89
+ )
90
+ finally:
91
+ script_dest.unlink()
92
+
93
+ # BUNDLE CODE TO EXECUTE SCRIPT FOLLOWS
@@ -0,0 +1,45 @@
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
+ """
25
+ This is the bootstrapping script for the ducktools.pyz bundle itself
26
+ """
27
+ import sys
28
+
29
+ from _check_outdated_python import python_version_outdated # type: ignore
30
+
31
+ message = python_version_outdated()
32
+ if message:
33
+ print("The Python version used to unpack this bundle is outdated.")
34
+ print(message)
35
+ if sys.platform in {"win32", "darwin"}:
36
+ print("You can get the latest Python from: https://www.python.org/downloads/")
37
+ input("Press any key to close")
38
+ sys.exit()
39
+ del message
40
+
41
+ from _bootstrap import update_libraries, launch_ducktools # noqa
42
+
43
+ if __name__ == "__main__":
44
+ update_libraries()
45
+ launch_ducktools()