-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackages.py
More file actions
executable file
·72 lines (62 loc) · 2.4 KB
/
packages.py
File metadata and controls
executable file
·72 lines (62 loc) · 2.4 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
#!/usr/bin/env -S uv --quiet run --no-project --script --
# https://peps.python.org/pep-0723/
# https://github.com/astral-sh/uv
# /// script
# requires-python = ">=3.14,<4"
# dependencies = [
# ]
# ///
import subprocess, datetime, os, sys, pathlib, plistlib, json
try:
pkg_ids = sys.argv[1:]
if not pkg_ids:
volumes = ["/", pathlib.Path.home()]
packages = []
for volume in volumes:
p = subprocess.run(["pkgutil", "--volume", volume, "--pkgs"], stdout=subprocess.PIPE)
if p.returncode:
continue
for pkg_id in p.stdout.splitlines():
pinfo = {}
for f in subprocess.check_output(["pkgutil", "--volume", volume, "--pkg-info", pkg_id], encoding="UTF-8").splitlines():
key, value = f.split(": ")
pinfo[key] = value
pinfo["install-time"] = datetime.datetime.fromtimestamp(int(pinfo["install-time"])).astimezone()
packages.append(pinfo)
for p in sorted(packages, key=lambda p: p["install-time"]):
print("{install-time:%Y-%m-%dT%H:%M:%S%z} {package-id:60} {version:30} {volume:13} {location}".format(**p))
sys.exit(0)
for pkg_id in pkg_ids:
p = subprocess.run(["pkgutil", "--export-plist", pkg_id], stdout=subprocess.PIPE, encoding=None)
if p.returncode:
continue
metadata = plistlib.loads(p.stdout)
assert metadata.pop("pkgid") == pkg_id
install_root = os.path.join(metadata.pop("volume"), metadata.pop("install-location"))
install_time = datetime.datetime.fromtimestamp(metadata.pop("install-time"))
installed_files = [os.path.join(install_root, i) for i in metadata.pop("paths").keys()]
print(install_time, pkg_id)
installed_files_missing = []
installed_files_present = []
for i in installed_files:
if os.path.exists(i):
installed_files_present.append(i)
else:
installed_files_missing.append(i)
print("Missing files:", json.dumps(installed_files_missing, indent=" "))
print("Present files:", json.dumps(installed_files_present, indent=" "))
user_input = input("\nTo delete present files and forget the package receipt type \"delete\": ")
if user_input != "delete":
print("Doing nothing!")
sys.exit(0)
for i in reversed(installed_files):
if not os.path.exists(i):
continue
print("Unlinking", json.dumps(i))
os.unlink(i)
print("Forgetting package receipt for", json.dumps(pkg_id))
p = subprocess.run(["pkgutil", "--forget", pkg_id])
if p.returncode:
print("FAILED:", p.returncode)
except KeyboardInterrupt:
print()