| 1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
|
| 4 |
# Copyright (C) 2011 Google Inc. |
| 5 |
# All rights reserved. |
| 6 |
# |
| 7 |
# Redistribution and use in source and binary forms, with or without |
| 8 |
# modification, are permitted provided that the following conditions are |
| 9 |
# met: |
| 10 |
# |
| 11 |
# 1. Redistributions of source code must retain the above copyright notice, |
| 12 |
# this list of conditions and the following disclaimer. |
| 13 |
# |
| 14 |
# 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
# notice, this list of conditions and the following disclaimer in the |
| 16 |
# documentation and/or other materials provided with the distribution. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 19 |
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 20 |
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 21 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 25 |
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 26 |
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 27 |
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 |
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
|
| 31 |
"""Script to check module imports. |
| 32 |
|
| 33 |
""" |
| 34 |
|
| 35 |
# pylint: disable=C0103, C0413 |
| 36 |
# C0103: Invalid name |
| 37 |
# C0413: Wrong import position |
| 38 |
|
| 39 |
import sys |
| 40 |
|
| 41 |
# All modules imported after this line are removed from the global list before |
| 42 |
# importing a module to be checked |
| 43 |
_STANDARD_MODULES = set(sys.modules.keys()) |
| 44 |
|
| 45 |
import os.path |
| 46 |
|
| 47 |
from ganeti import build |
| 48 |
|
| 49 |
|
| 50 |
def main(): |
| 51 |
args = sys.argv[1:] |
| 52 |
|
| 53 |
# Get references to functions used later on |
| 54 |
load_module = build.LoadModule |
| 55 |
abspath = os.path.abspath |
| 56 |
commonprefix = os.path.commonprefix |
| 57 |
normpath = os.path.normpath |
| 58 |
|
| 59 |
script_path = abspath(__file__) |
| 60 |
srcdir = normpath(abspath(args.pop(0))) |
| 61 |
|
| 62 |
assert "ganeti" in sys.modules |
| 63 |
|
| 64 |
for filename in args: |
| 65 |
# Reset global state |
| 66 |
modules_to_remove = [] |
| 67 |
|
| 68 |
for name in sys.modules: |
| 69 |
if name not in _STANDARD_MODULES: |
| 70 |
modules_to_remove.append(name) |
| 71 |
|
| 72 |
for name in modules_to_remove: |
| 73 |
sys.modules.pop(name, None) |
| 74 |
|
| 75 |
assert "ganeti" not in sys.modules |
| 76 |
|
| 77 |
# Load module (this might import other modules) |
| 78 |
module = load_module(filename) |
| 79 |
|
| 80 |
result = [] |
| 81 |
|
| 82 |
for (name, checkmod) in sorted(sys.modules.items()): |
| 83 |
if checkmod is None or checkmod == module: |
| 84 |
continue |
| 85 |
|
| 86 |
try: |
| 87 |
checkmodpath = getattr(checkmod, "__file__") |
| 88 |
except AttributeError: |
| 89 |
# Built-in module |
| 90 |
continue |
| 91 |
|
| 92 |
if checkmodpath is None and hasattr(checkmod, "__path__"): |
| 93 |
# Namespace module |
| 94 |
continue |
| 95 |
|
| 96 |
abscheckmodpath = os.path.abspath(checkmodpath) |
| 97 |
|
| 98 |
if abscheckmodpath == script_path: |
| 99 |
# Ignore check script |
| 100 |
continue |
| 101 |
|
| 102 |
if commonprefix([abscheckmodpath, srcdir]) == srcdir: |
| 103 |
result.append(name) |
| 104 |
|
| 105 |
if result: |
| 106 |
raise Exception("Module '%s' has illegal imports: %s" % |
| 107 |
(filename, ", ".join(result))) |
| 108 |
|
| 109 |
|
| 110 |
if __name__ == "__main__": |
| 111 |
main() |