Add --record-pkgs option to output more package info
authorZhang Qiang <qiang.z.zhang@intel.com>
Thu, 13 Oct 2011 03:20:30 +0000 (11:20 +0800)
committerZhang Qiang <qiang.z.zhang@intel.com>
Thu, 13 Oct 2011 03:20:30 +0000 (11:20 +0800)
Record the installed packages info, valid values are: name, content,
license. --release options would add 'name' to record_pkgs.

User can specify multi-values using comma, such as follow example:
--record-pkgs=name,license

mic/configmgr.py
mic/creator.py
mic/imager/baseimager.py
plugins/backend/yumpkgmgr.py
plugins/backend/zypppkgmgr.py
plugins/imager/fs_plugin.py
plugins/imager/livecd_plugin.py
plugins/imager/liveusb_plugin.py
plugins/imager/loop_plugin.py
plugins/imager/raw_plugin.py

index 4385f6a..e95c6c6 100644 (file)
@@ -41,6 +41,7 @@ DEFAULT_CREATE = {
     "repomd": None,
     "local_pkgs_path": None,
     "release": None,
+    "record_pkgs": [],
 }
 
 class ConfigMgr(object):
index 6a2342e..7b31488 100644 (file)
@@ -61,6 +61,7 @@ class Creator(cmdln.Cmdln):
         optparser.add_option('-o', '--outdir', type='string', action='store', dest='outdir', default=None, help='Output directory')
         optparser.add_option('-A', '--arch', type='string', dest='arch', default=None, help='Specify repo architecture')
         optparser.add_option('', '--release', type='string', dest='release', default=None, metavar='RID', help='Generate a release of RID with all neccessary files ')
+        optparser.add_option("", "--record-pkgs", type="string", dest="record_pkgs", default=None, help="Record the installed packages, valid values: name, content, license")
         optparser.add_option('', '--pkgmgr', type='string', dest='pkgmgr', default=None, help='Specify backend package manager')
         optparser.add_option('', '--local-pkgs-path', type='string', dest='local_pkgs_path', default=None, help='Path for local pkgs(rpms) to be installed')
         return optparser
@@ -121,6 +122,13 @@ class Creator(cmdln.Cmdln):
         if self.options.release:
             self.configmgr.create['release'] = self.options.release
 
+        if self.options.record_pkgs:
+            self.configmgr.create['record_pkgs'] = []
+            for type in self.options.record_pkgs.split(','):
+                if type not in ('name', 'content', 'license'):
+                    msger.error("Please specify what to be recorded: \"name\", \"content\", \"license\"")
+                self.configmgr.create['record_pkgs'].append(type)
+
         if self.options.arch is not None:
             self.configmgr.create['arch'] = self.options.arch
 
index f1f5338..832fa7b 100644 (file)
@@ -105,7 +105,7 @@ class BaseImageCreator(object):
         # This value is set with compression_method function. """
         self.__img_compression_method = None
 
-        self._recording_pkgs = None
+        self._recording_pkgs = []
 
         # available size in root fs, init to 0
         self._root_fs_avail = 0
@@ -263,25 +263,21 @@ class BaseImageCreator(object):
     def _save_recording_pkgs(self, destdir):
         """Save the list or content of installed packages to file.
         """
-        if self._recording_pkgs not in ('content', 'name'):
-            return
-
         pkgs = self._pkgs_content.keys()
         pkgs.sort() # inplace op
 
-        # save package name list anyhow
         if not os.path.exists(destdir):
             os.makedirs(destdir)
-
-        namefile = os.path.join(destdir, self.name + '-pkgs.txt')
-        f = open(namefile, "w")
-        content = '\n'.join(pkgs)
-        f.write(content)
-        f.close()
-        self.outimage.append(namefile);
+        if 'name' in self._recording_pkgs :
+            namefile = os.path.join(destdir, self.name + '-pkgs.txt')
+            f = open(namefile, "w")
+            content = '\n'.join(pkgs)
+            f.write(content)
+            f.close()
+            self.outimage.append(namefile);
 
         # if 'content', save more details
-        if self._recording_pkgs == 'content':
+        if 'content' in self._recording_pkgs :
             contfile = os.path.join(destdir, self.name + '-pkgs-content.txt')
             f = open(contfile, "w")
 
@@ -305,6 +301,21 @@ class BaseImageCreator(object):
             f.close()
             self.outimage.append(contfile)
 
+        if 'license' in self._recording_pkgs:
+            licensefile = os.path.join(destdir, self.name + '-license.txt')
+            f = open(licensefile, "w")
+            f.write('Summary:\n')
+            for license in sorted (self._pkgs_license, key=lambda license: len(self._pkgs_license[license])):
+                f.write("\t- %s: %s\n" % (license, len(self._pkgs_license[license])))
+            f.write('\nDetails:\n')
+            for license in sorted (self._pkgs_license, key=lambda license: len(self._pkgs_license[license])):
+                f.write("\t- %s:\n" % (license))
+                for pkg in sorted(self._pkgs_license[license]):
+                    f.write("\t\t- %s\n" % (pkg))
+                f.write('\n')
+            f.close()
+            self.outimage.append(licensefile);
+
     def _get_required_packages(self):
         """Return a list of required packages.
 
@@ -805,10 +816,8 @@ class BaseImageCreator(object):
 
         yum_conf = self._mktemp(prefix = "yum.conf-")
 
-        keep_record = None
-        if hasattr(self, '_include_src') and self._include_src:
-            keep_record = 'include_src'
-        if self._recording_pkgs in ('name', 'content'):
+        keep_record = []
+        if len(self._recording_pkgs) > 0:
             keep_record = self._recording_pkgs
 
         pkg_manager = self.get_pkg_manager(keep_record)
@@ -842,8 +851,9 @@ class BaseImageCreator(object):
             except CreatorError, e:
                 raise
         finally:
-            if keep_record:
+            if len(keep_record):
                 self._pkgs_content = pkg_manager.getAllContent()
+                self._pkgs_license = pkg_manager.getPkgLicense()
 
             pkg_manager.closeRpmDB()
             pkg_manager.close()
index 7efe8f4..ecfdc73 100644 (file)
@@ -154,6 +154,7 @@ class Yum(BackendPlugin, yum.YumBase):
             self.arch.setup_arch(self.creator.target_arch)
 
         self.__recording_pkgs = recording_pkgs
+        self.__pkgs_license = {}
         self.__pkgs_content = {}
 
     def doFileLogSetup(self, uid, logfile):
@@ -347,11 +348,16 @@ class Yum(BackendPlugin, yum.YumBase):
         if checksize and pkgs_total_size > checksize:
             raise CreatorError("Size of specified root partition in kickstart file is too small to install all selected packages.")
 
-        if self.__recording_pkgs:
+        if len(self.__recording_pkgs) > 0:
             # record all pkg and the content
             for pkg in dlpkgs:
                 pkg_long_name = "%s-%s.%s.rpm" % (pkg.name, pkg.printVer(), pkg.arch)
                 self.__pkgs_content[pkg_long_name] = pkg.files
+                license = pkg.license
+                if license in self.__pkgs_license.keys():
+                    self.__pkgs_license[license].append(pkg_long_name)
+                else:
+                    self.__pkgs_license[license] = [pkg_long_name]
 
         total_count = len(dlpkgs)
         cached_count = 0
@@ -402,3 +408,6 @@ class Yum(BackendPlugin, yum.YumBase):
 
     def getAllContent(self):
         return self.__pkgs_content
+
+    def getPkgLicense(self):
+        return self.__pkgs_license
index ff91282..50bd7de 100644 (file)
@@ -60,6 +60,7 @@ class Zypp(BackendPlugin):
             raise CreatorError("Invalid argument: creator")
 
         self.__recording_pkgs = recording_pkgs
+        self.__pkgs_license = {}
         self.__pkgs_content = {}
         self.creator = creator
         self.repos = []
@@ -307,16 +308,24 @@ class Zypp(BackendPlugin):
         if checksize and pkgs_total_size > checksize:
             raise CreatorError("Size of specified root partition in kickstart file is too small to install all selected packages.")
 
-        if self.__recording_pkgs:
+        if len(self.__recording_pkgs) > 0:
             # record all pkg and the content
             localpkgs = self.localpkgs.keys()
             for pkg in dlpkgs:
+                license = ''
                 if pkg.name() in localpkgs:
                     hdr = rpmmisc.readRpmHeader(self.ts, self.localpkgs[pkg.name()])
                     pkg_long_name = "%s-%s-%s.%s.rpm" % (hdr['name'], hdr['version'], hdr['release'], hdr['arch'])
+                    license = hdr['license']
                 else:
                     pkg_long_name = "%s-%s.%s.rpm" % (pkg.name(), pkg.edition(), pkg.arch())
+                    package = zypp.asKindPackage(pkg)
+                    license = package.license()
                 self.__pkgs_content[pkg_long_name] = {} #TBD: to get file list
+                if license in self.__pkgs_license.keys():
+                    self.__pkgs_license[license].append(pkg_long_name)
+                else:
+                    self.__pkgs_license[license] = [pkg_long_name]
 
         total_count = len(dlpkgs)
         cached_count = 0
@@ -349,6 +358,9 @@ class Zypp(BackendPlugin):
     def getAllContent(self):
         return self.__pkgs_content
 
+    def getPkgLicense(self):
+        return self.__pkgs_license
+
     def __initialize_repo_manager(self):
         if self.repo_manager:
             return
index 5e9339b..e52a9ed 100644 (file)
@@ -44,9 +44,12 @@ class FsPlugin(ImagerPlugin):
         createopts = cfgmgr.create
         ksconf = args[0]
 
-        recording_pkgs = None
+        recording_pkgs = []
+        if len(createopts['record_pkgs']) > 0:
+            recording_pkgs = createopts['record_pkgs']
         if createopts['release'] is not None:
-            recording_pkgs = "name"
+            if 'name' not in recording_pkgs:
+                recording_pkgs.append('name')
             ksconf = misc.save_ksconf_file(ksconf, createopts['release'])
             name = os.path.splitext(os.path.basename(ksconf))[0]
             createopts['outdir'] = "%s/%s-%s/" % (createopts['outdir'], name, createopts['release'])
@@ -66,7 +69,7 @@ class FsPlugin(ImagerPlugin):
         creator = fs.FsImageCreator(createopts, pkgmgr)
         creator._include_src = opts.include_src
 
-        if recording_pkgs is not None:
+        if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         destdir = os.path.abspath(os.path.expanduser(createopts["outdir"]))
index ae6c709..d1c2bb7 100644 (file)
@@ -49,9 +49,12 @@ class LiveCDPlugin(ImagerPlugin):
             msger.warning('livecd cannot support arm images, Quit')
             return
 
-        recording_pkgs = None
+        recording_pkgs = []
+        if len(creatoropts['record_pkgs']) > 0:
+            recording_pkgs = creatoropts['record_pkgs']
         if creatoropts['release'] is not None:
-            recording_pkgs = "name"
+            if 'name' not in recording_pkgs:
+                recording_pkgs.append('name')
             ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
             name = os.path.splitext(os.path.basename(ksconf))[0]
             creatoropts['outdir'] = "%s/%s-%s/" % (creatoropts['outdir'], name, creatoropts['release'])
@@ -70,7 +73,7 @@ class LiveCDPlugin(ImagerPlugin):
 
         creator = livecd.LiveCDImageCreator(creatoropts, pkgmgr)
 
-        if recording_pkgs is not None:
+        if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         try:
index c9f8843..e52b6e7 100644 (file)
@@ -51,9 +51,12 @@ class LiveUSBPlugin(ImagerPlugin):
             msger.warning('liveusb cannot support arm images, Quit')
             return
 
-        recording_pkgs = None
+        recording_pkgs = []
+        if len(creatoropts['record_pkgs']) > 0:
+            recording_pkgs = creatoropts['record_pkgs']
         if creatoropts['release'] is not None:
-            recording_pkgs = "name"
+            if 'name' not in recording_pkgs:
+                recording_pkgs.append('name')
             ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
             name = os.path.splitext(os.path.basename(ksconf))[0]
             creatoropts['outdir'] = "%s/%s-%s/" % (creatoropts['outdir'], name, creatoropts['release'])
@@ -72,7 +75,7 @@ class LiveUSBPlugin(ImagerPlugin):
 
         creator = liveusb.LiveUSBImageCreator(creatoropts, pkgmgr)
 
-        if recording_pkgs is not None:
+        if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         try:
index c614b35..8332dcb 100644 (file)
@@ -46,9 +46,12 @@ class LoopPlugin(ImagerPlugin):
         creatoropts = cfgmgr.create
         ksconf = args[0]
 
-        recording_pkgs = None
+        recording_pkgs = []
+        if len(creatoropts['record_pkgs']) > 0:
+            recording_pkgs = creatoropts['record_pkgs']
         if creatoropts['release'] is not None:
-            recording_pkgs = "name"
+            if 'name' not in recording_pkgs:
+                recording_pkgs.append('name')
             ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
             name = os.path.splitext(os.path.basename(ksconf))[0]
             creatoropts['outdir'] = "%s/%s-%s/" % (creatoropts['outdir'], name, creatoropts['release'])
@@ -67,7 +70,7 @@ class LoopPlugin(ImagerPlugin):
 
         creator = loop.LoopImageCreator(creatoropts, pkgmgr, opts.taring_to)
 
-        if recording_pkgs is not None:
+        if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         try:
index b5facb0..132ec9c 100644 (file)
@@ -48,9 +48,12 @@ class RawPlugin(ImagerPlugin):
         creatoropts = cfgmgr.create
         ksconf = args[0]
 
-        recording_pkgs = None
+        recording_pkgs = []
+        if len(creatoropts['record_pkgs']) > 0:
+            recording_pkgs = creatoropts['record_pkgs']
         if creatoropts['release'] is not None:
-            recording_pkgs = "name"
+            if 'name' not in recording_pkgs:
+                recording_pkgs.append('name')
             ksconf = misc.save_ksconf_file(ksconf, creatoropts['release'])
             name = os.path.splitext(os.path.basename(ksconf))[0]
             creatoropts['outdir'] = "%s/%s-%s/" % (creatoropts['outdir'], name, creatoropts['release'])
@@ -69,7 +72,7 @@ class RawPlugin(ImagerPlugin):
 
         creator = raw.RawImageCreator(creatoropts, pkgmgr)
 
-        if recording_pkgs is not None:
+        if len(recording_pkgs) > 0:
             creator._recording_pkgs = recording_pkgs
 
         try: