[PATCH] Rewrote parts of the pkgorder script (#451083)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Improved the speed by removing unecessary glob.glob calls.
Removed unneeded imports.
Rewrote the functions to use more object oriented approach.
---
 scripts/pkgorder |  221 +++++++++++++++++++++++++++++------------------------
 1 files changed, 121 insertions(+), 100 deletions(-)

diff --git a/scripts/pkgorder b/scripts/pkgorder
index 7722718..6322fb2 100755
--- a/scripts/pkgorder
+++ b/scripts/pkgorder
@@ -9,13 +9,14 @@
 # You should have received a copy of the GNU Library Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-import os.path
-import glob
+
+import os
+import sys
+import shutil
+import fnmatch
+
 import rpm
 import rpmUtils
-import shutil
-import string
-import sys
 import yum
 
 sys.path.append("/usr/lib/anaconda")
@@ -31,14 +32,44 @@ from optparse import OptionParser
 from yum.packageSack import PackageSack
 from yum.packages import PackageObject
 from yuminstall import YumSorter
-import iutil
 
-class PackageOrderer(YumSorter):
+import tempfile
+
+
+class Packages(object):
+    def __init__(self, toppath, product):
+        self.list = []
+        for dir in ("%s/%s/RPMS" % (toppath, product),
+                    "%s/%s" % (toppath, product),
+                    toppath):
+            if os.path.isdir(dir):
+                self.list = os.listdir(dir)
+                break
+        self.list.sort()
+        
+        self.processed = {}
+        
+    def printMatching(self, fpattern):
+        matches = fnmatch.filter(self.list, fpattern)
+        
+        for match in matches:
+            mname = os.path.basename(match)
+            if self.processed.has_key(mname):
+                continue
+            else:
+                self.processed[mname] = True
+                print mname
+
 
-    def __init__(self, arch=None):
+class PackageOrderer(YumSorter):
+    def __init__(self, pkgs, arch=None):
         YumSorter.__init__(self)
         self.arch = arch
         self._installed = PackageSack()
+        
+        assert isinstance(pkgs, Packages) == True
+        self.pkgs = pkgs
+        self.config = None
 
     def doFileLogSetup(self, uid, logfile):
         pass
@@ -47,11 +78,14 @@ class PackageOrderer(YumSorter):
         pass
 
     def setup(self, fn="/etc/yum.conf", root="/", excludes=[]):
-        self.doConfigSetup(fn, root, init_plugins = False)
+        if self.config:
+            fn = self.config
+        
+        self.doConfigSetup(fn, root, init_plugins=False)
         self.conf.cache = 0
-#         if hasattr(self.repos, 'sqlite'):
-#             self.repos.sqlite = False
-#             self.repos._selectSackType()
+#        if hasattr(self.repos, 'sqlite'):
+#            self.repos.sqlite = False
+#            self.repos._selectSackType()
         exclude = self.conf.exclude
         exclude.extend(excludes)
         self.conf.exclude = exclude
@@ -67,49 +101,31 @@ class PackageOrderer(YumSorter):
 
     def getDownloadPkgs(self):
         pass
-
-#XXX: sigh
-processed = {}
-def processTransaction(ds):
-    del ds.ts
-    ds.initActionTs()
-    ds.populateTs(keepold=0)
-    ds.ts.check()
-    ds.ts.order()
-
-    # Return if there are no packages in the transaction set
-    if not ds.ts.ts.getKeys():
-        return
-
-    for (hdr, path) in ds.ts.ts.getKeys():
-        fname = os.path.basename(path)
-        fpattern = "%s*" % fname.rsplit('.', 2)[0]
-        printMatchingPkgs(fpattern)
-
-def printMatchingPkgs(fpattern):
-    global processed
-
-    if os.path.isdir("%s/%s/RPMS" % (toppath, product)):
-        matches = glob.glob("%s/%s/RPMS/%s" % (toppath, product, fpattern))
-    elif os.path.isdir("%s/%s" %(toppath, product)):
-        matches = glob.glob("%s/%s/%s" % (toppath, product, fpattern))
-    else:
-        matches = glob.glob("%s/%s" % (toppath, fpattern))
-
-    for match in matches:
-        mname = os.path.basename(match)
-        if processed.has_key(mname): continue
-        processed[mname] = True
-        print mname
-
-def addGroups(ds, groupLst):
-    ds.initActionTs()
-    map(ds.selectGroup, filter(lambda x: ds.comps.has_group(x), groupLst))
-    ds.resolveDeps()
-    processTransaction(ds)
-
-def createConfig(toppath):
-    yumconfstr = """
+                
+    def processTransaction(self):
+        del self.ts
+        self.initActionTs()
+        self.populateTs(keepold=0)
+        self.ts.check()
+        self.ts.order()
+
+        # Return if there are no packages in the transaction set
+        if not self.ts.ts.getKeys():
+            return
+
+        for (hdr, path) in self.ts.ts.getKeys():
+            fname = os.path.basename(path)
+            fpattern = "%s*" % fname.rsplit('.', 2)[0]
+            self.pkgs.printMatching(fpattern)
+        
+    def addGroups(self, groupLst):
+        self.initActionTs()
+        map(self.selectGroup, filter(lambda x: self.comps.has_group(x), groupLst))
+        self.resolveDeps()
+        self.processTransaction()
+        
+    def createConfig(self, toppath):
+        yumconfstr = """
 [main]
 distroverpkg=redhat-release
 gpgcheck=0
@@ -120,84 +136,89 @@ exclude=*debuginfo*
 name=Anaconda
 baseurl=file://%s
 enabled=1
-""" % (toppath)
-    
-    try:
-        (fd, path) = tempfile.mkstemp("", "yum-conf-", toppath)
-    except (OSError, IOError), e:
-        print >> sys.stderr, "Error writing to %s" % (toppath,)
-        sys.exit(1)
-    os.write(fd, yumconfstr)
-    os.close(fd)
-    return path
+""" % (toppath,)
+        
+        try:
+            (fd, path) = tempfile.mkstemp("", "yum-conf-", toppath)
+        except (OSError, IOError), e:
+            print >> sys.stderr, "Error writing to %s" % (toppath,)
+            sys.exit(1)
+        os.write(fd, yumconfstr)
+        os.close(fd)
+        self.config = path
+        
+    def unlinkConfig(self):
+        if self.config:
+            os.unlink(self.config)
+
 
 def usage():
     print >> sys.stderr, "pkgorder <toppath> <arch> <productpath>"
     print >> sys.stderr, "<arch>: use rpm architecture for tree, eg i686"
 
-if __name__ == "__main__":
-    import tempfile
+
+if __name__ == "__main__":    
     parser = OptionParser()
     parser.add_option("--debug", action="store_true", dest="debug", default=False)
     parser.add_option("--file", action="store", dest="file")
-    parser.add_option("--product", action="store", dest="productPath", )
-    parser.add_option("--exclude", action="append", dest="excludeList",
-                      default=[])
+    parser.add_option("--product", action="store", dest="productPath")
+    parser.add_option("--exclude", action="append", dest="excludeList", default=[])
 
     (options, args) = parser.parse_args()
-     
     if len(args) != 3:
-	usage()
+        usage()
         sys.exit(1)
-
     (toppath, arch, product) = args
-    config = createConfig(toppath)
-
+    
     # Boo.
     if arch == "i386":
         arch = "i686"
+        
+    pkgs = Packages(toppath, product)
 
     # print out kernel related packages first
-    printMatchingPkgs("kernel-*")
+    pkgs.printMatching("kernel-*")
 
-    testpath = "/tmp/pkgorder-%d" %(os.getpid(),)
-    os.system("mkdir -p %s/var/lib/rpm" %(testpath,))
+    testpath = "/tmp/pkgorder-%d" % (os.getpid(),)
+    os.system("mkdir -p %s/var/lib/rpm" % (testpath,))
     
-    ds = PackageOrderer(arch=arch)
-    ds.setup(fn=config, excludes=options.excludeList, root = testpath)
+    ds = PackageOrderer(pkgs=pkgs, arch=arch)
+    ds.createConfig(toppath)
+    ds.setup(root=testpath, excludes=options.excludeList)
 
-    addGroups(ds, ["core", "base", "text-internet"])
+    ds.addGroups(["core", "base", "text-internet"])
 
     # hack, hack, hack... make sure iscsi ends up on disc1 (#208832)
-    printMatchingPkgs("iscsi-*")
+    pkgs.printMatching("iscsi-*")
 
-    addGroups(ds, ["base-x", "dial-up",
-                   "graphical-internet", "editors", 
-                   "graphics", "gnome-desktop", "sound-and-video",
-                   "printing"])
+    ds.addGroups(["base-x", "dial-up",
+                  "graphical-internet", "editors", 
+                  "graphics", "gnome-desktop", "sound-and-video",
+                  "printing"])
 
-    addGroups(ds, ["office", "engineering-and-scientific",
-                   "authoring-and-publishing", "games"])
+    ds.addGroups(["office", "engineering-and-scientific",
+                  "authoring-and-publishing", "games"])
 
-    addGroups(ds, ["web-server", "ftp-server", "sql-server",
-                   "mysql", "server-cfg", "dns-server",
-                   "smb-server", "admin-tools"])
+    ds.addGroups(["web-server", "ftp-server", "sql-server",
+                  "mysql", "server-cfg", "dns-server",
+                  "smb-server", "admin-tools"])
 
-    addGroups(ds, ["kde-desktop", "development-tools", "development-libs",
-                   "gnome-software-development", "eclipse",
-                   "x-software-development",
-                   "java-development", "kde-software-development",
-                   "mail-server", "network-server", "legacy-network-server"])
+    ds.addGroups(["kde-desktop", "development-tools", "development-libs",
+                  "gnome-software-development", "eclipse",
+                  "x-software-development",
+                  "java-development", "kde-software-development",
+                  "mail-server", "network-server", "legacy-network-server"])
 
-    addGroups(ds, ["news-server", "legacy-software-development"])
+    ds.addGroups(["news-server", "legacy-software-development"])
 
-    #Everthing else but kernels
+    # Everything else but kernels
     for po in ds.pkgSack.returnPackages():
         if po.pkgtup not in ds._installed.returnPackages():
             if po.name.find("kernel") == -1:
                 member = ds.tsInfo.addInstall(po)
 
     ds.resolveDeps()
-    processTransaction(ds)
-    os.unlink(config)
+    ds.processTransaction()
+    ds.unlinkConfig()
     shutil.rmtree(testpath)
+    
-- 
1.6.0.6

_______________________________________________
Anaconda-devel-list mailing list
Anaconda-devel-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/anaconda-devel-list

[Index of Archives]     [Kickstart]     [Fedora Users]     [Fedora Legacy List]     [Fedora Maintainers]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]
  Powered by Linux