]> git.pld-linux.org Git - projects/pld-ftp-admin.git/commitdiff
Some more pythonic optimizations
authorJan Rękorajski <baggins@pld-linux.org>
Sat, 5 Jan 2019 15:09:32 +0000 (00:09 +0900)
committerJan Rękorajski <baggins@pld-linux.org>
Sat, 5 Jan 2019 15:09:32 +0000 (00:09 +0900)
- slicing is faster that str methods
- splitting is faster than regexps
- in operator is faster han has_key

wwwbin/ftp-freshness.py

index 7af8122fa6a31895de3cd65e0b3d64962599fc27..3208269c239f8d8483914ef1feb165ca21e28eb7 100755 (executable)
@@ -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()
 
This page took 0.256583 seconds and 4 git commands to generate.