Fix flake8

This commit is contained in:
lord63 2015-07-14 19:56:14 +08:00
parent 9671b53ff3
commit d59c9a1e84
3 changed files with 28 additions and 20 deletions

View File

@ -6,7 +6,7 @@ Usage:
pipreqs [options] <path> pipreqs [options] <path>
Options: Options:
--use-local Use ONLY local package information instead of querying PyPI --use-local Use ONLY local package info instead of querying PyPI
--debug Print debug information --debug Print debug information
--savepath <file> Save the list of requirements in the given file --savepath <file> Save the list of requirements in the given file
--force Overwrite existing requirements.txt --force Overwrite existing requirements.txt
@ -101,7 +101,8 @@ def get_locally_installed_packages():
except: except:
continue continue
for i_item in package_import: for i_item in package_import:
if i_item not in ignore and package[0] not in ignore: if ((i_item not in ignore) and
(package[0] not in ignore)):
packages[i_item] = { packages[i_item] = {
'version': package[1].replace(".dist", ""), 'version': package[1].replace(".dist", ""),
'name': package[0] 'name': package[0]
@ -157,14 +158,16 @@ def init(args):
logging.debug("Getting packages information from Local/PyPI") logging.debug("Getting packages information from Local/PyPI")
local = get_import_local(candidates) local = get_import_local(candidates)
# Get packages that were not found locally # Get packages that were not found locally
difference = [x for x in candidates if x.lower() not in [z['name'].lower() difference = [x for x in candidates
for z in local]] if x.lower() not in [z['name'].lower() for z in local]]
imports = local + get_imports_info(difference) imports = local + get_imports_info(difference)
path = args["--savepath"] if args["--savepath"] else os.path.join(args['<path>'], "requirements.txt") path = (args["--savepath"] if args["--savepath"] else
os.path.join(args['<path>'], "requirements.txt"))
if not args["--savepath"] and not args["--force"] and os.path.exists(path): if not args["--savepath"] and not args["--force"] and os.path.exists(path):
logging.info("Requirements.txt already exists, use --force to overwrite it") logging.info("Requirements.txt already exists, "
"use --force to overwrite it")
return return
generate_requirements_file(path, imports) generate_requirements_file(path, imports)
logging.info("Successfully saved requirements file in " + path) logging.info("Successfully saved requirements file in " + path)

View File

@ -7,8 +7,8 @@ from sys import argv as test2 # [unused-import]
from sys import flags # [unused-import] from sys import flags # [unused-import]
# +1:[unused-import,unused-import] # +1:[unused-import,unused-import]
from collections import deque, OrderedDict, Counter from collections import deque, OrderedDict, Counter
# All imports above should be ignored # All imports above should be ignored
import requests # [unused-import] import requests # [unused-import]
# setuptools # setuptools
import zipimport # command/easy_install.py import zipimport # command/easy_install.py
@ -24,8 +24,9 @@ import importlib # html/notebookapp.py
from IPython.utils.importstring import import_item # Many files from IPython.utils.importstring import import_item # Many files
# pyflakes # pyflakes
from pyflakes.test.test_imports import Test as TestImports # test/test_doctests.py # test/test_doctests.py
from pyflakes.test.test_imports import Test as TestImports
# Nose # Nose
from nose.importer import Importer, add_path, remove_path # loader.py from nose.importer import Importer, add_path, remove_path # loader.py
@ -44,7 +45,7 @@ import bs4
import nonexistendmodule import nonexistendmodule
import boto as b, import peewee as p, import boto as b, import peewee as p,
# import django # import django
import flask.ext.somext # # # import flask.ext.somext # # #
from sqlalchemy import model from sqlalchemy import model
try: try:
import ujson as json import ujson as json
@ -53,7 +54,8 @@ except ImportError:
import models import models
def main(): def main():
pass pass
import after_method_should_be_ignored import after_method_should_be_ignored

View File

@ -18,7 +18,8 @@ class TestPipreqs(unittest.TestCase):
def setUp(self): def setUp(self):
self.modules = ['flask', 'requests', 'sqlalchemy', self.modules = ['flask', 'requests', 'sqlalchemy',
'docopt', 'boto', 'ipython', 'pyflakes', 'nose', 'peewee', 'ujson', 'nonexistendmodule', 'bs4', ] 'docopt', 'boto', 'ipython', 'pyflakes', 'nose',
'peewee', 'ujson', 'nonexistendmodule', 'bs4', ]
self.modules2 = ['beautifulsoup4'] self.modules2 = ['beautifulsoup4']
self.local = ["docopt", "requests", "nose"] self.local = ["docopt", "requests", "nose"]
self.project = os.path.join(os.path.dirname(__file__), "_data") self.project = os.path.join(os.path.dirname(__file__), "_data")
@ -45,8 +46,9 @@ class TestPipreqs(unittest.TestCase):
# Should contain only 5 Elements without the "nonexistendmodule" # Should contain only 5 Elements without the "nonexistendmodule"
self.assertEqual(len(with_info), 10) self.assertEqual(len(with_info), 10)
for item in with_info: for item in with_info:
self.assertTrue(item['name'].lower( self.assertTrue(
) in self.modules, "Import item appears to be missing " + item['name']) item['name'].lower() in self.modules,
"Import item appears to be missing " + item['name'])
def test_get_use_local_only(self): def test_get_use_local_only(self):
# should find only docopt and requests # should find only docopt and requests
@ -55,8 +57,8 @@ class TestPipreqs(unittest.TestCase):
self.assertTrue(item['name'].lower() in self.local) self.assertTrue(item['name'].lower() in self.local)
def test_init(self): def test_init(self):
pipreqs.init( pipreqs.init({'<path>': self.project, '--savepath': None,
{'<path>': self.project, '--savepath': None, '--use-local': None, '--force': True}) '--use-local': None, '--force': True})
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()
@ -64,8 +66,8 @@ class TestPipreqs(unittest.TestCase):
self.assertTrue(item.lower() in data) self.assertTrue(item.lower() in data)
def test_init_local_only(self): def test_init_local_only(self):
pipreqs.init( pipreqs.init({'<path>': self.project, '--savepath': None,
{'<path>': self.project, '--savepath': None, '--use-local': True, '--force': True}) '--use-local': True, '--force': True})
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.readlines() data = f.readlines()
@ -87,7 +89,8 @@ class TestPipreqs(unittest.TestCase):
def test_init_overwrite(self): def test_init_overwrite(self):
with open(self.requirements_path, "w") as f: with open(self.requirements_path, "w") as f:
f.write("should_not_be_overwritten") f.write("should_not_be_overwritten")
pipreqs.init({'<path>': self.project, '--savepath': None, '--use-local': None, '--force': None}) pipreqs.init({'<path>': self.project, '--savepath': None,
'--use-local': None, '--force': None})
assert os.path.exists(self.requirements_path) == 1 assert os.path.exists(self.requirements_path) == 1
with open(self.requirements_path, "r") as f: with open(self.requirements_path, "r") as f:
data = f.read().lower() data = f.read().lower()