-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmake_single_executable.sh
More file actions
executable file
·99 lines (72 loc) · 3.09 KB
/
make_single_executable.sh
File metadata and controls
executable file
·99 lines (72 loc) · 3.09 KB
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
#!/usr/bin/env bash
set -x # Print command traces before executing command
set -e # Exit immediately if a simple command exits with a non-zero status.
set -o pipefail # Return value of a pipeline as the value of the last command to
# exit with a non-zero status, or zero if all commands in the
# pipeline exit successfully.
# directory containing setup.py and project files
PROJDIR=`pwd`
PROJNAME='mcpartools'
#make single file executable with given name, for given entrypoint
make_zipapp() {
EXENAME=$1
ENTRYPOINT=$2
# temporary dir, convenient for packaging
TMPDIR=`mktemp -d`
# make directory, named the same way as the single executable we want to create
mkdir -p $TMPDIR/$EXENAME
# wheel package contains all module sources (and extracted version)
# we will unpack it and use as core code for single executable
WHEEL_FILE=$(find dist/ -name "$PROJNAME-*.whl" | head -n 1)
if [ -z "$WHEEL_FILE" ]; then
echo "Error: Wheel file not found in dist/. Please ensure 'python -m build --wheel .' has run."
exit 1
fi
unzip -q "$WHEEL_FILE" -d "$TMPDIR/$EXENAME"
# go to TMPDIR
cd $TMPDIR
# use zipapp module to make a single executable zip file
# zipapp was introduced in Python 3.5: https://docs.python.org/3/library/zipapp.html
# files generated by zipapp can be executed by all Python versions greater than 2.6
# zipapp will generate __main__.py file in $EXENAME directory
python3 -m zipapp $EXENAME -p "/usr/bin/env python3" -m $ENTRYPOINT
# add executable bits
chmod ugo+x $EXENAME.pyz
# copy back to project dir
cp $EXENAME.pyz $PROJDIR
cd -
}
test_zipapp() {
# temporary dir, convenient for packaging
TMPDIR=`mktemp -d`
# packaged app
APPFILE=$1
# copy app to temp dir
cp -r $APPFILE $TMPDIR
# go to TMPDIR
cd $TMPDIR
echo "--- Testing $(basename "$APPFILE") --version ---"
./$(basename "$APPFILE") --version || { echo "Error: --version failed for $(basename "$APPFILE")"; exit 1; }
echo "--- Testing $(basename "$APPFILE") --help ---"
./$(basename "$APPFILE") --help || { echo "Error: --help failed for $(basename "$APPFILE")"; exit 1; }
# go back
cd -
}
# --- Main Script Execution ---
echo "--- Building wheel package for zipapp ---"
# Use the 'build' module to generate wheel based on pyproject.toml
# This replaces 'python3 setup.py bdist_wheel'
# Ensure 'build' is installed in the environment where this script runs.
python3 -m build --wheel .
# check if wheels are generated
echo "--- Checking generated wheels ---"
ls -al dist/*.whl || { echo "Error: No wheel files found in dist/"; exit 1; }
# make single executable called generatemc.pyz:
# - containing modules from wheel package
# - executing mcpartools.generatemc:main function
echo "--- Creating generatemc.pyz executable (zipapp) ---"
make_zipapp 'generatemc' 'mcpartools.generatemc:main'
# check if single executable can be called
echo "--- Testing generatemc.pyz executable ---"
test_zipapp "$PROJDIR/generatemc.pyz"
echo "--- Zipapp creation and testing complete ---"