manage f18 hostname setting in vbuild-init-lxc.sh
[build.git] / spec2make.py
1 #!/usr/bin/python
2
3 import sys
4 import os.path
5 import rpm
6
7 from optparse import OptionParser
8
9 def main ():
10     usage="%prog [rpmbuild-options] specfile pkg-name"
11     parser=OptionParser(usage=usage)
12     parser.add_option('-t','--target',action='store',dest='target',default=None,
13                       help='specify target arch')
14     parser.add_option('-w','--whitelist-rpms',action='store',dest='whitelist',default='',
15                       help='comma separated list of rpms to expose in makefile')
16     parser.add_option('-1','--with',action='store',dest='with',default='',
17                       help='accept but ignore --with option')
18     parser.add_option('-0','--without',action='store',dest='without',default='',
19                       help='accept but ignore --without option')
20     parser.add_option('-d','--define',action='store',dest='define',default='',
21                       help='accept but ignore --define option')
22     (options,args) = parser.parse_args()
23
24     try:
25         [specfile,package_name]=args
26     except:
27         parser.print_help()
28         sys.exit(1)
29
30     ts=rpm.TransactionSet()
31     specobj = ts.parseSpec(specfile)
32     for (fullsource,_,__) in specobj.sources:
33         #print '###fullsource=',fullsource
34         print "%s.tarballs += SOURCES/%s" %(package_name,os.path.basename(fullsource))
35         for suffix in [".tar.gz", ".tgz", ".tar.bz2", ] : 
36             #print "# trying %s"%suffix
37             if fullsource.endswith(suffix):
38                 sourcename=fullsource.replace(suffix,"")
39                 print "%s.source := SOURCES/%s" %(package_name,os.path.basename(sourcename))
40                 break
41
42     # Get SRPM name from name of first package
43     package=specobj.packages[0]
44     header0=package.header
45     name=header0.format('%{name}')
46     version=header0.format('%{version}')
47     release=header0.format('%{release}')
48     print "%s.srpm := SRPMS/%s-%s-%s.src.rpm"%(package_name, name, version, release)
49
50     target = options.target
51     whitelist=[ x for x in options.whitelist.split(',') if x]
52     # Print non-empty packages
53     counter=0
54     for package in specobj.packages:
55         counter += 1
56         header=package.header
57         name=header.format('%{name}')
58         version=header.format('%{version}')
59         release=header.format('%{release}')
60         arch=target or header.format('%{arch}')
61         
62         # skip dummy entries
63         if not (name and version and release and arch) : continue
64
65         # select relevant packages
66         # could not find the magic recipe to do this properly yet
67         # so ugly temporary hack :
68         # when whitelisted, we expose the first package plus the white-listed ones
69         # otherwise we expose everything
70         relevant=False
71         if not whitelist: relevant=True
72         else:
73             if counter==1: relevant=True
74             else: relevant=name in whitelist
75
76         if relevant:
77             # attach (add) rpm path to package
78             print "%s.rpms += RPMS/%s/%s-%s-%s.%s.rpm"%\
79                 (package_name, arch, name, version, release, arch)
80             # convenience
81             print "%s.rpmnames += %s"%\
82                 (package_name, name);
83             # attach path to rpm name
84             print "%s.rpm-path := RPMS/%s/%s-%s-%s.%s.rpm"%\
85                 (name,arch, name, version, release, arch)
86             # attach package to rpm name for backward resolution - should be unique
87             print "%s.package := %s"%\
88                 (name,package_name)
89             
90     for macro in ["release" , "name" , "version" , "taglevel" , ] :
91         format="%%{%s}"%macro
92         try:     print "%s.rpm-%s := %s"%(package_name,macro,header0.format(format))
93         except : print "# %s.rpm-%s undefined"%(package_name,macro)
94
95     # export arch
96     print "%s.rpm-arch := %s"%(package_name,target)
97
98 if __name__ == '__main__':
99     main()