parse_spec function
authorhwangx <huix.wang@intel.com>
Thu, 29 Mar 2012 06:35:11 +0000 (14:35 +0800)
committerhwangx <huix.wang@intel.com>
Thu, 29 Mar 2012 07:34:10 +0000 (15:34 +0800)
gitbuildsys/utils.py

index 94c6f409416d9ab210cbfc76e174166346458cb6..1f26e4ddc4637bf5bf941a00ad0a88a109b49c95 100644 (file)
@@ -25,6 +25,7 @@ import re
 import msger
 import runner
 import errors
+import rpm
 
 compressor_opts = { 'gzip'  : [ '-n', 'gz' ],
                     'bzip2' : [ '', 'bz2' ],
@@ -96,47 +97,24 @@ def get_share_dir():
 def parse_spec(spec_path, macro):
     """Parse the spec file to get the specified `macro`
     """
+    specinfo = rpm.spec(spec_path)
 
-    rpmb_cmd = 'rpmbuild'
-
-    if which(rpmb_cmd):
-        # rpmbuild has been installed in system, use it
-        rpmb_cmdline = ("%s -bp --nodeps --force "
-                        "tmp.spec --define '_topdir .' "
-                        "--define '_builddir .' "
-                        "--define '_sourcedir .' "
-                        "--define '_rpmdir .' "
-                        "--define '_specdir .' "
-                        "--define '_srcrpmdir .'") % rpmb_cmd
-
-        wf = open('tmp.spec', 'w')
-        with file(spec_path) as f:
-            for line in f:
-                if line.startswith('%prep'):
-                    line ='%%prep\necho %%{%s}\nexit\n' % macro
-                wf.write(line)
-        wf.close()
-
-        outs = runner.outs(rpmb_cmdline, catch=3)
-
-        # clean up
-        os.unlink('tmp.spec')
-        if os.path.isdir('BUILDROOT'):
-            import shutil
-            shutil.rmtree('BUILDROOT', ignore_errors=True)
-
-        for line in outs.splitlines():
-            if line.startswith('+ echo '):
-                return line[7:].rstrip()
-
-        msger.warning('invalid spec file, cannot get the value of macro %s' \
-                      % macro)
-        return ''
-
-    else:
-        # TBD parse it directly
-        msger.warning('cannot support parsing spec without rpmbuild command')
-        return ''
+    try:
+        package = specinfo.packages[0]
+        try:
+            if macro in package.header:
+                return package.header[macro]
+        except ValueError:
+            if (macro.lower().find('source') == 0) or (macro.lower().find('patch') == 0):
+                for name, number, flg in specinfo.sources:
+                    if ((flg == rpm.RPMBUILD_ISSOURCE) and (str(number) == macro[6:])) \
+                    or ((flg == rpm.RPMBUILD_ISPATCH) and (str(number) == macro[5:])):
+                        return name
+            else:
+                return specinfo.__getattribute__(macro)
+    except:
+        raise
+    raise Exception("%s has no attribute %s" %(spec_path, macro))
 
 def get_processors():
     """