Debian Codegraph @

upstream/opensuse-leap-16-1/archive/update-packages.py

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright © 2020 Dr. Tobias Quathamer <[email protected]>
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 import json
19 import os
20 import re
21 import shutil
22 import subprocess
23 import sys
24 import urllib.request
25
26
27 # Read package information from a JSON file, if available
28 package_data = {}
29 if os.path.exists("downloads/packages.json"):
30 with open("downloads/packages.json") as input_file:
31 package_data = json.loads(input_file.read())
32
33 # Get current package information
34 download_page = urllib.request.urlopen(
35 "https://ftp.gwdg.de/pub/opensuse/distribution/leap/15.4/repo/oss/x86_64/"
36 )
37 contents = download_page.read().decode()
38 download_page.close()
39
40 # Also get noarch package information as a fallback
41 download_page = urllib.request.urlopen(
42 "https://ftp.gwdg.de/pub/opensuse/distribution/leap/15.4/repo/oss/noarch/"
43 )
44 contents_fallback = download_page.read().decode()
45 download_page.close()
46
47 # Read all packages names
48 with open("packages.txt") as input_file:
49 for line in input_file:
50 package = line.strip()
51
52 # Download HTML page and discover the correct link
53 print("Checking package '{}' ... ".format(package), end="")
54
55 # Match package names from x86_64, excluding 32-bit ones
56 package_url = re.findall(
57 r"\"({}-(?!32bit-)(?:[0-9a-z.%B]+)-(?:[0-9]+)\.(?:[0-9]+)\.x86_64.rpm)\"".format(
58 package
59 ),
60 contents,
61 )
62 arch = "x86_64"
63
64 # Fall back to noarch, place of packages without ELF binary
65 if len(package_url) == 0:
66 package_url = re.findall(
67 r"\"({}-(?:[0-9a-z.%B]+)-(?:[0-9]+)\.(?:[0-9]+)\.noarch.rpm)\"".format(
68 package
69 ),
70 contents_fallback,
71 )
72 arch = "noarch"
73
74 if len(package_url) != 1:
75 print()
76 print(
77 "Error: Could not find URL for '{}'.".format(package), file=sys.stderr
78 )
79 print(
80 "If this is a permanent error, please remove the package from the file packages.txt.",
81 file=sys.stderr,
82 )
83 continue
84 else:
85 package_url = package_url[0]
86
87 package_url = "https://ftp.gwdg.de/pub/opensuse/distribution/leap/15.3/repo/oss/{}/{}".format(
88 arch, package_url
89 )
90 package_filename = re.split(r"/", package_url)[-1]
91
92 # If the latest download is already available, skip this package
93 if package in package_data:
94 if package_data[package]["filename"] == package_filename:
95 print("current.")
96 continue
97
98 # Store information about downloaded package
99 package_data[package] = {
100 "filename": package_filename,
101 "needs_templates": True,
102 "needs_update": True,
103 }
104
105 # Prepare for downloading the file
106 package_dir = "downloads/{}".format(package)
107 local_filename = "downloads/{}/{}".format(package, package_filename)
108
109 # Ensure there are no leftovers of previous packages
110 os.makedirs(package_dir, exist_ok=True)
111 shutil.rmtree(package_dir)
112 os.makedirs(package_dir, exist_ok=True)
113
114 # Download new package
115 urllib.request.urlretrieve(package_url, local_filename)
116 print("downloaded.")
117
118 # Get the exact filename of data.tar with the extension
119 os.makedirs("{}/tmp".format(package_dir), exist_ok=True)
120
121 # Extract the package into a temporary file
122 with open("{}/tmp/tmpcpio".format(package_dir), "w") as outfile:
123 subprocess.run(["rpm2cpio", local_filename], stdout=outfile)
124 with open("{}/tmp/tmpcpio".format(package_dir), "r") as infile:
125 subprocess.run(
126 ["bsdcpio", "--quiet", "-i", "--make-directories"],
127 cwd="{}/tmp".format(package_dir),
128 stdin=infile,
129 )
130
131 # Move the manpages up
132 manpath = "{}/tmp/usr/share/man".format(package_dir)
133 for mandir in os.listdir(manpath):
134 if not mandir.startswith("man"):
135 continue
136 # Make sure the directory exists
137 os.makedirs(mandir, exist_ok=True)
138 shutil.move("{}/{}".format(manpath, mandir), "{}".format(package_dir))
139
140 # Cleanups
141 shutil.rmtree("downloads/{}/tmp".format(package))
142 os.remove(local_filename)
143
144 # Save package information in a JSON file
145 with open("downloads/packages.json", "w") as output_file:
146 output_file.write(json.dumps(package_data, sort_keys=True, indent=4))