How to parse an RPM file name?

Jos Vos jos at xos.nl
Tue Nov 26 16:18:36 UTC 2013


On Tue, Nov 26, 2013 at 10:52:43AM -0500, Tom Limoncelli wrote:

> How's this for a regular expression?  It works on all the test cases
> I've written so far.
> 
> #! /usr/bin/python
> 
> import sys
> import re
> 
> for x in sys.argv[1:]:
>     m = re.search(r'(.*)/*(.*)-(.*)-(.*?)\.(.*)(\.rpm)', x)
>     if m:
>         (path, name, version, release, platform, _) = m.groups()
>         path = path or ''
>         verrel = version + '-' + release
>         print "\t".join([path, name, verrel, version, release, platform])
>     else:
>       sys.stderr.write('ERROR: Invalid name: %s\n' % x)
>       sys.exit(1)

This won't work.  Your path will include the name (first (.*) is eating all), 
your /* will be empty and the second (.*) will be empty too.

This one will work I think:

  m = re.search(r'(.*?)([^/]+)-([^-/]+)-([^-/]+)\.([^\./]+)(\.rpm)', x)

This can be shorter, but I linke more explicit expressions like [^\./].
You could make life easier by separating path and filename via the
basename utility and then use a much simpler expression.

Note: formally this does NOT guarantee the tags of the file, as
you of course could rename a generated rpm file to whatever you want
(and you can even ask rpmbuild to generate a different filename).

Furthermore, purely theoretical (AFAIK) an rpm package could have a
version or release containing a dash, but I've never seen one "in
the wild" and probably rpmbuild prohibits building one.

-- 
--    Jos Vos <jos at xos.nl>
--    X/OS Experts in Open Systems BV   |   Phone: +31 20 6938364
--    Amsterdam, The Netherlands        |     Fax: +31 20 6948204


More information about the Rpm-list mailing list