-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
78 lines (58 loc) · 1.67 KB
/
utils.py
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
import os
import subprocess
import sys
def cmssw():
return "CMSSW_VERSION" in os.environ
def shortList(lst):
""" see test_transformation.py"""
s = ""
hyphen = False
l = sorted(set(lst))
for i, fed in enumerate(l):
if not i:
s += "%d" % fed
continue
last = i == len(l) - 1
prevFed = l[i - 1]
if fed == 1 + prevFed:
if last:
s += "-%d" % fed
else:
hyphen = True
continue
if hyphen:
s += "-%d,%d" % (prevFed, fed)
else:
s += ",%d" % fed
hyphen = False
return s
def commandOutput(cmd=""):
return commandOutputFull(cmd="")["stdout"].split()
def commandOutputFull(cmd=""):
p = subprocess.Popen(cmd,
shell=True,
universal_newlines=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout, stderr = p.communicate()
return {"stdout": stdout, "stderr": stderr, "returncode": p.returncode}
def delete(thing):
# free up memory (http://wlav.web.cern.ch/wlav/pyroot/memory.html)
thing.IsA().Destructor(thing)
def bail():
url = "https://root.cern.ch/how/how-use-pyroot-root-python-bindings"
sys.exit("Could not find ROOT.py. See "+url)
def findROOT():
try:
libDir = commandOutput("root-config --libdir")[0]
sys.path.append(libDir)
except IndexError:
bail()
def ROOT():
try:
import ROOT as r
r.PyConfig.IgnoreCommandLineOptions = True
return r
except ImportError:
bail()