4 # Copyright (C) 2011 Google Inc.
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 """Script to check module imports.
26 # pylint: disable=C0103
31 # All modules imported after this line are removed from the global list before
32 # importing a module to be checked
33 _STANDARD_MODULES = sys.modules.keys()
37 from ganeti import build
43 # Get references to functions used later on
44 load_module = build.LoadModule
45 abspath = os.path.abspath
46 commonprefix = os.path.commonprefix
47 normpath = os.path.normpath
49 script_path = abspath(__file__)
50 srcdir = normpath(abspath(args.pop(0)))
52 assert "ganeti" in sys.modules
56 for name in sys.modules.keys():
57 if name not in _STANDARD_MODULES:
58 sys.modules.pop(name, None)
60 assert "ganeti" not in sys.modules
62 # Load module (this might import other modules)
63 module = load_module(filename)
67 for (name, checkmod) in sorted(sys.modules.items()):
68 if checkmod is None or checkmod == module:
72 checkmodpath = getattr(checkmod, "__file__")
73 except AttributeError:
77 abscheckmodpath = os.path.abspath(checkmodpath)
79 if abscheckmodpath == script_path:
83 if commonprefix([abscheckmodpath, srcdir]) == srcdir:
87 raise Exception("Module '%s' has illegal imports: %s" %
88 (filename, ", ".join(result)))
91 if __name__ == "__main__":