Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | # just import all python3 modules used by osmo-gsm-tester to make sure they are |
| 4 | # installed. |
| 5 | |
Neels Hofmeyr | dae3d3c | 2017-03-28 12:16:58 +0200 | [diff] [blame] | 6 | |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import sys |
| 10 | import argparse |
| 11 | import pprint |
| 12 | import subprocess |
| 13 | |
| 14 | feature_module_map = { |
Pau Espin Pedrol | 4077569 | 2020-05-05 16:51:51 +0200 | [diff] [blame] | 15 | '2g': ['bsc_osmo', 'bts_nanobts', 'bts_oc2g', 'bts_octphy', 'bts_osmo', 'bts_osmotrx', 'bts_osmovirtual', 'bts_sysmo', 'bts', 'bts', 'esme', 'ggsn_osmo', 'hlr_osmo', 'mgcpgw_osmo', 'mgw_osmo', 'ms_ofono', 'ms_driver', 'msc_osmo', 'nitb_osmo', 'osmo_ctrl', 'osmocon', 'pcap_recorder', 'pcu_oc2g', 'pcu_osmo', 'pcu_sysmo', 'pcu', 'sgsn_osmo', 'sms', 'smsc', 'stp_osmo'], |
| 16 | '4g': [], |
| 17 | 'srs': ['enb_srs', 'epc_srs', 'ms_srs'], |
| 18 | 'powersupply': ['powersupply', 'powersupply_intellinet', 'powersupply_sispm'], |
| 19 | 'rfemu': ['rfemu', 'rfemu_amarisoftctrl', 'rfemu_minicircuits'], |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 20 | } |
| 21 | |
| 22 | def skip_features_to_skip_modules(skip_features): |
| 23 | skip_obj_modules = [] |
| 24 | |
| 25 | for skip_feature in skip_features: |
Pau Espin Pedrol | 4077569 | 2020-05-05 16:51:51 +0200 | [diff] [blame] | 26 | if skip_feature in feature_module_map: |
| 27 | for skip_module in feature_module_map[skip_feature]: |
| 28 | skip_obj_modules.append(skip_module) |
| 29 | else: |
| 30 | skip_obj_modules.append(skip_feature) |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 31 | return skip_obj_modules |
| 32 | |
| 33 | def import_runtime_dependencies(): |
| 34 | # we don't have any right now, but in the future if we import a module during runtime (eg inside a function), then we need to place it here: |
| 35 | # import foobar |
| 36 | pass |
| 37 | |
| 38 | def import_all_py_in_dir(rel_path, skip_modules=[]): |
Pau Espin Pedrol | 1337fb8 | 2020-05-12 13:28:55 +0200 | [diff] [blame] | 39 | selfdir = os.getcwd() |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 40 | dir = os.path.join(selfdir, rel_path) |
| 41 | print('importing files in directory %s' % dir) |
| 42 | for entry in os.listdir(dir): |
| 43 | full_entry = os.path.join(selfdir, rel_path, entry) |
| 44 | if not os.path.isfile(full_entry): |
| 45 | if args.verbose: |
| 46 | print('skipping entry %s' % full_entry) |
| 47 | continue |
| 48 | if not full_entry.endswith('.py'): |
| 49 | if args.verbose: |
| 50 | print('skipping file %s' % full_entry) |
| 51 | continue |
| 52 | modulename = entry[:-3] |
| 53 | if modulename in skip_modules: |
| 54 | if args.verbose: |
| 55 | print('skipping module %s' % modulename) |
| 56 | continue |
| 57 | modulepath = rel_path.replace('/', '.') + '.' + modulename |
| 58 | print('importing %s' % modulepath) |
| 59 | __import__(modulepath, globals(), locals()) |
| 60 | |
| 61 | def get_module_names(): |
| 62 | all_modules=sys.modules.items() |
| 63 | all_modules_filtered = {} |
| 64 | for mname, m in all_modules: |
| 65 | if not hasattr(m, '__file__'): |
| 66 | continue # skip built-in modules |
| 67 | if mname.startswith('_'): |
| 68 | continue # skip internal modules |
| 69 | if mname.startswith('src.osmo_') or 'osmo_gsm_tester' in mname or 'osmo_ms_driver' in mname: |
| 70 | continue # skip our own local modules |
| 71 | mname = mname.split('.')[0] # store only main module |
| 72 | if m not in all_modules_filtered.values(): |
| 73 | all_modules_filtered[mname] = m |
| 74 | return all_modules_filtered |
| 75 | |
| 76 | def print_deb_packages(modules): |
| 77 | packages_deb = [] |
| 78 | modules_err = [] |
| 79 | for mname, m in modules.items(): |
| 80 | proc = subprocess.Popen(["dpkg", "-S", m.__file__], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 81 | outs, errs = proc.communicate() |
| 82 | if args.verbose: |
| 83 | print('out: %s, err: %s' %(outs, errs)) |
| 84 | if len(errs): # error -> package not found (installed through pip?) |
| 85 | modules_err.append((mname, errs.decode('utf-8'))) |
| 86 | elif len(outs): |
| 87 | outs = outs.decode('utf-8') |
| 88 | outs = outs.split()[0].rstrip(':') # first part is debian package name |
| 89 | if not outs in packages_deb: |
| 90 | packages_deb.append(outs) |
| 91 | else: |
| 92 | print('WARNING: dpkg returns empty!') |
| 93 | |
| 94 | print('Debian packages:') |
| 95 | for pkgname in packages_deb: |
| 96 | print("\t" + pkgname) |
| 97 | print() |
| 98 | print('Modules without debian package (pip or setuptools?):') |
| 99 | for mname, err in modules_err: |
| 100 | print("\t" + mname.ljust(20) + " [" + err.rstrip() +"]") |
| 101 | |
| 102 | parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter) |
Pau Espin Pedrol | 4077569 | 2020-05-05 16:51:51 +0200 | [diff] [blame] | 103 | parser.add_argument('-s', '--skip-feature', dest='skip_features', action='append', |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 104 | help='''All osmo-gsm-tester features not used by the user running the script''') |
| 105 | parser.add_argument('-p', '--distro-packages', dest='distro_packages', action='store_true', |
| 106 | help='Print distro packages installing modules') |
| 107 | parser.add_argument('-v', '--verbose', dest='verbose', action='store_true', |
| 108 | help='Print a lot more information') |
| 109 | args = parser.parse_args() |
| 110 | |
| 111 | skip_obj_modules = skip_features_to_skip_modules(list(args.skip_features or [])) |
| 112 | |
| 113 | print('Skip checking modules: %r' % skip_obj_modules) |
| 114 | |
Pau Espin Pedrol | 1337fb8 | 2020-05-12 13:28:55 +0200 | [diff] [blame] | 115 | rootdir = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..')) |
| 116 | print('Changing workdir dir to %s' % rootdir) |
| 117 | os.chdir(rootdir) |
| 118 | sys.path.insert(0, rootdir) |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 119 | # We need to add it for cross-references between osmo_ms_driver and osmo_gsm_tester to work: |
Pau Espin Pedrol | 1337fb8 | 2020-05-12 13:28:55 +0200 | [diff] [blame] | 120 | sys.path.insert(0, os.path.join(rootdir, 'src/')) |
Pau Espin Pedrol | 045245d | 2020-04-17 19:37:10 +0200 | [diff] [blame] | 121 | import_all_py_in_dir('src/osmo_ms_driver') |
| 122 | import_all_py_in_dir('src/osmo_gsm_tester/core') |
| 123 | import_all_py_in_dir('src/osmo_gsm_tester/obj', skip_obj_modules) |
| 124 | import_all_py_in_dir('src/osmo_gsm_tester') |
| 125 | import_runtime_dependencies() |
| 126 | print('Importing dependencies ok, all installed') |
| 127 | |
| 128 | print('Retreiving list of imported modules...') |
| 129 | modules = get_module_names() |
| 130 | if args.verbose: |
| 131 | for mname, m in modules.items(): |
| 132 | print('%s --> %s' %(mname, m.__file__)) |
| 133 | |
| 134 | if args.distro_packages: |
| 135 | print('Generating distro package list from imported module list...') |
| 136 | print_deb_packages(modules) |