Show More
@@ -1,125 +1,125 b'' | |||||
1 | """Utility functions for finding modules |
|
1 | """Utility functions for finding modules | |
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_mod` finds named module on sys.path. | |
6 |
|
6 | |||
7 | `get_init` helper function that finds __init__ file in a directory. |
|
7 | `get_init` helper function that finds __init__ file in a directory. | |
8 |
|
8 | |||
9 | `find_module` variant of imp.find_module in std_lib that only returns |
|
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. |
|
10 | path to module and not an open file object as well. | |
11 |
|
11 | |||
12 |
|
12 | |||
13 |
|
13 | |||
14 | """ |
|
14 | """ | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 | # Copyright (c) 2011, the IPython Development Team. |
|
16 | # Copyright (c) 2011, the IPython Development Team. | |
17 | # |
|
17 | # | |
18 | # Distributed under the terms of the Modified BSD License. |
|
18 | # Distributed under the terms of the Modified BSD License. | |
19 | # |
|
19 | # | |
20 | # The full license is in the file COPYING.txt, distributed with this software. |
|
20 | # The full license is in the file COPYING.txt, distributed with this software. | |
21 | #----------------------------------------------------------------------------- |
|
21 | #----------------------------------------------------------------------------- | |
22 |
|
22 | |||
23 | #----------------------------------------------------------------------------- |
|
23 | #----------------------------------------------------------------------------- | |
24 | # Imports |
|
24 | # Imports | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 | from __future__ import print_function |
|
26 | from __future__ import print_function | |
27 |
|
27 | |||
28 | # Stdlib imports |
|
28 | # Stdlib imports | |
29 | import imp |
|
29 | import imp | |
30 | import os |
|
30 | import os | |
31 |
|
31 | |||
32 | # Third-party imports |
|
32 | # Third-party imports | |
33 |
|
33 | |||
34 | # Our own imports |
|
34 | # Our own imports | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 | # Globals and constants |
|
38 | # Globals and constants | |
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 |
|
40 | |||
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 | # Local utilities |
|
42 | # Local utilities | |
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 | #----------------------------------------------------------------------------- |
|
45 | #----------------------------------------------------------------------------- | |
46 | # Classes and functions |
|
46 | # Classes and functions | |
47 | #----------------------------------------------------------------------------- |
|
47 | #----------------------------------------------------------------------------- | |
48 | def find_module(name, path=None): |
|
48 | def find_module(name, path=None): | |
49 | """imp.find_module variant that only return path of module. |
|
49 | """imp.find_module variant that only return path of module. | |
50 |
|
50 | |||
51 | The `imp.find_module` returns a filehandle that we are not interested in. |
|
51 | The `imp.find_module` returns a filehandle that we are not interested in. | |
52 | Also we ignore any bytecode files that `imp.find_module` finds. |
|
52 | Also we ignore any bytecode files that `imp.find_module` finds. | |
53 |
|
53 | |||
54 | Parameters |
|
54 | Parameters | |
55 | ---------- |
|
55 | ---------- | |
56 | name : str |
|
56 | name : str | |
57 | name of module to locate |
|
57 | name of module to locate | |
58 | path : list of str |
|
58 | path : list of str | |
59 | list of paths to search for `name`. If path=None then search sys.path |
|
59 | list of paths to search for `name`. If path=None then search sys.path | |
60 |
|
60 | |||
61 | Returns |
|
61 | Returns | |
62 | ------- |
|
62 | ------- | |
63 | filename : str |
|
63 | filename : str | |
64 | Return full path of module or None if module is missing or does not have |
|
64 | Return full path of module or None if module is missing or does not have | |
65 | .py or .pyw extension |
|
65 | .py or .pyw extension | |
66 | """ |
|
66 | """ | |
67 | if name is None: |
|
67 | if name is None: | |
68 | return None |
|
68 | return None | |
69 | try: |
|
69 | try: | |
70 | file, filename, _ = imp.find_module(name, path) |
|
70 | file, filename, _ = imp.find_module(name, path) | |
71 | except ImportError: |
|
71 | except ImportError: | |
72 | return None |
|
72 | return None | |
73 | if file is None: |
|
73 | if file is None: | |
74 | return filename |
|
74 | return filename | |
75 | else: |
|
75 | else: | |
76 | file.close() |
|
76 | file.close() | |
77 | if os.path.splitext(filename)[1] in [".py", "pyc"]: |
|
77 | if os.path.splitext(filename)[1] in [".py", ".pyc"]: | |
78 | return filename |
|
78 | return filename | |
79 | else: |
|
79 | else: | |
80 | return None |
|
80 | return None | |
81 |
|
81 | |||
82 | def get_init(dirname): |
|
82 | def get_init(dirname): | |
83 | """Get __init__ file path for module directory |
|
83 | """Get __init__ file path for module directory | |
84 |
|
84 | |||
85 | Parameters |
|
85 | Parameters | |
86 | ---------- |
|
86 | ---------- | |
87 | dirname : str |
|
87 | dirname : str | |
88 | Find the __init__ file in directory `dirname` |
|
88 | Find the __init__ file in directory `dirname` | |
89 |
|
89 | |||
90 | Returns |
|
90 | Returns | |
91 | ------- |
|
91 | ------- | |
92 | init_path : str |
|
92 | init_path : str | |
93 | Path to __init__ file |
|
93 | Path to __init__ file | |
94 | """ |
|
94 | """ | |
95 | fbase = os.path.join(dirname, "__init__") |
|
95 | fbase = os.path.join(dirname, "__init__") | |
96 | for ext in [".py", ".pyw"]: |
|
96 | for ext in [".py", ".pyw"]: | |
97 | fname = fbase + ext |
|
97 | fname = fbase + ext | |
98 | if os.path.isfile(fname): |
|
98 | if os.path.isfile(fname): | |
99 | return fname |
|
99 | return fname | |
100 |
|
100 | |||
101 |
|
101 | |||
102 | def find_mod(module_name): |
|
102 | def find_mod(module_name): | |
103 | """Find module `module_name` on sys.path |
|
103 | """Find module `module_name` on sys.path | |
104 |
|
104 | |||
105 | Return the path to module `module_name`. If `module_name` refers to |
|
105 | Return the path to module `module_name`. If `module_name` refers to | |
106 | a module directory then return path to __init__ file. Return full |
|
106 | a module directory then return path to __init__ file. Return full | |
107 | path of module or None if module is missing or does not have .py or .pyw |
|
107 | path of module or None if module is missing or does not have .py or .pyw | |
108 | extension. We are not interested in running bytecode. |
|
108 | extension. We are not interested in running bytecode. | |
109 |
|
109 | |||
110 | Parameters |
|
110 | Parameters | |
111 | ---------- |
|
111 | ---------- | |
112 | module_name : str |
|
112 | module_name : str | |
113 |
|
113 | |||
114 | Returns |
|
114 | Returns | |
115 | ------- |
|
115 | ------- | |
116 | modulepath : str |
|
116 | modulepath : str | |
117 | Path to module `module_name`. |
|
117 | Path to module `module_name`. | |
118 | """ |
|
118 | """ | |
119 | parts = module_name.split(".") |
|
119 | parts = module_name.split(".") | |
120 | basepath = find_module(parts[0]) |
|
120 | basepath = find_module(parts[0]) | |
121 | for submodname in parts[1:]: |
|
121 | for submodname in parts[1:]: | |
122 | basepath = find_module(submodname, [basepath]) |
|
122 | basepath = find_module(submodname, [basepath]) | |
123 | if basepath and os.path.isdir(basepath): |
|
123 | if basepath and os.path.isdir(basepath): | |
124 | basepath = get_init(basepath) |
|
124 | basepath = get_init(basepath) | |
125 | return basepath |
|
125 | return basepath |
@@ -1,125 +1,128 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """Tests for IPython.utils.module_paths.py""" |
|
2 | """Tests for IPython.utils.module_paths.py""" | |
3 |
|
3 | |||
4 | #----------------------------------------------------------------------------- |
|
4 | #----------------------------------------------------------------------------- | |
5 | # Copyright (C) 2008-2011 The IPython Development Team |
|
5 | # Copyright (C) 2008-2011 The IPython Development Team | |
6 | # |
|
6 | # | |
7 | # Distributed under the terms of the BSD License. The full license is in |
|
7 | # Distributed under the terms of the BSD License. The full license is in | |
8 | # the file COPYING, distributed as part of this software. |
|
8 | # the file COPYING, distributed as part of this software. | |
9 | #----------------------------------------------------------------------------- |
|
9 | #----------------------------------------------------------------------------- | |
10 |
|
10 | |||
11 | #----------------------------------------------------------------------------- |
|
11 | #----------------------------------------------------------------------------- | |
12 | # Imports |
|
12 | # Imports | |
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 |
|
14 | |||
15 | from __future__ import with_statement |
|
15 | from __future__ import with_statement | |
16 |
|
16 | |||
17 | import os |
|
17 | import os | |
18 | import shutil |
|
18 | import shutil | |
19 | import sys |
|
19 | import sys | |
20 | import tempfile |
|
20 | import tempfile | |
21 |
|
21 | |||
22 | from os.path import join, abspath, split |
|
22 | from os.path import join, abspath, split | |
23 |
|
23 | |||
24 | from IPython.testing.tools import make_tempfile |
|
24 | from IPython.testing.tools import make_tempfile | |
25 |
|
25 | |||
26 | import IPython.utils.module_paths as mp |
|
26 | import IPython.utils.module_paths as mp | |
27 |
|
27 | |||
|
28 | import nose.tools as nt | |||
|
29 | ||||
28 | env = os.environ |
|
30 | env = os.environ | |
29 | TEST_FILE_PATH = split(abspath(__file__))[0] |
|
31 | TEST_FILE_PATH = split(abspath(__file__))[0] | |
30 | TMP_TEST_DIR = tempfile.mkdtemp() |
|
32 | TMP_TEST_DIR = tempfile.mkdtemp() | |
31 | # |
|
33 | # | |
32 | # Setup/teardown functions/decorators |
|
34 | # Setup/teardown functions/decorators | |
33 | # |
|
35 | # | |
34 |
|
36 | |||
35 | old_syspath = sys.path |
|
37 | old_syspath = sys.path | |
36 |
|
38 | |||
37 | def make_empty_file(fname): |
|
39 | def make_empty_file(fname): | |
38 | f = open(fname, 'w') |
|
40 | f = open(fname, 'w') | |
39 | f.close() |
|
41 | f.close() | |
40 |
|
42 | |||
41 |
|
43 | |||
42 | def setup(): |
|
44 | def setup(): | |
43 | """Setup testenvironment for the module: |
|
45 | """Setup testenvironment for the module: | |
44 |
|
46 | |||
45 | """ |
|
47 | """ | |
46 | # Do not mask exceptions here. In particular, catching WindowsError is a |
|
48 | # Do not mask exceptions here. In particular, catching WindowsError is a | |
47 | # problem because that exception is only defined on Windows... |
|
49 | # problem because that exception is only defined on Windows... | |
48 | os.makedirs(join(TMP_TEST_DIR, "xmod")) |
|
50 | os.makedirs(join(TMP_TEST_DIR, "xmod")) | |
49 | os.makedirs(join(TMP_TEST_DIR, "nomod")) |
|
51 | os.makedirs(join(TMP_TEST_DIR, "nomod")) | |
50 | make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py")) |
|
52 | make_empty_file(join(TMP_TEST_DIR, "xmod/__init__.py")) | |
51 | make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py")) |
|
53 | make_empty_file(join(TMP_TEST_DIR, "xmod/sub.py")) | |
52 | make_empty_file(join(TMP_TEST_DIR, "pack.py")) |
|
54 | make_empty_file(join(TMP_TEST_DIR, "pack.py")) | |
53 | make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc")) |
|
55 | make_empty_file(join(TMP_TEST_DIR, "packpyc.pyc")) | |
54 | sys.path = [TMP_TEST_DIR] |
|
56 | sys.path = [TMP_TEST_DIR] | |
55 |
|
57 | |||
56 | def teardown(): |
|
58 | def teardown(): | |
57 | """Teardown testenvironment for the module: |
|
59 | """Teardown testenvironment for the module: | |
58 |
|
60 | |||
59 | - Remove tempdir |
|
61 | - Remove tempdir | |
60 | - restore sys.path |
|
62 | - restore sys.path | |
61 | """ |
|
63 | """ | |
62 | # Note: we remove the parent test dir, which is the root of all test |
|
64 | # Note: we remove the parent test dir, which is the root of all test | |
63 | # subdirs we may have created. Use shutil instead of os.removedirs, so |
|
65 | # subdirs we may have created. Use shutil instead of os.removedirs, so | |
64 | # that non-empty directories are all recursively removed. |
|
66 | # that non-empty directories are all recursively removed. | |
65 | shutil.rmtree(TMP_TEST_DIR) |
|
67 | shutil.rmtree(TMP_TEST_DIR) | |
66 | sys.path = old_syspath |
|
68 | sys.path = old_syspath | |
67 |
|
69 | |||
68 |
|
70 | |||
69 | def test_get_init_1(): |
|
71 | def test_get_init_1(): | |
70 | """See if get_init can find __init__.py in this testdir""" |
|
72 | """See if get_init can find __init__.py in this testdir""" | |
71 | with make_tempfile(join(TMP_TEST_DIR, "__init__.py")): |
|
73 | with make_tempfile(join(TMP_TEST_DIR, "__init__.py")): | |
72 | assert mp.get_init(TMP_TEST_DIR) |
|
74 | assert mp.get_init(TMP_TEST_DIR) | |
73 |
|
75 | |||
74 | def test_get_init_2(): |
|
76 | def test_get_init_2(): | |
75 | """See if get_init can find __init__.pyw in this testdir""" |
|
77 | """See if get_init can find __init__.pyw in this testdir""" | |
76 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")): |
|
78 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyw")): | |
77 | assert mp.get_init(TMP_TEST_DIR) |
|
79 | assert mp.get_init(TMP_TEST_DIR) | |
78 |
|
80 | |||
79 | def test_get_init_3(): |
|
81 | def test_get_init_3(): | |
80 | """get_init can't find __init__.pyc in this testdir""" |
|
82 | """get_init can't find __init__.pyc in this testdir""" | |
81 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")): |
|
83 | with make_tempfile(join(TMP_TEST_DIR, "__init__.pyc")): | |
82 |
assert |
|
84 | nt.assert_is_none(mp.get_init(TMP_TEST_DIR)) | |
83 |
|
85 | |||
84 | def test_get_init_4(): |
|
86 | def test_get_init_4(): | |
85 | """get_init can't find __init__ in empty testdir""" |
|
87 | """get_init can't find __init__ in empty testdir""" | |
86 |
assert |
|
88 | nt.assert_is_none(mp.get_init(TMP_TEST_DIR)) | |
87 |
|
89 | |||
88 |
|
90 | |||
89 | def test_find_mod_1(): |
|
91 | def test_find_mod_1(): | |
90 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") |
|
92 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") | |
91 |
assert |
|
93 | nt.assert_equal(mp.find_mod("xmod"), modpath) | |
92 |
|
94 | |||
93 | def test_find_mod_2(): |
|
95 | def test_find_mod_2(): | |
94 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") |
|
96 | modpath = join(TMP_TEST_DIR, "xmod", "__init__.py") | |
95 |
assert |
|
97 | nt.assert_equal(mp.find_mod("xmod"), modpath) | |
96 |
|
98 | |||
97 | def test_find_mod_3(): |
|
99 | def test_find_mod_3(): | |
98 | modpath = join(TMP_TEST_DIR, "xmod", "sub.py") |
|
100 | modpath = join(TMP_TEST_DIR, "xmod", "sub.py") | |
99 |
assert |
|
101 | nt.assert_equal(mp.find_mod("xmod.sub"), modpath) | |
100 |
|
102 | |||
101 | def test_find_mod_4(): |
|
103 | def test_find_mod_4(): | |
102 | modpath = join(TMP_TEST_DIR, "pack.py") |
|
104 | modpath = join(TMP_TEST_DIR, "pack.py") | |
103 |
assert |
|
105 | nt.assert_equal(mp.find_mod("pack"), modpath) | |
104 |
|
106 | |||
105 | def test_find_mod_5(): |
|
107 | def test_find_mod_5(): | |
106 | assert mp.find_mod("packpyc") is None |
|
108 | modpath = join(TMP_TEST_DIR, "packpyc.pyc") | |
|
109 | nt.assert_equal(mp.find_mod("packpyc"), modpath) | |||
107 |
|
110 | |||
108 | def test_find_module_1(): |
|
111 | def test_find_module_1(): | |
109 | modpath = join(TMP_TEST_DIR, "xmod") |
|
112 | modpath = join(TMP_TEST_DIR, "xmod") | |
110 |
assert |
|
113 | nt.assert_equal(mp.find_module("xmod"), modpath) | |
111 |
|
114 | |||
112 | def test_find_module_2(): |
|
115 | def test_find_module_2(): | |
113 | """Testing sys.path that is empty""" |
|
116 | """Testing sys.path that is empty""" | |
114 |
assert |
|
117 | nt.assert_is_none(mp.find_module("xmod", [])) | |
115 |
|
118 | |||
116 | def test_find_module_3(): |
|
119 | def test_find_module_3(): | |
117 | """Testing sys.path that is empty""" |
|
120 | """Testing sys.path that is empty""" | |
118 |
assert |
|
121 | nt.assert_is_none(mp.find_module(None, None)) | |
119 |
|
122 | |||
120 | def test_find_module_4(): |
|
123 | def test_find_module_4(): | |
121 | """Testing sys.path that is empty""" |
|
124 | """Testing sys.path that is empty""" | |
122 |
assert |
|
125 | nt.assert_is_none(mp.find_module(None)) | |
123 |
|
126 | |||
124 | def test_find_module_5(): |
|
127 | def test_find_module_5(): | |
125 |
assert |
|
128 | nt.assert_is_none(mp.find_module("xmod.nopack")) |
General Comments 0
You need to be logged in to leave comments.
Login now