forked from score-p/scorep_binding_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__main__.py
More file actions
130 lines (108 loc) · 4.04 KB
/
__main__.py
File metadata and controls
130 lines (108 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import sys
import importlib
import scorep.instrumenter
import scorep.subsystem
def _err_exit(msg):
sys.stderr.write("%s: %s\n" % ("scorep", msg))
sys.exit(1)
def scorep_main(argv=None):
# print(sys.flags)
if argv is None:
argv = sys.argv
scorep_config = []
prog_argv = []
parse_scorep_commands = True
keep_files = False
no_default_threads = False
no_default_compiler = False
no_instrumenter = False
instrumenter_type = "profile"
for elem in argv[1:]:
if parse_scorep_commands:
if elem == "--mpi":
scorep_config.append("--mpp=mpi")
elif elem == "--keep-files":
keep_files = True
elif "--thread=" in elem:
scorep_config.append(elem)
no_default_threads = True
elif elem == "--nocompiler":
scorep_config.append(elem)
no_default_compiler = True
elif elem == "--nopython":
no_instrumenter = True
elif elem == "--noinstrumenter":
no_instrumenter = True
elif "--instrumenter-type" in elem:
param = elem.split("=")
instrumenter_type = param[1]
elif elem[0] == "-":
scorep_config.append(elem)
else:
prog_argv.append(elem)
parse_scorep_commands = False
else:
prog_argv.append(elem)
if not no_default_threads:
scorep_config.append("--thread=pthread")
if not no_default_compiler:
scorep_config.append("--compiler")
if len(prog_argv) == 0:
_err_exit("Did not find a script to run")
if ("SCOREP_PYTHON_BINDINGS_INITALISED" not in os.environ) or (
os.environ["SCOREP_PYTHON_BINDINGS_INITALISED"] != "true"):
scorep.subsystem.init_environment(scorep_config, keep_files)
os.environ["SCOREP_PYTHON_BINDINGS_INITALISED"] = "true"
"""
python -m starts the module as skript. i.e. sys.argv will loke like:
['/home/gocht/Dokumente/code/scorep_python/scorep.py', '--mpi', 'mpi_test.py']
To restart python we need to remove this line, and add `python -m scorep ...` again
"""
new_args = [sys.executable, "-m", "scorep"]
for elem in sys.argv:
if "scorep/__main__.py" in elem:
continue
else:
new_args.append(elem)
os.execve(sys.executable, new_args, os.environ)
else:
scorep.subsystem.reset_pereload()
scorep_bindings = importlib.import_module("scorep.scorep_bindings")
# everything is ready
sys.argv = prog_argv
progname = prog_argv[0]
sys.path[0] = os.path.split(progname)[0]
tracer = scorep.instrumenter.get_instrumenter(scorep_bindings,
not no_instrumenter,
instrumenter_type)
try:
with open(progname) as fp:
code = compile(fp.read(), progname, 'exec')
# try to emulate __main__ namespace as much as possible
globs = {
'__file__': progname,
'__name__': '__main__',
'__package__': None,
'__cached__': None,
}
tracer.runctx(code, globs, globs)
except OSError as err:
_err_exit("Cannot run file %r because: %s" % (sys.argv[0], err))
finally:
scorep.subsystem.clean_up(keep_files)
def main(argv=None):
import traceback
call_stack = traceback.extract_stack()
call_stack_array = traceback.format_list(call_stack)
call_stack_string = ""
for elem in call_stack_array[:-1]:
call_stack_string += elem
_err_exit(
"Someone called scorep.__main__.main(argv).\n"
"This is not supposed to happen, but might be triggered, "
"if your application calls \"sys.modules['__main__'].main\".\n"
"This python stacktrace might be helpfull to find the reason:\n%s" %
call_stack_string)
if __name__ == '__main__':
scorep_main()