Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
launcher.py
Go to the documentation of this file.
1# Module to launch python tutorials.
2# It serves the purpose of enabling batch mode,
3# since PyROOT does not parse anymore by default
4# the command line arguments
5
6import sys, os
7
8if len(sys.argv) < 2:
9 raise RuntimeError('Please specify tutorial file path as only argument of this script')
10
11# Get path to the tutorial file
12file_path = os.path.expanduser(sys.argv[1])
13sys.argv.remove(sys.argv[1])
14
15if os.path.exists(file_path):
16 # Module needs to run as main.
17 # Some tutorials have "if __name__ == '__main__'"
18 module_name = "__main__"
19
20 # Ensure batch mode (some tutorials use graphics)
21 import ROOT
22 ROOT.gROOT.SetBatch(True)
23
24 # Prevent import from generating .pyc files in source directory
25 sys.dont_write_bytecode = True
26
27 # Execute test
28 if sys.version_info >= (3,5):
29 import importlib.util
30 spec = importlib.util.spec_from_file_location(module_name, file_path)
31 module = importlib.util.module_from_spec(spec)
32 sys.modules[module_name] = module
33 spec.loader.exec_module(module)
34 else:
35 import imp
36 imp.load_module(module_name, open(file_path, 'r'), file_path, ('.py','r',1))
37else:
38 raise RuntimeError('Cannot execute test, {} is not a valid tutorial file path'.format(file_path))