20915N/A#!/usr/bin/python
20915N/A
20915N/A# Author: Jeff Cai
20915N/A#
20915N/A# This script is for tranverting svr4 package names to ips packages name
20915N/A# in one spec file.
20915N/A# Input:
20915N/A# 1. spec file xxx.spec
20915N/A# 2. mapping file
20915N/A# Output:
20915N/A# 1. new spec file xxx.spec.new
20915N/A#
20915N/A
20915N/Aimport sys
20915N/Aimport re
20915N/Aimport string
20915N/Aimport os
20915N/A
20915N/Aif len(sys.argv) < 2:
20915N/A print "Usage: transform.py [spec-file-name]"
20915N/A sys.exit(0)
20915N/A
20915N/A# Read mapping file
20915N/A
20915N/Adict = {}
20915N/Atry:
20915N/A rf = open("svr4-ips-namemapping")
20915N/Aexcept:
20915N/A print "Error: Can not open name mapping file svr4-ips-namemapping"
20915N/A sys.exit(0)
20915N/A
20915N/Aprint "Reading the mapping file svr4-ips-namemapping..."
20915N/A
20915N/Afor line in rf:
20915N/A entries = re.split("[\t ]+", line)
20915N/A svr4name = string.strip(entries[0])
20915N/A ipsname = string.strip(entries[1])
20915N/A dict[svr4name] = ipsname
20915N/Arf.close()
20915N/A
20915N/Aspecfile = sys.argv[1]
20915N/A
20915N/Apat1="Requires:"
20915N/Apat2="BuildRequires:"
20915N/Aprog1 = re.compile(pat1, re.I)
20915N/Aprog2 = re.compile(pat2, re.I)
20915N/A
20915N/Atry:
20915N/A rf = open(specfile)
20915N/Aexcept:
20915N/A print "Error: can not open the file " + specfile
20915N/A sys.exit(0)
20915N/A
20915N/Atry:
20915N/A wf = open(specfile + ".new", "w")
20915N/Aexcept:
20915N/A print "Error: can not open the file " + specfile + ".new for writing"
20915N/A sys.exit(0)
20915N/A
20915N/AlnCnt = 0
20915N/AnlnCnt = 0
20915N/A
20915N/Afor line in rf:
20915N/A if prog1.match(line) or prog2.match(line):
20915N/A entries = re.split(":", line)
20915N/A svr4_name=string.strip(entries[1])
20915N/A if svr4_name in dict:
20915N/A wf.write("%s: %s\n" % (entries[0], dict[svr4_name]))
20915N/A print "transverting %s to %s" % (svr4_name, dict[svr4_name])
20915N/A lnCnt = lnCnt + 1
20915N/A else:
20915N/A print "not found mapping for %s" % svr4_name
20915N/A nlnCnt = nlnCnt + 1
20915N/A wf.write(line)
20915N/A else:
20915N/A wf.write(line)
20915N/A
20915N/Arf.close()
20915N/Awf.close()
20915N/A
20915N/Aprint
20915N/Aprint "Finished"
20915N/Aprint "Transverted %d lines" % lnCnt
20915N/Aprint "%d lines mapping not found" % nlnCnt