[projects/pld-ftp-admin] Some more pythonic optimizations

baggins baggins at pld-linux.org
Sat Jan 5 16:11:09 CET 2019


commit 57e1c0c3a255125165572832833a49cee0c2c5d1
Author: Jan Rękorajski <baggins at pld-linux.org>
Date:   Sun Jan 6 00:09:32 2019 +0900

    Some more pythonic optimizations
    
    - slicing is faster that str methods
    - splitting is faster than regexps
    - in operator is faster han has_key

 wwwbin/ftp-freshness.py | 28 +++++++---------------------
 1 file changed, 7 insertions(+), 21 deletions(-)
---
diff --git a/wwwbin/ftp-freshness.py b/wwwbin/ftp-freshness.py
index 7af8122..3208269 100755
--- a/wwwbin/ftp-freshness.py
+++ b/wwwbin/ftp-freshness.py
@@ -2,9 +2,7 @@
 # arekm, 2007
 
 import os
-import re
 import rpm
-import struct
 import subprocess
 import sys
 
@@ -31,7 +29,6 @@ class Pkgs(object):
         self.cvs = {}
         self.cvs_new_nvr = []
         self.cvs_new_nv = []
-        self.re_n = re.compile('^(.*)-([^-]*)-([^-]*)\.src\.rpm$')
         rpm.addMacro('patch', '%{nil}')
         rpm.addMacro('requires_eq', '#')
         rpm.addMacro('requires_eq_to', '#')
@@ -45,18 +42,9 @@ class Pkgs(object):
             rpm.delMacro(macro)
         macros = rpm.getMacros()
         for macro in macros.iterkeys():
-            if macro.startswith('with_') or macro.startswith('without_'):
+            if macro[:5] == 'with_' or macro[:8] == 'without_':
                 rpm.delMacro(macro.split()[0])
 
-    def __get_from_rpm(self, file_name):
-        m = self.re_n.match(file_name)
-        if not m:
-            return False
-        name =  m.group(1).strip()
-        version = m.group(2).strip()
-        release = m.group(3).strip()
-        return (name, version, release)
-
     def __get_from_cvs(self, name):
         spec = os.path.join(specsdir, name + '.spec')
         if not os.path.isfile(spec):
@@ -76,7 +64,7 @@ class Pkgs(object):
             return ('','','')
 
     def __update_cvs(self, name):
-        if not self.cvs.has_key(name):
+        if name not in self.cvs:
             self.cvs[name] = self.__get_from_cvs(name)
 
     def __update_new(self, name):
@@ -104,13 +92,10 @@ class Pkgs(object):
             self.__update_new(name)
 
     def add_rpm(self, file_name):
-        nvr = self.__get_from_rpm(file_name)
-        if not nvr:
-            return False
+        nvr = tuple(file_name.rsplit('-', 2))
         name = nvr[0]
-        if self.pkg.has_key(name):
+        if name in self.pkg:
             if rpm.labelCompare(nvr, self.pkg[name]) > 0:
-                del self.pkg[name]
                 self.pkg[name] = nvr
         else:
             self.pkg[name] = nvr
@@ -149,8 +134,9 @@ class Pkgs(object):
 p = Pkgs()
 
 for d in dirs:
-    for rpm_file_name in os.listdir(d):
-        p.add_rpm(rpm_file_name)
+    rpm_files = [f for f in os.listdir(d) if f[-8:] == '.src.rpm']
+    for rpm_file_name in rpm_files:
+        p.add_rpm(rpm_file_name[:-8])
 
 p.prepare()
 
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/projects/pld-ftp-admin.git/commitdiff/57e1c0c3a255125165572832833a49cee0c2c5d1



More information about the pld-cvs-commit mailing list