Exemple #1
0
def getImports(path):
    sys.path.insert(0, os.path.dirname(path))
    sizes = []
    imports = []
    sizes.append(os.stat(path).st_size)
    imp = []
    obj_loaded = {}
    for im in findimports.find_imports(path):
        imp.append(im)
    imports.extend(imp)
    mdls = list(
        filter(lambda x: x.name.split('.')[0] not in modulelist, imports))
    mdls = set(map(lambda x: x.name.split('.')[0], mdls))
    stdlibs = list(
        filter(lambda x: x.name.split('.')[0] in modulelist, imports))
    stdlibs = set(map(lambda x: x.name.split('.')[0], stdlibs))
    sub = set()
    for n in mdls:
        try:
            m = importlib.import_module(n)
            obj_loaded[n] = m
            #print(sys.getsizeof(m)) ??? 80 all
            if "site-packages" in str(m.__file__):
                stdlibs.add(n)
                sub.add(n)
        except Exception as e:
            print(e, 'here!')

    mdls = mdls - sub
    #print(m_with_addr)
    # getSizeMetrics(mdls_w_addr)
    return list(mdls), list(stdlibs), obj_loaded
Exemple #2
0
def libraries_from_imports(code_py, mod_names):
    """
    Parse the given code.py file and return the imported libraries

    :param str code_py: Full path of the code.py file
    :return: sequence of library names
    """
    imports = [
        info.name.split(".", 1)[0]
        for info in findimports.find_imports(code_py)
    ]
    return [r for r in imports if r in mod_names]
def get_libs_for_example(example_path):
    """Get the set of libraries for a library example"""
    found_libs = set()
    found_imports = []
    found_imports = findimports.find_imports(example_path)

    for cur_import in found_imports:
        cur_lib = cur_import.name.split(".")[0]
        if cur_lib in bundle_data:
            found_libs.add(cur_lib)

    return found_libs
Exemple #4
0
def test_mystery_package():
    package_imports: typing.List[
        findimports.ImportInfo] = findimports.find_imports(
            mystery.__mystery_init_py__)
    for package_import in package_imports:
        if package_import.name in ALLOWED_IMPORTS:
            continue
        try:
            importlib.import_module(package_import.name)
        except ModuleNotFoundError:
            # Package listed (PyPI) name is different than it's actual package name (the one you import).
            assert package_import.name == mystery.__mystery_package_name__
        else:
            # No exception - package is importable and mystery should have worked.
            assert package_import.name == mystery.__name__
    def __get_dependencies(self):
        """
        Retrieves all dependencies from the projects located at `path`
        :return: list of dependencies
        """
        all_symnames = set()
        for root, dirs, files in os.walk(self.path):
            for file in files:
                # only analyze python files
                if file.endswith(".py"):
                    file_path = os.path.join(root, file)
                    all_symnames.update(
                        x.name.split('.')[0] for x in find_imports(file_path))

        return all_symnames
def get_libs_for_project(project_name):
    """Get the set of libraries for a learn project"""
    found_libs = set()
    found_imports = []
    project_dir = "{}{}/".format(LEARN_GUIDE_REPO, project_name)
    for file in os.listdir(project_dir):
        if file.endswith(".py"):

            found_imports = findimports.find_imports("{}{}".format(project_dir, file))

            for cur_import in found_imports:
                cur_lib = cur_import.name.split(".")[0]
                if cur_lib in bundle_data:
                    found_libs.add(cur_lib)

    return found_libs
Exemple #7
0
def test_mystery_package():
    package_imports: typing.List[findimports.ImportInfo] = findimports.find_imports(
        mystery.__mystery_init_py__
    )
    print(f'mystery_file: {mystery.__file__}')
    print(f'mystery_name: {mystery.__name__}')
    print(f'mystery_package_name: {mystery.__mystery_package_name__}')
    for package_import in package_imports:
        if package_import.name in ALLOWED_IMPORTS:
            continue
        print(f'package_import_name: {package_import.name}')
        try:
            _ = importlib.import_module(package_import.name)
        except ModuleNotFoundError:
            print('MODULE_NOT_FOUND!!')
            assert package_import.name == mystery.__mystery_package_name__
        else:
            assert package_import.name == mystery.__name__
Exemple #8
0
    def __detect_installed_dependencies(self):
        """
        Gets the dependencies with versions that are used and installed
        in the path.
        """
        all_symnames = set()
        for root, dirs, files in os.walk(self.path):
            for file in files:
                # only analyze python files
                if file.endswith(".py"):
                    file_path = os.path.join(root, file)
                    all_symnames.update(x.name.split('.')[0] for x in find_imports(file_path))

        packages = pip.utils.get_installed_distributions()

        for package in packages:
            for dependency in all_symnames:
                if package.project_name == dependency:
                    self.installed_dependencies.append(Dependency(
                        package.project_name,
                        package.version,
                        []
                    ))