|
| 1 | +import os |
| 2 | +import re |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import tempfile |
| 6 | +import time |
| 7 | +import venv |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | + |
| 11 | +def ttime(timestamp=None, |
| 12 | + tzone=int(-time.timezone / 3600), |
| 13 | + fail="", |
| 14 | + fmt="%Y-%m-%d %H:%M:%S"): |
| 15 | + fix_tz = tzone * 3600 |
| 16 | + if timestamp is None: |
| 17 | + timestamp = time.time() |
| 18 | + else: |
| 19 | + timestamp = float(timestamp) |
| 20 | + if 1e12 <= timestamp < 1e13: |
| 21 | + # Compatible timestamp with 13-digit milliseconds |
| 22 | + timestamp = timestamp / 1000 |
| 23 | + try: |
| 24 | + timestamp = time.time() if timestamp is None else timestamp |
| 25 | + return time.strftime(fmt, time.gmtime(timestamp + fix_tz)) |
| 26 | + except Exception: |
| 27 | + return fail |
| 28 | + |
| 29 | + |
| 30 | +class FreezeTool(object): |
| 31 | + VENV_NAME = 'zipapps_venv' |
| 32 | + # not stable |
| 33 | + FASTER_PREPARE_PIP = False |
| 34 | + |
| 35 | + def __init__(self, output: str, pip_args: list): |
| 36 | + if not pip_args: |
| 37 | + raise RuntimeError('pip args is null') |
| 38 | + self.temp_dir: tempfile.TemporaryDirectory = None |
| 39 | + self.pip_args = pip_args |
| 40 | + self.output_path = output |
| 41 | + |
| 42 | + def log(self, msg, flush=False): |
| 43 | + _msg = f'{ttime()} | {msg}' |
| 44 | + print(_msg, file=sys.stderr, flush=flush) |
| 45 | + |
| 46 | + def run(self): |
| 47 | + self.log( |
| 48 | + 'All the logs will be redirected to stderr to ensure the output is stdout.' |
| 49 | + ) |
| 50 | + self.temp_dir = tempfile.TemporaryDirectory(prefix='zipapps_') |
| 51 | + self.temp_path = Path(self.temp_dir.name) |
| 52 | + self.log( |
| 53 | + f'Start mkdir temp folder: {self.temp_path.absolute()}, exist={self.temp_path.is_dir()}' |
| 54 | + ) |
| 55 | + self.install_env() |
| 56 | + output = self.install_packages() |
| 57 | + self.freeze_requirements(output) |
| 58 | + return output |
| 59 | + |
| 60 | + def install_env(self): |
| 61 | + venv_path = self.temp_path / self.VENV_NAME |
| 62 | + self.log(f'Initial venv with pip: {venv_path.absolute()}') |
| 63 | + if self.FASTER_PREPARE_PIP: |
| 64 | + venv.create(env_dir=venv_path, with_pip=False) |
| 65 | + import shutil |
| 66 | + |
| 67 | + import pip |
| 68 | + pip_dir = Path(pip.__file__).parent |
| 69 | + if os.name == 'nt': |
| 70 | + target = venv_path / 'Lib' / 'site-packages' / 'pip' |
| 71 | + else: |
| 72 | + pyv = 'python%d.%d' % sys.version_info[:2] |
| 73 | + target = venv_path / 'lib' / pyv / 'site-packages' / 'pip' |
| 74 | + shutil.copytree(pip_dir, target) |
| 75 | + else: |
| 76 | + venv.create(env_dir=venv_path, with_pip=True) |
| 77 | + if not venv_path.is_dir(): |
| 78 | + raise FileNotFoundError(str(venv_path)) |
| 79 | + |
| 80 | + def install_packages(self): |
| 81 | + if os.name == 'nt': |
| 82 | + python_path = self.temp_path / self.VENV_NAME / 'Scripts' / 'python.exe' |
| 83 | + else: |
| 84 | + python_path = self.temp_path / self.VENV_NAME / 'bin' / 'python' |
| 85 | + args = [ |
| 86 | + str(python_path.absolute()), |
| 87 | + '-m', |
| 88 | + 'pip', |
| 89 | + 'install', |
| 90 | + ] + self.pip_args |
| 91 | + self.log(f'Install packages in venv: {args}\n{"-" * 30}') |
| 92 | + with subprocess.Popen(args, stdout=subprocess.PIPE) as proc: |
| 93 | + for line in proc.stdout: |
| 94 | + try: |
| 95 | + line = line.decode() |
| 96 | + except ValueError: |
| 97 | + line = line.decode('utf-8', 'ignore') |
| 98 | + print(line.rstrip(), file=sys.stderr, flush=True) |
| 99 | + args = [str(python_path.absolute()), '-m', 'pip', 'freeze'] |
| 100 | + print("-" * 30, file=sys.stderr) |
| 101 | + self.log(f'Freeze packages in venv: {args}') |
| 102 | + output = subprocess.check_output(args) |
| 103 | + try: |
| 104 | + result = output.decode('utf-8') |
| 105 | + except ValueError: |
| 106 | + result = output.decode() |
| 107 | + result = re.sub('(\n|\r)+', '\n', result).strip() |
| 108 | + return result |
| 109 | + |
| 110 | + def freeze_requirements(self, output): |
| 111 | + print(output, flush=True) |
| 112 | + if self.output_path != '-': |
| 113 | + with open(self.output_path, 'w', encoding='utf-8') as f: |
| 114 | + print(output, file=f, flush=True) |
| 115 | + |
| 116 | + def remove_env(self): |
| 117 | + if self.temp_dir and self.temp_path.is_dir(): |
| 118 | + self.temp_dir.cleanup() |
| 119 | + self.log( |
| 120 | + f'Delete temp folder: {self.temp_path.absolute()}, exist={self.temp_path.is_dir()}' |
| 121 | + ) |
| 122 | + |
| 123 | + def __del__(self): |
| 124 | + self.remove_env() |
| 125 | + |
| 126 | + def __enter__(self): |
| 127 | + return self |
| 128 | + |
| 129 | + def __exit__(self, *e): |
| 130 | + self.remove_env() |
| 131 | + |
| 132 | + |
| 133 | +def test(): |
| 134 | + with FreezeTool('-', ['six==1.15.0']) as ft: |
| 135 | + result = ft.run() |
| 136 | + # print(result) |
| 137 | + assert result == 'six==1.15.0' |
| 138 | + |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + test() |
0 commit comments