Beispiel #1
0
def get_files_with_modules(directory_path, search_recursively=False):
    files_with_modules = []
    if search_recursively:
        original_file_paths = gfp.get_paths(directory_path)
        unique_file_paths = []
        for file_path_index, file_path in enumerate(original_file_paths):
            files_with_modules.append([
                file_path,
                os.path.dirname(file_path).rsplit(os.path.sep, 1)[1]
            ])
        finder = ModuleFinder()
        for file_path in original_file_paths:
            try:
                finder.run_script(file_path)
            except ImportError:
                print("Could not load", file_path)
            for key in finder.modules.items():
                item_path = key[1].__file__ if key[
                    1].__file__ else key[1].__name__ + " (built-in)"
                if '.' not in item_path or item_path.endswith('.py'):
                    item_data = [item_path, key[1].__name__]
                    if item_data[0] not in unique_file_paths and item_data[
                            0] not in original_file_paths:
                        unique_file_paths.append(item_data[0])
                        files_with_modules.append(item_data)
        files_with_modules.sort(key=lambda item: item[0])
    else:
        files_with_modules = gfp.get_paths(directory_path)
        for file_path_index, file_path in enumerate(files_with_modules):
            files_with_modules[file_path_index] = [
                file_path,
                os.path.dirname(file_path).rsplit(os.path.sep, 1)[1]
            ]
        files_with_modules.sort(key=lambda item: item[0])
    return files_with_modules
Beispiel #2
0
    def test_get_function_data_with_multiple_files_in_one_directory(self):
        path_dir = 'test_dir2'
        os.mkdir(path_dir)

        path1 = os.path.join(path_dir, 'test_file1.py')

        f1 = open(path1, "w+")
        f1.write("# !/usr/bin/env python\n")
        f1.write("# -*- coding: utf-8 -*-\n")
        f1.write("\n\n")
        f1.write("import os\n")
        f1.write("\n\n")
        f1.write("def testa():\n")
        f1.write("\tpass")
        f1.write("\n\n")
        f1.write("def testb():\n")
        f1.write("\ttesta()\n")
        f1.write("\n\n")

        f1.close()

        path2 = os.path.join(path_dir, 'test_file2.py')
        f2 = open(path2, "w+")
        f2.write("# !/usr/bin/env python\n")
        f2.write("# -*- coding: utf-8 -*-\n")
        f2.write("\n\n")
        f2.write("import os\n")
        f2.write("\n\n")
        f2.write("def testc():\n")
        f2.write("\tpass")
        f2.write("\n\n")
        f2.write("def testd():\n")
        f2.write("\ttestc()\n")
        f2.write("\n\n")

        f2.write("def main(argument_path=None):\n")
        f2.write("\tteste()\n")
        f2.write("\n\n")
        f2.write("if __name__ == '__main__':\n")
        f2.write("\tmain()\n\n")

        f2.close()

        function_data = gfu.get_function_data(path_dir, gfp.get_paths(path_dir))

        expected_data1 = ['test_file1.testa', 19, [None, 0, 0, 0, 0]]
        expected_data2 = ['test_file1.testb', 22, [1, None, 0, 0, 0]]
        expected_data3 = ['test_file2.main', 39, [0, 0, None, 0, 0]]
        expected_data4 = ['test_file2.testc', 19, [0, 0, 0, None, 0]]
        expected_data5 = ['test_file2.testd', 22, [0, 0, 0, 1, None]]

        self.assertEqual(function_data[0].get_data(), expected_data1)
        self.assertEqual(function_data[1].get_data(), expected_data2)
        self.assertEqual(function_data[2].get_data(), expected_data3)
        self.assertEqual(function_data[3].get_data(), expected_data4)
        self.assertEqual(function_data[4].get_data(), expected_data5)

        os.remove(path1)
        os.remove(path2)
        os.rmdir(path_dir)
Beispiel #3
0
def main(argument_path=None):
    if argument_path is None:
        root_dir = os.path.dirname(os.getcwd())
    else:
        root_dir = argument_path
    dirs = gfp.get_paths(root_dir)
    nodes = get_function_data(root_dir, dirs)
    return nodes
Beispiel #4
0
def main(argument_path=None):

    if argument_path is None:
        root_dir = os.path.dirname(os.getcwd())
    else:
        root_dir = argument_path
    print(root_dir)
    dirs = gfp.get_paths(root_dir)
    data = gfif.get_function_data(root_dir, dirs)
    print(data)
    try:
        nodes = gfif.main(root_dir)
        dg.main(nodes)
    except IOError:
        print("File with path ", root_dir, "could not be opened")
    def test_get_paths(self):
        path_dir = 'test_dir'
        os.mkdir(path_dir)

        path1 = os.path.join(path_dir, 'test_file1.py')
        path2 = os.path.join(path_dir, 'test_file2.txt')
        path3 = os.path.join(path_dir, 'test_file3.py')

        f1 = open(path1, "w+")
        f2 = open(path2, "w+")
        f3 = open(path3, "w+")

        f1.close()
        f2.close()
        f3.close()

        self.assertEqual(gfp.get_paths(path_dir), [path1, path3])
        os.remove(path1)
        os.remove(path2)
        os.remove(path3)
        os.rmdir(path_dir)
Beispiel #6
0
    def test_get_function_data_with_one_file_one_directory(self):
        path_dir = 'test_dir'
        os.mkdir(path_dir)

        path1 = os.path.join(path_dir, 'test_file1.py')

        f1 = open(path1, "w+")
        f1.write("# !/usr/bin/env python\n")
        f1.write("# -*- coding: utf-8 -*-\n")
        f1.write("\n\n")
        f1.write("import os\n")
        f1.write("\n\n")
        f1.write("def test():\n")
        f1.write("\tpass")
        f1.write("\n\n")
        f1.write("def test2():\n")
        f1.write("\ttest()")
        f1.write("\n\n")
        f1.write("def test3():\n")
        f1.write("\ttest()\n")
        f1.write("\ttest2()")
        f1.write("\n\n")

        f1.close()
        function_data = gfu.get_function_data(path_dir, gfp.get_paths(path_dir))

        expected_data1 = ['test_file1.test', 18, [None, 0, 0]]
        expected_data2 = ['test_file1.test2', 21, [1, None, 0]]
        expected_data3 = ['test_file1.test3', 30, [1, 1, None]]

        self.assertEqual(function_data[0].get_data(), expected_data1)
        self.assertEqual(function_data[1].get_data(), expected_data2)
        self.assertEqual(function_data[2].get_data(), expected_data3)

        os.remove(path1)
        os.rmdir(path_dir)
Beispiel #7
0
    for file_path in file_paths:
        if platform.system() == 'Windows':
            file_name = file_path.split('\\')[-2:]
        else:
            file_name = file_path.split('/')[-2:]

        for node in ast.walk(ast.parse(open(file_path).read())):
            if isinstance(node, ast.FunctionDef) or isinstance(node, ast.ClassDef):
                name = node.name
                if name is not None and name != "__init__" and name != "__str__" and name != "main":
                    functions.append(name)
                if name == "main":
                    name = file_name[1]+"/"+name
                    functions.append(name)
        data.append([file_name, functions])
        functions = []
    return data


def main(argument_path=None):
    if argument_path is None:
        root_dir = os.path.dirname(os.getcwd())
    else:
        root_dir = argument_path
    dirs = gfp.get_paths(root_dir)
    nodes = get_function_data(root_dir, dirs)
    return nodes

if __name__ == "__main__":
    print(get_function_data(os.path.dirname(os.getcwd()), gfp.get_paths(os.path.dirname(os.getcwd()))))