[aur-dev] [PATCH/RFC] git-update: Replace aurinfo.py with python-srcinfo

Johannes Löthberg johannes at kyriasis.com
Sat Jun 13 15:13:13 UTC 2015


python-srcinfo is a more transparent and simpler library for parsing
SRCINFO files.

Signed-off-by: Johannes Löthberg <johannes at kyriasis.com>
---
This patch has not been tested yet so should not be merged

 git-interface/aurinfo.py    | 208 --------------------------------------------
 git-interface/git-update.py |  39 ++++-----
 2 files changed, 18 insertions(+), 229 deletions(-)
 delete mode 100644 git-interface/aurinfo.py

diff --git a/git-interface/aurinfo.py b/git-interface/aurinfo.py
deleted file mode 100644
index 86d0705..0000000
--- a/git-interface/aurinfo.py
+++ /dev/null
@@ -1,208 +0,0 @@
-#!/usr/bin/env python
-
-from copy import copy, deepcopy
-import pprint
-import sys
-
-class Attr(object):
-    def __init__(self, name, is_multivalued=False, allow_arch_extensions=False):
-        self.name = name
-        self.is_multivalued = is_multivalued
-        self.allow_arch_extensions = allow_arch_extensions
-
-PKGBUILD_ATTRIBUTES = {
-    'arch':         Attr('arch',            True),
-    'backup':       Attr('backup',          True),
-    'changelog':    Attr('changelog',       False),
-    'checkdepends': Attr('checkdepends',    True),
-    'conflicts':    Attr('conflicts',       True, True),
-    'depends':      Attr('depends',         True, True),
-    'epoch':        Attr('epoch',           False),
-    'groups':       Attr('groups',          True),
-    'install':      Attr('install',         False),
-    'license':      Attr('license',         True),
-    'makedepends':  Attr('makedepends',     True, True),
-    'md5sums':      Attr('md5sums',         True, True),
-    'noextract':    Attr('noextract',       True),
-    'optdepends':   Attr('optdepends',      True, True),
-    'options':      Attr('options',         True),
-    'pkgname':      Attr('pkgname',         False),
-    'pkgrel':       Attr('pkgrel',          False),
-    'pkgver':       Attr('pkgver',          False),
-    'provides':     Attr('provides',        True, True),
-    'replaces':     Attr('replaces',        True, True),
-    'sha1sums':     Attr('sha1sums',        True, True),
-    'sha224sums':   Attr('sha224sums',      True, True),
-    'sha256sums':   Attr('sha256sums',      True, True),
-    'sha384sums':   Attr('sha384sums',      True, True),
-    'sha512sums':   Attr('sha512sums',      True, True),
-    'source':       Attr('source',          True, True),
-    'url':          Attr('url',             False),
-    'validpgpkeys': Attr('validpgpkeys',    True),
-}
-
-def find_attr(attrname):
-    # exact match
-    attr = PKGBUILD_ATTRIBUTES.get(attrname, None)
-    if attr:
-        return attr
-
-    # prefix match
-    # XXX: this could break in the future if PKGBUILD(5) ever
-    # introduces a key which is a subset of another.
-    for k in PKGBUILD_ATTRIBUTES.keys():
-        if attrname.startswith(k + '_'):
-            return PKGBUILD_ATTRIBUTES[k]
-
-def IsMultiValued(attrname):
-    attr = find_attr(attrname)
-    return attr and attr.is_multivalued
-
-class AurInfo(object):
-    def __init__(self):
-        self._pkgbase = {}
-        self._packages = {}
-
-    def GetPackageNames(self):
-        return self._packages.keys()
-
-    def GetMergedPackage(self, pkgname):
-        package = deepcopy(self._pkgbase)
-        package['pkgname'] = pkgname
-        for k, v in self._packages.get(pkgname).items():
-            package[k] = deepcopy(v)
-        return package
-
-    def AddPackage(self, pkgname):
-        self._packages[pkgname] = {}
-        return self._packages[pkgname]
-
-    def SetPkgbase(self, pkgbasename):
-        self._pkgbase = {'pkgname' : pkgbasename}
-        return self._pkgbase
-
-
-class StderrECatcher(object):
-    def Catch(self, lineno, error):
-        print('ERROR[%d]: %s' % (lineno, error), file=sys.stderr)
-
-
-class CollectionECatcher(object):
-    def __init__(self):
-        self._errors = []
-
-    def Catch(self, lineno, error):
-        self._errors.append((lineno, error))
-
-    def HasErrors(self):
-        return len(self._errors) > 0
-
-    def Errors(self):
-        return copy(self._errors)
-
-
-def ParseAurinfoFromIterable(iterable, ecatcher=None):
-    aurinfo = AurInfo()
-
-    if ecatcher is None:
-        ecatcher = StderrECatcher()
-
-    current_package = None
-    lineno = 0
-
-    for line in iterable:
-        lineno += 1
-
-        if line.startswith('#'):
-            continue
-
-        if not line.strip():
-            # end of package
-            current_package = None
-            continue
-
-        if not line.startswith('\t'):
-            # start of new package
-            try:
-                key, value = map(str.strip, line.split('=', 1))
-            except ValueError:
-                ecatcher.Catch(lineno, 'unexpected header format in section=%s' %
-                    current_package['pkgname'])
-                continue
-
-            if key == 'pkgbase':
-                current_package = aurinfo.SetPkgbase(value)
-            elif key == 'pkgname':
-                current_package = aurinfo.AddPackage(value)
-            else:
-                ecatcher.Catch(lineno, 'unexpected new section not starting '
-                               'with \'pkgname\' found')
-                continue
-        else:
-            # package attribute
-            if current_package is None:
-                ecatcher.Catch(lineno, 'package attribute found outside of '
-                               'a package section')
-                continue
-
-            try:
-                key, value = map(str.strip, line.split('=', 1))
-            except ValueError:
-                ecatcher.Catch(lineno, 'unexpected attribute format in '
-                               'section=%s' % current_package['pkgname'])
-
-            if IsMultiValued(key):
-                if not current_package.get(key):
-                    current_package[key] = []
-                if value:
-                    current_package[key].append(value)
-            else:
-                if not current_package.get(key):
-                    current_package[key] = value
-                else:
-                    ecatcher.Catch(lineno, 'overwriting attribute '
-                                   '%s: %s -> %s' % (key, current_package[key],
-                                                     value))
-
-    return aurinfo
-
-
-def ParseAurinfo(filename='.AURINFO', ecatcher=None):
-    with open(filename) as f:
-        return ParseAurinfoFromIterable(f, ecatcher)
-
-
-def ValidateAurinfo(filename='.AURINFO'):
-    ecatcher = CollectionECatcher()
-    ParseAurinfo(filename, ecatcher)
-    errors = ecatcher.Errors()
-    for error in errors:
-        print('error on line %d: %s' % error, file=sys.stderr)
-    return not errors
-
-
-if __name__ == '__main__':
-    pp = pprint.PrettyPrinter(indent=4)
-
-    if len(sys.argv) == 1:
-        print('error: not enough arguments')
-        sys.exit(1)
-    elif len(sys.argv) == 2:
-        action = sys.argv[1]
-        filename = '.AURINFO'
-    else:
-        action, filename = sys.argv[1:3]
-
-    if action == 'parse':
-        aurinfo = ParseAurinfo()
-        for pkgname in aurinfo.GetPackageNames():
-            print(">>> merged package: %s" % pkgname)
-            pp.pprint(aurinfo.GetMergedPackage(pkgname))
-            print()
-    elif action == 'validate':
-        sys.exit(not ValidateAurinfo(filename))
-    else:
-        print('unknown action: %s' % action)
-        sys.exit(1)
-
-# vim: set et ts=4 sw=4:
diff --git a/git-interface/git-update.py b/git-interface/git-update.py
index d87fac7..a3347d1 100755
--- a/git-interface/git-update.py
+++ b/git-interface/git-update.py
@@ -8,7 +8,8 @@ import pygit2
 import re
 import sys
 
-import aurinfo
+import srcinfo.parse
+import srcinfo.utils
 
 config = configparser.RawConfigParser()
 config.read(os.path.dirname(os.path.realpath(__file__)) + "/../conf/config")
@@ -38,7 +39,7 @@ def extract_arch_fields(pkginfo, field):
 
 def save_srcinfo(srcinfo, db, cur, user):
     # Obtain package base ID and previous maintainer.
-    pkgbase = srcinfo._pkgbase['pkgname']
+    pkgbase = srcinfo['pkgbase']
     cur.execute("SELECT ID, MaintainerUID FROM PackageBases "
                 "WHERE Name = %s", [pkgbase])
     (pkgbase_id, maintainer_uid) = cur.fetchone()
@@ -58,8 +59,8 @@ def save_srcinfo(srcinfo, db, cur, user):
     cur.execute("DELETE FROM Packages WHERE PackageBaseID = %s",
                 [pkgbase_id])
 
-    for pkgname in srcinfo.GetPackageNames():
-        pkginfo = srcinfo.GetMergedPackage(pkgname)
+    for pkgname in srcinfo.utils.get_package_names(srcinfo):
+        pkginfo = srcinfo.utils.get_merged_package(pkgname, srcinfo)
 
         if 'epoch' in pkginfo and int(pkginfo['epoch']) > 0:
             ver = '%d:%s-%s' % (int(pkginfo['epoch']), pkginfo['pkgver'],
@@ -220,24 +221,21 @@ for commit in walker:
             die_commit("maximum blob size (250kB) exceeded", commit.id)
 
     srcinfo_raw = repo[commit.tree['.SRCINFO'].id].data.decode()
-    srcinfo_raw = srcinfo_raw.split('\n')
-    ecatcher = aurinfo.CollectionECatcher()
-    srcinfo = aurinfo.ParseAurinfoFromIterable(srcinfo_raw, ecatcher)
-    errors = ecatcher.Errors()
+    (srcinfo, errors) = srcinfo.parse.parse_srcinfo(srcinfo_raw)
     if errors:
-        sys.stderr.write("error: The following errors occurred "
-                         "when parsing .SRCINFO in commit\n")
-        sys.stderr.write("error: %s:\n" % (commit.id))
+        print('error: The following errors occured '
+              'while parsing .SRCINFO in commit', file=sys.stderr)
+        print('error: {:s}'.format(commit.id), file=sys.stderr)
         for error in errors:
-            sys.stderr.write("error: line %d: %s\n" % error)
+            print('error: {:s}'.format(error), file=sys.stderr)
         exit(1)
 
-    srcinfo_pkgbase = srcinfo._pkgbase['pkgname']
+    srcinfo_pkgbase = srcinfo['pkgbase']
     if not re.match(repo_regex, srcinfo_pkgbase):
         die_commit('invalid pkgbase: %s' % (srcinfo_pkgbase), commit.id)
 
-    for pkgname in srcinfo.GetPackageNames():
-        pkginfo = srcinfo.GetMergedPackage(pkgname)
+    for pkgname in set(srcinfo['packages'].keys()):
+        pkginfo = srcinfo.utils.get_merged_package(pkgname, srcinfo)
 
         for field in ('pkgver', 'pkgrel', 'pkgname', 'pkgdesc', 'url'):
             if not field in pkginfo:
@@ -271,19 +269,18 @@ for commit in walker:
                 die_commit('missing source file: %s' % (fname), commit.id)
 
 srcinfo_raw = repo[repo[sha1_new].tree['.SRCINFO'].id].data.decode()
-srcinfo_raw = srcinfo_raw.split('\n')
-srcinfo = aurinfo.ParseAurinfoFromIterable(srcinfo_raw)
+srcinfo = srcinfo.parse.parse_srcinfo(srcinfo_raw)
 
-srcinfo_pkgbase = srcinfo._pkgbase['pkgname']
+srcinfo_pkgbase = srcinfo['pkgbase']
 if srcinfo_pkgbase != pkgbase:
     die('invalid pkgbase: %s' % (srcinfo_pkgbase))
 
-pkgbase = srcinfo._pkgbase['pkgname']
+pkgbase = srcinfo['pkgbase']
 cur.execute("SELECT ID FROM PackageBases WHERE Name = %s", [pkgbase])
 pkgbase_id = cur.fetchone()[0]
 
-for pkgname in srcinfo.GetPackageNames():
-    pkginfo = srcinfo.GetMergedPackage(pkgname)
+for pkgname in srcinfo.utils.get_package_names(srcinfo):
+    pkginfo = srcinfo.utils.get_merged_package(pkgname, srcinfo)
     pkgname = pkginfo['pkgname']
 
     if pkgname in blacklist:
-- 
2.4.2


More information about the aur-dev mailing list