Getting Into that Python Frame of Mind
by
on September 24th, 2007 at 05:38 PM (1351 Views)
I was working on a Kusu kit which would pull in all the packages necessary for Kusu development, creatively named the SDK Kit. The goal is you have a fresh Kusu box and want to hack on Kusu, you install this kit and everything you need will be there.
To do this I basically install everything in the Development Tools and Development Libraries yum groups, as well as mkisofs, cmake, and a few other packages. I use our buildkit tool to generate the RPMs and ISO for me. buildkit reads a build.kit file and packages what it’s told. The build.kit file for the SDK kit describes two components, one for the Development Tools, the other for the Development Libraries, each of which has a (long) list of dependencies (the packages we want installed).
Typing out by hand all those dependencies is tedious monkey work, considering each package requires six identical lines of code. Being slightly more intelligent than a monkey I decided to write a script to do this for me.
The goal was to turn this:
rpm-python i386 4.4.2.1-1.fc6 updates 56 k into this:
# package rpm-python
rpmpythonpkg = DistroPackage(ostype='fedora')
rpmpythonpkg.name = 'rpm-python'
rpmpythonpkg.version = '4.4.2.1'
rpmpythonpkg.release = '1.fc6'
comp_dev_libs.addDep(rpmpythonpkg)
many times.
Looking at this tabular output I thought “this is a job for awk.” So I put together this nifty little script:
{ pkg = $1 "pkg"; gsub(/-/, "", pkg); gsub(/+/, "p", pkg)
# version strings are of the format epoch:version-release
colon_idx = index($3, ":")
dash_idx = index($3, "\-")
epoch = substr($3, 0, colon_idx - 1)
version = substr($3, colon_idx + 1, dash_idx - colon_idx - 1)
release = substr($3, dash_idx + 1)
print "# package", $1
print pkg" = DistroPackage(ostype='__OS_TYPE__')"
print pkg".name = '"$1"'"
print pkg".version = '"version"'"
print pkg".release = '"release"'"
print "__REPLACE_ME__.addDep("pkg")"
print ""
}
It does exactly what I need.
Najib, our project lead, noted “mike’s yumgroup2buildkit.awk is cute, would love a python one though.” Of course, I should have written this in Python to begin with! It would have taken less time, too.
Today I re-wrote my awk script in Python:
import sys
def parse_version(version):
version = version.split(':')
if len(version) > 1: # the epoch is optional
epoch = version[0]
version = version[1]
else:
epoch = None
version = version[0]
version = version.split('-')
release = version[1]
version = version[0]
return epoch, version, release
if __name__ == "__main__":
for line in sys.stdin.readlines():
pkgname, arch, version, repo, size, units = line.split()
epoch, version, release = parse_version(version)
pkg = pkgname.replace('-', '').replace('+', 'p') + 'pkg'
print "# package %s" % pkgname
print "%s = DistroPackage(ostype='__OS_TYPE__')" % pkg
print "%s.name = '%s'" % (pkg, pkgname)
print "%s.version = '%s'" % (pkg, version)
print "%s.release = '%s'" % (pkg, release)
print "__REPLACE_ME__.addDep(%s)" % pkg
“Works Great!”



Email Blog Entry