CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
8 class TreeAnalyzer(object):
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 
const uint16_t range(const Frame &aFrame)
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
Definition: main.py:1
#define str(s)