Skip to content

Commit 87d7b8f

Browse files
committed
Build a dtrun zipapp.
1 parent 6ac7818 commit 87d7b8f

File tree

5 files changed

+77
-28
lines changed

5 files changed

+77
-28
lines changed

src/ducktools/env/__main__.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ def delete_env_command(manager, args):
431431
return 0
432432

433433

434-
def main() -> int:
434+
def main_command() -> int:
435435
executable_name = os.path.splitext(os.path.basename(sys.executable))[0]
436436

437437
if zipapp_path := globals().get("zipapp_path"):
@@ -485,13 +485,16 @@ def main() -> int:
485485
raise RuntimeError(f"Invalid Command {args.command!r}")
486486

487487

488-
if __name__ == "__main__":
488+
def main() -> int:
489489
try:
490-
result = main()
490+
result = main_command()
491491
except (RuntimeError, EnvError) as e:
492492
errors = "\n".join(e.args) + "\n"
493493
if sys.stderr:
494494
sys.stderr.write(errors)
495-
sys.exit(1)
495+
return 1
496+
return 0
497+
496498

497-
sys.exit(result)
499+
if __name__ == "__main__":
500+
sys.exit(main())

src/ducktools/env/_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,6 @@ def run():
6565
msg = "\n".join(e.args) + "\n"
6666
if sys.stderr:
6767
sys.stderr.write(msg)
68-
sys.exit(1)
68+
return 1
6969

70-
sys.exit(0)
70+
return 0

src/ducktools/env/bootstrapping/bootstrap.py

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,28 +102,24 @@ def update_libraries():
102102

103103
def launch_script(script_file, zipapp_path, args, lockdata=None):
104104
sys.path.insert(0, default_paths.env_folder)
105-
try:
106-
from ducktools.env.manager import Manager
107-
manager = Manager(project_name=PROJECT_NAME)
108-
returncode = manager.run_bundle(
109-
script_path=script_file,
110-
script_args=args,
111-
lockdata=lockdata,
112-
zipapp_path=zipapp_path,
113-
)
114-
finally:
115-
sys.path.pop(0)
116-
105+
from ducktools.env.manager import Manager
106+
manager = Manager(project_name=PROJECT_NAME)
107+
returncode = manager.run_bundle(
108+
script_path=script_file,
109+
script_args=args,
110+
lockdata=lockdata,
111+
zipapp_path=zipapp_path,
112+
)
117113
return returncode
118114

119115

120116
def launch_ducktools():
121-
zipapp_path = sys.argv[0]
122-
init_globals = {"zipapp_path": zipapp_path}
123-
124-
import runpy
125-
runpy.run_path(
126-
default_paths.env_folder,
127-
init_globals=init_globals,
128-
run_name="__main__"
129-
)
117+
sys.path.insert(0, default_paths.env_folder)
118+
from ducktools.env.__main__ import main
119+
return main()
120+
121+
122+
def launch_dtrun():
123+
sys.path.insert(0, default_paths.env_folder)
124+
from ducktools.env._run import run
125+
return run()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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-env.pyz bundle itself
26+
"""
27+
import sys
28+
29+
from _version_check import version_check # type: ignore
30+
version_check()
31+
32+
33+
if __name__ == "__main__":
34+
from _bootstrap import update_libraries, launch_dtrun # type: ignore
35+
update_libraries()
36+
returncode = launch_dtrun()
37+
38+
sys.exit(returncode)

src/ducktools/env/scripts/create_zipapp.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ def build_zipapp(
144144
clear_old_builds=True
145145
) -> None:
146146
archive_name = "ducktools-env.pyz"
147+
dtrun_name = "dtrun.pyz"
147148

148149
with paths.build_folder() as build_folder:
149150

@@ -227,3 +228,14 @@ def build_zipapp(
227228
target=dist_folder / archive_name,
228229
interpreter="/usr/bin/env python"
229230
)
231+
232+
print(f"Creating {dtrun_name}")
233+
with importlib.resources.as_file(resources) as env_folder:
234+
main_dtrun_path = env_folder / "bootstrapping" / "zipapp_main_dtrun.py"
235+
shutil.copy(main_dtrun_path, build_path / "__main__.py")
236+
237+
zipapp.create_archive(
238+
source=build_folder,
239+
target=dist_folder / dtrun_name,
240+
interpreter="/usr/bin/env python"
241+
)

0 commit comments

Comments
 (0)