Script db_checker_py
[hide private]
[frames] | no frames]

Source Code for Script script-db_checker_py

 1  #!/usr/bin/python2 
 2  # 
 3  # Copyright (c) 2008--2018 Red Hat, Inc. 
 4  # 
 5  # This software is licensed to you under the GNU General Public License, 
 6  # version 2 (GPLv2). There is NO WARRANTY for this software, express or 
 7  # implied, including the implied warranties of MERCHANTABILITY or FITNESS 
 8  # FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2 
 9  # along with this software; if not, see 
10  # http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt. 
11  # 
12  # Red Hat trademarks are not licensed under GPLv2. No permission is 
13  # granted to use or replicate Red Hat trademarks that are incorporated 
14  # in this software or its documentation. 
15  # 
16   
17  import os 
18  import sys 
19  from spacewalk.common import usix 
20   
21  _topdir = os.path.abspath(os.path.dirname(sys.argv[0])) 
22  if _topdir not in sys.path: 
23      sys.path.append(_topdir) 
24   
25  from spacewalk.server import rhnSQL 
26   
27   
28 -def main():
29 rhnSQL.initDB() 30 31 if not args: 32 print("No module specified") 33 return 0 34 35 if '.' not in sys.path: 36 sys.path.append('.') 37 38 g = globals() 39 40 for module_name in args: 41 print("Checking module %s" % module_name) 42 pmn = proper_module_name(module_name) 43 try: 44 m = __import__(pmn) 45 g[module_name] = m 46 except ImportError: 47 e = sys.exc_info()[1] 48 print("Unable to import module %s: %s" % (module_name, e)) 49 continue 50 51 comps = pmn.split('.') 52 for c in comps[1:]: 53 m = getattr(m, c) 54 55 for mod, name, statement in get_class_instances(m, rhnSQL.Statement): 56 try: 57 rhnSQL.prepare(statement) 58 except rhnSQL.SQLStatementPrepareError: 59 e = sys.exc_info()[1] 60 print("Error: %s.%s: %s" % (mod.__name__, name, e))
61 62
63 -def proper_module_name(module_name):
64 suffix = '.py' 65 if module_name.endswith(suffix): 66 module_name = module_name[:-len(suffix)] 67 68 return os.path.normpath(module_name).replace('/', '.')
69 70 _objs_seen = {} 71 72
73 -def get_class_instances(obj, class_obj):
74 if not hasattr(obj, "__dict__"): 75 return [] 76 id_obj = id(obj) 77 if id_obj in _objs_seen: 78 return [] 79 _objs_seen[id_obj] = None 80 result = [] 81 for k, v in obj.__dict__.items(): 82 if isinstance(v, class_obj): 83 result.append((obj, k, v)) 84 elif isinstance(v, usix.ClassType): 85 result.extend(get_class_instances(v, class_obj)) 86 return result
87 88 if __name__ == '__main__': 89 sys.exit(main() or 0) 90