blob: e775c3a6ecdc20e5c859ea4ed63fb9e77384ef95 [file] [log] [blame]
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001def create_socket(url, d):
2 import urllib
Brad Bishop6e60e8b2018-02-01 10:27:11 -05003 from bb.utils import export_proxies
Patrick Williamsf1e5d692016-03-30 15:21:19 -05004
Brad Bishop6e60e8b2018-02-01 10:27:11 -05005 export_proxies(d)
6 return urllib.request.urlopen(url)
Patrick Williamsf1e5d692016-03-30 15:21:19 -05007
8def get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009 "Return all the href links found on the web location"
10
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011 from bs4 import BeautifulSoup, SoupStrainer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012
Brad Bishop6e60e8b2018-02-01 10:27:11 -050013 soup = BeautifulSoup(create_socket(url,d), "html.parser", parse_only=SoupStrainer("a"))
Patrick Williamsc0f7c042017-02-23 20:41:17 -060014 hyperlinks = []
Patrick Williamsc0f7c042017-02-23 20:41:17 -060015 for line in soup.find_all('a', href=True):
16 hyperlinks.append(line['href'].strip('/'))
17 return hyperlinks
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018
Patrick Williamsf1e5d692016-03-30 15:21:19 -050019def find_latest_numeric_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050020 "Find the latest listed numeric release on the given url"
21 max=0
22 maxstr=""
Patrick Williamsf1e5d692016-03-30 15:21:19 -050023 for link in get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050024 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 # TODO use LooseVersion
Patrick Williamsc124f4f2015-09-15 14:41:29 -050026 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"
Brad Bishop6e60e8b2018-02-01 10:27:11 -050036 return name.endswith(".src.rpm")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050037
38def package_name_from_srpm(srpm):
39 "Strip out the package name from the src.rpm filename"
Patrick Williamsc124f4f2015-09-15 14:41:29 -050040
Brad Bishop6e60e8b2018-02-01 10:27:11 -050041 # 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045
Patrick Williamsf1e5d692016-03-30 15:21:19 -050046def get_source_package_list_from_url(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050047 "Return a sectioned list of package names from a URL list"
48
49 bb.note("Reading %s: %s" % (url, section))
Patrick Williamsf1e5d692016-03-30 15:21:19 -050050 links = get_links_from_url(url, d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050051 srpms = filter(is_src_rpm, links)
52 names_list = map(package_name_from_srpm, srpms)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053
Brad Bishop6e60e8b2018-02-01 10:27:11 -050054 new_pkgs = set()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 for pkgs in names_list:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050056 new_pkgs.add(pkgs + ":" + section)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057 return new_pkgs
58
Brad Bishop6e60e8b2018-02-01 10:27:11 -050059def 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
Patrick Williamsf1e5d692016-03-30 15:21:19 -050071def get_latest_released_fedora_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050072 "Returns list of all the name os packages in the latest fedora distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050073 latest = find_latest_numeric_release("http://archive.fedoraproject.org/pub/fedora/linux/releases/", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050074 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050077
Patrick Williamsf1e5d692016-03-30 15:21:19 -050078def get_latest_released_opensuse_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050079 "Returns list of all the name os packages in the latest opensuse distro"
Brad Bishopd7bf8c12018-02-25 22:55:05 -050080 latest = find_latest_numeric_release("http://download.opensuse.org/source/distribution/leap", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082 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)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050084 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Brad Bishop6e60e8b2018-02-01 10:27:11 -050086def 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -050090
Patrick Williamsf1e5d692016-03-30 15:21:19 -050091def find_latest_debian_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092 "Find the latest listed debian release on the given url"
93
Brad Bishop6e60e8b2018-02-01 10:27:11 -050094 releases = [link.replace("Debian", "")
95 for link in get_links_from_url(url, d)
96 if link.startswith("Debian")]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097 releases.sort()
98 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050099 return releases[-1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500100 except:
101 return "_NotFound_"
102
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500103def get_debian_style_source_package_list(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 "Return the list of package-names stored in the debian style Sources.gz file"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 import gzip
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600106
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500107 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 return package_names
113
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500114def get_latest_released_debian_source_package_list(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500115 "Returns list of all the name of packages in the latest debian distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500116 latest = find_latest_debian_release("http://ftp.debian.org/debian/dists/", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500117 url = "http://ftp.debian.org/debian/dists/stable/main/source/Sources.gz"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500118 package_names = get_debian_style_source_package_list(url, "main", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500119 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500123def find_latest_ubuntu_release(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 """
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 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500130 url += "?C=M;O=D" # Descending Sort by Last Modified
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500131 for link in get_links_from_url(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500132 if "-updates" in link:
133 distro = link.replace("-updates", "")
134 return distro
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500135 return "_NotFound_"
136
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500137def get_latest_released_ubuntu_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500138 "Returns list of all the name os packages in the latest ubuntu distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500139 latest = find_latest_ubuntu_release("http://archive.ubuntu.com/ubuntu/dists/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500140 url = "http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.gz" % latest
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500141 package_names = get_debian_style_source_package_list(url, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 url = "http://archive.ubuntu.com/ubuntu/dists/%s-updates/main/source/Sources.gz" % latest
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500143 package_names |= get_debian_style_source_package_list(url, "updates", d)
144 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500146def create_distro_packages_list(distro_check_dir, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500147 import shutil
148
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 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),
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500157 ("openSUSE", get_latest_released_opensuse_source_package_list),
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500158 ("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))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500166 bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500167 if len(package_list) == 0:
168 bb.error("Didn't fetch any packages for %s %s" % (name, release))
169
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500170 package_list_file = os.path.join(pkglst_dir, name + "-" + release)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500171 with open(package_list_file, 'w') as f:
172 for pkg in sorted(package_list):
173 f.write(pkg + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500175def update_distro_data(distro_check_dir, datetime, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500177 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.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180 """
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):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600194 open(datetime_file, 'w+').close() # touch the file so that the next open won't fail
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600196 f = open(datetime_file, "r+")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 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")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500202 create_distro_packages_list(distro_check_dir, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203 f.seek(0)
204 f.write(datetime)
205
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500206 except OSError as e:
207 raise Exception('Unable to open timestamp: %s' % e)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500208 finally:
209 fcntl.lockf(f, fcntl.LOCK_UN)
210 f.close()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500211
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212def 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")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500215
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216 localdata = bb.data.createCopy(d)
217 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
218 matching_distros = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500219 pn = recipe_name = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220 bb.note("Checking: %s" % pn)
221
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 if pn.find("-native") != -1:
223 pnstripped = pn.split("-native")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225 recipe_name = pnstripped[0]
226
227 if pn.startswith("nativesdk-"):
228 pnstripped = pn.split("nativesdk-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500229 localdata.setVar('OVERRIDES', "pn-" + pnstripped[1] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 recipe_name = pnstripped[1]
231
232 if pn.find("-cross") != -1:
233 pnstripped = pn.split("-cross")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500234 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500235 recipe_name = pnstripped[0]
236
237 if pn.find("-initial") != -1:
238 pnstripped = pn.split("-initial")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500239 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500240 recipe_name = pnstripped[0]
241
242 bb.note("Recipe: %s" % recipe_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243
244 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 -0500245 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500249
250 distro_pn_aliases = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500251 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
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256 for file in os.listdir(pkglst_dir):
257 (distro, distro_release) = file.split("-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500258 f = open(os.path.join(pkglst_dir, file), "r")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500259 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
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500271 for item in tmp.split():
272 matching_distros.append(item)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500273 bb.note("Matching: %s" % matching_distros)
274 return matching_distros
275
276def create_log_file(d, logname):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500277 logpath = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500278 bb.utils.mkdirhier(logpath)
279 logfn, logsuffix = os.path.splitext(logname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500280 logfile = os.path.join(logpath, "%s.%s%s" % (logfn, d.getVar('DATETIME'), logsuffix))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 if not os.path.exists(logfile):
282 slogfile = os.path.join(logpath, logname)
283 if os.path.exists(slogfile):
284 os.remove(slogfile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 open(logfile, 'w+').close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286 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):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500292 pn = d.getVar('PN')
293 logdir = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294 if not logdir:
295 bb.error("LOG_DIR variable is not defined, can't write the distro_check results")
296 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500297 bb.utils.mkdirhier(logdir)
298
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500299 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()