CMS 3D CMS Logo

storeTreeInfo.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 from __future__ import print_function
4 from builtins import range
5 import os, sys, stat
6 from operator import itemgetter
7 
9 
10  def __init__(self, outFileName):
11  self.dirSizes = {}
12  self.fileSizes = {}
13  self.outFileName = outFileName
14  print("going to write to:",self.outFileName)
15 
16  def analyzePath(self, dirIn) :
17 
18  for (path, dirs, files) in os.walk(dirIn):
19 
20  if 'CVS' in path: continue
21  if '.glimpse_' in path: continue
22  if 'Configuration/PyReleaseValidation/data/run/' in path: continue
23 
24  for file in files:
25  if '.glimpse_index' in file: continue
26  fileName = os.path.join(path, file)
27  fileSize = os.path.getsize(fileName)
28  if path in self.dirSizes.keys() :
29  self.dirSizes[path] += fileSize
30  else:
31  self.dirSizes[path] = fileSize
32  if os.path.isfile(fileName):
33  self.fileSizes[fileName] = fileSize
34 
35  try:
36  import json
37  jsonFileName = self.outFileName
38  jsonFile = open(jsonFileName, 'w')
39  json.dump([os.path.abspath(dirIn), self.dirSizes, self.fileSizes], jsonFile)
40  jsonFile.close()
41  print('treeInfo info written to ', jsonFileName)
42  except Exception as e:
43  print("error writing json file:", str(e))
44 
45  try:
46  import pickle
47  pklFileName = self.outFileName.replace('.json','.pkl')
48  pickle.dump([os.path.abspath(dirIn), self.dirSizes, self.fileSizes], open(pklFileName, 'w') )
49  print('treeInfo info written to ', pklFileName)
50  except Exception as e:
51  print("error writing pkl file:", str(e))
52 
53  def show(self):
54 
55  # for p,s in self.dirSizes.items():
56  # print p, s
57 
58  topDirs = sorted(self.dirSizes.items() , key=itemgetter(1), reverse=True)
59  topFiles = sorted(self.fileSizes.items(), key=itemgetter(1), reverse=True)
60 
61  emptyFiles = []
62  for pair in topFiles:
63  p, s = pair
64  if s == 0:
65  emptyFiles.append(p)
66  print("found ",len(emptyFiles),"empty files. ")
67 
68  print("found ", len(self.dirSizes), 'directories, top 10 are:')
69  for i in range(10):
70  print(topDirs[i])
71 
72  print("found ", len(self.fileSizes), 'files, top 10 are:')
73  for i in range(10):
74  print(topFiles[i])
75 
76 
77 def main():
78 
79  import getopt
80 
81  try:
82  opts, args = getopt.getopt(sys.argv[1:], "c:o:", ['checkDir=', 'outFile='])
83 
84  checkDir = '.'
85  outFile = None
86  for opt, arg in opts :
87 
88  if opt in ('-c', "--checkDir", ):
89  checkDir = arg
90 
91  if opt in ('-o', "--outFile", ):
92  outFile = arg
93 
94  ta = TreeAnalyzer(outFile)
95  ta.analyzePath(checkDir)
96  ta.show()
97 
98  except getopt.GetoptError as e:
99  print("unknown option", str(e))
100  sys.exit(2)
101 
102 if __name__ == '__main__':
103  main()
104 
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
storeTreeInfo.TreeAnalyzer.__init__
def __init__(self, outFileName)
Definition: storeTreeInfo.py:10
storeTreeInfo.TreeAnalyzer.fileSizes
fileSizes
Definition: storeTreeInfo.py:12
storeTreeInfo.TreeAnalyzer.analyzePath
def analyzePath(self, dirIn)
Definition: storeTreeInfo.py:16
relativeConstraints.keys
keys
Definition: relativeConstraints.py:89
storeTreeInfo.TreeAnalyzer.dirSizes
dirSizes
Definition: storeTreeInfo.py:11
mps_monitormerge.items
list items
Definition: mps_monitormerge.py:29
str
#define str(s)
Definition: TestProcessor.cc:53
print
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:46
storeTreeInfo.TreeAnalyzer
Definition: storeTreeInfo.py:8
main
Definition: main.py:1
storeTreeInfo.main
def main()
Definition: storeTreeInfo.py:77
storeTreeInfo.TreeAnalyzer.show
def show(self)
Definition: storeTreeInfo.py:53
storeTreeInfo.TreeAnalyzer.outFileName
outFileName
Definition: storeTreeInfo.py:13
python.rootplot.root2matplotlib.replace
def replace(string, replacements)
Definition: root2matplotlib.py:444