babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
delete_treeglobal.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import argparse
4 import glob
5 import os
6 import tempfile
7 import shutil
8 
9 import utilities
10 
11 def singleFileDelete(path):
12  dirname = os.path.dirname(path)
13  prefix = os.path.splitext(os.path.basename(path))[0]+"_TMP_"
14  with tempfile.NamedTemporaryFile(dir=dirname, prefix=prefix, suffix=".root") as tmp:
15  try:
16  with utilities.ROOTFile(path, "read") as orig:
17  tree = orig.Get("tree")
18  if not tree:
19  utilities.ePrint("Could not find tree in "+path+". Skipping.")
20  return
21  with utilities.ROOTFile(tmp.name, "recreate") as outfile:
22  clone = tree.CloneTree(-1, "fast")
23  clone.Write()
24  print "Deleting treeglobal from "+path
25  shutil.copy(tmp.name, path)
27  utilities.ePrint("Could not open "+path+". Skipping.")
28  return
29 
30 def recursiveDelete(file_dir):
31  try:
32  singleFileDelete(file_dir)
33  return
35  pass
36  except IOError as e:
37  #Directory, not file
38  if e.errno != 21:
39  raise
40 
41  for root, dirs, files in os.walk(file_dir):
42  for d in dirs:
43  recursiveDelete(os.path.join(root, d))
44  for f in files:
45  try: singleFileDelete(os.path.join(root, f))
46  except utilities.NonROOTFileError: pass
47 
48 def deleteTreeglobal(in_files):
49  in_files = [ utilities.fullPath(f) for sublist in in_files for f in glob.glob(sublist) ]
50  for file_dir in in_files:
51  recursiveDelete(file_dir)
52 
53 if __name__ == "__main__":
54  parser = argparse.ArgumentParser(description="Deletes treeglobal from provided list of files and directories, recursing through subdirectories.")
55  parser.add_argument("files", nargs="+",
56  help="List of ROOT files from which to remove treeglobal. Directories will be recursively search for ROOT files.")
57  args = parser.parse_args()
58 
59  deleteTreeglobal(args.files)
def recursiveDelete(file_dir)
def deleteTreeglobal(in_files)
def ePrint(args, kwargs)
Definition: utilities.py:44
def singleFileDelete(path)
def fullPath(path)
Definition: utilities.py:34