Statistics
| Branch: | Tag: | Revision:

root / autotools / check-imports @ 36c70d4d

History | View | Annotate | Download (2.3 kB)

1
#!/usr/bin/python
2
#
3

    
4
# Copyright (C) 2011 Google Inc.
5
#
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.
10
#
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.
15
#
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
19
# 02110-1301, USA.
20

    
21

    
22
"""Script to check module imports.
23

    
24
"""
25

    
26
# pylint: disable=C0103
27
# C0103: Invalid name
28

    
29
import sys
30

    
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()
34

    
35
import os.path
36

    
37
from ganeti import build
38

    
39

    
40
def main():
41
  args = sys.argv[1:]
42

    
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
48

    
49
  script_path = abspath(__file__)
50
  srcdir = normpath(abspath(args.pop(0)))
51

    
52
  assert "ganeti" in sys.modules
53

    
54
  for filename in args:
55
    # Reset global state
56
    for name in sys.modules.keys():
57
      if name not in _STANDARD_MODULES:
58
        sys.modules.pop(name, None)
59

    
60
    assert "ganeti" not in sys.modules
61

    
62
    # Load module (this might import other modules)
63
    module = load_module(filename)
64

    
65
    result = []
66

    
67
    for (name, checkmod) in sorted(sys.modules.items()):
68
      if checkmod is None or checkmod == module:
69
        continue
70

    
71
      try:
72
        checkmodpath = getattr(checkmod, "__file__")
73
      except AttributeError:
74
        # Built-in module
75
        pass
76
      else:
77
        abscheckmodpath = os.path.abspath(checkmodpath)
78

    
79
        if abscheckmodpath == script_path:
80
          # Ignore check script
81
          continue
82

    
83
        if commonprefix([abscheckmodpath, srcdir]) == srcdir:
84
          result.append(name)
85

    
86
    if result:
87
      raise Exception("Module '%s' has illegal imports: %s" %
88
                      (filename, ", ".join(result)))
89

    
90

    
91
if __name__ == "__main__":
92
  main()