blob: 3494520f40d5312697bb7fead4e0636f57fa8c27 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: GPL-2.0-only
5#
6
Patrick Williamsf1e5d692016-03-30 15:21:19 -05007def create_socket(url, d):
8 import urllib
Brad Bishop6e60e8b2018-02-01 10:27:11 -05009 from bb.utils import export_proxies
Patrick Williamsf1e5d692016-03-30 15:21:19 -050010
Brad Bishop6e60e8b2018-02-01 10:27:11 -050011 export_proxies(d)
12 return urllib.request.urlopen(url)
Patrick Williamsf1e5d692016-03-30 15:21:19 -050013
14def get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015 "Return all the href links found on the web location"
16
Patrick Williamsc0f7c042017-02-23 20:41:17 -060017 from bs4 import BeautifulSoup, SoupStrainer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Brad Bishop6e60e8b2018-02-01 10:27:11 -050019 soup = BeautifulSoup(create_socket(url,d), "html.parser", parse_only=SoupStrainer("a"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060020 hyperlinks = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -060021 for line in soup.find_all('a', href=True):
22 hyperlinks.append(line['href'].strip('/'))
23 return hyperlinks
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024
Patrick Williamsf1e5d692016-03-30 15:21:19 -050025def find_latest_numeric_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 "Find the latest listed numeric release on the given url"
27 max=0
28 maxstr=""
Patrick Williamsf1e5d692016-03-30 15:21:19 -050029 for link in get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 try:
Andrew Geissler595f6302022-01-24 19:11:47 +000031 # TODO use bb.utils.vercmp_string_op()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050032 release = float(link)
33 except:
34 release = 0
35 if release > max:
36 max = release
37 maxstr = link
38 return maxstr
39
40def is_src_rpm(name):
41 "Check if the link is pointing to a src.rpm file"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050042 return name.endswith(".src.rpm")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043
44def package_name_from_srpm(srpm):
45 "Strip out the package name from the src.rpm filename"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050046
Brad Bishop6e60e8b2018-02-01 10:27:11 -050047 # ca-certificates-2016.2.7-1.0.fc24.src.rpm
48 # ^name ^ver ^release^removed
49 (name, version, release) = srpm.replace(".src.rpm", "").rsplit("-", 2)
50 return name
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
Patrick Williamsf1e5d692016-03-30 15:21:19 -050052def get_source_package_list_from_url(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 "Return a sectioned list of package names from a URL list"
54
55 bb.note("Reading %s: %s" % (url, section))
Patrick Williamsf1e5d692016-03-30 15:21:19 -050056 links = get_links_from_url(url, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050057 srpms = filter(is_src_rpm, links)
58 names_list = map(package_name_from_srpm, srpms)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050059
Brad Bishop6e60e8b2018-02-01 10:27:11 -050060 new_pkgs = set()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 for pkgs in names_list:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050062 new_pkgs.add(pkgs + ":" + section)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050063 return new_pkgs
64
Brad Bishop6e60e8b2018-02-01 10:27:11 -050065def get_source_package_list_from_url_by_letter(url, section, d):
66 import string
67 from urllib.error import HTTPError
68 packages = set()
69 for letter in (string.ascii_lowercase + string.digits):
70 # Not all subfolders may exist, so silently handle 404
71 try:
72 packages |= get_source_package_list_from_url(url + "/" + letter, section, d)
73 except HTTPError as e:
74 if e.code != 404: raise
75 return packages
76
Patrick Williamsf1e5d692016-03-30 15:21:19 -050077def get_latest_released_fedora_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050078 "Returns list of all the name os packages in the latest fedora distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050079 latest = find_latest_numeric_release("http://archive.fedoraproject.org/pub/fedora/linux/releases/", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050080 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)
81 package_names |= get_source_package_list_from_url_by_letter("http://archive.fedoraproject.org/pub/fedora/linux/updates/%s/SRPMS/" % latest, "updates", d)
82 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050083
Patrick Williamsf1e5d692016-03-30 15:21:19 -050084def get_latest_released_opensuse_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 "Returns list of all the name os packages in the latest opensuse distro"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086 latest = find_latest_numeric_release("http://download.opensuse.org/source/distribution/leap", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088 package_names = get_source_package_list_from_url("http://download.opensuse.org/source/distribution/leap/%s/repo/oss/suse/src/" % latest, "main", d)
89 package_names |= get_source_package_list_from_url("http://download.opensuse.org/update/leap/%s/oss/src/" % latest, "updates", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
Brad Bishop6e60e8b2018-02-01 10:27:11 -050092def get_latest_released_clear_source_package_list(d):
93 latest = find_latest_numeric_release("https://download.clearlinux.org/releases/", d)
94 package_names = get_source_package_list_from_url("https://download.clearlinux.org/releases/%s/clear/source/SRPMS/" % latest, "main", d)
95 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050096
Patrick Williamsf1e5d692016-03-30 15:21:19 -050097def find_latest_debian_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050098 "Find the latest listed debian release on the given url"
99
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500100 releases = [link.replace("Debian", "")
101 for link in get_links_from_url(url, d)
102 if link.startswith("Debian")]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 releases.sort()
104 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500105 return releases[-1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106 except:
107 return "_NotFound_"
108
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500109def get_debian_style_source_package_list(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500110 "Return the list of package-names stored in the debian style Sources.gz file"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 import gzip
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600112
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500113 package_names = set()
114 for line in gzip.open(create_socket(url, d), mode="rt"):
115 if line.startswith("Package:"):
116 pkg = line.split(":", 1)[1].strip()
117 package_names.add(pkg + ":" + section)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500118 return package_names
119
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500120def get_latest_released_debian_source_package_list(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500121 "Returns list of all the name of packages in the latest debian distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500122 latest = find_latest_debian_release("http://ftp.debian.org/debian/dists/", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500123 url = "http://ftp.debian.org/debian/dists/stable/main/source/Sources.gz"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500124 package_names = get_debian_style_source_package_list(url, "main", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500125 url = "http://ftp.debian.org/debian/dists/stable-proposed-updates/main/source/Sources.gz"
126 package_names |= get_debian_style_source_package_list(url, "updates", d)
127 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500128
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500129def find_latest_ubuntu_release(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500130 """
131 Find the latest listed Ubuntu release on the given ubuntu/dists/ URL.
132
133 To avoid matching development releases look for distributions that have
134 updates, so the resulting distro could be any supported release.
135 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 url += "?C=M;O=D" # Descending Sort by Last Modified
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500137 for link in get_links_from_url(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500138 if "-updates" in link:
139 distro = link.replace("-updates", "")
140 return distro
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500141 return "_NotFound_"
142
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500143def get_latest_released_ubuntu_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500144 "Returns list of all the name os packages in the latest ubuntu distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500145 latest = find_latest_ubuntu_release("http://archive.ubuntu.com/ubuntu/dists/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 url = "http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.gz" % latest
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500147 package_names = get_debian_style_source_package_list(url, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 url = "http://archive.ubuntu.com/ubuntu/dists/%s-updates/main/source/Sources.gz" % latest
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500149 package_names |= get_debian_style_source_package_list(url, "updates", d)
150 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500151
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500152def create_distro_packages_list(distro_check_dir, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500153 import shutil
154
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500156 bb.utils.remove(pkglst_dir, True)
157 bb.utils.mkdirhier(pkglst_dir)
158
159 per_distro_functions = (
160 ("Debian", get_latest_released_debian_source_package_list),
161 ("Ubuntu", get_latest_released_ubuntu_source_package_list),
162 ("Fedora", get_latest_released_fedora_source_package_list),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500163 ("openSUSE", get_latest_released_opensuse_source_package_list),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500164 ("Clear", get_latest_released_clear_source_package_list),
165 )
166
167 for name, fetcher_func in per_distro_functions:
168 try:
169 release, package_list = fetcher_func(d)
170 except Exception as e:
171 bb.warn("Cannot fetch packages for %s: %s" % (name, e))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500172 bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173 if len(package_list) == 0:
174 bb.error("Didn't fetch any packages for %s %s" % (name, release))
175
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 package_list_file = os.path.join(pkglst_dir, name + "-" + release)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 with open(package_list_file, 'w') as f:
178 for pkg in sorted(package_list):
179 f.write(pkg + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500181def update_distro_data(distro_check_dir, datetime, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500183 If distro packages list data is old then rebuild it.
184 The operations has to be protected by a lock so that
185 only one thread performes it at a time.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 """
187 if not os.path.isdir (distro_check_dir):
188 try:
189 bb.note ("Making new directory: %s" % distro_check_dir)
190 os.makedirs (distro_check_dir)
191 except OSError:
192 raise Exception('Unable to create directory %s' % (distro_check_dir))
193
194
195 datetime_file = os.path.join(distro_check_dir, "build_datetime")
196 saved_datetime = "_invalid_"
197 import fcntl
198 try:
199 if not os.path.exists(datetime_file):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600200 open(datetime_file, 'w+').close() # touch the file so that the next open won't fail
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202 f = open(datetime_file, "r+")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 fcntl.lockf(f, fcntl.LOCK_EX)
204 saved_datetime = f.read()
205 if saved_datetime[0:8] != datetime[0:8]:
206 bb.note("The build datetime did not match: saved:%s current:%s" % (saved_datetime, datetime))
207 bb.note("Regenerating distro package lists")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500208 create_distro_packages_list(distro_check_dir, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209 f.seek(0)
210 f.write(datetime)
211
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500212 except OSError as e:
213 raise Exception('Unable to open timestamp: %s' % e)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500214 finally:
215 fcntl.lockf(f, fcntl.LOCK_UN)
216 f.close()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500217
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500218def compare_in_distro_packages_list(distro_check_dir, d):
219 if not os.path.isdir(distro_check_dir):
220 raise Exception("compare_in_distro_packages_list: invalid distro_check_dir passed")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500221
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 localdata = bb.data.createCopy(d)
223 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
224 matching_distros = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500225 pn = recipe_name = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 bb.note("Checking: %s" % pn)
227
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 if pn.find("-native") != -1:
229 pnstripped = pn.split("-native")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500230 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500231 recipe_name = pnstripped[0]
232
233 if pn.startswith("nativesdk-"):
234 pnstripped = pn.split("nativesdk-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500235 localdata.setVar('OVERRIDES', "pn-" + pnstripped[1] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500236 recipe_name = pnstripped[1]
237
238 if pn.find("-cross") != -1:
239 pnstripped = pn.split("-cross")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500240 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500241 recipe_name = pnstripped[0]
242
243 if pn.find("-initial") != -1:
244 pnstripped = pn.split("-initial")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500245 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500246 recipe_name = pnstripped[0]
247
248 bb.note("Recipe: %s" % recipe_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249
250 distro_exceptions = dict({"OE-Core":'OE-Core', "OpenedHand":'OpenedHand', "Intel":'Intel', "Upstream":'Upstream', "Windriver":'Windriver', "OSPDT":'OSPDT Approved', "Poky":'poky'})
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500251 tmp = localdata.getVar('DISTRO_PN_ALIAS') or ""
252 for str in tmp.split():
253 if str and str.find("=") == -1 and distro_exceptions[str]:
254 matching_distros.append(str)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255
256 distro_pn_aliases = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500257 for str in tmp.split():
258 if "=" in str:
259 (dist, pn_alias) = str.split('=')
260 distro_pn_aliases[dist.strip().lower()] = pn_alias.strip()
261
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500262 for file in os.listdir(pkglst_dir):
263 (distro, distro_release) = file.split("-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500264 f = open(os.path.join(pkglst_dir, file), "r")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500265 for line in f:
266 (pkg, section) = line.split(":")
267 if distro.lower() in distro_pn_aliases:
268 pn = distro_pn_aliases[distro.lower()]
269 else:
270 pn = recipe_name
271 if pn == pkg:
272 matching_distros.append(distro + "-" + section[:-1]) # strip the \n at the end
273 f.close()
274 break
275 f.close()
276
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500277 for item in tmp.split():
278 matching_distros.append(item)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500279 bb.note("Matching: %s" % matching_distros)
280 return matching_distros
281
282def create_log_file(d, logname):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500283 logpath = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500284 bb.utils.mkdirhier(logpath)
285 logfn, logsuffix = os.path.splitext(logname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500286 logfile = os.path.join(logpath, "%s.%s%s" % (logfn, d.getVar('DATETIME'), logsuffix))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500287 if not os.path.exists(logfile):
288 slogfile = os.path.join(logpath, logname)
289 if os.path.exists(slogfile):
290 os.remove(slogfile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500291 open(logfile, 'w+').close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500292 os.symlink(logfile, slogfile)
293 d.setVar('LOG_FILE', logfile)
294 return logfile
295
296
297def save_distro_check_result(result, datetime, result_file, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500298 pn = d.getVar('PN')
299 logdir = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500300 if not logdir:
301 bb.error("LOG_DIR variable is not defined, can't write the distro_check results")
302 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500303 bb.utils.mkdirhier(logdir)
304
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500305 line = pn
306 for i in result:
307 line = line + "," + i
308 f = open(result_file, "a")
309 import fcntl
310 fcntl.lockf(f, fcntl.LOCK_EX)
311 f.seek(0, os.SEEK_END) # seek to the end of file
312 f.write(line + "\n")
313 fcntl.lockf(f, fcntl.LOCK_UN)
314 f.close()