#!/usr/bin/python import os import sys import string import commands import datetime def writeSPEC(pkgName, pkgVersion, summary, license, url, source, description, dateStamp, packager, changelog): specContent = """Name: %s Version: %s Release: 1%%{?dist} Summary: %s Group: Applications/Activities License: %s URL: %s Source0: %s BuildRoot: %%{_tmppath}/%%{name}-%%{version}-%%{release}-root-%%(%%{__id_u} -n) BuildArch: noarch Requires: sugar %%description %s %%prep %%build %%install rm -rf $RPM_BUILD_ROOT mkdir -p $RPM_BUILD_ROOT%%{_datadir}/activities unzip -d $RPM_BUILD_ROOT%%{_datadir}/activities %%{SOURCE0} %%clean rm -rf $RPM_BUILD_ROOT %%files %%defattr(-,root,root,-) %%doc %%{_datadir}/activities/* %%changelog * %s %s - %s-1 - %s """ % (pkgName, pkgVersion, summary, license, url, source, description, dateStamp, packager, pkgVersion, changelog) specname = pkgName + ".spec" spec = open(specname, 'w') spec.write(specContent) spec.close() return specname def getName(src): name = src.split('-') return name[0] def getPath(): return os.getcwd() def getVersion(src): version = src.split('-') return version[1].strip('.xo') def getToday(): todayDate = datetime.date.today() dayOfWeekList = {0:'Mon',1:'Tue',2:'Wed',3:'Thu',4:'Fri',5:'Sat',6:'Sun'} monthList = {1:'Jan',2:'Feb',3:'Mar',4:'Apr',5:'May',6:'Jun',7:'Jul',8:'Aug',9:'Sep',10:'Oct',11:'Nov',12:'Dec'} dayOfWeek = dayOfWeekList[todayDate.weekday()] month = monthList[todayDate.month] day = todayDate.day year = todayDate.year return "%s %s %s %s" % (dayOfWeek, month, day, year) def getInfo(srcpkg): (status, activity) = commands.getstatusoutput("unzip -p %s */activity/activity.info" % srcpkg) if status != 0: print "Error: %s" % activity return activity def get_entry(a, entry): e = entry.lower() if not a.has_key(e): return "" return a[e] def parseInfo(pkgInfo): tmpdict = {} for attr in pkgInfo.split("\n"): if not len(attr): continue vals = attr.split('=') if len(vals) != 2: # This is the [Activity] Line continue name, value = vals[0].strip(), vals[1].strip() tmpdict[name] = value summary = get_entry(tmpdict, 'summary') description = get_entry(tmpdict, 'description') packager = get_entry(tmpdict, 'packager') license = get_entry(tmpdict, 'license') url = get_entry(tmpdict, 'url') pkgVersion = get_entry(tmpdict, 'activity_version') for item in 'ummary description packager license url' if item == '': print "Error: %s is empty I can not go on" sys.exit(1) return summary, description, packager, license, url def getChangeLog(srcpkg): return "TODO FIXME" def makerpm(spec, path): #rpmbuild --define "_sourcedir %s" --define "_specdir %s" --define "_builddir %s" --define "_srcrpmdir %s" --define "_rpmdir %s" --define "dist .olpc2" --define "fedora 7" --nodeps -ba %s (s, o) = commands.getstatusoutput("rpmbuild --define \"_sourcedir %s\" --define \"_specdir %s\" --define \"_builddir %s\" --define \"_srcrpmdir %s\" --define \"_rpmdir %s\" --define \"dist .olpc2\" --define \"fedora 7\" --nodeps -ba %s" % (path, path, path, path, path, spec)) if s != 0: print "Error: %s" % o def main(srcpkg): pkgName = getName(srcpkg) pkgVersion = getVersion(srcpkg) pkgInfo = getInfo(srcpkg) (summary, description, packager, license, url, pkgVersion) = parseInfo(pkgInfo) changelog = getChangeLog(srcpkg) dateStamp = getToday() spec = writeSPEC(pkgName, pkgVersion, summary, license, url, srcpkg, description, dateStamp, packager, changelog) path = getPath() makerpm(spec, path) if __name__ == '__main__': if len(sys.argv) < 2: print "Bad usage" sys.exit(1) #log file is first option src = sys.argv[1] if not os.path.exists(src): print "Source file does not exist." sys.exit(1) main(src)