3 # Gnucomo repository report.
5 # Create a 'rpm -qa' - like report from a directory of (rpm)
6 # packages. Select the latest version of each package if there
7 # are multiple files of the same package available.
14 # Tokenize a string into parts of consequetive letters or digits.
17 def tokenize(version):
20 state = 0 # 0: No alphanumeric, 1: digits, 2: letters
21 for letter in version:
57 # compare alpha and numeric segments of two versions
58 # return 1: a is newer than b
59 # 0: a and b are the same version
60 # -1: b is newer than a
62 def compare_versions(a, b):
64 version1 = tokenize(a)
65 version2 = tokenize(b)
66 for v1, v2 in zip(version1, version2):
67 if rc == 0 and v1 != v2:
68 if v1.isdigit() and v2.isdigit():
76 if rc == 0 and len(version1) != len(version2):
77 if len(version1) > len(version2):
85 # The string method rsplit() is available in python version 2.4 and later.
86 # This is a limited implementation for older python versions.
89 def rsplit2(src, sep):
91 words = src.split(sep)
93 result.append(sep.join(words[0:-2]))
95 result.append(words[-2])
96 result.append(words[-1])
100 # Make sure we have the repository directory as argument.
102 if len(sys.argv) != 2:
103 print ("Usage: ", sys.argv[0], " <directory>")
107 for name in glob.glob(os.path.join(sys.argv[1],"*.rpm")):
108 pack = os.path.basename(name).rsplit(".",2)
110 packages.append(pack[0])
117 # Select the latest package in case of duplicates.
119 for name in packages:
120 p1 = previous.rsplit("-",2)
121 p2 = name.rsplit("-", 2)
123 if compare_versions(p1[1], p2[1]) < 0:
124 #print "Skipping old package ", previous
126 elif compare_versions(p1[1], p2[1]) == 0 and compare_versions(p1[2], p2[2]) < 0:
127 #print "Skipping old package ", previous
131 latest_packages.append(previous)
134 latest_packages.append(previous)
136 for p in latest_packages: