Repository navigation
Expand file tree
/
Copy pathwppm.py
More file actions
514 lines (475 loc) · 28 KB
/
Copy pathwppm.py
File metadata and controls
514 lines (475 loc) · 28 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
# -*- coding: utf-8 -*-
#
# WinPython Package Manager
# Copyright © 2012 Pierre Raybaut
# Copyright © 2014-2025+ The Winpython development team https://github.com/winpython/
# Licensed under the terms of the MIT License
# (see wppm/__init__.py for details)
import os
import re
import sys
import shutil
import subprocess
import json
from datetime import datetime
from pathlib import Path
from argparse import ArgumentParser, RawTextHelpFormatter
from . import utils, piptree, diff, __version__
from . import wheelhouse as wh
from operator import itemgetter
# Workaround for installing PyVISA on Windows from source:
# 2026-05-10 not helping oin non WinPython: os.environ["HOME"] = os.environ["USERPROFILE"]
class Package:
"""Standardize a Package from filename or pip list."""
def __init__(self, fname: str, suggested_summary: str = None):
self.fname = fname
self.description = (utils.sum_up(suggested_summary) if suggested_summary else "").strip()
self.name, self.version = fname, '?.?.?'
if fname.lower().endswith((".zip", ".tar.gz", ".whl")):
bname = Path(self.fname).name # e.g., "sqlite_bro-1.0.0..."
infos = utils.get_source_package_infos(bname) # get name, version
if infos:
self.name, self.version = utils.normalize(infos[0]), infos[1]
self.url = f"https://pypi.org/project/{self.name}"
self.files = []
def __str__(self):
return f"{self.name} {self.version}\r\n{self.description}\r\nWebsite: {self.url}"
class Distribution:
"""Handles operations on a WinPython distribution."""
def __init__(self, target: str = None, verbose: bool = False):
self.target = target or str(Path(sys.executable).parent) # Default target more explicit
self.verbose = verbose
self.pip = None
self.to_be_removed = []
self.version, self.architecture = utils.get_python_infos(self.target)
self.python_exe = utils.get_python_executable(self.target)
self.short_exe = Path(self.python_exe).name
self.wheelhouse = Path(self.target).parent / "wheelhouse"
def create_file(self, package, name, dstdir, contents):
"""Generate data file -- path is relative to distribution root dir"""
dst = Path(dstdir) / name
if self.verbose:
print(f"create: {dst}")
full_dst = Path(self.target) / dst
with open(full_dst, "w") as fd:
fd.write(contents)
package.files.append(str(dst))
def get_installed_packages(self, update: bool = False) -> list[Package]:
"""Return installed packages."""
if str(Path(sys.executable).parent) == self.target:
self.pip = piptree.PipData()
else:
self.pip = piptree.PipData(utils.get_python_executable(self.target))
pip_list = self.pip.pip_list(full=True)
return [Package(f"{i[0].replace('-', '_').lower()}-{i[1]}-py3-none-any.whl", suggested_summary=i[2]) for i in pip_list]
def render_markdown_for_list(self, title, items):
"""Generates a Markdown section; name, url, version, summary"""
md = f"### {title}\n\n"
md += "Name | Version | Description\n"
md += "-----|---------|------------\n"
for name, url, version, summary in sorted(items, key=lambda p: (p[0].lower(), p[2])):
md += f"[{name}]({url}) | {version} | {summary}\n"
md += "\n"
return md
def get_package_index_data(self, python_executable_directory: str|None = None, winpyver2: str|None = None,
flavor: str|None = None, release_level: str|None = None,
wheeldir: str|None = None) -> dict:
"""Collects the package index data: distribution identity, tools, packages, wheelhouse."""
my_ver , my_arch = utils.get_python_infos(python_executable_directory or self.target)
my_winpyver2 = winpyver2 or os.getenv("WINPYVER2","")
is_winpython = my_winpyver2 != "" # otherwise a plain Python: don't brand it WinPython
my_winpyver2 = my_winpyver2 if my_winpyver2 != "" else my_ver
my_flavor = flavor or os.getenv("WINPYFLAVOR", "")
my_release_level = release_level or os.getenv("WINPYVER", "").replace(my_winpyver2+my_flavor, "")
tools_list = utils.get_installed_tools(utils.get_python_executable(python_executable_directory or self.target))
package_list = [(pkg.name, pkg.url, pkg.version, pkg.description) for pkg in self.get_installed_packages()]
wheelhouse_list = []
my_wheeldir = Path(wheeldir) if wheeldir else self.wheelhouse / 'included.wheels'
if my_wheeldir.is_dir():
wheelhouse_list = [(name, f"https://pypi.org/project/{name}", version, utils.sum_up(summary))
for name, version, summary in wh.list_packages_with_metadata(str(my_wheeldir)) ]
as_records = lambda items: [{"name": n, "url": u, "version": v, "summary": s} for n, u, v, s in items]
return {
"distribution": {
"name": "WinPython" if is_winpython else "Python",
"version": my_winpyver2 + my_flavor,
"python_version": my_ver,
"architecture_bits": my_arch,
"release_level": my_release_level,
},
"tools": as_records(tools_list),
"packages": as_records(package_list),
"wheelhouse": as_records(wheelhouse_list),
}
def generate_package_index_markdown(self, python_executable_directory: str|None = None, winpyver2: str|None = None,
flavor: str|None = None, release_level: str|None = None,
wheeldir: str|None = None) -> str:
"""Generates a Markdown formatted package index page."""
data = self.get_package_index_data(python_executable_directory, winpyver2, flavor, release_level, wheeldir)
d = data["distribution"]
as_tuples = lambda items: [(i["name"], i["url"], i["version"], i["summary"]) for i in items]
return f"""## {d["name"]} {d["version"]}
The following packages are included in {d["name"]}-{d["architecture_bits"]}bit v{d["version"]} {d["release_level"]}.
{self.render_markdown_for_list("Tools", as_tuples(data["tools"]))}
{self.render_markdown_for_list("Python packages", as_tuples(data["packages"]))}
{self.render_markdown_for_list("WheelHouse packages", as_tuples(data["wheelhouse"]))}
"""
def find_package(self, name: str) -> Package | None:
"""Find installed package by name."""
for pack in self.get_installed_packages():
if utils.normalize(pack.name) == utils.normalize(name):
return pack
def patch_all_shebang(self, to_movable: bool = True, max_exe_size: int = 999999, targetdir: str = ""):
"""Make all python launchers relative."""
for ffname in Path(self.target).glob("Scripts/*.exe"):
if ffname.stat().st_size <= max_exe_size:
utils.patch_shebang_line(ffname, to_movable=to_movable, targetdir=targetdir)
for ffname in Path(self.target).glob("Scripts/*.py"):
utils.patch_shebang_line_py(ffname, to_movable=to_movable, targetdir=targetdir)
def install(self, package: Package, install_options: list[str] = None):
"""Install package in distribution."""
if package.fname.endswith((".whl", ".tar.gz", ".zip")) or (
' ' not in package.fname and ';' not in package.fname and len(package.fname) >1): # Check extension with tuple
self.install_bdist_direct(package, install_options=install_options)
self.handle_specific_packages(package)
# minimal post-install actions
self.patch_standard_packages(package.name)
def do_pip_action(self, actions: list[str] = None, install_options: list[str] = None):
"""Execute pip action in the distribution."""
my_list = install_options or []
my_actions = actions or []
executing = str(Path(self.target).parent / "scripts" / "env.bat")
if Path(executing).is_file():
complement = [r"&&", "cd", "/D", self.target, r"&&", utils.get_python_executable(self.target), "-m", "pip"]
else:
executing = utils.get_python_executable(self.target)
complement = ["-m", "pip"]
try:
fname = utils.do_script(this_script=None, python_exe=executing, verbose=self.verbose, install_options=complement + my_actions + my_list)
except RuntimeError as e:
if not self.verbose:
print("Failed!")
raise
else:
print(f"Pip action failed with error: {e}") # Print error if verbose
def patch_standard_packages(self, package_name="", to_movable=True):
"""patch Winpython packages in need"""
import filecmp
# 'pywin32' minimal post-install (pywin32_postinstall.py do too much)
if package_name.lower() in ("", "pywin32"):
origin = Path(self.target) / "site-packages" / "pywin32_system32"
destin = Path(self.target)
if origin.is_dir():
for name in os.listdir(origin):
here, there = origin / name, destin / name
if not there.exists() or not filecmp.cmp(here, there):
shutil.copyfile(here, there)
# 'pip' to do movable launchers (around line 100) !!!!
# rational: https://github.com/pypa/pip/issues/2328
if package_name.lower() == "pip" or package_name == "":
# ensure pip will create movable launchers
# sheb_mov1 = classic way up to WinPython 2016-01
# sheb_mov2 = tried way, but doesn't work for pip (at least)
the_place = Path(self.target) / "lib" / "site-packages" / "pip" / "_vendor" / "distlib" / "scripts.py"
sheb_fix = " executable = get_executable()"
sheb_mov1 = " executable = os.path.join(os.path.basename(get_executable()))"
sheb_mov2 = " executable = os.path.join('..',os.path.basename(get_executable()))"
if to_movable:
utils.patch_sourcefile(the_place, sheb_fix, sheb_mov1)
utils.patch_sourcefile(the_place, sheb_mov2, sheb_mov1)
else:
utils.patch_sourcefile(the_place, sheb_mov1, sheb_fix)
utils.patch_sourcefile(the_place, sheb_mov2, sheb_fix)
# create movable launchers for previous package installations
self.patch_all_shebang(to_movable=to_movable)
if package_name.lower() in ("", "spyder"):
# spyder don't goes on internet without you ask
utils.patch_sourcefile(
Path(self.target) / "lib" / "site-packages" / "spyder" / "config" / "main.py",
"'check_updates_on_startup': True,",
"'check_updates_on_startup': False,",
)
def handle_specific_packages(self, package):
"""Packages requiring additional configuration"""
if package.name.lower() in ("pyqt4", "pyqt5", "pyside2"):
# Qt configuration file (where to find Qt)
name = "qt.conf"
contents = """[Paths]\nPrefix = .\nBinaries = ."""
self.create_file(package, name, str(Path("Lib") / "site-packages" / package.name), contents)
self.create_file(package, name, ".", contents.replace(".", f"./Lib/site-packages/{package.name}"))
# pyuic script
if package.name.lower() == "pyqt5":
# see http://code.activestate.com/lists/python-list/666469/
tmp_string = r"""@echo off
if "%WINPYDIR%"=="" call "%~dp0..\..\scripts\env.bat"
"%WINPYDIR%\python.exe" -m PyQt5.uic.pyuic %1 %2 %3 %4 %5 %6 %7 %8 %9"""
else:
tmp_string = r"""@echo off
if "%WINPYDIR%"=="" call "%~dp0..\..\scripts\env.bat"
"%WINPYDIR%\python.exe" "%WINPYDIR%\Lib\site-packages\package.name\uic\pyuic.py" %1 %2 %3 %4 %5 %6 %7 %8 %9"""
tmp_string = tmp_string.replace("package.name", package.name)
self.create_file(package, f"pyuic{package.name[-1]}.bat", "Scripts", tmp_string)
# Adding missing __init__.py files (fixes Issue 8)
uic_path = str(Path("Lib") / "site-packages" / package.name / "uic")
for dirname in ("Loader", "port_v2", "port_v3"):
self.create_file(package, "__init__.py", str(Path(uic_path) / dirname), "")
def _print(self, package: Package, action: str):
"""Print package-related action text."""
text = f"{action} {package.name} {package.version}"
if self.verbose:
utils.print_box(text)
else:
print(f" {text}...", end=" ")
def _print_done(self):
"""Print OK at the end of a process"""
if not self.verbose:
print("OK")
def uninstall(self, package):
"""Uninstall package from distribution"""
self._print(package, "Uninstalling")
if package.name != "pip":
# trick to get true target (if not current)
this_exec = utils.get_python_executable(self.target)
subprocess.call([this_exec, "-m", "pip", "uninstall", package.name, "-y"], cwd=self.target)
self._print_done()
def install_bdist_direct(self, package, install_options=None):
"""Install a package directly !"""
self._print(package,f"Installing {package.fname.split('.')[-1]}")
try:
fname = utils.direct_pip_install(
package.fname,
python_exe=utils.get_python_executable(self.target),
verbose=self.verbose,
install_options=install_options,
)
except RuntimeError:
if not self.verbose:
print("Failed!")
raise
package = Package(fname)
self._print_done()
def few(names, limit=6):
"""First few names of a list, and how many more there were."""
return ", ".join(names[:limit]) + (f", and {len(names) - limit} more" if len(names) > limit else "")
def top_level_as_requirements(result, comments=(), source=None, verbose=False):
"""Render piptree.top_level() as the requirements file it proposes.
Plainly, it is that file and nothing else: the entries, plus the notes the
source itself carried, so redirecting the output replaces the source
without losing what its author wrote in it. -v adds the reasoning -- what
the list is made from and when, the same counts stderr gives, and every
dropped entry commented out with what pulls it in, so re-asking for one is
uncommenting it.
"""
lines = []
if verbose:
lines += [f"# {Path(source).name if source else 'installed packages'}, sorted,"
f" {datetime.now():%Y-%m-%d %H:%M:%S}"]
lines += top_level_summary(result) + [""]
lines += result["kept"]
dropped = result["dropped"]
if verbose and dropped:
lines += ["", "# ---- already pulled in by an entry above ----"]
for text, pullers in dropped.items():
lines.append(f"#{text} # <- {', '.join(pullers[:5])}{', ...' if len(pullers) > 5 else ''}")
if comments:
lines += ["", "# ---- notes kept from the source ----"] + list(comments)
return lines
def top_level_summary(result):
"""What the caller should know about the answer, rather than of it.
Goes to stderr, so a redirected list stays a list -- and heads the list
itself under -v, where the reasoning belongs in the file. Commented either
way: someone will fold the two streams into one file sooner or later, and a
comment costs nothing.
"""
kept, dropped = result["kept"], result["dropped"]
notes = [f"{len(kept) + len(dropped)} entries -> {len(kept)} kept, {len(dropped)} already pulled in"]
if result["duplicates"]:
notes.append(f"{len(result['duplicates'])} repeated, collapsed: {few(result['duplicates'], 12)}")
if result["unknown"]:
notes.append(f"{len(result['unknown'])} not installed in the target, so left unresolved: {few(result['unknown'])}")
return [f"# {note}" for note in notes]
def main(test=False):
# package summaries may contain characters the console codepage can't encode (emoji): don't crash
if sys.stdout and hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(errors="replace")
registerWinPythonHelp = f"Register the target Python in Windows (file extensions, icons, context menu, start menu), under the 'WinPython' PEP-514 vendor key"
unregisterWinPythonHelp = f"Unregister the target Python from Windows: de-associate file extensions, icons and context menu, and remove its start menu folder"
parser = ArgumentParser(prog="wppm",
description=f"WinPython Package Manager: handle a Python distribution (WinPython or not) and its packages ({__version__})",
formatter_class=RawTextHelpFormatter,
)
parser.add_argument("fname", metavar="package(s) or lockfile", nargs="*", default=[""], type=str, help="optional package names, wheels, or lockfile")
parser.add_argument("-v", "--verbose", action="store_true", help="show more details on packages and actions")
parser.add_argument( "--register", dest="registerWinPython", action="store_true", help=registerWinPythonHelp)
parser.add_argument("--unregister", dest="unregisterWinPython", action="store_true", help=unregisterWinPythonHelp)
parser.add_argument("--fix", action="store_true", help="make the target Python use absolute (fixed) paths in launchers and shebangs")
parser.add_argument("--movable", action="store_true", help="make the target Python (any Windows Python) movable/portable: relative paths in launchers and shebangs")
parser.add_argument("-ws", dest="wheelsource", default=None, type=str, help="wheels location, ('.' = WheelHouse): wppm pylock.toml -ws source_of_wheels, wppm -ls -ws .")
parser.add_argument("-wd", dest="wheeldrain" , default=None, type=str, help="wheels destination: wppm pylock.toml -wd destination_of_wheels")
parser.add_argument("-ls", "--list", action="store_true", help="list installed packages matching [optional] expression: wppm -ls, wppm -ls pand")
parser.add_argument("-lsa", dest="all", action="store_true",help=f"list details of packages matching [optional] expression: wppm -lsa pandas -l1")
parser.add_argument("-md", dest="markdown", action="store_true",help=f"markdown summary of the installation")
parser.add_argument("-diff", dest="diff", nargs=2, metavar=("INDEX1", "INDEX2"), default=None,
help="compare two package indexes, whichever two you choose: wppm -diff WinPythonslim-64bit-3.13.5.0.md WinPythonslim-64bit-3.14.7.0.md\nany two releases or flavors, in either direction -- they need not be consecutive")
parser.add_argument("-p",dest="pipdown",action="store_true",help="show Package (!= missing) dependencies of the given package[option], [.]=all: wppm -p pandas[.]")
parser.add_argument("-r", dest="pipup", action="store_true", help=f"show Reverse (!= constraining) dependancies of the given package[option]: wppm -r pytest![test]")
parser.add_argument("-tl", "--top-level", action="store_true", help="keep only the entries no other entry pulls in, sorted: wppm -tl, wppm requirements.txt -tl -v\nwith -p or -r, start the tree from them instead of every package: wppm -tl -p")
parser.add_argument("-l", dest="levels", type=int, default=-1, help="show 'LEVELS' levels of dependencies (with -p, -r): wppm -p pandas -l1")
parser.add_argument("-j", "--json", dest="json", action="store_true", help="machine-readable JSON output (with -p, -r, -ls, -md, -tl): wppm -p pandas[.] -j")
parser.add_argument("-t", dest="target", default=sys.prefix, help=f'path to target Python distribution (default: "{sys.prefix}")')
parser.add_argument("-i", "--install", action="store_true", help="install a given package wheel or pylock file (use pip for more features)")
parser.add_argument("-u", "--uninstall", action="store_true", help="uninstall package (use pip for more features)")
args = parser.parse_args()
if args.diff:
# no distribution involved: this reads two files. The indexes are in
# the changelogs/ directory of the winpython repository, one per
# release and flavor, and any two of them can be compared.
# utf-8 here and not just errors="replace": an upgrade is written
# "1.2.3 → 1.2.4", and replacing that arrow with "?" is most of the
# output on a page whose whole subject is upgrades.
if sys.stdout and hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
print(diff.compare_files(*args.diff))
sys.exit()
targetpython = None
if args.target and args.target != sys.prefix:
targetpython = args.target if args.target.lower().endswith('.exe') else str(Path(args.target) / 'python.exe')
if args.wheelsource == ".": # play in default WheelHouse
if utils.is_python_distribution(args.target):
dist = Distribution(args.target)
args.wheelsource = dist.wheelhouse / 'included.wheels'
if args.install and args.uninstall:
raise RuntimeError("Incompatible arguments: --install and --uninstall")
if args.registerWinPython and args.unregisterWinPython:
raise RuntimeError("Incompatible arguments: --install and --uninstall")
if args.pipdown or args.pipup:
pip = piptree.PipData(targetpython, args.wheelsource)
# -tl is then a root filter, not a list of its own: "." asked for implicitly,
# since the top-level entries are the only sensible roots of a whole-environment forest
names = args.fname if any(args.fname) else (["."] if args.top_level else args.fname)
walk, default_depth = (pip.down, 2) if args.pipdown else (pip.up, 1)
if args.top_level:
# a forest of roots is only worth reading whole, and any round number is a
# limit someone hits: go deeper than any real tree, -l says otherwise
default_depth = 99
for args_fname in names:
pack, extra, *other = (args_fname + "[").replace("]", "[").split("[")
print(walk(pack, extra, args.levels if args.levels>=0 else default_depth, verbose=args.verbose,
format="json" if args.json else "text", top_level=args.top_level))
sys.exit()
elif args.top_level:
pip = piptree.PipData(targetpython, args.wheelsource)
source = next((Path(f) for f in args.fname if f and Path(f).is_file()), None)
entries, comments = utils.read_requirements(source) if source else (None, [])
result = pip.top_level(entries)
if args.json:
print(json.dumps(result, indent=4))
sys.exit()
for line in top_level_as_requirements(result, comments, source, args.verbose):
print(line)
if not args.verbose: # -v already heads the list with them; don't say it twice
for note in top_level_summary(result):
print(note, file=sys.stderr)
sys.exit()
elif args.list:
pip = piptree.PipData(targetpython, args.wheelsource)
todo= []
for args_fname in args.fname:
todo += [l for l in pip.pip_list(full=True) if bool(re.search(args_fname, l[0]))]
todo = sorted(set(todo)) #, key=lambda p: (p[0].lower(), p[2])
if args.json:
print(json.dumps([{"package": p, "version": v, "summary": s} for p, v, s in todo], indent=4))
sys.exit()
titles = [['Package', 'Version', 'Summary'], ['_' * max(x, 6) for x in utils.columns_width(todo)]]
listed = utils.formatted_list(titles + todo, max_width=70)
for p in listed:
print(*p)
sys.exit()
elif args.all:
pip = piptree.PipData(targetpython, args.wheelsource)
for args_fname in args.fname:
todo = [l for l in pip.pip_list(full=True) if bool(re.search(args_fname, l[0]))]
for l in sorted(set(todo)):
title = f"** Package: {l[0]} **"
print("\n" + "*" * len(title), f"\n{title}", "\n" + "*" * len(title))
for key, value in pip.raw[l[0]].items():
rawtext = json.dumps(value, indent=2, ensure_ascii=False)
lines = [l for l in rawtext.split(r"\n") if len(l.strip()) > 2]
if key.lower() != 'description' or args.verbose:
print(f"{key}: ", "\n".join(lines).replace('"', ""))
sys.exit()
if args.registerWinPython:
print(registerWinPythonHelp)
if utils.is_python_distribution(args.target):
dist = Distribution(args.target)
else:
raise OSError(f"Invalid Python distribution {args.target}")
print(f"registering {args.target}")
print("continue ? Y/N")
theAnswer = input()
if theAnswer == "Y":
import associate
associate.register(dist.target, verbose=args.verbose)
sys.exit()
if args.unregisterWinPython:
print(unregisterWinPythonHelp)
if utils.is_python_distribution(args.target):
dist = Distribution(args.target)
else:
raise OSError(f"Invalid Python distribution {args.target}")
print(f"unregistering {args.target}")
print("continue ? Y/N")
theAnswer = input()
if theAnswer == "Y":
import associate
associate.unregister(dist.target, verbose=args.verbose)
sys.exit()
if utils.is_python_distribution(args.target):
dist = Distribution(args.target, verbose=True)
cmd_fix = rf"from wppm import wppm;dist=wppm.Distribution(r'{dist.target}');dist.patch_standard_packages('pip', to_movable=False)"
cmd_mov = rf"from wppm import wppm;dist=wppm.Distribution(r'{dist.target}');dist.patch_standard_packages('pip', to_movable=True)"
if args.fix:
# dist.patch_standard_packages('pip', to_movable=False) # would fail on wppm.exe
p = subprocess.Popen(["start", "cmd", "/k",dist.python_exe, "-c" , cmd_fix], shell = True, cwd=dist.target)
sys.exit()
if args.movable:
p = subprocess.Popen(["start", "cmd", "/k",dist.python_exe, "-c" , cmd_mov], shell = True, cwd=dist.target)
sys.exit()
if args.markdown:
if args.json:
print(json.dumps(dist.get_package_index_data(wheeldir=args.wheelsource), indent=4))
sys.exit()
default = dist.generate_package_index_markdown()
if args.wheelsource:
compare = dist.generate_package_index_markdown(wheeldir = args.wheelsource)
print(diff.compare_markdown_sections(default, compare,'python', 'wheelhouse', 'installed', 'wheelhouse'))
else:
print(default)
sys.exit()
if not args.install and not args.uninstall and args.fname[0].endswith(".toml"):
args.install = True # for Drag & Drop of .toml (and not wheel)
if args.fname[0] == "" or (not args.install and not args.uninstall):
parser.print_help()
sys.exit()
else:
try:
for args_fname in args.fname:
filename = Path(args_fname).name
install_from_wheelhouse = ["--no-index", "--trusted-host=None", f"--find-links={dist.wheelhouse / 'included.wheels'}"]
if filename.split('.')[0] == "pylock" and filename.split('.')[-1] == 'toml':
print(' a lock file !', args_fname, dist.target)
wh.get_pylock_wheels(dist.wheelhouse, Path(args_fname), args.wheelsource, args.wheeldrain)
sys.exit()
if args.uninstall:
package = dist.find_package(args_fname)
dist.uninstall(package)
elif args.install:
package = Package(args_fname)
if args.install:
dist.install(package, install_options=install_from_wheelhouse)
except NotImplementedError:
raise RuntimeError("Package is not (yet) supported by WPPM")
else:
raise OSError(f"Invalid Python distribution {args.target}")
if __name__ == "__main__":
main()