Skip to content

Commit 4f64498

Browse files
authored
Merge pull request #122 from Flamefire/verboseAndTypos
Add --verbose switch and fix some style issues and typos
2 parents c1073fe + 7880fba commit 4f64498

4 files changed

Lines changed: 60 additions & 33 deletions

File tree

scorep/__main__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
import scorep.instrumenter
55
import scorep.subsystem
6+
from scorep.helper import print_err
67

78

89
def _err_exit(msg):
9-
sys.stderr.write("%s: %s\n" % ("scorep", msg))
10+
print_err("scorep: " + msg)
1011
sys.exit(1)
1112

1213

@@ -19,17 +20,22 @@ def scorep_main(argv=None):
1920
parse_scorep_commands = True
2021

2122
keep_files = False
23+
verbose = False
2224
no_default_threads = False
2325
no_default_compiler = False
2426
no_instrumenter = False
2527
instrumenter_type = "profile"
2628

2729
for elem in argv[1:]:
2830
if parse_scorep_commands:
29-
if elem == "--mpi":
31+
if elem == "--":
32+
parse_scorep_commands = False
33+
elif elem == "--mpi":
3034
scorep_config.append("--mpp=mpi")
3135
elif elem == "--keep-files":
3236
keep_files = True
37+
elif elem == "--verbose" or elem == '-v':
38+
verbose = True
3339
elif "--thread=" in elem:
3440
scorep_config.append(elem)
3541
no_default_threads = True
@@ -61,7 +67,7 @@ def scorep_main(argv=None):
6167
_err_exit("Did not find a script to run")
6268

6369
if os.environ.get("SCOREP_PYTHON_BINDINGS_INITIALISED") != "true":
64-
scorep.subsystem.init_environment(scorep_config, keep_files)
70+
scorep.subsystem.init_environment(scorep_config, keep_files, verbose)
6571
os.environ["SCOREP_PYTHON_BINDINGS_INITIALISED"] = "true"
6672
"""
6773
python -m starts the module as skript. i.e. sys.argv will loke like:

scorep/helper.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import re
55

66

7+
def print_err(*args):
8+
"""Print to stderr"""
9+
sys.stderr.write(' '.join(map(str, args)) + '\n')
10+
11+
712
def call(arguments):
813
"""
914
return a triple with (returncode, stdout, stderr) from the call to subprocess
1015
"""
11-
result = ()
1216
if sys.version_info > (3, 5):
1317
out = subprocess.run(
1418
arguments,
@@ -74,15 +78,15 @@ def add_to_ld_library_path(path):
7478
os.environ["LD_LIBRARY_PATH"] = ':'.join([path] + library_paths)
7579

7680

77-
def generate_compile_deps(config=[]):
81+
def generate_compile_deps(config):
7882
"""
7983
Generates the data needed for compilation.
8084
"""
8185

82-
scorep_config = ["scorep-config"] + config + ["--user"]
86+
scorep_config = ["scorep-config"] + config
8387

84-
(retrun_code, _, _) = call(scorep_config)
85-
if retrun_code != 0:
88+
(return_code, _, _) = call(scorep_config)
89+
if return_code != 0:
8690
raise ValueError(
8791
"given config {} is not supported".format(config))
8892

scorep/subsystem.py

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
import os
2-
import sys
32
import distutils.ccompiler
43
import tempfile
54
import shutil
65

76
import scorep.helper
7+
from scorep.helper import print_err
8+
9+
10+
def _print_info(msg):
11+
"""Print an info message with a prefix"""
12+
print_err("scorep: " + msg)
813

914

1015
def generate_subsystem_lib_name():
@@ -24,20 +29,19 @@ def generate_ld_preload(scorep_config):
2429
@return ld_preload string which needs to be passed to LD_PRELOAD
2530
"""
2631

27-
(_, preload, _) = scorep.helper.call(
28-
["scorep-config"] + scorep_config + ["--user", "--preload-libs"])
29-
return preload
32+
(_, preload, _) = scorep.helper.call(["scorep-config"] + scorep_config + ["--preload-libs"])
33+
return preload.strip()
3034

3135

32-
def generate_subsystem_code(config=[]):
36+
def generate_subsystem_code(config):
3337
"""
3438
Generates the data needed to be preloaded.
3539
"""
3640

37-
scorep_config = ["scorep-config"] + config + ["--user"]
41+
scorep_config = ["scorep-config"] + config
3842

39-
(retrun_code, _, _) = scorep.helper.call(scorep_config)
40-
if retrun_code != 0:
43+
(return_code, _, _) = scorep.helper.call(scorep_config)
44+
if return_code != 0:
4145
raise ValueError(
4246
"given config {} is not supported".format(scorep_config))
4347
(_, scorep_adapter_init, _) = scorep.helper.call(
@@ -75,9 +79,7 @@ def generate(scorep_config, keep_files=False):
7579

7680
temp_dir = tempfile.mkdtemp(prefix="scorep.")
7781
if keep_files:
78-
sys.stderr.write(
79-
"Score-P files are keept at: {}\n".format(temp_dir))
80-
sys.stderr.flush()
82+
_print_info("Score-P files are kept at: " + temp_dir)
8183

8284
with open(temp_dir + "/scorep_init.c", "w") as f:
8385
f.write(scorep_adapter_init)
@@ -99,35 +101,50 @@ def generate(scorep_config, keep_files=False):
99101
return(subsystem_lib_name, temp_dir)
100102

101103

102-
def init_environment(scorep_config=[], keep_files=False):
104+
def init_environment(scorep_config, keep_files=False, verbose=False):
103105
"""
104-
Set the inital needed environmet variables, to get everythin up an running.
105-
As a few variables interact with LD env vars, the programms needs to be restarted after this.
106+
Set the inital needed environment variables, to get everything up an running.
107+
As a few variables interact with LD env vars, the program needs to be restarted after this.
106108
107109
@param scorep_config configuration flags for score-p
108110
@param keep_files whether to keep the generated files, or not.
111+
@param verbose Set to True to output information about config used and environment variables set.
109112
"""
110113

111114
if "libscorep" in os.environ.get("LD_PRELOAD", ""):
112-
raise RuntimeError(
113-
"Score-P is already loaded. This should not happen at this point")
115+
raise RuntimeError("Score-P is already loaded. This should not happen at this point")
116+
117+
if "--user" not in scorep_config:
118+
scorep_config.append("--user")
114119

115-
subsystem_lib_name, temp_dir = scorep.subsystem.generate(
116-
scorep_config, keep_files)
120+
if verbose:
121+
_print_info("Score-P config: %s" % scorep_config)
122+
123+
old_env = os.environ.copy()
124+
125+
subsystem_lib_name, temp_dir = generate(scorep_config, keep_files)
117126
scorep_ld_preload = generate_ld_preload(scorep_config)
118127

119128
scorep.helper.add_to_ld_library_path(temp_dir)
120129

121130
preload_str = scorep_ld_preload + " " + subsystem_lib_name
122-
if "LD_PRELOAD" in os.environ:
123-
sys.stderr.write(
124-
"LD_PRELOAD is already specified. If Score-P is already loaded this might lead to errors.")
131+
if os.environ.get("LD_PRELOAD"):
132+
print_err("LD_PRELOAD is already specified. If Score-P is already loaded this might lead to errors.")
125133
preload_str = os.environ["LD_PRELOAD"] + " " + preload_str
126134
os.environ["SCOREP_LD_PRELOAD_BACKUP"] = os.environ["LD_PRELOAD"]
127135
else:
128136
os.environ["SCOREP_LD_PRELOAD_BACKUP"] = ""
129137
os.environ["LD_PRELOAD"] = preload_str
130138

139+
if verbose:
140+
for var in ("LD_LIBRARY_PATH", "LD_PRELOAD"):
141+
# Shorten the setting to e.g.: FOO=new:$FOO
142+
old_val = old_env.get(var)
143+
new_val = os.environ[var]
144+
if old_val:
145+
new_val = new_val.replace(old_val, '$' + var)
146+
_print_info('%s="%s"' % (var, new_val))
147+
131148

132149
def reset_preload():
133150
"""

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
if "gcc" in check_compiler:
1717
gcc_plugin = scorep.helper.get_scorep_config("GCC plug-in support:")
1818
if not ("yes" in gcc_plugin):
19-
raise RuntimeError(
20-
"Score-P uses GCC but is not build with GCC Compiler Plugin. GCC plug-in support is:\n{}".format(gcc_plugin))
19+
raise RuntimeError("Score-P uses GCC but is not build with GCC Compiler Plugin. "
20+
"GCC plug-in support is:\n{}".format(gcc_plugin))
2121

2222

2323
cmodules = []
24-
(include, _, _, _, _) = scorep.helper.generate_compile_deps()
24+
(include, _, _, _, _) = scorep.helper.generate_compile_deps([])
2525
src_folder = os.path.abspath('src')
2626
include += [src_folder]
2727
sources = ['src/methods.cpp', 'src/scorep_bindings.cpp', 'src/scorepy/events.cpp']
@@ -42,7 +42,7 @@
4242
setup(
4343
name='scorep',
4444
version=scorep._version.__version__,
45-
description='This is a scorep tracing package for python',
45+
description='This is a Score-P tracing package for python',
4646
author='Andreas Gocht',
4747
author_email='andreas.gocht@tu-dresden.de',
4848
url='https://github.com/score-p/scorep_binding_python',

0 commit comments

Comments
 (0)