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