Skip to content

Commit ea8be5b

Browse files
committed
mostly just typing/linting and minor refactoring - untested and more to do...
1 parent 627f2c5 commit ea8be5b

1 file changed

Lines changed: 26 additions & 19 deletions

File tree

overlays/php/usr/local/bin/tkl-upgrade-php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ NOTE:
2525
Older version of PHP may not be actively maintained, this includes not just
2626
bug fixes but also security fixes.
2727
28-
Versions which have reached EOL should not be expected to be secure.
28+
Versions which have reached EOL should not be expected to be secure.
2929
3030
It is always encouraged that you only install the newest version that
3131
applies for your use case.
@@ -51,8 +51,9 @@ def get_php_versions() -> list[str]:
5151
"""Get all available php versions and return them in the form phpx.y"""
5252
print("\nChecking Available PHP Versions")
5353

54-
url = f"https://packages.sury.org/php/dists/{CODENAME}/main/binary-{ARCH}/Packages"
55-
with request.urlopen(url) as iob:
54+
url = "https://packages.sury.org"
55+
pkg_file_url = f"{url}/php/dists/{CODENAME}/main/binary-{ARCH}/Packages"
56+
with request.urlopen(pkg_file_url) as iob:
5657
data = iob.read().decode()
5758

5859
php_versions = set()
@@ -82,29 +83,28 @@ def choose_version(choices, version_support, deb_version) -> str:
8283
print("Input must be a number! Exiting")
8384
sys.exit(1)
8485

85-
inp = int(inp)
86+
choice_inp = int(inp)
8687

87-
if inp < 1 or inp > len(choices):
88+
if choice_inp < 1 or choice_inp > len(choices):
8889
print(f"Input out of range [1-{len(choices)}]! Exiting")
8990
sys.exit(1)
9091

91-
return choices[inp - 1]
92+
return choices[choice_inp - 1]
9293

9394

94-
def get_packages(php_version):
95-
curr_package = None
96-
packages = {}
95+
def get_packages(php_version: str) -> list[str]:
96+
curr_package = ""
97+
packages: dict[str, str] = {}
9798
for line in get_output(
9899
["apt-cache", "policy", "--", "*" + php_version]
99100
).splitlines():
100101
if not line[0].isspace():
101102
curr_package = line.rstrip(":")
102-
packages[curr_package] = []
103-
else:
104-
if "Installed: (none)" in line:
105-
del packages[curr_package]
106-
curr_package = None
107-
return packages.keys()
103+
packages[curr_package] = ""
104+
elif "Installed: (none)" in line:
105+
del packages[curr_package]
106+
curr_package = ""
107+
return list(packages.keys())
108108

109109

110110
def debian_version():
@@ -113,7 +113,7 @@ def debian_version():
113113

114114
last_line = None
115115
for line in out.splitlines():
116-
if "deb.debian.org/debian bookworm/main" in line:
116+
if f"deb.debian.org/debian {CODENAME}/main" in line:
117117
if last_line:
118118
return last_line.strip().split(":")[1].split("+")[0]
119119
return None
@@ -162,10 +162,15 @@ def php_sources(codename: str = CODENAME, enabled: bool = True) -> None:
162162
f"Suites: {CODENAME}\n"
163163
"Components: main\n"
164164
f"Enabled: {enabled_str}\n"
165-
"Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg\n"
165+
"Signed-By: /usr/share/keyrings/debsuryorg-archive-keyring.gpg\n"
166166
)
167167

168168

169+
def update_pins(new_php_version: str) -> None:
170+
"""Update/write apt pins (preferences)."""
171+
# TODO....
172+
173+
169174
if __name__ == "__main__":
170175
print("Starting PHP upgrade script ...")
171176
print("Current PHP version")
@@ -184,7 +189,9 @@ if __name__ == "__main__":
184189
subprocess.run(["apt-get", "update"])
185190
deb_version = debian_version()
186191

187-
new_php_version = choose_version(get_php_versions(), version_support, deb_version)
192+
new_php_version = choose_version(
193+
get_php_versions(), version_support, deb_version
194+
)
188195
new_php_pkgs = [
189196
pkg.replace("php" + current_php_version, new_php_version)
190197
for pkg in get_packages(current_php_version)
@@ -196,7 +203,7 @@ if __name__ == "__main__":
196203

197204
if new_php_version[3:] == deb_version:
198205
if os.path.isfile("/etc/apt/sources.list.d/php.list"):
199-
subprocess.run(["dpkg", "-r", "debsuryorg-archive-keyring"])
206+
subprocess.run(["dpkg", "-r", "debsuryorg-archive-keyring"])
200207
else:
201208
print("Downloading Sury Archive Keyring ...")
202209

0 commit comments

Comments
 (0)