blob: 37f04ed3595781969be15eb9184c9ae488fa2faa [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"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050080 latest = find_latest_numeric_release("http://download.opensuse.org/source/distribution/",d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050081
Patrick Williamsf1e5d692016-03-30 15:21:19 -050082 package_names = get_source_package_list_from_url("http://download.opensuse.org/source/distribution/%s/repo/oss/suse/src/" % latest, "main", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050083 package_names |= get_source_package_list_from_url("http://download.opensuse.org/update/%s/src/" % latest, "updates", d)
84 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085
Patrick Williamsf1e5d692016-03-30 15:21:19 -050086def get_latest_released_mandriva_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050087 "Returns list of all the name os packages in the latest mandriva distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088 latest = find_latest_numeric_release("http://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/", d)
89 package_names = get_source_package_list_from_url("http://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/%s/SRPMS/main/release/" % latest, "main", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -050090 package_names |= get_source_package_list_from_url("http://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/%s/SRPMS/main/updates/" % latest, "updates", d)
91 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050092
Brad Bishop6e60e8b2018-02-01 10:27:11 -050093def get_latest_released_clear_source_package_list(d):
94 latest = find_latest_numeric_release("https://download.clearlinux.org/releases/", d)
95 package_names = get_source_package_list_from_url("https://download.clearlinux.org/releases/%s/clear/source/SRPMS/" % latest, "main", d)
96 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -050097
Patrick Williamsf1e5d692016-03-30 15:21:19 -050098def find_latest_debian_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 "Find the latest listed debian release on the given url"
100
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500101 releases = [link.replace("Debian", "")
102 for link in get_links_from_url(url, d)
103 if link.startswith("Debian")]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500104 releases.sort()
105 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500106 return releases[-1]
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500107 except:
108 return "_NotFound_"
109
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500110def get_debian_style_source_package_list(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500111 "Return the list of package-names stored in the debian style Sources.gz file"
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 import gzip
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600113
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500114 package_names = set()
115 for line in gzip.open(create_socket(url, d), mode="rt"):
116 if line.startswith("Package:"):
117 pkg = line.split(":", 1)[1].strip()
118 package_names.add(pkg + ":" + section)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500119 return package_names
120
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500121def get_latest_released_debian_source_package_list(d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500122 "Returns list of all the name of packages in the latest debian distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500123 latest = find_latest_debian_release("http://ftp.debian.org/debian/dists/", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500124 url = "http://ftp.debian.org/debian/dists/stable/main/source/Sources.gz"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500125 package_names = get_debian_style_source_package_list(url, "main", d)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500126 url = "http://ftp.debian.org/debian/dists/stable-proposed-updates/main/source/Sources.gz"
127 package_names |= get_debian_style_source_package_list(url, "updates", d)
128 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500129
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500130def find_latest_ubuntu_release(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500131 """
132 Find the latest listed Ubuntu release on the given ubuntu/dists/ URL.
133
134 To avoid matching development releases look for distributions that have
135 updates, so the resulting distro could be any supported release.
136 """
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500137 url += "?C=M;O=D" # Descending Sort by Last Modified
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500138 for link in get_links_from_url(url, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500139 if "-updates" in link:
140 distro = link.replace("-updates", "")
141 return distro
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500142 return "_NotFound_"
143
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500144def get_latest_released_ubuntu_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 "Returns list of all the name os packages in the latest ubuntu distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500146 latest = find_latest_ubuntu_release("http://archive.ubuntu.com/ubuntu/dists/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500147 url = "http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.gz" % latest
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500148 package_names = get_debian_style_source_package_list(url, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500149 url = "http://archive.ubuntu.com/ubuntu/dists/%s-updates/main/source/Sources.gz" % latest
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500150 package_names |= get_debian_style_source_package_list(url, "updates", d)
151 return latest, package_names
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500152
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500153def create_distro_packages_list(distro_check_dir, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500154 import shutil
155
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500156 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500157 bb.utils.remove(pkglst_dir, True)
158 bb.utils.mkdirhier(pkglst_dir)
159
160 per_distro_functions = (
161 ("Debian", get_latest_released_debian_source_package_list),
162 ("Ubuntu", get_latest_released_ubuntu_source_package_list),
163 ("Fedora", get_latest_released_fedora_source_package_list),
164 ("OpenSuSE", get_latest_released_opensuse_source_package_list),
165 ("Mandriva", get_latest_released_mandriva_source_package_list),
166 ("Clear", get_latest_released_clear_source_package_list),
167 )
168
169 for name, fetcher_func in per_distro_functions:
170 try:
171 release, package_list = fetcher_func(d)
172 except Exception as e:
173 bb.warn("Cannot fetch packages for %s: %s" % (name, e))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500174 bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500175 if len(package_list) == 0:
176 bb.error("Didn't fetch any packages for %s %s" % (name, release))
177
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500178 package_list_file = os.path.join(pkglst_dir, name + "-" + release)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500179 with open(package_list_file, 'w') as f:
180 for pkg in sorted(package_list):
181 f.write(pkg + "\n")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500182
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500183def update_distro_data(distro_check_dir, datetime, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500184 """
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500185 If distro packages list data is old then rebuild it.
186 The operations has to be protected by a lock so that
187 only one thread performes it at a time.
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500188 """
189 if not os.path.isdir (distro_check_dir):
190 try:
191 bb.note ("Making new directory: %s" % distro_check_dir)
192 os.makedirs (distro_check_dir)
193 except OSError:
194 raise Exception('Unable to create directory %s' % (distro_check_dir))
195
196
197 datetime_file = os.path.join(distro_check_dir, "build_datetime")
198 saved_datetime = "_invalid_"
199 import fcntl
200 try:
201 if not os.path.exists(datetime_file):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600202 open(datetime_file, 'w+').close() # touch the file so that the next open won't fail
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500203
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600204 f = open(datetime_file, "r+")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 fcntl.lockf(f, fcntl.LOCK_EX)
206 saved_datetime = f.read()
207 if saved_datetime[0:8] != datetime[0:8]:
208 bb.note("The build datetime did not match: saved:%s current:%s" % (saved_datetime, datetime))
209 bb.note("Regenerating distro package lists")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500210 create_distro_packages_list(distro_check_dir, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500211 f.seek(0)
212 f.write(datetime)
213
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500214 except OSError as e:
215 raise Exception('Unable to open timestamp: %s' % e)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500216 finally:
217 fcntl.lockf(f, fcntl.LOCK_UN)
218 f.close()
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500219
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500220def compare_in_distro_packages_list(distro_check_dir, d):
221 if not os.path.isdir(distro_check_dir):
222 raise Exception("compare_in_distro_packages_list: invalid distro_check_dir passed")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500223
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500224 localdata = bb.data.createCopy(d)
225 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
226 matching_distros = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500227 pn = recipe_name = d.getVar('PN')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500228 bb.note("Checking: %s" % pn)
229
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230 if pn.find("-native") != -1:
231 pnstripped = pn.split("-native")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500232 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500233 recipe_name = pnstripped[0]
234
235 if pn.startswith("nativesdk-"):
236 pnstripped = pn.split("nativesdk-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500237 localdata.setVar('OVERRIDES', "pn-" + pnstripped[1] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500238 recipe_name = pnstripped[1]
239
240 if pn.find("-cross") != -1:
241 pnstripped = pn.split("-cross")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500242 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500243 recipe_name = pnstripped[0]
244
245 if pn.find("-initial") != -1:
246 pnstripped = pn.split("-initial")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500247 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES'))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500248 recipe_name = pnstripped[0]
249
250 bb.note("Recipe: %s" % recipe_name)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500251
252 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 -0500253 tmp = localdata.getVar('DISTRO_PN_ALIAS') or ""
254 for str in tmp.split():
255 if str and str.find("=") == -1 and distro_exceptions[str]:
256 matching_distros.append(str)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500257
258 distro_pn_aliases = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500259 for str in tmp.split():
260 if "=" in str:
261 (dist, pn_alias) = str.split('=')
262 distro_pn_aliases[dist.strip().lower()] = pn_alias.strip()
263
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264 for file in os.listdir(pkglst_dir):
265 (distro, distro_release) = file.split("-")
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500266 f = open(os.path.join(pkglst_dir, file), "r")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500267 for line in f:
268 (pkg, section) = line.split(":")
269 if distro.lower() in distro_pn_aliases:
270 pn = distro_pn_aliases[distro.lower()]
271 else:
272 pn = recipe_name
273 if pn == pkg:
274 matching_distros.append(distro + "-" + section[:-1]) # strip the \n at the end
275 f.close()
276 break
277 f.close()
278
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500279 for item in tmp.split():
280 matching_distros.append(item)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500281 bb.note("Matching: %s" % matching_distros)
282 return matching_distros
283
284def create_log_file(d, logname):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500285 logpath = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500286 bb.utils.mkdirhier(logpath)
287 logfn, logsuffix = os.path.splitext(logname)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500288 logfile = os.path.join(logpath, "%s.%s%s" % (logfn, d.getVar('DATETIME'), logsuffix))
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500289 if not os.path.exists(logfile):
290 slogfile = os.path.join(logpath, logname)
291 if os.path.exists(slogfile):
292 os.remove(slogfile)
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500293 open(logfile, 'w+').close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500294 os.symlink(logfile, slogfile)
295 d.setVar('LOG_FILE', logfile)
296 return logfile
297
298
299def save_distro_check_result(result, datetime, result_file, d):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500300 pn = d.getVar('PN')
301 logdir = d.getVar('LOG_DIR')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500302 if not logdir:
303 bb.error("LOG_DIR variable is not defined, can't write the distro_check results")
304 return
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500305 bb.utils.mkdirhier(logdir)
306
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500307 line = pn
308 for i in result:
309 line = line + "," + i
310 f = open(result_file, "a")
311 import fcntl
312 fcntl.lockf(f, fcntl.LOCK_EX)
313 f.seek(0, os.SEEK_END) # seek to the end of file
314 f.write(line + "\n")
315 fcntl.lockf(f, fcntl.LOCK_UN)
316 f.close()