ra4_stats  0341147a0dc35f80f4e12c6003afb76a38e2ed6e
utils.py
Go to the documentation of this file.
1 import os
2 import subprocess
3 import tempfile
4 
5 import ROOT
6 
7 def full_path(path):
8  return os.path.realpath(os.path.abspath(os.path.expanduser(path)))
9 
10 def cmsenv(path):
11  path = full_path(path)
12  cwd = full_path(os.getcwd())
13  os.chdir(path)
14  with tempfile.TemporaryFile() as cout, open(os.devnull) as cerr:
15  subprocess.check_call(["scramv1","runtime","-sh"],stdout=cout,stderr=cerr)
16  cout.seek(0)
17  for byte_line in cout:
18  line = byte_line.decode("utf-8").strip()
19  if line[0:6] == "unset ":
20  var_list = line.split()[1:]
21  for v in var_list:
22  os.unsetenv(v)
23  elif line[0:7] == "export ":
24  vname, vval = line[7:].rstrip(";").split("=")
25  os.environ[vname] = vval.strip('"')
26  else:
27  raise Exception("Did not understand line: {}".format(line))
28  os.chdir(cwd)
29 
31  def __init__(self, path):
32  self.path = path
33  def __str__(self):
34  return self.path+" is not a ROOT file"
35 
37  def __init__(self, path, mode):
38  self.path = path
39  self.mode = mode
40  def __str__(self):
41  return "Could not open "+self.path+" in "+self.mode+" mode"
42 
44  def __init__(self, path, mode):
45  if os.path.splitext(path)[1] != ".root":
46  raise NonROOTFileError(path)
47  self.path = path
48  self.mode = mode
49  def __enter__(self):
50  self.file = ROOT.TFile(self.path, self.mode)
51  if self.file.IsZombie() or not self.file.IsOpen():
52  raise ROOTOpenError(self.path, self.mode)
53  return self.file
54  def __exit__(self, type, value, traceback):
55  self.file.Close()
56 
def __str__(self)
Definition: utils.py:33
def __init__(self, path, mode)
Definition: utils.py:44
def __init__(self, path)
Definition: utils.py:31
def __exit__(self, type, value, traceback)
Definition: utils.py:54
def cmsenv(path)
Definition: utils.py:10
def __enter__(self)
Definition: utils.py:49
def full_path(path)
Definition: utils.py:7
def __str__(self)
Definition: utils.py:40
def __init__(self, path, mode)
Definition: utils.py:37