[arch-projects] [namcap 1/6] Don't assume local DB when looking up providers
Fixes the first warning in the following output. It is clearly invalid due to the fact that I don't even have [testing] enabled on this machine. $ ./namcap-devel -r shebangdepends pyalpm-0.4.2-1-i686.pkg.tar.xz pyalpm W: Dependency 'python3' on your system is a testing release pyalpm W: Dependency included and not needed ('pacman') Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/package.py | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Namcap/package.py b/Namcap/package.py index dae6c44..245169c 100644 --- a/Namcap/package.py +++ b/Namcap/package.py @@ -240,13 +240,13 @@ def load_from_db(pkgname, dbname = None): p = db.get_pkg(pkgname) if p is None: - p = lookup_provider(pkgname) + p = lookup_provider(pkgname, db) if p is not None: p = load_from_alpm(p) return p -def lookup_provider(pkgname): - for pkg in pyalpm.get_localdb().pkgcache: +def lookup_provider(pkgname, db): + for pkg in db.pkgcache: if pkgname in pkg.provides: return pkg -- 1.7.5.2
Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/rules/sodepends.py | 39 +++++++++++++++++---------------------- 1 files changed, 17 insertions(+), 22 deletions(-) diff --git a/Namcap/rules/sodepends.py b/Namcap/rules/sodepends.py index d375492..00200f7 100644 --- a/Namcap/rules/sodepends.py +++ b/Namcap/rules/sodepends.py @@ -21,9 +21,11 @@ """Checks dependencies resulting from linking of shared libraries.""" +from collections import defaultdict import re import os import subprocess +import pyalpm import tempfile import Namcap.package from Namcap.ruleclass import * @@ -93,7 +95,7 @@ def finddepends(liblist): dependlist -- a dictionary { package => set(libraries) } orphans -- the list of libraries without owners """ - dependlist = {} + dependlist = defaultdict(set) somatches = {} actualpath = {} @@ -101,8 +103,7 @@ def finddepends(liblist): knownlibs = set(liblist) foundlibs = set() - for j in knownlibs: - actualpath[j] = os.path.realpath('/'+j)[1:] + actualpath = dict((j, os.path.realpath('/' + j)[1:]) for j in knownlibs) # Sometimes packages don't include all so .so, .so.1, .so.1.13, .so.1.13.19 files # They rely on ldconfig to create all the symlinks @@ -111,25 +112,18 @@ def finddepends(liblist): # Whether we should even look at a particular file is_so = re.compile('\.so') - pacmandb = '/var/lib/pacman/local' - for i in os.listdir(pacmandb): - if os.path.isfile(pacmandb+'/'+i+'/files'): - file = open(pacmandb+'/'+i+'/files', errors='ignore') - for j in file: - if not is_so.search(j): - continue - - for k in knownlibs: - # File must be an exact match or have the right .so ending numbers - # i.e. gpm includes libgpm.so and libgpm.so.1.19.0, but everything links to libgpm.so.1 - # We compare find libgpm.so.1.19.0 startswith libgpm.so.1 and .19.0 matches the regexp - if j == actualpath[k] or (j.startswith(actualpath[k]) and so_end.match(j[len(actualpath[k]):])): - n = re.match('(.*)-([^-]*)-([^-]*)', i) - if n.group(1) not in dependlist: - dependlist[n.group(1)] = set() - dependlist[n.group(1)].add(k) - foundlibs.add(k) - file.close() + for pkg in pyalpm.get_localdb().pkgcache: + for j in pkg.files: + if not is_so.search(j): + continue + + for k in knownlibs: + # File must be an exact match or have the right .so ending numbers + # i.e. gpm includes libgpm.so and libgpm.so.1.19.0, but everything links to libgpm.so.1 + # We compare find libgpm.so.1.19.0 startswith libgpm.so.1 and .19.0 matches the regexp + if j == actualpath[k] or (j.startswith(actualpath[k]) and so_end.match(j[len(actualpath[k]):])): + dependlist[pkg.name].add(k) + foundlibs.add(k) orphans = list(knownlibs - foundlibs) return dependlist, orphans @@ -147,6 +141,7 @@ def filllibcache(): if g.group(2).startswith('libc6,x86-64'): libcache['x86-64'][g.group(1)] = g.group(3) else: + # TODO: This is bogus; what do non x86-architectures print? libcache['i686'][g.group(1)] = g.group(3) -- 1.7.5.2
We need to use re.search() rather than re.match() most of the time in this rule. Also simplify it, add some more architectures to look for, and update the tests accordingly so they are actually correct and match what the rule is trying to do. Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/rules/carch.py | 18 ++++++++-------- Namcap/tests/pkgbuild/test_carch.py | 38 +++++++++++++++++++++++----------- namcap-tags | 2 +- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Namcap/rules/carch.py b/Namcap/rules/carch.py index 0469460..7cb3319 100644 --- a/Namcap/rules/carch.py +++ b/Namcap/rules/carch.py @@ -26,15 +26,15 @@ class package(PkgbuildRule): name = "carch" description = "Verifies that no specific host type is used" def analyze(self, pkginfo, tar): - arches = ['i686', 'i586', 'x86_64'] - archmatch = re.compile('%s' % '|'.join(arches)) + arches = ['arm', 'i586', 'i686', 'ppc', 'x86_64'] + archmatch = re.compile(r'\b(%s)\b' % '|'.join(arches)) # Match an arch=(i686) line - archline = re.compile('arch=\w*(%s)' % '|'.join(arches)) - # Match a [ "$CARCH" = "x86_64" ] line - archif = re.compile('''\[\s*("|')\$CARCH("|').*("|')(%s)("|')\s*\]''' % '|'.join(arches)) - for i in pkginfo.pkgbuild: - if archmatch.match(i): - if not archline.match(i) and not archif.match(i): - self.warnings.append(("specific-host-type-used %s", ",".join(arches))) + archline = re.compile(r'\w*arch=') + for line in pkginfo.pkgbuild: + # strip any comments + line = line.split('#')[0] + match = archmatch.search(line) + if match and not archline.match(line) and not '$CARCH' in line: + self.warnings.append(("specific-host-type-used %s", match.group(1))) # vim: set ts=4 sw=4 noet: diff --git a/Namcap/tests/pkgbuild/test_carch.py b/Namcap/tests/pkgbuild/test_carch.py index 1ec8597..2d95b81 100644 --- a/Namcap/tests/pkgbuild/test_carch.py +++ b/Namcap/tests/pkgbuild/test_carch.py @@ -25,24 +25,32 @@ import Namcap.rules.carch as module class NamcapSpecialArchTest(PkgbuildTest): pkgbuild1 = """ -# Maintainer: Arch Linux <archlinux@example.com> -# Contributor: Arch Linux <archlinux@example.com> +pkgname=mypackage +pkgver=1.0 +pkgrel=1 +pkgdesc="A package" +arch=('i686') + +build() { + cd "${srcdir}"/${pkgname}-${pkgver} + [[ $CARCH == x86_64 ]] && CFLAGS+="-m64" + [ '$CARCH' = 'i686' ] && CFLAGS+="-m32" + ./configure --prefix=/usr + make +} +""" + pkgbuild2 = """ pkgname=mypackage pkgver=1.0 pkgrel=1 pkgdesc="A package" arch=('i686') -url="http://www.example.com/" -license=('GPL') -depends=('glibc') -options=('!libtool') -source=(ftp://ftp.example.com/pub/mypackage-0.1.tar.gz) -md5sums=('abcdefabcdef12345678901234567890') build() { cd "${srcdir}"/${pkgname}-${pkgver} - [[ $CARCH == x86_64 ]] && CFLAGS+="-m32" + [[ $CARCH == x86_64 ]] && CFLAGS+="-m64" + [ '$CARCH' = 'i686' ] && CFLAGS+="-m32" ./configure --prefix=/usr make } @@ -51,6 +59,7 @@ package() { cd "${srcdir}"/${pkgname}-${pkgver} ./configure --prefix=/usr make DESTDIR="${pkgdir}" install + cp foobar /usr/lib/i686/pkg/ } """ @@ -59,12 +68,17 @@ package() { def preSetUp(self): self.rule = module.package - @unittest.expectedFailure def test_example1(self): - # Example 1 r = self.run_on_pkg(self.pkgbuild1) self.assertEqual(r.errors, []) - self.assertEqual(r.warnings, ("specific-host-type-used %s", "i686")) + self.assertEqual(r.warnings, []) self.assertEqual(r.infos, []) + def test_example2(self): + r = self.run_on_pkg(self.pkgbuild2) + self.assertEqual(r.errors, []) + self.assertEqual(r.warnings, [("specific-host-type-used %s", "i686")]) + self.assertEqual(r.infos, []) + + # vim: set ts=4 sw=4 noet: diff --git a/namcap-tags b/namcap-tags index 46ef2a0..a342590 100644 --- a/namcap-tags +++ b/namcap-tags @@ -66,7 +66,7 @@ potential-non-fhs-info-page %s :: Potential non-FHS info page (%s) found. potential-non-fhs-man-page %s :: Potential non-FHS man page (%s) found. script-link-detected %s in %s :: Script link detected (%s) in file %s scrollkeeper-dir-exists %s :: Scrollkeeper directory exists (%s). Remember to not run scrollkeeper till post_{install,upgrade,remove}. -specific-host-type-used %s :: Reference to one of %s should be changed to $CARCH +specific-host-type-used %s :: Reference to %s should be changed to $CARCH specific-sourceforge-mirror :: Attempting to use specific sourceforge mirror, use downloads.sourceforge.net instead symlink-found %s points to %s :: Symlink (%s) found that points to %s too-many-checksums %s %i needed :: Too many %s: %i needed -- 1.7.5.2
Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/rules/shebangdepends.py | 6 +----- 1 files changed, 1 insertions(+), 5 deletions(-) diff --git a/Namcap/rules/shebangdepends.py b/Namcap/rules/shebangdepends.py index bace844..7858abe 100644 --- a/Namcap/rules/shebangdepends.py +++ b/Namcap/rules/shebangdepends.py @@ -27,13 +27,9 @@ import tempfile import subprocess import pyalpm import Namcap.package -from Namcap.util import is_elf, script_type +from Namcap.util import script_type from Namcap.ruleclass import * -if not pyalpm.checkinit(): - pyalpm.initialize() - pyalpm.options.dbpath = "/var/lib/pacman" - def scanshebangs(fileobj, filename, scripts): """ Scan a file for shebang and stores the interpreter name. -- 1.7.5.2
* Do each import separate as is normal python convention * Fix some comment grammar and word usage * Rename verify_package() to open_package() to signify what it actually does, and remove a lot of unnecessary code in the process Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/ruleclass.py | 2 +- namcap.py | 37 +++++++++++++++---------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/Namcap/ruleclass.py b/Namcap/ruleclass.py index 17bc2b2..f34c657 100644 --- a/Namcap/ruleclass.py +++ b/Namcap/ruleclass.py @@ -24,7 +24,7 @@ This module defines the base classes from which Namcap rules are derived and how they are meant to be used. """ -# pacman 3 does not need classes to derive from object +# python 3 does not need classes to derive from object class AbstractRule(object): "The parent class of all rules" def __init__(self): diff --git a/namcap.py b/namcap.py index a53879b..dcebc55 100755 --- a/namcap.py +++ b/namcap.py @@ -19,13 +19,16 @@ # # -# System wide global stuff -import sys -import os -import imp import getopt -import types, tarfile, re, string, Namcap +import imp +import os +import re import shutil +import string +import sys +import tarfile +import types + import Namcap.depends import Namcap.tags @@ -51,26 +54,16 @@ def usage(): sys.exit(2) -def verify_package(filename): - """Checks if a tarball is a valid package""" - if not os.path.isfile(filename): - return 0 - if not tarfile.is_tarfile(filename): - return 0 +def open_package(filename): try: - tar = tarfile.open(package, "r") - if not tar: - return 0 - if not len(tar.getmembers()) > 0: - tar.close() - return 0 - if not '.PKGINFO' in tar.getnames(): + tar = tarfile.open(filename, "r") + if '.PKGINFO' not in tar.getnames(): tar.close() - return 0 + return None except IOError: if tar: tar.close() - return 0 + return None return tar def check_rules_exclude(optlist): @@ -91,7 +84,7 @@ def show_messages(name, key, messages): def process_realpackage(package, modules): """Runs namcap checks over a package tarball""" extracted = 0 - pkgtar = verify_package(package) + pkgtar = open_package(package) if not pkgtar: print("Error: %s is empty or is not a valid package" % package) @@ -105,7 +98,7 @@ def process_realpackage(package, modules): if isinstance(rule, Namcap.ruleclass.PkgInfoRule): rule.analyze(pkginfo, None) elif isinstance(rule, Namcap.ruleclass.PkgdirRule): - # If it's not extracted, then extract it and then analyze the package + # If it's not extracted, extract it and then analyze the package if not extracted: os.mkdir(sandbox_directory) for j in pkgtar.getmembers(): -- 1.7.5.2
Signed-off-by: Dan McGee <dan@archlinux.org> --- Namcap/tests/package/test_fileownership.py | 2 +- Namcap/tests/package/test_gnomemime.py | 2 +- Namcap/tests/package/test_hicoloricons.py | 2 +- Namcap/tests/package/test_kdeprograms.py | 4 +--- Namcap/tests/package/test_mimefiles.py | 2 +- Namcap/tests/package/test_permissions.py | 2 +- 6 files changed, 6 insertions(+), 8 deletions(-) diff --git a/Namcap/tests/package/test_fileownership.py b/Namcap/tests/package/test_fileownership.py index 66a2392..6065394 100644 --- a/Namcap/tests/package/test_fileownership.py +++ b/Namcap/tests/package/test_fileownership.py @@ -23,7 +23,7 @@ import os from Namcap.tests.makepkg import MakepkgTest import Namcap.rules.fileownership -class fileownershipTest(MakepkgTest): +class FileOwnershipTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_fileownership pkgver=1.0 diff --git a/Namcap/tests/package/test_gnomemime.py b/Namcap/tests/package/test_gnomemime.py index 40aa682..1f95300 100644 --- a/Namcap/tests/package/test_gnomemime.py +++ b/Namcap/tests/package/test_gnomemime.py @@ -23,7 +23,7 @@ import os from Namcap.tests.makepkg import MakepkgTest import Namcap.rules.gnomemime -class gnomemimeTest(MakepkgTest): +class GnomeMimeTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_gnomemime pkgver=1.0 diff --git a/Namcap/tests/package/test_hicoloricons.py b/Namcap/tests/package/test_hicoloricons.py index e9b9006..f47798a 100644 --- a/Namcap/tests/package/test_hicoloricons.py +++ b/Namcap/tests/package/test_hicoloricons.py @@ -24,7 +24,7 @@ from Namcap.tests.makepkg import MakepkgTest import Namcap.depends import Namcap.rules.hicoloricons -class hicoloriconsTest(MakepkgTest): +class HiColorIconsTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_hicoloricons pkgver=1.0 diff --git a/Namcap/tests/package/test_kdeprograms.py b/Namcap/tests/package/test_kdeprograms.py index a8b2642..77a6253 100644 --- a/Namcap/tests/package/test_kdeprograms.py +++ b/Namcap/tests/package/test_kdeprograms.py @@ -23,7 +23,7 @@ import os from Namcap.tests.makepkg import MakepkgTest import Namcap.rules.kdeprograms -class kdeprogramsTest(MakepkgTest): +class KdeProgramsTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_kdeprograms pkgver=1.0 @@ -143,6 +143,4 @@ package() { in r.infos) - # vim: set ts=4 sw=4 noet: - diff --git a/Namcap/tests/package/test_mimefiles.py b/Namcap/tests/package/test_mimefiles.py index 64f258f..a4a7d2f 100644 --- a/Namcap/tests/package/test_mimefiles.py +++ b/Namcap/tests/package/test_mimefiles.py @@ -23,7 +23,7 @@ import os from Namcap.tests.makepkg import MakepkgTest import Namcap.rules.mimefiles -class mimefilesTest(MakepkgTest): +class MimeFilesTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_mimefiles pkgver=1.0 diff --git a/Namcap/tests/package/test_permissions.py b/Namcap/tests/package/test_permissions.py index ac7aa2e..b3c5573 100644 --- a/Namcap/tests/package/test_permissions.py +++ b/Namcap/tests/package/test_permissions.py @@ -23,7 +23,7 @@ import os from Namcap.tests.makepkg import MakepkgTest import Namcap.rules.permissions -class permissionsTest(MakepkgTest): +class PermissionsTest(MakepkgTest): pkgbuild = """ pkgname=__namcap_test_permissions pkgver=1.0 -- 1.7.5.2
All applied, thanks. Rémy.
participants (2)
-
Dan McGee
-
Rémy Oudompheng