Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Make pep8 and flake8 happy
  • Loading branch information
Scott Sweeny committed Dec 12, 2016
1 parent c2aa6a6 commit 32d0d61
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
24 changes: 14 additions & 10 deletions bin/snaplint
Expand Up @@ -23,11 +23,13 @@ import os
import sys
import textwrap


def _snap_path(path):
if os.path.exists(path):
if os.path.exists(os.path.join(path, 'snapcraft.yaml')):
# a valid snap directory should contain meta/snap.yaml
if os.path.exists(os.path.join(path, 'prime', 'meta', 'snap.yaml')):
# a valid snap directory should contain meta/snap.yaml
if os.path.exists(os.path.join(path, 'prime', 'meta',
'snap.yaml')):
return os.path.join(path, 'prime')
else:
print("Please run 'snapcraft prime' in your project to"
Expand All @@ -39,9 +41,11 @@ def _snap_path(path):
print("%s does not exist" % path)
return None


def _parse_args():
parser = argparse.ArgumentParser(description='Clean up your snap',
formatter_class=argparse.RawTextHelpFormatter)
parser = argparse.ArgumentParser(
description='Clean up your snap',
formatter_class=argparse.RawTextHelpFormatter)

parser.add_argument('project',
help=textwrap.dedent('''\
Expand All @@ -51,6 +55,7 @@ def _parse_args():
args = parser.parse_args()
return args


def _get_scanner(module):
for attr in vars(module).values():
if not isinstance(attr, type):
Expand All @@ -70,17 +75,18 @@ if __name__ == '__main__':

args = _parse_args()
snap = _snap_path(args.project)
if snap == None:
if snap is None:
print("Please specify a valid snapcraft directory")
sys.exit(1)

# This logic stolen shamelessly from snapcraft
import snaplint
import snaplint.rules
rules_to_run = []
for importer, modname, is_package in pkgutil.iter_modules(snaplint.rules.__path__):
for importer, modname, is_package in pkgutil.iter_modules(
snaplint.rules.__path__):
rules_to_run.append(modname)

print('Rules to run: ', rules_to_run)

print("Running scan")
Expand All @@ -106,5 +112,3 @@ if __name__ == '__main__':
sys.exit(1)
else:
sys.exit(0)


2 changes: 1 addition & 1 deletion snaplint/_rule.py
Expand Up @@ -17,6 +17,7 @@

import os


class Rule:

def __init__(self, path):
Expand All @@ -42,7 +43,6 @@ def get_dir_list(self):
self.path))
return dir_list


def scan(self):
'''Override this method to implement your rule checking logic'''
pass
6 changes: 3 additions & 3 deletions snaplint/rules/copyright.py
Expand Up @@ -19,6 +19,7 @@

from snaplint._rule import Rule


class CopyrightRule(Rule):
'''Scan for copyright files in usr/share/doc within the snap. System
Enablement Team policy requires that these files be present'''
Expand All @@ -27,12 +28,12 @@ def __init__(self, path):
super().__init__(path)

def scan(self):
'''Really dumb check that looks for at least one instance of
'''Really dumb check that looks for at least one instance of
usr/share/doc/*/*copyright*
'''
print('Scanning {} for copyright compliance...'.format(self.path),
end=' ')

pattern = self.path + 'usr/share/doc/*/*copyright*'
if not glob.glob(pattern, recursive=True):
print('FAIL')
Expand All @@ -41,4 +42,3 @@ def scan(self):

print('OK')
return True

7 changes: 4 additions & 3 deletions snaplint/rules/developer_cruft.py
Expand Up @@ -17,18 +17,19 @@

from snaplint._rule import Rule

NAUGHTY_FILES=[
NAUGHTY_FILES = [
'.o', # object files
'.a', # static libs
'.h', # header files
'.hpp',
]

NAUGHTY_DIRS=[
NAUGHTY_DIRS = [
'.git',
'.bzr',
]


class DeveloperCruft(Rule):

def __init__(self, path):
Expand All @@ -37,7 +38,7 @@ def __init__(self, path):
def scan(self):
print('Scanning {} for developer cruft...'.format(self.path), end=' ')

fail_list=[]
fail_list = []
for f in self.get_file_list():
for suffix in NAUGHTY_FILES:
if f.endswith(suffix):
Expand Down
19 changes: 10 additions & 9 deletions snaplint/rules/libs.py
Expand Up @@ -15,16 +15,13 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from snaplint._rule import Rule

import os

from elftools.common.exceptions import ELFError
from elftools.elf.elffile import ELFFile

# Make things work easier on python3
from elftools.common.py3compat import (
ifilter, byte2int, bytes2str, itervalues, str2bytes)
from snaplint._rule import Rule


def _traverse_deps(root, elves):
for lib in elves[root]['needed']:
Expand All @@ -34,6 +31,7 @@ def _traverse_deps(root, elves):
else:
continue


class LibraryRule(Rule):
'''Examine the executables in the snap and make sure that only needed
libraries are included'''
Expand All @@ -46,20 +44,23 @@ def _get_elves(self):
with open(os.path.join(self.path, filename), "rb") as fp:
try:
elf = ELFFile(fp)
except ELFError as e:
except ELFError:
# Probably not an ELF file
continue
dynamic = elf.get_section_by_name(b'.dynamic')
if dynamic is None:
continue
needed = frozenset(tag.needed for tag in dynamic.iter_tags('DT_NEEDED'))
needed = frozenset(
tag.needed for tag in dynamic.iter_tags('DT_NEEDED'))
for tag in dynamic.iter_tags('DT_SONAME'):
soname = tag.soname
elves[soname] = {'filename': filename, 'needed': needed, 'used': False}
elves[soname] = {'filename': filename,
'needed': needed,
'used': False}
break
else:
roots[filename] = needed
except Exception as error:
except Exception:
# If we can't open the file just skip to the next one
continue

Expand Down

0 comments on commit 32d0d61

Please sign in to comment.