Copying latest version of the file

Jeremy Dillworth jwdillworth at
Fri Sep 12 17:55:18 CEST 2003

I've seen RPM's named that way, I think the convention is 
something like this:


Here's how I would do it:

import re

rpm_name_parser = re.compile(r'^(.*)-(\d+)\.(\d+)\.(\d+)-(\d+)\.rpm$')

# a dictionary of packages and versions available
packages = {}

# ... read names of RPM's in dirA
# for example, we'll just use a list of 2 of the same package
for rpm_name in ['mypackage-1.1.0-37.rpm', 'mypackage-1.2.0-10.rpm']:
    match =

    # panic, the name isn't right!
    if not match:
        raise ValueError()

    package_name, majorv, minorv, bugv, rpmv = match.groups()
    majorv, minorv, bugv, rpmv = map(int, [majorv, minorv, bugv, rpmv])

    package_info = packages.setdefault(package_name, [])
    version = (rpm_name, majorv, minorv, bugv, rpmv)
    if version not in package_info:

# order all packages by version
for p in packages:

# ... to get the filenames of latest versions of all packages
latest = [packages[p][-1][0] for p in packages]

for pkgfile in latest:
    print pkgfile

--- Kali K E <kalike2003 at> wrote:
> Hi,
> I am new to Python and excuse me if this problem is very trivial. The
> problem is like this.
> There is a directory (dirA) that contains files like
> mypacakge-2.2.5-15.rpm, mypacakge-2.2.5-90.rpm,
> anotherpackage-3.4.25.rpm etc.. As can be seen, some of the files are
> new versions of same rpm (mypacakge-2.2.5-15.rpm,
> mypacakge-2.2.5-90.rpm in this example). It is also true that their
> timestamps are different. The program has to check for latest version
> of each package and copy them to another directory (dirB). That means
> in this case, it has to copy mypacakge-2.2.90.rpm to another
> directory.
> The directory dirA would be containing more than 500 files. Out of
> these many, about 100 files may be having new version.
> Another point is the number of files of same package is NOT limited to
> 2. So we may have mypacakge-2.2.5-15.rpm, mypacakge-2.2.5-90.rpm,
> mypacakge-2.2.5-100.rpm, mypacakge-2.2.5-227.rpm in dirA and only
> mypacakge-2.2.5-227.rpm has to be copied to dirB.
> The same thing has to be repeated in other directories at the same
> level as dirA.
> Please let me know how to do this.
> Thanks,
> Kali
> -- 

More information about the Python-list mailing list