Skip to content

Commit 2a238f5

Browse files
committedSep 12, 2023
add --download-pip-pyz to download pip.pyz
1 parent 0383f09 commit 2a238f5

File tree

2 files changed

+107
-3
lines changed

2 files changed

+107
-3
lines changed
 

‎changelog.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11

22
# Changelogs
33

4+
- 2023.09.12
5+
- add `--download-pip-pyz` to download pip.pyz
6+
- install pip module to win32 embeded exe
7+
- win32 embeded `./python.exe xxx/xxx/zipapps.pyz` auto download `pip.pyz` and append to _pth
8+
- fix `main.py` docs
49
- 2023.06.04
510
- save zipapps_config.json to pyz file
6-
-
711
- 2022.08.28
812
- add `-q` for quiet mode
913
- `-qqqqq` to mute all the logs

‎zipapps/__main__.py

+102-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import argparse
22
import json
33
import sys
4+
import time
5+
from pathlib import Path
46

57
from . import __version__
6-
from .main import ZipApp, create_app
8+
from .main import ZipApp
79

810
USAGE = r'''
911
===========================================================================
@@ -51,6 +53,95 @@
5153
PS: all the unknown args will be used by "pip install".
5254
==========================================================================='''
5355

56+
PIP_PYZ_URL = 'https://bootstrap.pypa.io/pip/pip.pyz'
57+
58+
59+
def _get_now():
60+
return time.strftime('%Y-%m-%d %H:%M:%S')
61+
62+
63+
def _get_pth_path():
64+
py_exe_path = Path(sys.executable)
65+
for _path in py_exe_path.parent.glob('*._pth'):
66+
_pth_path = _path
67+
break
68+
else:
69+
fname = f'python{sys.version_info.major}{sys.version_info.minor}._pth'
70+
_pth_path = py_exe_path.parent / fname
71+
return _pth_path
72+
73+
74+
def _append_pth():
75+
import re
76+
_pth_path = _get_pth_path()
77+
if _pth_path.is_file():
78+
print('find _pth file:',
79+
_pth_path.as_posix(),
80+
flush=True,
81+
file=sys.stderr)
82+
_path_bytes = _pth_path.read_bytes()
83+
else:
84+
_path_bytes = b''
85+
if not re.search(b'^import site$', _path_bytes):
86+
_path_bytes += b'\nimport site\n'
87+
if not re.search(b'^pip\.pyz$', _path_bytes):
88+
_path_bytes += b'\npip.pyz\n'
89+
_pth_path.write_bytes(_path_bytes)
90+
91+
92+
def download_pip_pyz(target: Path = None, log=True):
93+
from urllib.request import urlretrieve
94+
95+
pip_pyz_path = Path(target or (Path(sys.executable).parent / 'pip.pyz'))
96+
if log:
97+
msg = f'Download {PIP_PYZ_URL} -> {pip_pyz_path.absolute().as_posix()}'
98+
print(_get_now(), msg, flush=True, file=sys.stderr)
99+
if pip_pyz_path.is_file():
100+
pip_pyz_path.unlink()
101+
urlretrieve(url=PIP_PYZ_URL,
102+
filename=pip_pyz_path.absolute().as_posix(),
103+
reporthook=lambda a, b, c: print(
104+
_get_now(),
105+
f'Downloading {int(100*(1+a)*b/c)}%, {(1 + a) * b} / {c}',
106+
end='\r',
107+
flush=True,
108+
file=sys.stderr,
109+
) if log else None)
110+
try:
111+
sys.path.append(pip_pyz_path.absolute().as_posix())
112+
import pip as _
113+
if log:
114+
print(f'\n{_get_now()} install pip ok', flush=True, file=sys.stderr)
115+
return True
116+
except ImportError:
117+
if log:
118+
print(f'\n{_get_now()} install pip failed',
119+
flush=True,
120+
file=sys.stderr)
121+
122+
123+
def handle_win32_embeded():
124+
_pth_path = _get_pth_path()
125+
126+
if not _pth_path.is_file():
127+
return
128+
try:
129+
import pip as _
130+
return
131+
except ImportError:
132+
need_install = (
133+
input(f'\n{"=" * 50}\npip module not found, try installing?(Y/n)'
134+
).lower().strip() or 'y')
135+
if need_install != 'y':
136+
return
137+
print('find _pth file:', _pth_path.as_posix(), flush=True, file=sys.stderr)
138+
if download_pip_pyz():
139+
_append_pth()
140+
import os
141+
142+
os.system('%s -m pip -V' % Path(sys.executable).as_posix())
143+
os.system('pause')
144+
54145

55146
def main():
56147
parser = argparse.ArgumentParser(usage=USAGE, prog='Zipapps')
@@ -267,9 +358,18 @@ def main():
267358
dest='quite_mode',
268359
help='mute logs.',
269360
)
361+
parser.add_argument('--download-pip-pyz',
362+
default='',
363+
dest='download_pip_pyz',
364+
help=f'Download pip.pyz from "{PIP_PYZ_URL}"')
365+
270366
if len(sys.argv) == 1:
271-
return parser.print_help()
367+
parser.print_help()
368+
handle_win32_embeded()
369+
return
272370
args, pip_args = parser.parse_known_args()
371+
if args.download_pip_pyz:
372+
return download_pip_pyz(args.download_pip_pyz)
273373
if args.quite_mode:
274374
ZipApp.LOGGING = False
275375
if '-q' not in pip_args and '--quiet' not in pip_args:

0 commit comments

Comments
 (0)
Failed to load comments.