forked from DataMedSci/mcpartools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_single_executable.sh
executable file
·81 lines (57 loc) · 2.07 KB
/
make_single_executable.sh
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
#!/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
unzip dist/*.whl -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 python" -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
$APPFILE --version
$APPFILE --help
# go back
cd -
}
# generate wheel package
python3 setup.py bdist_wheel
# check if wheels are generated
ls -al dist/*.whl
# make single executable called convertmc.pyz:
# - containing modules from wheel package
# - executing bdo2txt:main function
make_zipapp 'generatemc' 'mcpartools.generatemc:main'
# check if single executable can be called
test_zipapp `pwd`/'generatemc.pyz'