2 # Gnucomo repository report.
4 # Create a 'rpm -qa' - like report from a directory of (rpm)
5 # packages. Select the latest version of each package if there
6 # are multiple files of the same package available.
13 # Tokenize a string into parts of consequetive letters or digits.
16 def tokenize(version):
19 state = 0 # 0: No alphanumeric, 1: digits, 2: letters
20 for letter in version:
56 # compare alpha and numeric segments of two versions
57 # return 1: a is newer than b
58 # 0: a and b are the same version
59 # -1: b is newer than a
61 def compare_versions(a, b):
63 version1 = tokenize(a)
64 version2 = tokenize(b)
65 for v1, v2 in zip(version1, version2):
66 if rc == 0 and v1 != v2:
67 if v1.isdigit() and v2.isdigit():
75 if rc == 0 and len(version1) != len(version2):
76 if len(version1) > len(version2):
84 # The string method rsplit() is available in python version 2.4 and later.
85 # This is a limited implementation for older python versions.
88 def rsplit2(src, sep):
90 words = src.split(sep)
92 result.append(sep.join(words[0:-2]))
94 result.append(words[-2])
95 result.append(words[-1])
99 # Make sure we have the repository directory as argument.
101 if len(sys.argv) != 2:
102 print "Usage: ", sys.argv[0], " <directory>"
106 for name in glob.glob(os.path.join(sys.argv[1],"*.rpm")):
107 pack = rsplit2(os.path.basename(name),".")
109 packages.append(pack[0])
116 # Select the latest package in case of duplicates.
118 for name in packages:
119 p1 = rsplit2(previous,"-")
120 p2 = rsplit2(name,"-")
122 if compare_versions(p1[1], p2[1]) < 0:
123 #print "Skipping old package ", previous
125 elif compare_versions(p1[1], p2[1]) == 0 and compare_versions(p1[2], p2[2]) < 0:
126 #print "Skipping old package ", previous
130 latest_packages.append(previous)
133 latest_packages.append(previous)
135 for p in latest_packages: