Show More
@@ -2,14 +2,7 b'' | |||||
2 |
|
2 | |||
3 | Utility functions for finding modules on sys.path. |
|
3 | Utility functions for finding modules on sys.path. | |
4 |
|
4 | |||
5 | `find_mod` finds named module on sys.path. |
|
5 | `find_module` returns a path to module or None, given certain conditions. | |
6 |
|
||||
7 | `get_init` helper function that finds __init__ file in a directory. |
|
|||
8 |
|
||||
9 | `find_module` variant of imp.find_module in std_lib that only returns |
|
|||
10 | path to module and not an open file object as well. |
|
|||
11 |
|
||||
12 |
|
||||
13 |
|
6 | |||
14 | """ |
|
7 | """ | |
15 | #----------------------------------------------------------------------------- |
|
8 | #----------------------------------------------------------------------------- | |
@@ -25,7 +18,7 b' path to module and not an open file object as well.' | |||||
25 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
26 |
|
19 | |||
27 | # Stdlib imports |
|
20 | # Stdlib imports | |
28 | import imp |
|
21 | import importlib | |
29 | import os |
|
22 | import os | |
30 |
|
23 | |||
31 | # Third-party imports |
|
24 | # Third-party imports | |
@@ -44,81 +37,34 b' import os' | |||||
44 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
45 | # Classes and functions |
|
38 | # Classes and functions | |
46 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
47 | def find_module(name, path=None): |
|
|||
48 | """imp.find_module variant that only return path of module. |
|
|||
49 |
|
||||
50 | The `imp.find_module` returns a filehandle that we are not interested in. |
|
|||
51 | Also we ignore any bytecode files that `imp.find_module` finds. |
|
|||
52 |
|
||||
53 | Parameters |
|
|||
54 | ---------- |
|
|||
55 | name : str |
|
|||
56 | name of module to locate |
|
|||
57 | path : list of str |
|
|||
58 | list of paths to search for `name`. If path=None then search sys.path |
|
|||
59 |
|
40 | |||
60 | Returns |
|
41 | def find_mod(module_name): | |
61 | ------- |
|
|||
62 | filename : str |
|
|||
63 | Return full path of module or None if module is missing or does not have |
|
|||
64 | .py or .pyw extension |
|
|||
65 | """ |
|
|||
66 | if name is None: |
|
|||
67 | return None |
|
|||
68 | try: |
|
|||
69 | file, filename, _ = imp.find_module(name, path) |
|
|||
70 | except ImportError: |
|
|||
71 | return None |
|
|||
72 | if file is None: |
|
|||
73 | return filename |
|
|||
74 | else: |
|
|||
75 | file.close() |
|
|||
76 | if os.path.splitext(filename)[1] in [".py", ".pyc"]: |
|
|||
77 | return filename |
|
|||
78 | else: |
|
|||
79 | return None |
|
|||
80 |
|
||||
81 | def get_init(dirname): |
|
|||
82 | """Get __init__ file path for module directory |
|
|||
83 |
|
||||
84 | Parameters |
|
|||
85 | ---------- |
|
|||
86 | dirname : str |
|
|||
87 | Find the __init__ file in directory `dirname` |
|
|||
88 |
|
||||
89 | Returns |
|
|||
90 | ------- |
|
|||
91 | init_path : str |
|
|||
92 | Path to __init__ file |
|
|||
93 |
|
|
42 | """ | |
94 | fbase = os.path.join(dirname, "__init__") |
|
43 | Find module `module_name` on sys.path, and return the path to module `module_name`. | |
95 | for ext in [".py", ".pyw"]: |
|
|||
96 | fname = fbase + ext |
|
|||
97 | if os.path.isfile(fname): |
|
|||
98 | return fname |
|
|||
99 |
|
|
44 | ||
|
45 | - If `module_name` refers to a module directory, then return path to __init__ file. | |||
|
46 | - If `module_name` is a directory without an __init__file, return None. | |||
|
47 | - If module is missing or does not have a `.py` or `.pyw` extension, return None. | |||
|
48 | - Note that we are not interested in running bytecode. | |||
|
49 | - Otherwise, return the fill path of the module. | |||
100 |
|
|
50 | ||
101 | def find_mod(module_name): |
|
|||
102 | """Find module `module_name` on sys.path |
|
|||
103 |
|
||||
104 | Return the path to module `module_name`. If `module_name` refers to |
|
|||
105 | a module directory then return path to __init__ file. Return full |
|
|||
106 | path of module or None if module is missing or does not have .py or .pyw |
|
|||
107 | extension. We are not interested in running bytecode. |
|
|||
108 |
|
||||
109 | Parameters |
|
51 | Parameters | |
110 | ---------- |
|
52 | ---------- | |
111 | module_name : str |
|
53 | module_name : str | |
112 |
|
54 | |||
113 | Returns |
|
55 | Returns | |
114 | ------- |
|
56 | ------- | |
115 | modulepath : str |
|
57 | module_path : str | |
116 | Path to module `module_name`. |
|
58 | Path to module `module_name`, its __init__.py, or None, | |
|
59 | depending on above conditions. | |||
117 | """ |
|
60 | """ | |
118 | parts = module_name.split(".") |
|
61 | loader = importlib.util.find_spec(module_name) | |
119 | basepath = find_module(parts[0]) |
|
62 | module_path = loader.origin | |
120 | for submodname in parts[1:]: |
|
63 | if module_path is None: | |
121 | basepath = find_module(submodname, [basepath]) |
|
64 | return None | |
122 | if basepath and os.path.isdir(basepath): |
|
65 | else: | |
123 | basepath = get_init(basepath) |
|
66 | split_path = module_path.split(".") | |
124 | return basepath |
|
67 | if split_path[1] in ["py", "pyw"]: | |
|
68 | return module_path | |||
|
69 | else: | |||
|
70 | return None |
@@ -66,62 +66,42 b' def teardown():' | |||||
66 | shutil.rmtree(TMP_TEST_DIR) |
|
66 | shutil.rmtree(TMP_TEST_DIR) | |
67 | sys.path = old_syspath |
|
67 | sys.path = old_syspath | |
68 |
|
68 | |||
69 |
|
||||
70 | def test_get_init_1(): |
|
|||
71 | """See if get_init can find __init__.py in this testdir""" |
|
|||
72 | with make_tempfile(join(TMP_TEST_DIR, "__init__.py")): |
|
|||
73 | assert mp.get_init(TMP_TEST_DIR) |
|
|||
74 |
|
||||
75 | def test_get_init_2(): |
|
|||
76 | """See if get_init can find __init__.pyw in this testdir""" |
|
|||
77 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")): |
|
|||
78 | assert mp.get_init(TMP_TEST_DIR) |
|
|||
79 |
|
||||
80 | def test_get_init_3(): |
|
|||
81 | """get_init can't find __init__.pyc in this testdir""" |
|
|||
82 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")): |
|
|||
83 | nt.assert_is_none(mp.get_init(TMP_TEST_DIR)) |
|
|||
84 |
|
||||
85 | def test_get_init_4(): |
|
|||
86 | """get_init can't find __init__ in empty testdir""" |
|
|||
87 | nt.assert_is_none(mp.get_init(TMP_TEST_DIR)) |
|
|||
88 |
|
||||
89 |
|
||||
90 | def test_find_mod_1(): |
|
69 | def test_find_mod_1(): | |
|
70 | """ | |||
|
71 | Search for a directory's file path. | |||
|
72 | Expected output: a path to that directory's __init__.py file. | |||
|
73 | """ | |||
91 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") |
|
74 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") | |
92 | nt.assert_equal(mp.find_mod("xmod"), modpath) |
|
75 | nt.assert_equal(mp.find_mod("xmod"), modpath) | |
93 |
|
76 | |||
94 | def test_find_mod_2(): |
|
77 | def test_find_mod_2(): | |
|
78 | """ | |||
|
79 | Search for a directory's file path. | |||
|
80 | Expected output: a path to that directory's __init__.py file. | |||
|
81 | TODO: Confirm why this is a duplicate test. | |||
|
82 | """ | |||
95 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") |
|
83 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") | |
96 | nt.assert_equal(mp.find_mod("xmod"), modpath) |
|
84 | nt.assert_equal(mp.find_mod("xmod"), modpath) | |
97 |
|
85 | |||
98 | def test_find_mod_3(): |
|
86 | def test_find_mod_3(): | |
|
87 | """ | |||
|
88 | Search for a directory + a filename without its .py extension | |||
|
89 | Expected output: full path with .py extension. | |||
|
90 | """ | |||
99 | modpath = join(TMP_TEST_DIR, "xmod", "sub.py") |
|
91 | modpath = join(TMP_TEST_DIR, "xmod", "sub.py") | |
100 | nt.assert_equal(mp.find_mod("xmod.sub"), modpath) |
|
92 | nt.assert_equal(mp.find_mod("xmod.sub"), modpath) | |
101 |
|
93 | |||
102 | def test_find_mod_4(): |
|
94 | def test_find_mod_4(): | |
|
95 | """ | |||
|
96 | Search for a filename without its .py extension | |||
|
97 | Expected output: full path with .py extension | |||
|
98 | """ | |||
103 | modpath = join(TMP_TEST_DIR, "pack.py") |
|
99 | modpath = join(TMP_TEST_DIR, "pack.py") | |
104 | nt.assert_equal(mp.find_mod("pack"), modpath) |
|
100 | nt.assert_equal(mp.find_mod("pack"), modpath) | |
105 |
|
101 | |||
106 | def test_find_mod_5(): |
|
102 | def test_find_mod_5(): | |
107 | modpath = join(TMP_TEST_DIR, "packpyc.pyc") |
|
103 | """ | |
108 | nt.assert_equal(mp.find_mod("packpyc"), modpath) |
|
104 | Search for a filename with a .pyc extension | |
109 |
|
105 | Expected output: TODO: do we exclude or include .pyc files? | ||
110 | def test_find_module_1(): |
|
106 | """ | |
111 | modpath = join(TMP_TEST_DIR, "xmod") |
|
107 | nt.assert_equal(mp.find_mod("packpyc"), None) | |
112 | nt.assert_equal(mp.find_module("xmod"), modpath) |
|
|||
113 |
|
||||
114 | def test_find_module_2(): |
|
|||
115 | """Testing sys.path that is empty""" |
|
|||
116 | nt.assert_is_none(mp.find_module("xmod", [])) |
|
|||
117 |
|
||||
118 | def test_find_module_3(): |
|
|||
119 | """Testing sys.path that is empty""" |
|
|||
120 | nt.assert_is_none(mp.find_module(None, None)) |
|
|||
121 |
|
||||
122 | def test_find_module_4(): |
|
|||
123 | """Testing sys.path that is empty""" |
|
|||
124 | nt.assert_is_none(mp.find_module(None)) |
|
|||
125 |
|
||||
126 | def test_find_module_5(): |
|
|||
127 | nt.assert_is_none(mp.find_module("xmod.nopack")) |
|
General Comments 0
You need to be logged in to leave comments.
Login now