@@ -12,11 +12,12 @@ import sys
1212PHP_VERSION_RE = re .compile (r"^Package: (php\d+\.\d+)-" )
1313
1414
15- def get_output (args ) :
15+ def get_output (args : list [ str ]) -> str :
1616 return subprocess .check_output (args , text = True ).strip ()
1717
18+
1819ARCH = get_output (["dpkg" , "--print-architecture" ])
19- CODENAME = get_output (["lsb_release" , "-c " ]). split ()[ 1 ]
20+ CODENAME = get_output (["lsb_release" , "-sc " ])
2021
2122
2223SECURITY_NOTICE = """
@@ -30,18 +31,19 @@ NOTE:
3031 applies for your use case.
3132"""
3233
34+
3335def get_supported_versions () -> dict [str , str ]:
34- with request .urlopen ('http ://www.php.net/releases/active' ) as iob :
36+ with request .urlopen ("https ://www.php.net/releases/active" ) as iob :
3537 data = json .loads (iob .read ().decode ())
3638 versions = {}
3739
3840 for major in data .keys ():
3941 for major_minor in data [major ].keys ():
40- tags = data [major ][major_minor ][' tags' ]
41- if ' security' in tags :
42- versions [major_minor ] = ' security only'
42+ tags = data [major ][major_minor ][" tags" ]
43+ if " security" in tags :
44+ versions [major_minor ] = " security only"
4345 else :
44- versions [major_minor ] = ' supported'
46+ versions [major_minor ] = " supported"
4547 return versions
4648
4749
@@ -68,11 +70,11 @@ def choose_version(choices, version_support, deb_version) -> str:
6870
6971 print ("Select the PHP version to install:" )
7072 for i , opt in enumerate (choices ):
71- support = version_support .get (opt [3 :], ' end of life' )
73+ support = version_support .get (opt [3 :], " end of life" )
7274 if opt [3 :] == deb_version :
73- print (f"{ i + 1 } ). { opt } ({ support } ) (provided by debian)" )
75+ print (f"{ i + 1 } ). { opt } ({ support } ) (provided by debian)" )
7476 else :
75- print (f"{ i + 1 } ). { opt } ({ support } )" )
77+ print (f"{ i + 1 } ). { opt } ({ support } )" )
7678
7779 inp = input (f"\n Enter choice [1-{ len (choices )} ]: " )
7880
@@ -88,6 +90,7 @@ def choose_version(choices, version_support, deb_version) -> str:
8890
8991 return choices [inp - 1 ]
9092
93+
9194def get_packages (php_version ):
9295 curr_package = None
9396 packages = {}
@@ -103,40 +106,67 @@ def get_packages(php_version):
103106 curr_package = None
104107 return packages .keys ()
105108
109+
106110def debian_version ():
107- out = get_output ([' apt-cache' , ' policy' , ' php' ])
108- out = out .split (' Version table:' )[1 ].strip ()
111+ out = get_output ([" apt-cache" , " policy" , " php" ])
112+ out = out .split (" Version table:" )[1 ].strip ()
109113
110114 last_line = None
111115 for line in out .splitlines ():
112- if ' deb.debian.org/debian bookworm/main' in line :
116+ if " deb.debian.org/debian bookworm/main" in line :
113117 if last_line :
114- return last_line .strip ().split (':' )[1 ].split ('+' )[0 ]
118+ return last_line .strip ().split (":" )[1 ].split ("+" )[0 ]
115119 return None
116120 last_line = line
117121 return None
118122
123+
119124def check_new_packages (new_packages ):
120125 missing_packages = []
121126 found_packages = []
122127 for pkg in new_packages :
123- if subprocess .run (
124- ['apt-cache' , 'show' , pkg ],
125- stdout = DEVNULL , stderr = DEVNULL ).returncode != 0 :
128+ if (
129+ subprocess .run (
130+ ["apt-cache" , "show" , pkg ], stdout = DEVNULL , stderr = DEVNULL
131+ ).returncode
132+ != 0
133+ ):
126134 missing_packages .append (pkg )
127135 else :
128136 found_packages .append (pkg )
129137
130138 if missing_packages :
131- print ('The following packages do not exist in the requested PHP version:' )
132- print (' ' + ', ' .join (missing_packages ))
139+ print (
140+ "The following packages do not exist in the requested PHP version:"
141+ )
142+ print (" " + ", " .join (missing_packages ))
133143
134144 return found_packages
135145
146+
136147def is_active (service ):
137- return subprocess .run (['systemctl' , 'is-active' , '--quiet' , service ]).returncode == 0
148+ return (
149+ subprocess .run (
150+ ["systemctl" , "is-active" , "--quiet" , service ]
151+ ).returncode == 0
152+ )
153+
138154
139- if __name__ == '__main__' :
155+ def php_sources (codename : str = CODENAME , enabled : bool = True ) -> None :
156+ enabled_str = "yes" if enabled else "no"
157+ with open ("/etc/apt/sources.list.d/php.sources" , "w" ) as fob :
158+ fob .write (
159+ "# DEB.SURY.ORG repo for php\n "
160+ "Types: deb\n "
161+ "URIs: https://packages.sury.org/php/\n "
162+ f"Suites: { CODENAME } \n "
163+ "Components: main\n "
164+ f"Enabled: { enabled_str } \n "
165+ "Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg\n "
166+ )
167+
168+
169+ if __name__ == "__main__" :
140170 print ("Starting PHP upgrade script ..." )
141171 print ("Current PHP version" )
142172 subprocess .run (["/usr/bin/php" , "-v" ])
@@ -145,72 +175,66 @@ if __name__ == '__main__':
145175 ["php" , "-r" , 'echo PHP_MAJOR_VERSION . "." . PHP_MINOR_VERSION;' ]
146176 )
147177
148- print (' Determining supported version ...' )
178+ print (" Determining supported version ..." )
149179 version_support = get_supported_versions ()
150180 deb_version = debian_version ()
151181 if not deb_version :
152182 print ('Running "apt-get update" ...' )
153183
154- subprocess .run ([' apt-get' , ' update' ])
184+ subprocess .run ([" apt-get" , " update" ])
155185 deb_version = debian_version ()
156186
157-
158- new_php_version = choose_version (get_php_versions (), version_support ,
159- deb_version )
187+ new_php_version = choose_version (get_php_versions (), version_support , deb_version )
160188 new_php_pkgs = [
161189 pkg .replace ("php" + current_php_version , new_php_version )
162190 for pkg in get_packages (current_php_version )
163191 ]
164192
165- if new_php_version [3 :] == deb_version :
166- if os .path .isfile ('/etc/apt/sources.list.d/php.list' ):
167- os .remove ('/etc/apt/sources.list.d/php.list' )
168- subprocess .run (['dpkg' , '-r' , 'debsuryorg-archive-keyring' ])
193+ # remove legacy php.list file if it exists
194+ if os .path .isfile ("/etc/apt/sources.list.d/php.list" ):
195+ os .remove ("/etc/apt/sources.list.d/php.list" )
169196
197+ if new_php_version [3 :] == deb_version :
198+ if os .path .isfile ("/etc/apt/sources.list.d/php.list" ):
199+ subprocess .run (["dpkg" , "-r" , "debsuryorg-archive-keyring" ])
170200 else :
201+ print ("Downloading Sury Archive Keyring ..." )
171202
172- print ('Downloading Sury Archive Keyring ...' )
173-
174- url = 'https://packages.sury.org/debsuryorg-archive-keyring.deb'
203+ url = "https://packages.sury.org/debsuryorg-archive-keyring.deb"
175204 with request .urlopen (url ) as iob :
176- with open (' /tmp/debsuryorg-archive.keyring.deb' , 'wb' ) as fob :
205+ with open (" /tmp/debsuryorg-archive.keyring.deb" , "wb" ) as fob :
177206 fob .write (iob .read ())
178207
179- subprocess .run (['dpkg' , '-i' , '/tmp/debsuryorg-archive.keyring.deb' ])
180-
181- with open ('/etc/apt/sources.list.d/php.list' , 'w' ) as fob :
182- fob .write (
183- 'deb [signed-by=/usr/share/keyrings/debsuryorg-archive-keyring.gpg]'
184- f' https://packages.sury.org/php/ { CODENAME } main'
185- )
208+ subprocess .run (["dpkg" , "-i" , "/tmp/debsuryorg-archive.keyring.deb" ])
186209
187210 print ('Running "apt-get update" ...' )
188211
189- subprocess .run ([' apt-get' , ' update' ])
212+ subprocess .run ([" apt-get" , " update" ])
190213
191214 found_packages = check_new_packages (new_php_pkgs )
192215
193- print (' Installing new packages ...' )
216+ print (" Installing new packages ..." )
194217
195- subprocess .run ([' apt-get' , '-y' , ' install' , * found_packages ])
218+ subprocess .run ([" apt-get" , "-y" , " install" , * found_packages ])
196219
197- print (' Configuring Webserver ...' )
220+ print (" Configuring Webserver ..." )
198221
199- if is_active (' apache2' ):
200- subprocess .run ([' a2dismod' , ' php' + current_php_version ])
201- subprocess .run ([' a2enmod' , new_php_version ])
202- subprocess .run ([' systemctl' , ' restart' , ' apache2' ])
203- elif is_active (' nginx' ):
204- subprocess .run ([' systemctl' , ' restart' , new_php_version + ' -fpm' ])
205- elif is_active (' lighttpd' ):
206- subprocess .run ([' systemctl' , ' restart' , new_php_version + ' -fpm' ])
222+ if is_active (" apache2" ):
223+ subprocess .run ([" a2dismod" , " php" + current_php_version ])
224+ subprocess .run ([" a2enmod" , new_php_version ])
225+ subprocess .run ([" systemctl" , " restart" , " apache2" ])
226+ elif is_active (" nginx" ):
227+ subprocess .run ([" systemctl" , " restart" , new_php_version + " -fpm" ])
228+ elif is_active (" lighttpd" ):
229+ subprocess .run ([" systemctl" , " restart" , new_php_version + " -fpm" ])
207230 else :
208231 print (
209232 "No supported web server detected. Please manually configure "
210233 "your web server to use the new PHP version."
211234 )
212235
213- subprocess .run (['update-alternatives' , '--set' , 'php' , '/usr/bin/' +
214- new_php_version ])
236+ subprocess .run (
237+ ["update-alternatives" , "--set" , "php" , "/usr/bin/" + new_php_version ]
238+ )
215239
216- print (' PHP upgrade completed successfully.' )
240+ print (" PHP upgrade completed successfully." )
0 commit comments