blob: 61d08b0c9d91eb24b5bdc3f893c9090c4d10e66a [file] [log] [blame]
Neels Hofmeyrdae3d3c2017-03-28 12:16:58 +02001#!/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 Hofmeyrdae3d3c2017-03-28 12:16:58 +02006
Pau Espin Pedrol045245d2020-04-17 19:37:10 +02007
8import os
9import sys
10import argparse
11import pprint
12import subprocess
13
14feature_module_map = {
Pau Espin Pedrol40775692020-05-05 16:51:51 +020015 '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 Pedrol045245d2020-04-17 19:37:10 +020020}
21
22def skip_features_to_skip_modules(skip_features):
23 skip_obj_modules = []
24
25 for skip_feature in skip_features:
Pau Espin Pedrol40775692020-05-05 16:51:51 +020026 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 Pedrol045245d2020-04-17 19:37:10 +020031 return skip_obj_modules
32
33def 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
38def import_all_py_in_dir(rel_path, skip_modules=[]):
Pau Espin Pedrol1337fb82020-05-12 13:28:55 +020039 selfdir = os.getcwd()
Pau Espin Pedrol045245d2020-04-17 19:37:10 +020040 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
61def 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
76def 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
102parser = argparse.ArgumentParser(epilog=__doc__, formatter_class=argparse.RawTextHelpFormatter)
Pau Espin Pedrol40775692020-05-05 16:51:51 +0200103parser.add_argument('-s', '--skip-feature', dest='skip_features', action='append',
Pau Espin Pedrol045245d2020-04-17 19:37:10 +0200104 help='''All osmo-gsm-tester features not used by the user running the script''')
105parser.add_argument('-p', '--distro-packages', dest='distro_packages', action='store_true',
106 help='Print distro packages installing modules')
107parser.add_argument('-v', '--verbose', dest='verbose', action='store_true',
108 help='Print a lot more information')
109args = parser.parse_args()
110
111skip_obj_modules = skip_features_to_skip_modules(list(args.skip_features or []))
112
113print('Skip checking modules: %r' % skip_obj_modules)
114
Pau Espin Pedrol1337fb82020-05-12 13:28:55 +0200115rootdir = os.path.realpath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
116print('Changing workdir dir to %s' % rootdir)
117os.chdir(rootdir)
118sys.path.insert(0, rootdir)
Pau Espin Pedrol045245d2020-04-17 19:37:10 +0200119# We need to add it for cross-references between osmo_ms_driver and osmo_gsm_tester to work:
Pau Espin Pedrol1337fb82020-05-12 13:28:55 +0200120sys.path.insert(0, os.path.join(rootdir, 'src/'))
Pau Espin Pedrol045245d2020-04-17 19:37:10 +0200121import_all_py_in_dir('src/osmo_ms_driver')
122import_all_py_in_dir('src/osmo_gsm_tester/core')
123import_all_py_in_dir('src/osmo_gsm_tester/obj', skip_obj_modules)
124import_all_py_in_dir('src/osmo_gsm_tester')
125import_runtime_dependencies()
126print('Importing dependencies ok, all installed')
127
128print('Retreiving list of imported modules...')
129modules = get_module_names()
130if args.verbose:
131 for mname, m in modules.items():
132 print('%s --> %s' %(mname, m.__file__))
133
134if args.distro_packages:
135 print('Generating distro package list from imported module list...')
136 print_deb_packages(modules)