summaryrefslogtreecommitdiffstats
path: root/meta/lib/oe
diff options
context:
space:
mode:
authorLaurentiu Palcu <laurentiu.palcu@intel.com>2014-03-18 11:03:51 +0000
committerRichard Purdie <richard.purdie@linuxfoundation.org>2014-03-21 12:05:52 +0000
commitd91e35640d19471213122d36288315f071c37432 (patch)
tree3cbdb38e967d7abd8835c642330593633f175671 /meta/lib/oe
parent5a78852a6e811ec699532b567008e36dec9d6c49 (diff)
downloadpoky-d91e35640d19471213122d36288315f071c37432.tar.gz
package_manager.py: create separate class for installed packages listing
This commit creates a new class that has the only purpose to generate various listings of installed packages in the rootfs. Basically, the methods involved in listing the installed packages, that were part of each backend PM class implementation, were moved to this new class. This change avoids instantiating a new PM object just to get the list of installed packages in a certain rootfs. (From OE-Core rev: a7290ed13378826723d1edc7e828eab848eaad10) Signed-off-by: Laurentiu Palcu <laurentiu.palcu@intel.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oe')
-rw-r--r--meta/lib/oe/package_manager.py376
1 files changed, 211 insertions, 165 deletions
diff --git a/meta/lib/oe/package_manager.py b/meta/lib/oe/package_manager.py
index ec96bb3d62..0cdf3b0857 100644
--- a/meta/lib/oe/package_manager.py
+++ b/meta/lib/oe/package_manager.py
@@ -219,6 +219,213 @@ class DpkgIndexer(Indexer):
219 return(result) 219 return(result)
220 220
221 221
222class PkgsList(object):
223 __metaclass__ = ABCMeta
224
225 def __init__(self, d, rootfs_dir):
226 self.d = d
227 self.rootfs_dir = rootfs_dir
228
229 @abstractmethod
230 def list(self, format=None):
231 pass
232
233
234class RpmPkgsList(PkgsList):
235 def __init__(self, d, rootfs_dir, arch_var=None, os_var=None):
236 super(RpmPkgsList, self).__init__(d, rootfs_dir)
237
238 self.rpm_cmd = bb.utils.which(os.getenv('PATH'), "rpm")
239 self.image_rpmlib = os.path.join(self.rootfs_dir, 'var/lib/rpm')
240
241 self.ml_prefix_list, self.ml_os_list = \
242 RpmIndexer(d, rootfs_dir).get_ml_prefix_and_os_list(arch_var, os_var)
243
244 '''
245 Translate the RPM/Smart format names to the OE multilib format names
246 '''
247 def _pkg_translate_smart_to_oe(self, pkg, arch):
248 new_pkg = pkg
249 fixed_arch = arch.replace('_', '-')
250 found = 0
251 for mlib in self.ml_prefix_list:
252 for cmp_arch in self.ml_prefix_list[mlib]:
253 fixed_cmp_arch = cmp_arch.replace('_', '-')
254 if fixed_arch == fixed_cmp_arch:
255 if mlib == 'default':
256 new_pkg = pkg
257 new_arch = cmp_arch
258 else:
259 new_pkg = mlib + '-' + pkg
260 # We need to strip off the ${mlib}_ prefix on the arch
261 new_arch = cmp_arch.replace(mlib + '_', '')
262
263 # Workaround for bug 3565. Simply look to see if we
264 # know of a package with that name, if not try again!
265 filename = os.path.join(self.d.getVar('PKGDATA_DIR', True),
266 'runtime-reverse',
267 new_pkg)
268 if os.path.exists(filename):
269 found = 1
270 break
271
272 if found == 1 and fixed_arch == fixed_cmp_arch:
273 break
274 #bb.note('%s, %s -> %s, %s' % (pkg, arch, new_pkg, new_arch))
275 return new_pkg, new_arch
276
277 def _list_pkg_deps(self):
278 cmd = [bb.utils.which(os.getenv('PATH'), "rpmresolve"),
279 "-t", self.image_rpmlib]
280
281 try:
282 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
283 except subprocess.CalledProcessError as e:
284 bb.fatal("Cannot get the package dependencies. Command '%s' "
285 "returned %d:\n%s" % (' '.join(cmd), e.returncode, e.output))
286
287 return output
288
289 def list(self, format=None):
290 if format == "deps":
291 return self._list_pkg_deps()
292
293 cmd = self.rpm_cmd + ' --root ' + self.rootfs_dir
294 cmd += ' -D "_dbpath /var/lib/rpm" -qa'
295 cmd += " --qf '[%{NAME} %{ARCH} %{VERSION} %{PACKAGEORIGIN}\n]'"
296
297 try:
298 # bb.note(cmd)
299 tmp_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
300
301 rpm_db_locks = glob.glob('%s/var/lib/rpm/__db.*' % self.rootfs_dir)
302 for f in rpm_db_locks:
303 bb.utils.remove(f, True)
304 except subprocess.CalledProcessError as e:
305 bb.fatal("Cannot get the installed packages list. Command '%s' "
306 "returned %d:\n%s" % (cmd, e.returncode, e.output))
307
308 output = list()
309 for line in tmp_output.split('\n'):
310 if len(line.strip()) == 0:
311 continue
312 pkg = line.split()[0]
313 arch = line.split()[1]
314 ver = line.split()[2]
315 pkgorigin = line.split()[3]
316 new_pkg, new_arch = self._pkg_translate_smart_to_oe(pkg, arch)
317
318 if format == "arch":
319 output.append('%s %s' % (new_pkg, new_arch))
320 elif format == "file":
321 output.append('%s %s %s' % (new_pkg, pkgorigin, new_arch))
322 elif format == "ver":
323 output.append('%s %s %s' % (new_pkg, new_arch, ver))
324 else:
325 output.append('%s' % (new_pkg))
326
327 output.sort()
328
329 return '\n'.join(output)
330
331
332class OpkgPkgsList(PkgsList):
333 def __init__(self, d, rootfs_dir, config_file):
334 super(OpkgPkgsList, self).__init__(d, rootfs_dir)
335
336 self.opkg_cmd = bb.utils.which(os.getenv('PATH'), "opkg-cl")
337 self.opkg_args = "-f %s -o %s " % (config_file, rootfs_dir)
338 self.opkg_args += self.d.getVar("OPKG_ARGS", True)
339
340 def list(self, format=None):
341 opkg_query_cmd = bb.utils.which(os.getenv('PATH'), "opkg-query-helper.py")
342
343 if format == "arch":
344 cmd = "%s %s status | %s -a" % \
345 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
346 elif format == "file":
347 cmd = "%s %s status | %s -f" % \
348 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
349 elif format == "ver":
350 cmd = "%s %s status | %s -v" % \
351 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
352 elif format == "deps":
353 cmd = "%s %s status | %s" % \
354 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
355 else:
356 cmd = "%s %s list_installed | cut -d' ' -f1" % \
357 (self.opkg_cmd, self.opkg_args)
358
359 try:
360 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
361 except subprocess.CalledProcessError as e:
362 bb.fatal("Cannot get the installed packages list. Command '%s' "
363 "returned %d:\n%s" % (cmd, e.returncode, e.output))
364
365 if output and format == "file":
366 tmp_output = ""
367 for line in output.split('\n'):
368 pkg, pkg_file, pkg_arch = line.split()
369 full_path = os.path.join(self.rootfs_dir, pkg_arch, pkg_file)
370 if os.path.exists(full_path):
371 tmp_output += "%s %s %s\n" % (pkg, full_path, pkg_arch)
372 else:
373 tmp_output += "%s %s %s\n" % (pkg, pkg_file, pkg_arch)
374
375 output = tmp_output
376
377 return output
378
379
380class DpkgPkgsList(PkgsList):
381 def list(self, format=None):
382 cmd = [bb.utils.which(os.getenv('PATH'), "dpkg-query"),
383 "--admindir=%s/var/lib/dpkg" % self.rootfs_dir,
384 "-W"]
385
386 if format == "arch":
387 cmd.append("-f=${Package} ${PackageArch}\n")
388 elif format == "file":
389 cmd.append("-f=${Package} ${Package}_${Version}_${Architecture}.deb ${PackageArch}\n")
390 elif format == "ver":
391 cmd.append("-f=${Package} ${PackageArch} ${Version}\n")
392 elif format == "deps":
393 cmd.append("-f=Package: ${Package}\nDepends: ${Depends}\nRecommends: ${Recommends}\n\n")
394 else:
395 cmd.append("-f=${Package}\n")
396
397 try:
398 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
399 except subprocess.CalledProcessError as e:
400 bb.fatal("Cannot get the installed packages list. Command '%s' "
401 "returned %d:\n%s" % (' '.join(cmd), e.returncode, e.output))
402
403 if format == "file":
404 tmp_output = ""
405 for line in tuple(output.split('\n')):
406 pkg, pkg_file, pkg_arch = line.split()
407 full_path = os.path.join(self.rootfs_dir, pkg_arch, pkg_file)
408 if os.path.exists(full_path):
409 tmp_output += "%s %s %s\n" % (pkg, full_path, pkg_arch)
410 else:
411 tmp_output += "%s %s %s\n" % (pkg, pkg_file, pkg_arch)
412
413 output = tmp_output
414 elif format == "deps":
415 opkg_query_cmd = bb.utils.which(os.getenv('PATH'), "opkg-query-helper.py")
416
417 try:
418 output = subprocess.check_output("echo -e '%s' | %s" %
419 (output, opkg_query_cmd),
420 stderr=subprocess.STDOUT,
421 shell=True)
422 except subprocess.CalledProcessError as e:
423 bb.fatal("Cannot compute packages dependencies. Command '%s' "
424 "returned %d:\n%s" % (e.cmd, e.returncode, e.output))
425
426 return output
427
428
222class PackageManager(object): 429class PackageManager(object):
223 """ 430 """
224 This is an abstract class. Do not instantiate this directly. 431 This is an abstract class. Do not instantiate this directly.
@@ -366,10 +573,10 @@ class RpmPM(PackageManager):
366 bb.utils.mkdirhier(self.d.expand('${T}/saved')) 573 bb.utils.mkdirhier(self.d.expand('${T}/saved'))
367 574
368 self.indexer = RpmIndexer(self.d, self.deploy_dir) 575 self.indexer = RpmIndexer(self.d, self.deploy_dir)
576 self.pkgs_list = RpmPkgsList(self.d, self.target_rootfs, arch_var, os_var)
369 577
370 self.ml_prefix_list, self.ml_os_list = self.indexer.get_ml_prefix_and_os_list(arch_var, os_var) 578 self.ml_prefix_list, self.ml_os_list = self.indexer.get_ml_prefix_and_os_list(arch_var, os_var)
371 579
372
373 def insert_feeds_uris(self): 580 def insert_feeds_uris(self):
374 if self.feed_uris == "": 581 if self.feed_uris == "":
375 return 582 return
@@ -448,39 +655,6 @@ class RpmPM(PackageManager):
448 bb.fatal("Could not invoke smart. Command " 655 bb.fatal("Could not invoke smart. Command "
449 "'%s' returned %d:\n%s" % (cmd, e.returncode, e.output)) 656 "'%s' returned %d:\n%s" % (cmd, e.returncode, e.output))
450 657
451 '''
452 Translate the RPM/Smart format names to the OE multilib format names
453 '''
454 def _pkg_translate_smart_to_oe(self, pkg, arch):
455 new_pkg = pkg
456 fixed_arch = arch.replace('_', '-')
457 found = 0
458 for mlib in self.ml_prefix_list:
459 for cmp_arch in self.ml_prefix_list[mlib]:
460 fixed_cmp_arch = cmp_arch.replace('_', '-')
461 if fixed_arch == fixed_cmp_arch:
462 if mlib == 'default':
463 new_pkg = pkg
464 new_arch = cmp_arch
465 else:
466 new_pkg = mlib + '-' + pkg
467 # We need to strip off the ${mlib}_ prefix on the arch
468 new_arch = cmp_arch.replace(mlib + '_', '')
469
470 # Workaround for bug 3565. Simply look to see if we
471 # know of a package with that name, if not try again!
472 filename = os.path.join(self.d.getVar('PKGDATA_DIR', True),
473 'runtime-reverse',
474 new_pkg)
475 if os.path.exists(filename):
476 found = 1
477 break
478
479 if found == 1 and fixed_arch == fixed_cmp_arch:
480 break
481 #bb.note('%s, %s -> %s, %s' % (pkg, arch, new_pkg, new_arch))
482 return new_pkg, new_arch
483
484 def _search_pkg_name_in_feeds(self, pkg, feed_archs): 658 def _search_pkg_name_in_feeds(self, pkg, feed_archs):
485 for arch in feed_archs: 659 for arch in feed_archs:
486 arch = arch.replace('-', '_') 660 arch = arch.replace('-', '_')
@@ -821,56 +995,8 @@ class RpmPM(PackageManager):
821 self.image_rpmlib, 995 self.image_rpmlib,
822 symlinks=True) 996 symlinks=True)
823 997
824 def _list_pkg_deps(self):
825 cmd = [bb.utils.which(os.getenv('PATH'), "rpmresolve"),
826 "-t", self.image_rpmlib]
827
828 try:
829 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
830 except subprocess.CalledProcessError as e:
831 bb.fatal("Cannot get the package dependencies. Command '%s' "
832 "returned %d:\n%s" % (' '.join(cmd), e.returncode, e.output))
833
834 return output
835
836 def list_installed(self, format=None): 998 def list_installed(self, format=None):
837 if format == "deps": 999 return self.pkgs_list.list(format)
838 return self._list_pkg_deps()
839
840 cmd = self.rpm_cmd + ' --root ' + self.target_rootfs
841 cmd += ' -D "_dbpath /var/lib/rpm" -qa'
842 cmd += " --qf '[%{NAME} %{ARCH} %{VERSION} %{PACKAGEORIGIN}\n]'"
843
844 try:
845 # bb.note(cmd)
846 tmp_output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
847 self._unlock_rpm_db()
848 except subprocess.CalledProcessError as e:
849 bb.fatal("Cannot get the installed packages list. Command '%s' "
850 "returned %d:\n%s" % (cmd, e.returncode, e.output))
851
852 output = list()
853 for line in tmp_output.split('\n'):
854 if len(line.strip()) == 0:
855 continue
856 pkg = line.split()[0]
857 arch = line.split()[1]
858 ver = line.split()[2]
859 pkgorigin = line.split()[3]
860 new_pkg, new_arch = self._pkg_translate_smart_to_oe(pkg, arch)
861
862 if format == "arch":
863 output.append('%s %s' % (new_pkg, new_arch))
864 elif format == "file":
865 output.append('%s %s %s' % (new_pkg, pkgorigin, new_arch))
866 elif format == "ver":
867 output.append('%s %s %s' % (new_pkg, new_arch, ver))
868 else:
869 output.append('%s' % (new_pkg))
870
871 output.sort()
872
873 return '\n'.join(output)
874 1000
875 ''' 1001 '''
876 If incremental install, we need to determine what we've got, 1002 If incremental install, we need to determine what we've got,
@@ -1213,43 +1339,7 @@ class OpkgPM(PackageManager):
1213 bb.utils.mkdirhier(self.opkg_dir) 1339 bb.utils.mkdirhier(self.opkg_dir)
1214 1340
1215 def list_installed(self, format=None): 1341 def list_installed(self, format=None):
1216 opkg_query_cmd = bb.utils.which(os.getenv('PATH'), "opkg-query-helper.py") 1342 return OpkgPkgsList(self.d, self.target_rootfs, self.config_file).list(format)
1217
1218 if format == "arch":
1219 cmd = "%s %s status | %s -a" % \
1220 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
1221 elif format == "file":
1222 cmd = "%s %s status | %s -f" % \
1223 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
1224 elif format == "ver":
1225 cmd = "%s %s status | %s -v" % \
1226 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
1227 elif format == "deps":
1228 cmd = "%s %s status | %s" % \
1229 (self.opkg_cmd, self.opkg_args, opkg_query_cmd)
1230 else:
1231 cmd = "%s %s list_installed | cut -d' ' -f1" % \
1232 (self.opkg_cmd, self.opkg_args)
1233
1234 try:
1235 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, shell=True).strip()
1236 except subprocess.CalledProcessError as e:
1237 bb.fatal("Cannot get the installed packages list. Command '%s' "
1238 "returned %d:\n%s" % (cmd, e.returncode, e.output))
1239
1240 if output and format == "file":
1241 tmp_output = ""
1242 for line in output.split('\n'):
1243 pkg, pkg_file, pkg_arch = line.split()
1244 full_path = os.path.join(self.deploy_dir, pkg_arch, pkg_file)
1245 if os.path.exists(full_path):
1246 tmp_output += "%s %s %s\n" % (pkg, full_path, pkg_arch)
1247 else:
1248 tmp_output += "%s %s %s\n" % (pkg, pkg_file, pkg_arch)
1249
1250 output = tmp_output
1251
1252 return output
1253 1343
1254 def handle_bad_recommendations(self): 1344 def handle_bad_recommendations(self):
1255 bad_recommendations = self.d.getVar("BAD_RECOMMENDATIONS", True) or "" 1345 bad_recommendations = self.d.getVar("BAD_RECOMMENDATIONS", True) or ""
@@ -1600,51 +1690,7 @@ class DpkgPM(PackageManager):
1600 "returned %d:\n%s" % (cmd, e.returncode, e.output)) 1690 "returned %d:\n%s" % (cmd, e.returncode, e.output))
1601 1691
1602 def list_installed(self, format=None): 1692 def list_installed(self, format=None):
1603 cmd = [bb.utils.which(os.getenv('PATH'), "dpkg-query"), 1693 return DpkgPkgsList(self.d, self.target_rootfs).list()
1604 "--admindir=%s/var/lib/dpkg" % self.target_rootfs,
1605 "-W"]
1606
1607 if format == "arch":
1608 cmd.append("-f=${Package} ${PackageArch}\n")
1609 elif format == "file":
1610 cmd.append("-f=${Package} ${Package}_${Version}_${Architecture}.deb ${PackageArch}\n")
1611 elif format == "ver":
1612 cmd.append("-f=${Package} ${PackageArch} ${Version}\n")
1613 elif format == "deps":
1614 cmd.append("-f=Package: ${Package}\nDepends: ${Depends}\nRecommends: ${Recommends}\n\n")
1615 else:
1616 cmd.append("-f=${Package}\n")
1617
1618 try:
1619 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT).strip()
1620 except subprocess.CalledProcessError as e:
1621 bb.fatal("Cannot get the installed packages list. Command '%s' "
1622 "returned %d:\n%s" % (' '.join(cmd), e.returncode, e.output))
1623
1624 if format == "file":
1625 tmp_output = ""
1626 for line in tuple(output.split('\n')):
1627 pkg, pkg_file, pkg_arch = line.split()
1628 full_path = os.path.join(self.deploy_dir, pkg_arch, pkg_file)
1629 if os.path.exists(full_path):
1630 tmp_output += "%s %s %s\n" % (pkg, full_path, pkg_arch)
1631 else:
1632 tmp_output += "%s %s %s\n" % (pkg, pkg_file, pkg_arch)
1633
1634 output = tmp_output
1635 elif format == "deps":
1636 opkg_query_cmd = bb.utils.which(os.getenv('PATH'), "opkg-query-helper.py")
1637
1638 try:
1639 output = subprocess.check_output("echo -e '%s' | %s" %
1640 (output, opkg_query_cmd),
1641 stderr=subprocess.STDOUT,
1642 shell=True)
1643 except subprocess.CalledProcessError as e:
1644 bb.fatal("Cannot compute packages dependencies. Command '%s' "
1645 "returned %d:\n%s" % (e.cmd, e.returncode, e.output))
1646
1647 return output
1648 1694
1649 1695
1650def generate_index_files(d): 1696def generate_index_files(d):