def test_get_imports_info(self): imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain only 4 Elements without the "nonexistendmodule" self.assertEqual(len(with_info),4, "Length of imports array with info is wrong") for item in with_info: self.assertTrue(item['name'] in self.modules, "Import item appears to be missing")
def requirements_declared(project_path: str) -> Union[float, str]: """ Calculates percentage of not declared dependencies. """ _declared_requirements = _get_requirements_from_file(path=project_path) _setup_requirements = _get_requirements_from_setup(path=project_path) try: _implied_dependencies = pipreqs.get_pkg_names( pipreqs.get_all_imports( path=project_path, encoding='ISO-8859-1', )) except (IndentationError, SyntaxError, ValueError): return None # We cannot calculate a percentage for declared dependencies, # if there are no dependencies through imports. if not _implied_dependencies: return None if (not _declared_requirements) and (_setup_requirements is None): return "Error" if _setup_requirements is None: # If this is None, # there was a problem parsing the setup file. # This is handled in the if statement above. # If we still calculate further, this needs # to be iterable since we make an "in" comparison with it. _setup_requirements = [] _correctly_declared_requirements_count = len(_implied_dependencies) for requirement in _implied_dependencies: if requirement not in _declared_requirements and requirement not in _setup_requirements: _correctly_declared_requirements_count = _correctly_declared_requirements_count - 1 return float(_correctly_declared_requirements_count) / len( _implied_dependencies)
def _set_imports(self): """Set required imports for the python script at self.script_path.""" # Get the names of packages imported in the script import_names = pipreqs.get_all_imports( os.path.dirname(self.script_path)) # Of those names, store the ones that are available via pip self._pip_imports = pipreqs.get_imports_info(import_names) if not self._pin_pip_versions: self._pip_imports = [{ "name": item["name"], "version": None } for item in self._pip_imports] # If some imports were left out, store their names pip_names = set([i["name"] for i in self.pip_imports]) self._missing_imports = list(set(import_names) - pip_names) if len(import_names) != (len(self.pip_imports) + len(self.github_installs)): # And warn the user mod_logger.warning( "Warning, some imports not found by pipreqs. You will " "need to edit the Dockerfile by hand, e.g by installing " "from github. You need to install the following packages " "{missing!s}".format(missing=self.missing_imports))
def _get_implied_dependencies(path: str) -> list: """ Attempt to replace _get_requirements_from_file Extracts import statements via regex. Does not catch all import statements and its use was rolled back. Might still be overhauled and integrated again. """ _python_files = search_filename(base_folder=path, file_name="**/*.py", recursive_flag=True) _tmp_project_path = tempfile.mkdtemp() _tmp_file_path = _tmp_project_path + "/dependencies.py" _tmp_file = open(_tmp_file_path, 'w') for file in _python_files: for _import in _get_imports(file): _tmp_file.write(_import.strip() + '\n') _tmp_file.close() try: _all_imports = pipreqs.get_all_imports(path=_tmp_project_path, encoding='utf-8') except (IndentationError, SyntaxError): return None # Clean up tmp folder if os.path.isfile(_tmp_file_path): os.remove(_tmp_file_path) if os.path.isdir(_tmp_project_path): os.rmdir(_tmp_project_path) _imports = _remove_local_dependencies(path, _all_imports) return pipreqs.get_pkg_names(_imports)
def main(path, extra_ignore_dirs): # Get all potential imports. candidates = pipreqs.get_all_imports(path, extra_ignore_dirs=extra_ignore_dirs, ignore_errors=True) if len(candidates) == 0: print(json.dumps([])) return # Get imports that can be found locally. # Note: although the pipreqs package gets the mapping first it seems that # `get_import_local` requires the names without the mapping. Must be a bug. local = pipreqs.get_import_local(candidates) # Now get the mapping (e.g. bs4 -> beatifulsoup4) # Start with our own custom mapping candidates = [ mapping[name.lower()] if name.lower() in mapping else name for name in candidates ] # pipreqs mapping candidates = pipreqs.get_pkg_names(candidates) # Compute the diff difference = [ x for x in candidates if x.lower() not in [z['name'].lower() for z in local] ] # To get the version we need to call pypi but that's too risky to do # on every call so, for now, we go with the latest version. # See https://goo.gl/3nJLMq print(json.dumps([{'version': '*', 'name': name} for name in difference]))
def __init__(self, tor=False, info={}, driver=False, log = {}, errorlog={}): self.path = os.path.dirname(os.path.realpath(__file__)) #le chemin du dossier où est exécuté le fichier self.info = info self.tor = tor self.log = log self.errorlog = errorlog self.programm = { 'software': "Python", 'software_version': platform.python_version(), 'modules': pipreqs.get_imports_info(pipreqs.get_all_imports(self.path, encoding='utf-8')) } self.system = { 'os_name': platform.system(), 'computer_name': platform.node(), 'os_release': platform.release(), 'os_version': platform.version(), 'processor': platform.processor() } self.ip = '' if driver: try: driver.get('http://httpbin.org/ip') content = driver.find_element_by_xpath("//body").text self.ip = json.loads(content)['origin'] except: print('Documentation - erreur à retrouver l\'adresse IP publique.') else: if self.tor: proxies = {'http': 'socks5h://127.0.0.1:9050', 'https': 'socks5h://127.0.0.1:9050'} else: proxies = {} try: self.ip = requests.get('http://httpbin.org/ip', proxies=proxies).json()['origin'] #utilisation du service ipinfo.io except: print('Documentation - erreur à retrouver l\'adresse IP publique.')
def test_get_imports_info(self): imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain only 5 Elements without the "nonexistendmodule" self.assertEqual(len(with_info), 10) for item in with_info: self.assertTrue(item['name'].lower( ) in self.modules, "Import item appears to be missing " + item['name'])
def test_get_imports_info(self): imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain only 4 Elements without the "nonexistendmodule" self.assertEqual(len(with_info), 4, "Length of imports array with info is wrong") for item in with_info: self.assertTrue(item['name'] in self.modules, "Import item appears to be missing")
def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) self.assertEqual(len(imports),5, "Incorrect Imports array length") for item in imports: self.assertTrue(item in self.modules, "Import is missing") self.assertFalse("time" in imports) self.assertFalse("logging" in imports) self.assertFalse("curses" in imports) self.assertFalse("__future__" in imports)
def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) self.assertEqual(len(imports), 5, "Incorrect Imports array length") for item in imports: self.assertTrue(item in self.modules, "Import is missing") self.assertFalse("time" in imports) self.assertFalse("logging" in imports) self.assertFalse("curses" in imports) self.assertFalse("__future__" in imports)
def setUp(self): """ Create a copy of this package to use it as test data. """ _this_package = pkg_resources.resource_filename('recoda', '') _this_package = pkg_resources.resource_filename('recoda', '') self.test_sandbox = tempfile.mkdtemp() self.import_list = pipreqs.get_pkg_names( pipreqs.get_all_imports(path=_this_package) ) # The above call to pipreqs always misses GitPython copytree(_this_package, self.test_sandbox+'/recoda')
def test_get_imports_info(self): """ Test to see that the right number of packages were found on PyPI """ imports = pipreqs.get_all_imports(self.project) with_info = pipreqs.get_imports_info(imports) # Should contain 10 items without the "nonexistendmodule" and "after_method_is_valid_even_if_not_pep8" self.assertEqual(len(with_info), 11) for item in with_info: self.assertTrue( item['name'].lower() in self.modules, "Import item appears to be missing " + item['name'])
def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) self.assertEqual(len(imports), 13) for item in imports: self.assertTrue( item.lower() in self.modules, "Import is missing: " + item) self.assertFalse("time" in imports) self.assertFalse("logging" in imports) self.assertFalse("curses" in imports) self.assertFalse("__future__" in imports) self.assertFalse("django" in imports) self.assertFalse("models" in imports)
def test_get_all_imports(self): imports = pipreqs.get_all_imports(self.project) self.assertEqual(len(imports), 15) for item in imports: self.assertTrue( item.lower() in self.modules, "Import is missing: " + item) self.assertFalse("time" in imports) self.assertFalse("logging" in imports) self.assertFalse("curses" in imports) self.assertFalse("__future__" in imports) self.assertFalse("django" in imports) self.assertFalse("models" in imports)
def cli(): '''Cli entry point ''' parser = argparse.ArgumentParser( description= "Use this module to get the sizes of used imports in a project") parser.add_argument("path", action="store", help="path of program") args = parser.parse_args() candidates = get_all_imports(args.path, encoding="utf8", extra_ignore_dirs=False, follow_links=True) candidates = get_pkg_names(candidates) for candidate in candidates: print("{0:16}".format(candidate) + ": " + getModuleSize(candidate))
def _get_packages(self, path): path = path.replace(".", "/") try: return set( filter( lambda p: p != self.dist.name and p not in self.IGNORE, map( lambda p: cfg.package_aliases.get(p, p), map( str.lower, pipreqs.get_pkg_names( pipreqs.get_all_imports(path)), )))) except Exception as exc: self._warn_exc(exc) return set()
def collect_reqs_specific(config, prompt=False, cwd='.'): if prompt: _prompt_and_clean(cwd) candidates = pipreqs.get_all_imports( str(cwd), extra_ignore_dirs=config.pipreqs_ignore) candidates = pipreqs.get_pkg_names(candidates) local = pipreqs.get_import_local(candidates) difference = [ x for x in candidates if x.lower() not in [z['name'].lower() for z in local] ] imports = local + pipreqs.get_imports_info(difference) reqs = [ f"{item['name']}=={item['version']}" for item in imports if 'INFO' not in item ] return reqs
def get_dependencies(cls, path: Optional[str] = None) -> List[InstalledPackage]: all_imports = pipreqs.get_all_imports(path or '.') pkg_names = pipreqs.get_pkg_names(all_imports) public_pkg_names = [ p['name'] for p in pipreqs.get_imports_info(pkg_names) ] installed_packages = [ InstalledPackage.from_name(name, private=name not in public_pkg_names) for name in pkg_names ] return [ installed_package for installed_package in installed_packages if installed_package ]
def _set_imports(self): """Set required imports for the python script at self.script_path""" # Get the names of packages imported in the script import_names = pipreqs.get_all_imports( os.path.dirname(self.script_path)) # Of those names, store that ones that are available via pip self._pip_imports = pipreqs.get_imports_info(import_names) # If some imports were left out, store their names pip_names = set([i['name'] for i in self.pip_imports]) self._missing_imports = list(set(import_names) - pip_names) if len(import_names) != (len(self.pip_imports) + len(self.github_installs)): # And warn the user mod_logger.warning( 'Warning, some imports not found by pipreqs. You will ' 'need to edit the Dockerfile by hand, e.g by installing ' 'from github. You need to install the following packages ' '{missing!s}'.format(missing=self.missing_imports))
def test_deduplicate_dependencies(self): imports = pipreqs.get_all_imports(self.project_with_duplicated_deps) pkgs = pipreqs.get_pkg_names(imports) self.assertEqual(len(pkgs), 1) self.assertTrue("pymongo" in pkgs)
def get_import_list(path): candidates = pipreqs.get_all_imports(path) candidates = pipreqs.get_pkg_names(candidates) imports = pipreqs.get_import_local(candidates) return imports
from setuptools import setup, find_packages from pipreqs.pipreqs import get_all_imports, get_pkg_names, get_import_local reqs = get_import_local(get_pkg_names(get_all_imports('./', encoding='utf-8')), encoding='utf-8') reqs = list(map(lambda req: '{}>={}'.format(req['name'], req['version']), reqs)) with open('requirements.txt', mode='w', encoding='utf-8') as f: f.write('\n'.join(reqs)) setup( name='py_bootstrap', version='0.0.15', description=('python端启动器'), long_description_content_type="text/markdown", long_description=open('README.md', encoding='utf-8').read(), author='zouwendi', author_email='*****@*****.**', maintainer='zouwendi', maintainer_email='*****@*****.**', license='GPL3 License', packages=find_packages(), platforms=["all"], url='https://github.com/MyCupOfTeaOo/py_bootstrap', install_requires=reqs, classifiers=[ 'Development Status :: 4 - Beta', 'Operating System :: OS Independent', 'Intended Audience :: Developers', 'Programming Language :: Python', 'Programming Language :: Python :: Implementation', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.4',