babymaker  e95a6a9342d4604277fe7cc6149b6b5b24447d89
count_entries.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import argparse
4 import os
5 import glob
6 import locale
7 
8 import ROOT
9 
10 from utilities import *
11 
12 def countFile(cut, path, tree_name, counts):
13  counts[path] = (0,0)
14  try:
15  with ROOTFile(path, "read") as f:
16  counts[path] = (0,1)
17  tree = f.Get(tree_name)
18  if not tree: return
19  if not cut:
20  counts[path] = (tree.GetEntries(), 1)
21  else:
22  counts[path] = (tree.GetEntries(cut), 1)
23  except NonROOTFileError:
24  pass
25  except ROOTOpenError:
26  ePrint("Could not open "+path+". Assuming 0 entries.")
27  counts[path] = (0,1)
28 
29 def countRecursive(cut, path, tree, exclusive, counts):
30  if os.path.isfile(path):
31  countFile(cut, path, tree, counts)
32  return
33 
34  for root, dirs, files in os.walk(path, topdown=False):
35  num_entries = 0
36  num_files = 0
37  for f in files:
38  p = fullPath(os.path.join(root, f))
39  countFile(cut, p, tree, counts)
40  num_entries += counts[p][0]
41  num_files += counts[p][1]
42  for d in dirs:
43  if os.path.islink(os.path.join(root, d)): continue
44  if not exclusive:
45  p = fullPath(os.path.join(root, d))
46  num_entries += counts[p][0]
47  num_files += counts[p][1]
48  counts[fullPath(root)] = (num_entries, num_files)
49 
50 def printCounts(path, counts, verbose):
51  for root, dirs, files in os.walk(path):
52  p = fullPath(root)
53  level = root.replace(path, "").count(os.sep)
54  indent = " "*2*level
55  if counts[p][1]>0 or root==path or verbose:
56  print(("{}"+Term.BOLD+Term.BLUE+"{}"+Term.END+Term.END+" [{} entries in {} ROOT files]")
57  .format(indent, os.path.basename(root),
58  locale.format("%d", counts[p][0], grouping=True),
59  locale.format("%d", counts[p][1], grouping=True)))
60  subindent = " "*2*(level+1)
61  for f in files:
62  p = fullPath(os.path.join(root, f))
63  if counts[p][1]>0 and verbose:
64  print(("{}"+Term.BOLD+Term.GREEN+"{}"+Term.END+Term.END+" [{} entries]")
65  .format(subindent, f,
66  locale.format("%d", counts[p][0], grouping=True)))
67 
68 def countEntries(cut, file_dirs, tree, verbose, exclusive):
69  locale.setlocale(locale.LC_ALL, "en_US")
70 
71  file_dirs = [ fullPath(f) for sublist in file_dirs for f in glob.glob(sublist) ]
72  counts = dict()
73 
74  for path in file_dirs:
75  countRecursive(cut, path, tree, exclusive, counts)
76  printCounts(path, counts, verbose)
77 
78 if __name__ == "__main__":
79  parser = argparse.ArgumentParser(description="Counts the number of entries in ROOT files and directories",
80  formatter_class = argparse.ArgumentDefaultsHelpFormatter)
81  parser.add_argument("-c","--cut", default=None,
82  help="Count only arguments passing provided cut")
83  parser.add_argument("-t","--tree", default="tree",
84  help="Name of tree for which to count entries")
85  parser.add_argument("-e","--exclusive", action="store_true",
86  help="Count entries only in files in each directory, but not in its subdirectories.")
87  parser.add_argument("-v","--verbose", action="store_true",
88  help="Print entries for individual files.")
89  parser.add_argument("files", default=["."], nargs="*",
90  help="List of files and directories in which to count root files")
91  args = parser.parse_args()
92 
93  countEntries(args.cut, args.files, args.tree, args.verbose, args.exclusive)
def countEntries(cut, file_dirs, tree, verbose, exclusive)
def fullPath(path)
Definition: lock_files.py:10
def countFile(cut, path, tree_name, counts)
def printCounts(path, counts, verbose)
def ePrint(args, kwargs)
Definition: utilities.py:44
def countRecursive(cut, path, tree, exclusive, counts)