blob: e775c3a6ecdc20e5c859ea4ed63fb9e77384ef95 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001def create_socket(url, d):
2 import urllib
3 from bb.utils import export_proxies
4
5 export_proxies(d)
6 return urllib.request.urlopen(url)
7
8def get_links_from_url(url, d):
9 "Return all the href links found on the web location"
10
11 from bs4 import BeautifulSoup, SoupStrainer
12
13 soup = BeautifulSoup(create_socket(url,d), "html.parser", parse_only=SoupStrainer("a"))
14 hyperlinks = []
15 for line in soup.find_all('a', href=True):
16 hyperlinks.append(line['href'].strip('/'))
17 return hyperlinks
18
19def find_latest_numeric_release(url, d):
20 "Find the latest listed numeric release on the given url"
21 max=0
22 maxstr=""
23 for link in get_links_from_url(url, d):
24 try:
25 # TODO use LooseVersion
26 release = float(link)
27 except:
28 release = 0
29 if release > max:
30 max = release
31 maxstr = link
32 return maxstr
33
34def is_src_rpm(name):
35 "Check if the link is pointing to a src.rpm file"
36 return name.endswith(".src.rpm")
37
38def package_name_from_srpm(srpm):
39 "Strip out the package name from the src.rpm filename"
40
41 # ca-certificates-2016.2.7-1.0.fc24.src.rpm
42 # ^name ^ver ^release^removed
43 (name, version, release) = srpm.replace(".src.rpm", "").rsplit("-", 2)
44 return name
45
46def get_source_package_list_from_url(url, section, d):
47 "Return a sectioned list of package names from a URL list"
48
49 bb.note("Reading %s: %s" % (url, section))
50 links = get_links_from_url(url, d)
51 srpms = filter(is_src_rpm, links)
52 names_list = map(package_name_from_srpm, srpms)
53
54 new_pkgs = set()
55 for pkgs in names_list:
56 new_pkgs.add(pkgs + ":" + section)
57 return new_pkgs
58
59def get_source_package_list_from_url_by_letter(url, section, d):
60 import string
61 from urllib.error import HTTPError
62 packages = set()
63 for letter in (string.ascii_lowercase + string.digits):
64 # Not all subfolders may exist, so silently handle 404
65 try:
66 packages |= get_source_package_list_from_url(url + "/" + letter, section, d)
67 except HTTPError as e:
68 if e.code != 404: raise
69 return packages
70
71def get_latest_released_fedora_source_package_list(d):
72 "Returns list of all the name os packages in the latest fedora distro"
73 latest = find_latest_numeric_release("http://archive.fedoraproject.org/pub/fedora/linux/releases/", d)
74 package_names = get_source_package_list_from_url_by_letter("http://archive.fedoraproject.org/pub/fedora/linux/releases/%s/Everything/source/tree/Packages/" % latest, "main", d)
75 package_names |= get_source_package_list_from_url_by_letter("http://archive.fedoraproject.org/pub/fedora/linux/updates/%s/SRPMS/" % latest, "updates", d)
76 return latest, package_names
77
78def get_latest_released_opensuse_source_package_list(d):
79 "Returns list of all the name os packages in the latest opensuse distro"
80 latest = find_latest_numeric_release("http://download.opensuse.org/source/distribution/leap", d)
81
82 package_names = get_source_package_list_from_url("http://download.opensuse.org/source/distribution/leap/%s/repo/oss/suse/src/" % latest, "main", d)
83 package_names |= get_source_package_list_from_url("http://download.opensuse.org/update/leap/%s/oss/src/" % latest, "updates", d)
84 return latest, package_names
85
86def get_latest_released_clear_source_package_list(d):
87 latest = find_latest_numeric_release("https://download.clearlinux.org/releases/", d)
88 package_names = get_source_package_list_from_url("https://download.clearlinux.org/releases/%s/clear/source/SRPMS/" % latest, "main", d)
89 return latest, package_names
90
91def find_latest_debian_release(url, d):
92 "Find the latest listed debian release on the given url"
93
94 releases = [link.replace("Debian", "")
95 for link in get_links_from_url(url, d)
96 if link.startswith("Debian")]
97 releases.sort()
98 try:
99 return releases[-1]
100 except:
101 return "_NotFound_"
102
103def get_debian_style_source_package_list(url, section, d):
104 "Return the list of package-names stored in the debian style Sources.gz file"
105 import gzip
106
107 package_names = set()
108 for line in gzip.open(create_socket(url, d), mode="rt"):
109 if line.startswith("Package:"):
110 pkg = line.split(":", 1)[1].strip()
111 package_names.add(pkg + ":" + section)
112 return package_names
113
114def get_latest_released_debian_source_package_list(d):
115 "Returns list of all the name of packages in the latest debian distro"
116 latest = find_latest_debian_release("http://ftp.debian.org/debian/dists/", d)
117 url = "http://ftp.debian.org/debian/dists/stable/main/source/Sources.gz"
118 package_names = get_debian_style_source_package_list(url, "main", d)
119 url = "http://ftp.debian.org/debian/dists/stable-proposed-updates/main/source/Sources.gz"
120 package_names |= get_debian_style_source_package_list(url, "updates", d)
121 return latest, package_names
122
123def find_latest_ubuntu_release(url, d):
124 """
125 Find the latest listed Ubuntu release on the given ubuntu/dists/ URL.
126
127 To avoid matching development releases look for distributions that have
128 updates, so the resulting distro could be any supported release.
129 """
130 url += "?C=M;O=D" # Descending Sort by Last Modified
131 for link in get_links_from_url(url, d):
132 if "-updates" in link:
133 distro = link.replace("-updates", "")
134 return distro
135 return "_NotFound_"
136
137def get_latest_released_ubuntu_source_package_list(d):
138 "Returns list of all the name os packages in the latest ubuntu distro"
139 latest = find_latest_ubuntu_release("http://archive.ubuntu.com/ubuntu/dists/", d)
140 url = "http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.gz" % latest
141 package_names = get_debian_style_source_package_list(url, "main", d)
142 url = "http://archive.ubuntu.com/ubuntu/dists/%s-updates/main/source/Sources.gz" % latest
143 package_names |= get_debian_style_source_package_list(url, "updates", d)
144 return latest, package_names
145
146def create_distro_packages_list(distro_check_dir, d):
147 import shutil
148
149 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
150 bb.utils.remove(pkglst_dir, True)
151 bb.utils.mkdirhier(pkglst_dir)
152
153 per_distro_functions = (
154 ("Debian", get_latest_released_debian_source_package_list),
155 ("Ubuntu", get_latest_released_ubuntu_source_package_list),
156 ("Fedora", get_latest_released_fedora_source_package_list),
157 ("openSUSE", get_latest_released_opensuse_source_package_list),
158 ("Clear", get_latest_released_clear_source_package_list),
159 )
160
161 for name, fetcher_func in per_distro_functions:
162 try:
163 release, package_list = fetcher_func(d)
164 except Exception as e:
165 bb.warn("Cannot fetch packages for %s: %s" % (name, e))
166 bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
167 if len(package_list) == 0:
168 bb.error("Didn't fetch any packages for %s %s" % (name, release))
169
170 package_list_file = os.path.join(pkglst_dir, name + "-" + release)
171 with open(package_list_file, 'w') as f:
172 for pkg in sorted(package_list):
173 f.write(pkg + "\n")
174
175def update_distro_data(distro_check_dir, datetime, d):
176 """
177 If distro packages list data is old then rebuild it.
178 The operations has to be protected by a lock so that
179 only one thread performes it at a time.
180 """
181 if not os.path.isdir (distro_check_dir):
182 try:
183 bb.note ("Making new directory: %s" % distro_check_dir)
184 os.makedirs (distro_check_dir)
185 except OSError:
186 raise Exception('Unable to create directory %s' % (distro_check_dir))
187
188
189 datetime_file = os.path.join(distro_check_dir, "build_datetime")
190 saved_datetime = "_invalid_"
191 import fcntl
192 try:
193 if not os.path.exists(datetime_file):
194 open(datetime_file, 'w+').close() # touch the file so that the next open won't fail
195
196 f = open(datetime_file, "r+")
197 fcntl.lockf(f, fcntl.LOCK_EX)
198 saved_datetime = f.read()
199 if saved_datetime[0:8] != datetime[0:8]:
200 bb.note("The build datetime did not match: saved:%s current:%s" % (saved_datetime, datetime))
201 bb.note("Regenerating distro package lists")
202 create_distro_packages_list(distro_check_dir, d)
203 f.seek(0)
204 f.write(datetime)
205
206 except OSError as e:
207 raise Exception('Unable to open timestamp: %s' % e)
208 finally:
209 fcntl.lockf(f, fcntl.LOCK_UN)
210 f.close()
211
212def compare_in_distro_packages_list(distro_check_dir, d):
213 if not os.path.isdir(distro_check_dir):
214 raise Exception("compare_in_distro_packages_list: invalid distro_check_dir passed")
215
216 localdata = bb.data.createCopy(d)
217 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
218 matching_distros = []
219 pn = recipe_name = d.getVar('PN')
220 bb.note("Checking: %s" % pn)
221
222 if pn.find("-native") != -1:
223 pnstripped = pn.split("-native")
224 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
225 recipe_name = pnstripped[0]
226
227 if pn.startswith("nativesdk-"):
228 pnstripped = pn.split("nativesdk-")
229 localdata.setVar('OVERRIDES', "pn-" + pnstripped[1] + ":" + d.getVar('OVERRIDES'))
230 recipe_name = pnstripped[1]
231
232 if pn.find("-cross") != -1:
233 pnstripped = pn.split("-cross")
234 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
235 recipe_name = pnstripped[0]
236
237 if pn.find("-initial") != -1:
238 pnstripped = pn.split("-initial")
239 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
240 recipe_name = pnstripped[0]
241
242 bb.note("Recipe: %s" % recipe_name)
243
244 distro_exceptions = dict({"OE-Core":'OE-Core', "OpenedHand":'OpenedHand', "Intel":'Intel', "Upstream":'Upstream', "Windriver":'Windriver', "OSPDT":'OSPDT Approved', "Poky":'poky'})
245 tmp = localdata.getVar('DISTRO_PN_ALIAS') or ""
246 for str in tmp.split():
247 if str and str.find("=") == -1 and distro_exceptions[str]:
248 matching_distros.append(str)
249
250 distro_pn_aliases = {}
251 for str in tmp.split():
252 if "=" in str:
253 (dist, pn_alias) = str.split('=')
254 distro_pn_aliases[dist.strip().lower()] = pn_alias.strip()
255
256 for file in os.listdir(pkglst_dir):
257 (distro, distro_release) = file.split("-")
258 f = open(os.path.join(pkglst_dir, file), "r")
259 for line in f:
260 (pkg, section) = line.split(":")
261 if distro.lower() in distro_pn_aliases:
262 pn = distro_pn_aliases[distro.lower()]
263 else:
264 pn = recipe_name
265 if pn == pkg:
266 matching_distros.append(distro + "-" + section[:-1]) # strip the \n at the end
267 f.close()
268 break
269 f.close()
270
271 for item in tmp.split():
272 matching_distros.append(item)
273 bb.note("Matching: %s" % matching_distros)
274 return matching_distros
275
276def create_log_file(d, logname):
277 logpath = d.getVar('LOG_DIR')
278 bb.utils.mkdirhier(logpath)
279 logfn, logsuffix = os.path.splitext(logname)
280 logfile = os.path.join(logpath, "%s.%s%s" % (logfn, d.getVar('DATETIME'), logsuffix))
281 if not os.path.exists(logfile):
282 slogfile = os.path.join(logpath, logname)
283 if os.path.exists(slogfile):
284 os.remove(slogfile)
285 open(logfile, 'w+').close()
286 os.symlink(logfile, slogfile)
287 d.setVar('LOG_FILE', logfile)
288 return logfile
289
290
291def save_distro_check_result(result, datetime, result_file, d):
292 pn = d.getVar('PN')
293 logdir = d.getVar('LOG_DIR')
294 if not logdir:
295 bb.error("LOG_DIR variable is not defined, can't write the distro_check results")
296 return
297 bb.utils.mkdirhier(logdir)
298
299 line = pn
300 for i in result:
301 line = line + "," + i
302 f = open(result_file, "a")
303 import fcntl
304 fcntl.lockf(f, fcntl.LOCK_EX)
305 f.seek(0, os.SEEK_END) # seek to the end of file
306 f.write(line + "\n")
307 fcntl.lockf(f, fcntl.LOCK_UN)
308 f.close()