Example #1
0
def test_get_directories():
    cwd = os.path.dirname(__file__)
    parent_dir = os.path.dirname(cwd)
    pig_path = os.path.join(parent_dir, 'guinea-pig')
    correct_value = set([
        pig_path,
        os.path.join(pig_path, 'cage1'),
        os.path.join(pig_path, 'cage2')
    ])
    # check getting all possible directories with .py extension files
    assert correct_value == set(pkginfo.get_directories(pig_path, '.py'))
    # check that an empty iterable is returned for an empty directory
    cage3_dir = os.path.join(pig_path, 'cage3')
    assert set() == set(pkginfo.get_directories(cage3_dir))
Example #2
0
def test_sketch_blocks():
    cwd = os.path.dirname(__file__)
    test_dir = os.path.dirname(cwd)
    pig_path = os.path.join(test_dir, 'guinea-pig')
    pig1 = os.path.join(pig_path, 'cage1', 'pig1.py')
    pig2 = os.path.join(pig_path, 'cage2', 'pig2.py')
    assistant = os.path.join(pig_path, 'lab_assistant.py')

    excluding = []
    dirs = list(pkginfo.get_directories(pig_path))
    modpaths = list(pkginfo.get_modules(dirs))
    modpaths = pkginfo.filter_modules(modpaths, excluding)
    if pkginfo.is_package(pig_path):
        dirs.append(os.path.dirname(pig_path))
    graph = draftsman.sketch_blocks(modpaths, dirs)

    answer = networkx.Graph()
    with open(pig1) as source:
        size1, color1 = scientist.get_size_color(source.read(), initsize=80)
    with open(pig2) as source:
        size2, color2 = scientist.get_size_color(source.read(), initsize=80)
    with open(assistant) as source:
        size_assist, color_assist = scientist.get_size_color(source.read(),
                                                             initsize=80)
    answer.add_node(pig1, {
        'shape': 'square',
        'name': pig1,
        'size': size1,
        'color': color1
    })
    answer.add_node(pig2, {
        'shape': 'square',
        'name': pig2,
        'size': size2,
        'color': color2
    })
    answer.add_node(
        assistant, {
            'shape': 'square',
            'name': assistant,
            'size': size_assist,
            'color': color_assist
        })
    answer.add_node(
        'python', {
            'shape': 'circle',
            'name': 'Python',
            'docstring': 'Python builtin modules',
            'filepath': 'builtin',
            'size': 300,
            'color': 'hsl(207, 51%, 44%)'
        })
    answer.add_edge(pig2, pig1)
    answer.add_edge(assistant, 'python')
    answer.add_edge(assistant, pig1)
    answer.add_edge(assistant, pig2)

    assert networkx.is_isomorphic(answer, graph)
Example #3
0
def test_filter_modules():
    cwd = os.path.dirname(__file__)
    parent_dir = os.path.dirname(cwd)
    pig_path = os.path.join(parent_dir, 'guinea-pig')
    dirs = pkginfo.get_directories(pig_path)
    # test not matching module paths
    modules = list(pkginfo.get_modules(dirs))
    patterns = ['*foo*']  # nothing to filter
    filtered_modules = pkginfo.filter_modules(modules, patterns)
    assert modules == filtered_modules
    # test matching module paths
    assert [] == pkginfo.filter_modules(modules, ['*test*'])
Example #4
0
def test_get_modules():
    cwd = os.path.dirname(__file__)
    parent_dir = os.path.dirname(cwd)
    pig_path = os.path.join(parent_dir, 'guinea-pig')
    dirs = pkginfo.get_directories(pig_path)
    # test correct module recognizition
    correct_value = set([
        os.path.join(pig_path, 'cage1', 'pig1.py'),
        os.path.join(pig_path, 'cage2', 'pig2.py'),
        os.path.join(pig_path, 'lab_assistant.py')
    ])
    assert correct_value == set(pkginfo.get_modules(dirs))
    # test correct non-module recognizition
    cage3_dir = [os.path.join(pig_path, 'cage3')]
    assert set() == set(pkginfo.get_modules(cage3_dir))
Example #5
0
def trace(absfilepath):
    """open a browsers tab to display the graph of all the modules imported by the
    module at `absfilepath` either directly or indirectly.

    Args:
        absfilepath (str): absolute filepath of the module to analyze.
    """
    # create a list of dirs where to search for modules
    rootpath = pkginfo.find_root_pkg(absfilepath)
    dirs = list(pkginfo.get_directories(rootpath))
    # analyze module
    graph = draftsman.sketch_footprint(absfilepath, dirs)
    # write json formatted data
    data = json_graph.node_link_data(graph)
    # create temp dir in cwd to avoid writing protected files
    start_drawing(data)
Example #6
0
def test_compute_edges():
    parent_dir = os.path.dirname(os.path.dirname(__file__))
    guineapig_path = os.path.join(parent_dir, 'guinea-pig')
    master_module = os.path.join(parent_dir, 'guinea-pig', 'lab_assistant.py')
    pig1_path = os.path.join(parent_dir, 'guinea-pig', 'cage1', 'pig1.py')
    pig2_path = os.path.join(parent_dir, 'guinea-pig', 'cage2', 'pig2.py')

    dirs = list(pkginfo.get_directories(guineapig_path, '.py'))
    finder = modulefinder.ModuleFinder(path=dirs)
    finder.run_script(master_module)
    base = 'mammal'
    edges = set(
        scientist.compute_edges(master_module, base, finder.modules.values(),
                                finder.badmodules.keys()))
    answer = set([(master_module, base), (master_module, pig1_path),
                  (master_module, pig2_path)])
    assert answer == edges
Example #7
0
def test_accusation():
    cwd = os.path.dirname(__file__)
    test_dir = os.path.dirname(cwd)
    pig_path = os.path.join(test_dir, 'guinea-pig')
    pig1 = os.path.join(pig_path, 'cage1', 'pig1.py')
    pig2 = os.path.join(pig_path, 'cage2', 'pig2.py')
    assistant = os.path.join(pig_path, 'lab_assistant.py')

    rootpath = pkginfo.find_root_pkg(pig1)
    dirs = list(pkginfo.get_directories(rootpath))
    modpaths = list(pkginfo.get_modules(dirs))
    modpaths = pkginfo.filter_modules(modpaths, [])
    graph = draftsman.sketch_accusation(pig1, modpaths, dirs)

    answer = networkx.Graph()
    with open(pig1) as source:
        size1, color1 = scientist.get_size_color(source.read(), initsize=80)
    with open(pig2) as source:
        size2, color2 = scientist.get_size_color(source.read(), initsize=80)
    with open(assistant) as source:
        size_assist, color_assist = scientist.get_size_color(source.read(),
                                                             initsize=80)
    answer.add_node(pig1, {
        'shape': 'square',
        'name': pig1,
        'size': size1,
        'color': color1
    })
    answer.add_node(pig2, {
        'shape': 'square',
        'name': pig2,
        'size': size2,
        'color': color2
    })
    answer.add_node(
        assistant, {
            'shape': 'square',
            'name': assistant,
            'size': size_assist,
            'color': color_assist
        })
    answer.add_edge(assistant, pig1)
    answer.add_edge(pig2, pig1)

    assert networkx.is_isomorphic(answer, graph)
Example #8
0
def blame(absfilepath, excluding):
    """open a browsers tab to display a graph of all the modules that import the
    module at `absfilepath`, excluding those specified by `excluding`

    Args:
        absfilepath (str): absolute filepath of the module to check for.
        excluding (List[str]): list of Unix shell-style wildcards strings used to 
          exclude modules from being analyzed.
    """
    rootpath = pkginfo.find_root_pkg(absfilepath)
    dirs = list(pkginfo.get_directories(rootpath))
    modpaths = list(pkginfo.get_modules(dirs))
    modpaths = pkginfo.filter_modules(modpaths, excluding)
    graph = draftsman.sketch_accusation(absfilepath, modpaths, dirs)
    # write json formatted data
    data = json_graph.node_link_data(graph)
    # create temp dir in cwd to avoid writing protected files
    start_drawing(data)
Example #9
0
def draw(absdirpath, excluding):
    """open a browsers tab to display a graph of all the modules under `absdirpath`, excluding those
    specified by `excluding`. 

    Args:
        absdirpath (str): absolute directory path used as the root for populating
          the modules list.
        excluding (List[str]): list of Unix shell-style wildcards strings used to 
          exclude modules from being analyzed.
    """
    dirs = list(pkginfo.get_directories(absdirpath))
    modpaths = list(pkginfo.get_modules(dirs))
    modpaths = pkginfo.filter_modules(modpaths, excluding)
    if pkginfo.is_package(absdirpath):
        dirs.append(os.path.dirname(absdirpath))
    graph = draftsman.sketch_blocks(modpaths, dirs)
    # write json formatted data
    data = json_graph.node_link_data(graph)
    # create temp dir in cwd to avoid writing protected files
    start_drawing(data)