[packages/pldnotify/py: 2/2] add specparser to parse out macros

glen glen at pld-linux.org
Tue Nov 3 22:49:54 CET 2015


commit 28c1c9e596748c147b0720de148647fc2f85c280
Author: Elan Ruusamäe <glen at delfi.ee>
Date:   Tue Nov 3 23:48:27 2015 +0200

    add specparser to parse out macros

 pldnotify.py | 44 ++++++++++++++++++++++++++++++++++++++------
 1 file changed, 38 insertions(+), 6 deletions(-)
---
diff --git a/pldnotify.py b/pldnotify.py
index daecd33..3dbbd8a 100755
--- a/pldnotify.py
+++ b/pldnotify.py
@@ -1,15 +1,47 @@
 #!/usr/bin/python
 
 import argparse
-import collections
-import os
 import rpm
-import shlex
-import subprocess
 import sys
 
+"""
+RPM Spec parser
+"""
+class RPMSpec:
+    def __init__(self, specfile):
+        self._specfile = specfile
+        self._spec = None
+        self._macros = None
+
+    def getSpec(self):
+        if not self._spec:
+            ts = rpm.TransactionSet()
+            self._spec = ts.parseSpec(self._specfile)
+
+        return self._spec
+
+    def macros(self):
+        if not self._macros:
+            s = self.getSpec()
+            macros = {}
+            for key, macro in s.macros().items():
+                # skip functions
+                if 'opts' in macro:
+                    continue
+                # skip unused macros
+                if macro['used'] <= 0:
+                    continue
+                macros[key] = macro['body']
+            self._macros = macros
+
+        return self._macros
+
 def check_package(package):
-    print package
+    s = RPMSpec(package)
+    macros = s.macros()
+    name = macros['name']
+    version = macros['version']
+    print "%s: %s" % (name, version)
 
 def main():
     parser = argparse.ArgumentParser(description='PLD-Notify: project to monitor upstream releases.')
@@ -19,7 +51,7 @@ def main():
     parser.add_argument('package',
         type=str,
         help='Package to check')
-        
+
     args = parser.parse_args()
     check_package(args.package)
 
================================================================

---- gitweb:

http://git.pld-linux.org/gitweb.cgi/packages/pldnotify.git/commitdiff/28c1c9e596748c147b0720de148647fc2f85c280



More information about the pld-cvs-commit mailing list