summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKyle Keen <keenerd@gmail.com>2017-03-19 08:48:20 -0400
committerKyle Keen <keenerd@gmail.com>2017-03-19 08:48:20 -0400
commitf35532ef29c472400a1ca61bb02a2d97ac3c532d (patch)
tree0280167307d5c724b240054d6a035862515f521e
parent28ed207670d5a40818252f1363b46ab18156c675 (diff)
downloadnamcap-f35532ef29c472400a1ca61bb02a2d97ac3c532d.tar.gz
namcap-f35532ef29c472400a1ca61bb02a2d97ac3c532d.zip
Clean some cruft
Signed-off-by: Kyle Keen <keenerd@gmail.com>
-rw-r--r--Namcap/rules/fhs.py20
-rw-r--r--Namcap/rules/filenames.py6
-rw-r--r--Namcap/rules/javafiles.py5
-rw-r--r--Namcap/rules/licensepkg.py34
-rw-r--r--Namcap/rules/missingvars.py4
-rw-r--r--Namcap/rules/pathdepends.py2
-rw-r--r--Namcap/rules/perllocal.py3
-rw-r--r--Namcap/rules/scrollkeeper.py3
-rw-r--r--Namcap/rules/sfurl.py4
-rw-r--r--Namcap/rules/shebangdepends.py20
-rw-r--r--Namcap/rules/unusedsodepends.py5
-rw-r--r--Namcap/util.py35
12 files changed, 55 insertions, 86 deletions
diff --git a/Namcap/rules/fhs.py b/Namcap/rules/fhs.py
index bca342e..6a40ca4 100644
--- a/Namcap/rules/fhs.py
+++ b/Namcap/rules/fhs.py
@@ -79,13 +79,14 @@ class FHSManpagesRule(TarballRule):
for i in tar.getmembers():
if not i.isfile():
continue
+ if i.name.startswith(gooddir):
+ continue
if i.name.startswith(bad_dir):
self.errors.append(("non-fhs-man-page %s", i.name))
- elif not i.name.startswith(gooddir):
- #Check everything else to see if it has a 'man' path component
- for part in i.name.split(os.sep):
- if part == "man":
- self.warnings.append(("potential-non-fhs-man-page %s", i.name))
+ continue
+ #Check everything else to see if it has a 'man' path component
+ if "man" in i.name.split(os.sep):
+ self.warnings.append(("potential-non-fhs-man-page %s", i.name))
class FHSInfoPagesRule(TarballRule):
name = "fhs-infopages"
@@ -94,12 +95,13 @@ class FHSInfoPagesRule(TarballRule):
for i in tar.getmembers():
if not i.isfile():
continue
+ if i.name.startswith('usr/share/info'):
+ continue
if i.name.startswith('usr/info'):
self.errors.append(("non-fhs-info-page %s", i.name))
- elif not i.name.startswith('usr/share/info'):
- for part in i.name.split(os.sep):
- if part == "info":
- self.warnings.append(("potential-non-fhs-info-page %s", i.name))
+ continue
+ if "info" in i.name.split(os.sep):
+ self.warnings.append(("potential-non-fhs-info-page %s", i.name))
class RubyPathsRule(TarballRule):
name = "rubypaths"
diff --git a/Namcap/rules/filenames.py b/Namcap/rules/filenames.py
index d7cba48..c5f5f0e 100644
--- a/Namcap/rules/filenames.py
+++ b/Namcap/rules/filenames.py
@@ -31,9 +31,7 @@ class package(TarballRule):
description = "Checks for invalid filenames."
def analyze(self, pkginfo, tar):
for i in tar.getnames():
- for c in i:
- if c not in VALID_CHARS:
- self.warnings.append(("invalid-filename", i))
- break
+ if not all(c in VALID_CHARS for c in i):
+ self.warnings.append(("invalid-filename", i))
# vim: set ts=4 sw=4 noet:
diff --git a/Namcap/rules/javafiles.py b/Namcap/rules/javafiles.py
index 080174f..40ff856 100644
--- a/Namcap/rules/javafiles.py
+++ b/Namcap/rules/javafiles.py
@@ -20,6 +20,7 @@
import os
from Namcap.ruleclass import *
+from Namcap.util import is_java
class JavaFiles(TarballRule):
name = "javafiles"
@@ -38,11 +39,11 @@ class JavaFiles(TarballRule):
continue
# is it a CLASS file ?
f = tar.extractfile(entry)
- if f.read(4) == b"\xCA\xFE\xBA\xBE":
+ if is_java(f):
javas.append(entry.name)
#self.infos.append( ('java-class-file-found %s', entry.name) )
f.close()
- if len(javas) > 0:
+ if javas:
reasons = pkginfo.detected_deps.setdefault('java-runtime', [])
reasons.append( ('java-runtime-needed %s', ', '.join(javas)) )
diff --git a/Namcap/rules/licensepkg.py b/Namcap/rules/licensepkg.py
index e4b5e9b..026caf4 100644
--- a/Namcap/rules/licensepkg.py
+++ b/Namcap/rules/licensepkg.py
@@ -26,22 +26,22 @@ class package(TarballRule):
def analyze(self, pkginfo, tar):
if 'license' not in pkginfo or len(pkginfo["license"]) == 0:
self.errors.append(("missing-license", ()))
- else:
- licensepaths = [x for x in tar.getnames() if x.startswith('usr/share/licenses') and not x.endswith('/')]
- licensedirs = [os.path.split(os.path.split(x)[0])[1] for x in licensepaths]
- licensefiles = [os.path.split(x)[1] for x in licensepaths]
- # Check all licenses for validity
- for license in pkginfo["license"]:
- lowerlicense, _, sublicense = license.lower().partition(':')
- if lowerlicense.startswith('custom') or lowerlicense in ("bsd", "mit", "isc", "python", "zlib", "libpng"):
- if pkginfo["name"] not in licensedirs:
- self.errors.append(("missing-custom-license-dir usr/share/licenses/%s", pkginfo["name"]))
- elif len(licensefiles) == 0:
- self.errors.append(("missing-custom-license-file usr/share/licenses/%s/*", pkginfo["name"]))
- # A common license
- else:
- commonlicenses = [x.lower() for x in os.listdir('/usr/share/licenses/common')]
- if lowerlicense not in commonlicenses:
- self.errors.append(("not-a-common-license %s", license))
+ return
+ licensepaths = [x for x in tar.getnames() if x.startswith('usr/share/licenses') and not x.endswith('/')]
+ licensedirs = [os.path.split(os.path.split(x)[0])[1] for x in licensepaths]
+ licensefiles = [os.path.split(x)[1] for x in licensepaths]
+ # Check all licenses for validity
+ for license in pkginfo["license"]:
+ lowerlicense, _, sublicense = license.lower().partition(':')
+ if lowerlicense.startswith('custom') or lowerlicense in ("bsd", "mit", "isc", "python", "zlib", "libpng"):
+ if pkginfo["name"] not in licensedirs:
+ self.errors.append(("missing-custom-license-dir usr/share/licenses/%s", pkginfo["name"]))
+ elif len(licensefiles) == 0:
+ self.errors.append(("missing-custom-license-file usr/share/licenses/%s/*", pkginfo["name"]))
+ # A common license
+ else:
+ commonlicenses = [x.lower() for x in os.listdir('/usr/share/licenses/common')]
+ if lowerlicense not in commonlicenses:
+ self.errors.append(("not-a-common-license %s", license))
# vim: set ts=4 sw=4 noet:
diff --git a/Namcap/rules/missingvars.py b/Namcap/rules/missingvars.py
index 440f883..2b8811c 100644
--- a/Namcap/rules/missingvars.py
+++ b/Namcap/rules/missingvars.py
@@ -65,9 +65,9 @@ class TagsRule(PkgbuildRule):
maintainertag = 0
idtag = 0
for i in pkginfo.pkgbuild:
- if re.match("#\s*Contributor\s*:", i) != None:
+ if re.match("#\s*Contributor\s*:", i):
contributortag = 1
- if re.match("#\s*Maintainer\s*:", i) != None:
+ if re.match("#\s*Maintainer\s*:", i):
maintainertag = 1
if contributortag != 1:
diff --git a/Namcap/rules/pathdepends.py b/Namcap/rules/pathdepends.py
index e50a6f6..5bc4313 100644
--- a/Namcap/rules/pathdepends.py
+++ b/Namcap/rules/pathdepends.py
@@ -24,7 +24,7 @@ If a certain path is detected then a certain dependency is expected.
Anything fancier than this should get its own rule.
"""
-import os, re
+import re
from Namcap.ruleclass import *
class PathDependsRule(TarballRule):
diff --git a/Namcap/rules/perllocal.py b/Namcap/rules/perllocal.py
index ac622b4..24923d9 100644
--- a/Namcap/rules/perllocal.py
+++ b/Namcap/rules/perllocal.py
@@ -23,9 +23,8 @@ class package(TarballRule):
name = "perllocal"
description = "Verifies the absence of perllocal.pod."
def analyze(self, pkginfo, tar):
- j = 'perllocal.pod'
for i in tar.getnames():
- if i[-len(j):] == j:
+ if i.endswith('perllocal.pod'):
self.errors.append(("perllocal-pod-present %s", i))
# vim: set ts=4 sw=4 noet:
diff --git a/Namcap/rules/scrollkeeper.py b/Namcap/rules/scrollkeeper.py
index 1c09e4e..8c813b8 100644
--- a/Namcap/rules/scrollkeeper.py
+++ b/Namcap/rules/scrollkeeper.py
@@ -26,8 +26,7 @@ class package(TarballRule):
def analyze(self, pkginfo, tar):
scroll = re.compile("var.*/scrollkeeper/?$")
for i in tar.getnames():
- n = scroll.search(i)
- if n != None:
+ if scroll.search(i):
self.errors.append(("scrollkeeper-dir-exists %s", i))
# vim: set ts=4 sw=4 noet:
diff --git a/Namcap/rules/sfurl.py b/Namcap/rules/sfurl.py
index 7f4dcc6..f3b6874 100644
--- a/Namcap/rules/sfurl.py
+++ b/Namcap/rules/sfurl.py
@@ -26,9 +26,9 @@ class package(PkgbuildRule):
def analyze(self, pkginfo, tar):
if 'source' in pkginfo:
for source in pkginfo["source"]:
- if re.match('(http://|ftp://)\w+.dl.(sourceforge|sf).net', source) != None:
+ if re.match('(http://|ftp://)\w+.dl.(sourceforge|sf).net', source):
self.warnings.append(("specific-sourceforge-mirror", ()))
- if re.match('(http://|ftp://)dl.(sourceforge|sf).net', source) != None:
+ if re.match('(http://|ftp://)dl.(sourceforge|sf).net', source):
self.warnings.append(("using-dl-sourceforge", ()))
# vim: set ts=4 sw=4 noet:
diff --git a/Namcap/rules/shebangdepends.py b/Namcap/rules/shebangdepends.py
index 088d888..7d83ff4 100644
--- a/Namcap/rules/shebangdepends.py
+++ b/Namcap/rules/shebangdepends.py
@@ -21,11 +21,8 @@
"""Checks dependencies on programs specified in shebangs."""
-import re
import os
-import tempfile
import shutil
-import pyalpm
import Namcap.package
from Namcap.util import is_script, script_type
from Namcap.ruleclass import *
@@ -41,18 +38,11 @@ def scanshebangs(fileobj, filename, scripts):
# test magic bytes
if not is_script(fileobj):
return
- # read the rest of file
- tmp = tempfile.NamedTemporaryFile(delete=False)
- tmp.write(fileobj.read())
- tmp.close()
-
- try:
- cmd = script_type(tmp.name)
- if cmd != None:
- assert(isinstance(cmd, str))
- scripts.setdefault(cmd, set()).add(filename)
- finally:
- os.unlink(tmp.name)
+ # process shebang line
+ cmd = script_type(fileobj)
+ if cmd != None:
+ assert(isinstance(cmd, str))
+ scripts.setdefault(cmd, set()).add(filename)
def findowners(scriptlist):
"""
diff --git a/Namcap/rules/unusedsodepends.py b/Namcap/rules/unusedsodepends.py
index fe2045c..abfb3ba 100644
--- a/Namcap/rules/unusedsodepends.py
+++ b/Namcap/rules/unusedsodepends.py
@@ -54,9 +54,10 @@ class package(TarballRule):
continue
# is it an ELF file ?
- if not is_elf(tar.extractfile(entry)):
- continue # not an ELF file
f = tar.extractfile(entry)
+ if not is_elf(f):
+ f.close()
+ continue
elf = f.read()
f.close()
diff --git a/Namcap/util.py b/Namcap/util.py
index fe7f280..f8d38dd 100644
--- a/Namcap/util.py
+++ b/Namcap/util.py
@@ -19,32 +19,6 @@
import os
import re
-import stat
-
-def _read_carefully(path, readcall):
- if not os.path.isfile(path):
- return False
- reset_perms = False
- if not os.access(path, os.R_OK):
- # don't mess with links we can't read
- if os.path.islink(path):
- return None
- reset_perms = True
- # attempt to make it readable if possible
- statinfo = os.stat(path)
- newmode = statinfo.st_mode | stat.S_IRUSR
- try:
- os.chmod(path, newmode)
- except IOError:
- return None
- fd = open(path, 'rb')
- val = readcall(fd)
- fd.close()
- # reset permissions if necessary
- if reset_perms:
- # set file back to original permissions
- os.chmod(path, statinfo.st_mode)
- return val
def _file_has_magic(fileobj, magic_bytes):
length = len(magic_bytes)
@@ -64,8 +38,13 @@ def is_script(fileobj):
"Take file object, peek at the magic bytes to check if script."
return _file_has_magic(fileobj, b"#!")
-def script_type(path):
- firstline = _read_carefully(path, lambda fd: fd.readline())
+def is_java(fileobj):
+ "Take file object, peek at the magic bytes to check if class file."
+ return _file_has_magic(fileobj, b"\xCA\xFE\xBA\xBE")
+
+def script_type(fileobj):
+ firstline = fileobj.readline()
+ fileobj.seek(0)
try:
firstline = firstline.decode('utf-8', 'strict')
except UnicodeDecodeError: