summaryrefslogtreecommitdiff
path: root/tools/fast-driver.py
diff options
context:
space:
mode:
authorDave Hibberd <d@vehibberd.com>2023-10-16 21:37:27 +0100
committerDave Hibberd <d@vehibberd.com>2023-10-16 21:37:27 +0100
commit91dbf844ad923e3c4bca1252dd104d8fc8ce5475 (patch)
tree93c4db659daf278db034b304e4cd910454504ec1 /tools/fast-driver.py
parent6f19923a918f7fb63a444b0c99950fdbe9d2f74c (diff)
New upstream version 20231016upstream/20231016
Diffstat (limited to 'tools/fast-driver.py')
-rwxr-xr-xtools/fast-driver.py104
1 files changed, 104 insertions, 0 deletions
diff --git a/tools/fast-driver.py b/tools/fast-driver.py
new file mode 100755
index 0000000..669a5e1
--- /dev/null
+++ b/tools/fast-driver.py
@@ -0,0 +1,104 @@
+#!/usr/bin/env Python3
+
+import glob
+import os
+import subprocess
+import sys
+
+
+def parse_import_line(line):
+ pieces = line.split()
+ if line.startswith('from'):
+ base = pieces[1]
+ if 'drivers.' in base:
+ imports = [base.split('.')[-1]]
+ return imports
+ else:
+ imports = pieces[3:]
+ if pieces[-1].endswith(','):
+ raise Exception('Failed to parse multi-line import')
+ return [x.strip().strip(',') for x in imports]
+ elif 'drivers' in line:
+ raise Exception('Unhandled bare import driver line: %s' % line)
+ else:
+ return []
+
+
+def find_imports(of_modules):
+ driver_imports = []
+ for fn in glob.glob('chirp/drivers/*.py'):
+ with open(fn) as f:
+ lines = [ln for ln in f.readlines()
+ if 'import ' in ln and 'drivers' in ln]
+ imports = []
+ for line in lines:
+ imports.extend(parse_import_line(line))
+ if set(imports) & of_modules:
+ driver_imports.append(os.path.splitext(os.path.basename(fn))[0])
+ return driver_imports
+
+
+files = subprocess.check_output(
+ ['git', 'diff', 'origin/master', '--name-only', '.']).decode().split()
+
+# A list of modules and their parent [(chirp/drivers, tk8180)]
+files_by_module = [x.rsplit('/', 1) for x in files if '/' in x and '.py' in x]
+
+# Grab just the drivers
+driver_modules = set([os.path.splitext(os.path.basename(mod))[0]
+ for parent, mod in files_by_module
+ if parent == 'chirp/drivers'])
+
+# Determine which are live drivers that we don't test with these tests
+live_drivers = set()
+for driver in driver_modules:
+ try:
+ with open(os.path.join('chirp', 'drivers', '%s.py' % driver)) as f:
+ content = f.read()
+ except FileNotFoundError:
+ # File must have been removed
+ continue
+ if 'LiveRadio' in content:
+ live_drivers.add(driver)
+
+print('Touched drivers: %s' % ','.join(driver_modules))
+# Find deps before we remove live modules in case there are any changed
+# modules that depend on the bases that have live drivers in them (i.e. icf or
+# kenwood_live)
+deps = find_imports(driver_modules)
+print('Found deps of touched modules: %s' % ','.join(deps))
+# Remove live drivers from driver_modules before we add in the deps of changed
+# modules, as they may imply some shared code (like icf)
+driver_modules -= live_drivers
+driver_modules.update(set(deps))
+
+# Determine if any base modules have been changed that would necessitate
+# running all the driver tests
+exclude_mods = ('chirp/wxui', 'chirp/cli', 'chirp/sources', 'chirp/drivers',
+ 'chirp/locale', 'chirp/share', 'chirp/stock_configs',
+ 'tools')
+base_modules = [mod for parent, mod in files_by_module
+ if parent not in exclude_mods]
+
+if base_modules:
+ print('Base modules touched; running all drivers: %s' % base_modules)
+ driver_exp = []
+elif driver_modules:
+ driver_exp = ' or '.join(f for f in driver_modules)
+else:
+ print('No driver tests necessary for changes in %s' % ','.join(
+ files))
+ sys.exit(0)
+
+args = ['pytest']
+if driver_exp:
+ args += ['-k', driver_exp]
+args += sys.argv[1:]
+print(args)
+rc = subprocess.call(args)
+if rc == 5:
+ # This means no tests were found likely because we generated a bad
+ # tag, so just run the full set
+ print('Running full driver set')
+ rc = subprocess.call(['pytest'] + sys.argv[1:])
+sys.exit(rc)