blob: 87c52fae9cb1d23df34b6fa217ef663851a9dc7a [file] [log] [blame]
Patrick Williamsf1e5d692016-03-30 15:21:19 -05001from contextlib import contextmanager
Patrick Williamsc0f7c042017-02-23 20:41:17 -06002
3from bb.utils import export_proxies
4
Patrick Williamsf1e5d692016-03-30 15:21:19 -05005def create_socket(url, d):
6 import urllib
Patrick Williamsf1e5d692016-03-30 15:21:19 -05007
Patrick Williamsc0f7c042017-02-23 20:41:17 -06008 socket = None
9 try:
10 export_proxies(d)
11 socket = urllib.request.urlopen(url)
12 except:
13 bb.warn("distro_check: create_socket url %s can't access" % url)
14
15 return socket
Patrick Williamsf1e5d692016-03-30 15:21:19 -050016
17def get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050018 "Return all the href links found on the web location"
19
Patrick Williamsc0f7c042017-02-23 20:41:17 -060020 from bs4 import BeautifulSoup, SoupStrainer
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021
Patrick Williamsc0f7c042017-02-23 20:41:17 -060022 hyperlinks = []
23
24 webpage = ''
25 sock = create_socket(url,d)
26 if sock:
Patrick Williamsf1e5d692016-03-30 15:21:19 -050027 webpage = sock.read()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050028
Patrick Williamsc0f7c042017-02-23 20:41:17 -060029 soup = BeautifulSoup(webpage, "html.parser", parse_only=SoupStrainer("a"))
30 for line in soup.find_all('a', href=True):
31 hyperlinks.append(line['href'].strip('/'))
32 return hyperlinks
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
Patrick Williamsf1e5d692016-03-30 15:21:19 -050034def find_latest_numeric_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 "Find the latest listed numeric release on the given url"
36 max=0
37 maxstr=""
Patrick Williamsf1e5d692016-03-30 15:21:19 -050038 for link in get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 try:
40 release = float(link)
41 except:
42 release = 0
43 if release > max:
44 max = release
45 maxstr = link
46 return maxstr
47
48def is_src_rpm(name):
49 "Check if the link is pointing to a src.rpm file"
50 if name[-8:] == ".src.rpm":
51 return True
52 else:
53 return False
54
55def package_name_from_srpm(srpm):
56 "Strip out the package name from the src.rpm filename"
57 strings = srpm.split('-')
58 package_name = strings[0]
59 for i in range(1, len (strings) - 1):
60 str = strings[i]
61 if not str[0].isdigit():
62 package_name += '-' + str
63 return package_name
64
65def clean_package_list(package_list):
66 "Removes multiple entries of packages and sorts the list"
67 set = {}
68 map(set.__setitem__, package_list, [])
69 return set.keys()
70
71
Patrick Williamsf1e5d692016-03-30 15:21:19 -050072def get_latest_released_meego_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073 "Returns list of all the name os packages in the latest meego distro"
74
75 package_names = []
76 try:
77 f = open("/tmp/Meego-1.1", "r")
78 for line in f:
79 package_names.append(line[:-1] + ":" + "main") # Also strip the '\n' at the end
80 except IOError: pass
81 package_list=clean_package_list(package_names)
82 return "1.0", package_list
83
Patrick Williamsf1e5d692016-03-30 15:21:19 -050084def get_source_package_list_from_url(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050085 "Return a sectioned list of package names from a URL list"
86
87 bb.note("Reading %s: %s" % (url, section))
Patrick Williamsf1e5d692016-03-30 15:21:19 -050088 links = get_links_from_url(url, d)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060089 srpms = list(filter(is_src_rpm, links))
90 names_list = list(map(package_name_from_srpm, srpms))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050091
92 new_pkgs = []
93 for pkgs in names_list:
94 new_pkgs.append(pkgs + ":" + section)
95
96 return new_pkgs
97
Patrick Williamsf1e5d692016-03-30 15:21:19 -050098def get_latest_released_fedora_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 "Returns list of all the name os packages in the latest fedora distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500100 latest = find_latest_numeric_release("http://archive.fedoraproject.org/pub/fedora/linux/releases/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500101
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500102 package_names = get_source_package_list_from_url("http://archive.fedoraproject.org/pub/fedora/linux/releases/%s/Fedora/source/SRPMS/" % latest, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103
104# package_names += get_source_package_list_from_url("http://download.fedora.redhat.com/pub/fedora/linux/releases/%s/Everything/source/SPRMS/" % latest, "everything")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500105 package_names += get_source_package_list_from_url("http://archive.fedoraproject.org/pub/fedora/linux/updates/%s/SRPMS/" % latest, "updates", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500106
107 package_list=clean_package_list(package_names)
108
109 return latest, package_list
110
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500111def get_latest_released_opensuse_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500112 "Returns list of all the name os packages in the latest opensuse distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500113 latest = find_latest_numeric_release("http://download.opensuse.org/source/distribution/",d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500114
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500115 package_names = get_source_package_list_from_url("http://download.opensuse.org/source/distribution/%s/repo/oss/suse/src/" % latest, "main", d)
116 package_names += get_source_package_list_from_url("http://download.opensuse.org/update/%s/rpm/src/" % latest, "updates", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500117
118 package_list=clean_package_list(package_names)
119 return latest, package_list
120
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500121def get_latest_released_mandriva_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500122 "Returns list of all the name os packages in the latest mandriva distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500123 latest = find_latest_numeric_release("http://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/", d)
124 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500125# package_names += get_source_package_list_from_url("http://distrib-coffee.ipsl.jussieu.fr/pub/linux/MandrivaLinux/official/%s/SRPMS/contrib/release/" % latest, "contrib")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500126 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)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500127
128 package_list=clean_package_list(package_names)
129 return latest, package_list
130
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500131def find_latest_debian_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500132 "Find the latest listed debian release on the given url"
133
134 releases = []
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500135 for link in get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500136 if link[:6] == "Debian":
137 if ';' not in link:
138 releases.append(link)
139 releases.sort()
140 try:
141 return releases.pop()[6:]
142 except:
143 return "_NotFound_"
144
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500145def get_debian_style_source_package_list(url, section, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500146 "Return the list of package-names stored in the debian style Sources.gz file"
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600147 import tempfile
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500148 import gzip
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600149
150 webpage = ''
151 sock = create_socket(url,d)
152 if sock:
153 webpage = sock.read()
154
155 tmpfile = tempfile.NamedTemporaryFile(mode='wb', prefix='oecore.', suffix='.tmp', delete=False)
156 tmpfilename=tmpfile.name
157 tmpfile.write(sock.read())
158 tmpfile.close()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500159 bb.note("Reading %s: %s" % (url, section))
160
161 f = gzip.open(tmpfilename)
162 package_names = []
163 for line in f:
164 if line[:9] == "Package: ":
165 package_names.append(line[9:-1] + ":" + section) # Also strip the '\n' at the end
166 os.unlink(tmpfilename)
167
168 return package_names
169
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500170def get_latest_released_debian_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500171 "Returns list of all the name os packages in the latest debian distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500172 latest = find_latest_debian_release("http://ftp.debian.org/debian/dists/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500173 url = "http://ftp.debian.org/debian/dists/stable/main/source/Sources.gz"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500174 package_names = get_debian_style_source_package_list(url, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500175# url = "http://ftp.debian.org/debian/dists/stable/contrib/source/Sources.gz"
176# package_names += get_debian_style_source_package_list(url, "contrib")
177 url = "http://ftp.debian.org/debian/dists/stable-proposed-updates/main/source/Sources.gz"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500178 package_names += get_debian_style_source_package_list(url, "updates", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500179 package_list=clean_package_list(package_names)
180 return latest, package_list
181
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500182def find_latest_ubuntu_release(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500183 "Find the latest listed ubuntu release on the given url"
184 url += "?C=M;O=D" # Descending Sort by Last Modified
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500185 for link in get_links_from_url(url, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500186 if link[-8:] == "-updates":
187 return link[:-8]
188 return "_NotFound_"
189
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500190def get_latest_released_ubuntu_source_package_list(d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500191 "Returns list of all the name os packages in the latest ubuntu distro"
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500192 latest = find_latest_ubuntu_release("http://archive.ubuntu.com/ubuntu/dists/", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500193 url = "http://archive.ubuntu.com/ubuntu/dists/%s/main/source/Sources.gz" % latest
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500194 package_names = get_debian_style_source_package_list(url, "main", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195# url = "http://archive.ubuntu.com/ubuntu/dists/%s/multiverse/source/Sources.gz" % latest
196# package_names += get_debian_style_source_package_list(url, "multiverse")
197# url = "http://archive.ubuntu.com/ubuntu/dists/%s/universe/source/Sources.gz" % latest
198# package_names += get_debian_style_source_package_list(url, "universe")
199 url = "http://archive.ubuntu.com/ubuntu/dists/%s-updates/main/source/Sources.gz" % latest
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500200 package_names += get_debian_style_source_package_list(url, "updates", d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500201 package_list=clean_package_list(package_names)
202 return latest, package_list
203
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500204def create_distro_packages_list(distro_check_dir, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500205 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
206 if not os.path.isdir (pkglst_dir):
207 os.makedirs(pkglst_dir)
208 # first clear old stuff
209 for file in os.listdir(pkglst_dir):
210 os.unlink(os.path.join(pkglst_dir, file))
211
212 per_distro_functions = [
213 ["Debian", get_latest_released_debian_source_package_list],
214 ["Ubuntu", get_latest_released_ubuntu_source_package_list],
215 ["Fedora", get_latest_released_fedora_source_package_list],
216 ["OpenSuSE", get_latest_released_opensuse_source_package_list],
217 ["Mandriva", get_latest_released_mandriva_source_package_list],
218 ["Meego", get_latest_released_meego_source_package_list]
219 ]
220
221 from datetime import datetime
222 begin = datetime.now()
223 for distro in per_distro_functions:
224 name = distro[0]
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500225 release, package_list = distro[1](d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500226 bb.note("Distro: %s, Latest Release: %s, # src packages: %d" % (name, release, len(package_list)))
227 package_list_file = os.path.join(pkglst_dir, name + "-" + release)
228 f = open(package_list_file, "w+b")
229 for pkg in package_list:
230 f.write(pkg + "\n")
231 f.close()
232 end = datetime.now()
233 delta = end - begin
234 bb.note("package_list generatiosn took this much time: %d seconds" % delta.seconds)
235
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500236def update_distro_data(distro_check_dir, datetime, d):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500237 """
238 If distro packages list data is old then rebuild it.
239 The operations has to be protected by a lock so that
240 only one thread performes it at a time.
241 """
242 if not os.path.isdir (distro_check_dir):
243 try:
244 bb.note ("Making new directory: %s" % distro_check_dir)
245 os.makedirs (distro_check_dir)
246 except OSError:
247 raise Exception('Unable to create directory %s' % (distro_check_dir))
248
249
250 datetime_file = os.path.join(distro_check_dir, "build_datetime")
251 saved_datetime = "_invalid_"
252 import fcntl
253 try:
254 if not os.path.exists(datetime_file):
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600255 open(datetime_file, 'w+').close() # touch the file so that the next open won't fail
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500256
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600257 f = open(datetime_file, "r+")
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500258 fcntl.lockf(f, fcntl.LOCK_EX)
259 saved_datetime = f.read()
260 if saved_datetime[0:8] != datetime[0:8]:
261 bb.note("The build datetime did not match: saved:%s current:%s" % (saved_datetime, datetime))
262 bb.note("Regenerating distro package lists")
Patrick Williamsf1e5d692016-03-30 15:21:19 -0500263 create_distro_packages_list(distro_check_dir, d)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500264 f.seek(0)
265 f.write(datetime)
266
267 except OSError:
268 raise Exception('Unable to read/write this file: %s' % (datetime_file))
269 finally:
270 fcntl.lockf(f, fcntl.LOCK_UN)
271 f.close()
272
273def compare_in_distro_packages_list(distro_check_dir, d):
274 if not os.path.isdir(distro_check_dir):
275 raise Exception("compare_in_distro_packages_list: invalid distro_check_dir passed")
276
277 localdata = bb.data.createCopy(d)
278 pkglst_dir = os.path.join(distro_check_dir, "package_lists")
279 matching_distros = []
280 pn = d.getVar('PN', True)
281 recipe_name = d.getVar('PN', True)
282 bb.note("Checking: %s" % pn)
283
284 trim_dict = dict({"-native":"-native", "-cross":"-cross", "-initial":"-initial"})
285
286 if pn.find("-native") != -1:
287 pnstripped = pn.split("-native")
288 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES', True))
289 bb.data.update_data(localdata)
290 recipe_name = pnstripped[0]
291
292 if pn.startswith("nativesdk-"):
293 pnstripped = pn.split("nativesdk-")
294 localdata.setVar('OVERRIDES', "pn-" + pnstripped[1] + ":" + d.getVar('OVERRIDES', True))
295 bb.data.update_data(localdata)
296 recipe_name = pnstripped[1]
297
298 if pn.find("-cross") != -1:
299 pnstripped = pn.split("-cross")
300 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES', True))
301 bb.data.update_data(localdata)
302 recipe_name = pnstripped[0]
303
304 if pn.find("-initial") != -1:
305 pnstripped = pn.split("-initial")
306 localdata.setVar('OVERRIDES', "pn-" + pnstripped[0] + ":" + d.getVar('OVERRIDES', True))
307 bb.data.update_data(localdata)
308 recipe_name = pnstripped[0]
309
310 bb.note("Recipe: %s" % recipe_name)
311 tmp = localdata.getVar('DISTRO_PN_ALIAS', True)
312
313 distro_exceptions = dict({"OE-Core":'OE-Core', "OpenedHand":'OpenedHand', "Intel":'Intel', "Upstream":'Upstream', "Windriver":'Windriver', "OSPDT":'OSPDT Approved', "Poky":'poky'})
314
315 if tmp:
316 list = tmp.split(' ')
317 for str in list:
318 if str and str.find("=") == -1 and distro_exceptions[str]:
319 matching_distros.append(str)
320
321 distro_pn_aliases = {}
322 if tmp:
323 list = tmp.split(' ')
324 for str in list:
325 if str.find("=") != -1:
326 (dist, pn_alias) = str.split('=')
327 distro_pn_aliases[dist.strip().lower()] = pn_alias.strip()
328
329 for file in os.listdir(pkglst_dir):
330 (distro, distro_release) = file.split("-")
331 f = open(os.path.join(pkglst_dir, file), "rb")
332 for line in f:
333 (pkg, section) = line.split(":")
334 if distro.lower() in distro_pn_aliases:
335 pn = distro_pn_aliases[distro.lower()]
336 else:
337 pn = recipe_name
338 if pn == pkg:
339 matching_distros.append(distro + "-" + section[:-1]) # strip the \n at the end
340 f.close()
341 break
342 f.close()
343
344
345 if tmp != None:
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600346 list = tmp.split(' ')
347 for item in list:
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500348 matching_distros.append(item)
349 bb.note("Matching: %s" % matching_distros)
350 return matching_distros
351
352def create_log_file(d, logname):
353 import subprocess
354 logpath = d.getVar('LOG_DIR', True)
355 bb.utils.mkdirhier(logpath)
356 logfn, logsuffix = os.path.splitext(logname)
357 logfile = os.path.join(logpath, "%s.%s%s" % (logfn, d.getVar('DATETIME', True), logsuffix))
358 if not os.path.exists(logfile):
359 slogfile = os.path.join(logpath, logname)
360 if os.path.exists(slogfile):
361 os.remove(slogfile)
362 subprocess.call("touch %s" % logfile, shell=True)
363 os.symlink(logfile, slogfile)
364 d.setVar('LOG_FILE', logfile)
365 return logfile
366
367
368def save_distro_check_result(result, datetime, result_file, d):
369 pn = d.getVar('PN', True)
370 logdir = d.getVar('LOG_DIR', True)
371 if not logdir:
372 bb.error("LOG_DIR variable is not defined, can't write the distro_check results")
373 return
374 if not os.path.isdir(logdir):
375 os.makedirs(logdir)
376 line = pn
377 for i in result:
378 line = line + "," + i
379 f = open(result_file, "a")
380 import fcntl
381 fcntl.lockf(f, fcntl.LOCK_EX)
382 f.seek(0, os.SEEK_END) # seek to the end of file
383 f.write(line + "\n")
384 fcntl.lockf(f, fcntl.LOCK_UN)
385 f.close()