예제 #1
0
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)
예제 #2
0
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]))
예제 #3
0
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)
예제 #4
0
    def get_imports(self):
        imports = []
        for name, val in self.context.items():
            if isinstance(val, types.ModuleType):
                imports.append(val.__name__.split(".")[0])
            elif isinstance(val, type):
                imports.append(val.__module__.split(".")[0])

        return pi.get_pkg_names(list(set(imports)))
예제 #5
0
 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')
예제 #6
0
    def get_formatted_imports(self):
        candidates = pi.get_pkg_names(self.get_imports())

        pypi_server = "https://pypi.python.org/pypi/"
        proxy = None
        local = pi.get_import_local(candidates)
        # Get packages that were not found locally
        difference = [x for x in candidates
                      if x.lower() not in [z['name'].lower() for z in local]]
        imports = local + pi.get_imports_info(difference,
                                           proxy=proxy,
                                           pypi_server=pypi_server)
        return imports
예제 #7
0
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))
예제 #8
0
 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()
예제 #9
0
    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
        ]
예제 #10
0
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
예제 #11
0
 def test_get_pkg_names(self):
     pkgs = ['jury', 'Japan', 'camel', 'Caroline']
     actual_output = pipreqs.get_pkg_names(pkgs)
     expected_output = ['camel', 'Caroline', 'Japan', 'jury']
     self.assertEqual(actual_output, expected_output)
예제 #12
0
 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)
예제 #13
0
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',
예제 #14
0
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
예제 #15
0
 def test_get_pkg_names(self):
     pkgs = ['jury', 'Japan', 'camel', 'Caroline']
     actual_output = pipreqs.get_pkg_names(pkgs)
     expected_output = ['camel', 'Caroline', 'Japan', 'jury']
     self.assertEqual(actual_output, expected_output)
예제 #16
0
 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)