Statistics
| Branch: | Tag: | Revision:

root / autotools / check-imports @ ea2bcb82

History | View | Annotate | Download (2.3 kB)

1 f6cbcc06 Michael Hanselmann
#!/usr/bin/python
2 f6cbcc06 Michael Hanselmann
#
3 f6cbcc06 Michael Hanselmann
4 f6cbcc06 Michael Hanselmann
# Copyright (C) 2011 Google Inc.
5 f6cbcc06 Michael Hanselmann
#
6 f6cbcc06 Michael Hanselmann
# This program is free software; you can redistribute it and/or modify
7 f6cbcc06 Michael Hanselmann
# it under the terms of the GNU General Public License as published by
8 f6cbcc06 Michael Hanselmann
# the Free Software Foundation; either version 2 of the License, or
9 f6cbcc06 Michael Hanselmann
# (at your option) any later version.
10 f6cbcc06 Michael Hanselmann
#
11 f6cbcc06 Michael Hanselmann
# This program is distributed in the hope that it will be useful, but
12 f6cbcc06 Michael Hanselmann
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 f6cbcc06 Michael Hanselmann
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 f6cbcc06 Michael Hanselmann
# General Public License for more details.
15 f6cbcc06 Michael Hanselmann
#
16 f6cbcc06 Michael Hanselmann
# You should have received a copy of the GNU General Public License
17 f6cbcc06 Michael Hanselmann
# along with this program; if not, write to the Free Software
18 f6cbcc06 Michael Hanselmann
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 f6cbcc06 Michael Hanselmann
# 02110-1301, USA.
20 f6cbcc06 Michael Hanselmann
21 f6cbcc06 Michael Hanselmann
22 f6cbcc06 Michael Hanselmann
"""Script to check module imports.
23 f6cbcc06 Michael Hanselmann
24 f6cbcc06 Michael Hanselmann
"""
25 f6cbcc06 Michael Hanselmann
26 f6cbcc06 Michael Hanselmann
# pylint: disable=C0103
27 f6cbcc06 Michael Hanselmann
# C0103: Invalid name
28 f6cbcc06 Michael Hanselmann
29 f6cbcc06 Michael Hanselmann
import sys
30 f6cbcc06 Michael Hanselmann
31 f6cbcc06 Michael Hanselmann
# All modules imported after this line are removed from the global list before
32 f6cbcc06 Michael Hanselmann
# importing a module to be checked
33 f6cbcc06 Michael Hanselmann
_STANDARD_MODULES = sys.modules.keys()
34 f6cbcc06 Michael Hanselmann
35 f6cbcc06 Michael Hanselmann
import os.path
36 f6cbcc06 Michael Hanselmann
37 f6cbcc06 Michael Hanselmann
from ganeti import build
38 f6cbcc06 Michael Hanselmann
39 f6cbcc06 Michael Hanselmann
40 f6cbcc06 Michael Hanselmann
def main():
41 f6cbcc06 Michael Hanselmann
  args = sys.argv[1:]
42 f6cbcc06 Michael Hanselmann
43 f6cbcc06 Michael Hanselmann
  # Get references to functions used later on
44 f6cbcc06 Michael Hanselmann
  load_module = build.LoadModule
45 f6cbcc06 Michael Hanselmann
  abspath = os.path.abspath
46 f6cbcc06 Michael Hanselmann
  commonprefix = os.path.commonprefix
47 f6cbcc06 Michael Hanselmann
  normpath = os.path.normpath
48 f6cbcc06 Michael Hanselmann
49 f6cbcc06 Michael Hanselmann
  script_path = abspath(__file__)
50 f6cbcc06 Michael Hanselmann
  srcdir = normpath(abspath(args.pop(0)))
51 f6cbcc06 Michael Hanselmann
52 f6cbcc06 Michael Hanselmann
  assert "ganeti" in sys.modules
53 f6cbcc06 Michael Hanselmann
54 f6cbcc06 Michael Hanselmann
  for filename in args:
55 f6cbcc06 Michael Hanselmann
    # Reset global state
56 f6cbcc06 Michael Hanselmann
    for name in sys.modules.keys():
57 f6cbcc06 Michael Hanselmann
      if name not in _STANDARD_MODULES:
58 f6cbcc06 Michael Hanselmann
        sys.modules.pop(name, None)
59 f6cbcc06 Michael Hanselmann
60 f6cbcc06 Michael Hanselmann
    assert "ganeti" not in sys.modules
61 f6cbcc06 Michael Hanselmann
62 f6cbcc06 Michael Hanselmann
    # Load module (this might import other modules)
63 f6cbcc06 Michael Hanselmann
    module = load_module(filename)
64 f6cbcc06 Michael Hanselmann
65 f6cbcc06 Michael Hanselmann
    result = []
66 f6cbcc06 Michael Hanselmann
67 f6cbcc06 Michael Hanselmann
    for (name, checkmod) in sorted(sys.modules.items()):
68 f6cbcc06 Michael Hanselmann
      if checkmod is None or checkmod == module:
69 f6cbcc06 Michael Hanselmann
        continue
70 f6cbcc06 Michael Hanselmann
71 f6cbcc06 Michael Hanselmann
      try:
72 f6cbcc06 Michael Hanselmann
        checkmodpath = getattr(checkmod, "__file__")
73 f6cbcc06 Michael Hanselmann
      except AttributeError:
74 f6cbcc06 Michael Hanselmann
        # Built-in module
75 f6cbcc06 Michael Hanselmann
        pass
76 f6cbcc06 Michael Hanselmann
      else:
77 f6cbcc06 Michael Hanselmann
        abscheckmodpath = os.path.abspath(checkmodpath)
78 f6cbcc06 Michael Hanselmann
79 f6cbcc06 Michael Hanselmann
        if abscheckmodpath == script_path:
80 f6cbcc06 Michael Hanselmann
          # Ignore check script
81 f6cbcc06 Michael Hanselmann
          continue
82 f6cbcc06 Michael Hanselmann
83 f6cbcc06 Michael Hanselmann
        if commonprefix([abscheckmodpath, srcdir]) == srcdir:
84 f6cbcc06 Michael Hanselmann
          result.append(name)
85 f6cbcc06 Michael Hanselmann
86 f6cbcc06 Michael Hanselmann
    if result:
87 f6cbcc06 Michael Hanselmann
      raise Exception("Module '%s' has illegal imports: %s" %
88 f6cbcc06 Michael Hanselmann
                      (filename, ", ".join(result)))
89 f6cbcc06 Michael Hanselmann
90 f6cbcc06 Michael Hanselmann
91 f6cbcc06 Michael Hanselmann
if __name__ == "__main__":
92 f6cbcc06 Michael Hanselmann
  main()