-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_python.py
executable file
·110 lines (90 loc) · 2.95 KB
/
update_python.py
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
#!/usr/bin/env python3
"""
Copyright 2024 Christian Rickert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Author: Christian Rickert <[email protected]>
Title: update_python.py
Summary: Update Python modules via `pip` (2024-11-11)
URL: https://github.com/christianrickert/updates
"""
# imports
import json
import os
import subprocess
import sys
os.environ["PIP_DISABLE_PIP_VERSION_CHECK"] = "True" # don't check PyPI for new version
os.environ["PIP_EXCLUDE"] = "packaging pip wheel" # may be externally managed
# functions
def clear_module_cache():
"""Clear `pip` module cache.
Keyword arguments:
None
"""
print("=> Clearing cached modules...")
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"cache",
"purge",
]
)
def find_outdated_modules():
"""Get the list of outdated module names from a `pip` JSON data structure.
Keyword arguments:
None
"""
print("=> Checking for outdated modules...")
outdated_dictionary = json.loads(
subprocess.check_output(
[
sys.executable,
"-m",
"pip",
"list",
"--outdated",
"--format=json",
]
).decode("utf-8")
) # name, version, latest_version, latest_filetype (keys)
outdated_modules = [
outdated_module["name"] for outdated_module in outdated_dictionary
] or None # name (value)
print(f"Old: {outdated_modules}")
return outdated_modules
def update_outdated_modules(outdated_modules=None):
"""Update outdated modules based on names from a `pip` JSON data structure.
Keyword arguments:
outdated_modules - list of outdated module names
"""
print("=> Updating outdated modules...")
if outdated_modules:
subprocess.check_call(
[
sys.executable,
"-m",
"pip",
"install",
"--break-system-packages", # modify externally-managed installation
"--upgrade",
*outdated_modules,
]
)
else:
print("New: None")
# main code
if __name__ == "__main__":
print(f"=> Using Python executable:\n{sys.executable}")
clear_module_cache()
names = find_outdated_modules()
update_outdated_modules(names)