1 """
2 Application-class that implements pyFoamBinarySize.py
3 """
4 from optparse import OptionGroup
5
6 from .PyFoamApplication import PyFoamApplication
7
8 from PyFoam.ThirdParty.six import print_
9
10 from PyFoam.Basics.Utilities import diskUsage,humanReadableSize
11
12 import PyFoam.FoamInformation as FI
13
14 from os import listdir,path
15
17 - def __init__(self,
18 args=None,
19 **kwargs):
20 description="""\
21 Goes through the OpenFOAM-installation and records the size of all the binary
22 files (linked as well as object files) and reports them for each compiliation
23 option separately (Debug/Opt, compilers)
24
25 This should provide the user with an overview how much disk space each variation
26 of the binaries needs.
27
28 The reported OpenfOAM-installation can be selected using the usual Foam-version
29 switching.
30 """
31 examples="""\
32 %prog
33
34 List the disk space used by the current OpenFOAM-installation
35
36 %prog --foamVersion=1.7.x
37
38 List the disk space used by binaries in OpenFOAM-1.7.x
39
40 %prog --all-installations
41
42 List the disk space used by all the available installations
43
44 %prog --all-installations --follow-symlinked-installations
45
46 Also follow symlinked installations (this may count binaries twice)"""
47
48 PyFoamApplication.__init__(self,
49 args=args,
50 description=description,
51 examples=examples,
52 usage="%prog <caseDirectory>",
53 interspersed=True,
54 changeVersion=True,
55 nr=0,
56 exactNr=True,
57 **kwargs)
58
60 what=OptionGroup(self.parser,
61 "What",
62 "What should be reported")
63 self.parser.add_option_group(what)
64
65 what.add_option("--all-installations",
66 action="store_true",
67 dest="allInstallations",
68 default=False,
69 help="Report the disk usage for all installations (this may take quite a while)")
70 what.add_option("--follow-symlinked-installations",
71 action="store_true",
72 dest="symlinkInstallations",
73 default=False,
74 help="Also count the installation if it is a symlink (otherwise only report the original installation and skip it)")
75
77 dName=path.basename(dPath)
78 if dName[0]==".":
79 return
80 elif dName in ["lnInclude","Doxygen"]:
81 return
82 elif dName in ["Make","platform","bin"]:
83 for f in listdir(dPath):
84 if f[0]==".":
85 continue
86 nPath=path.join(dPath,f)
87 if path.isdir(nPath):
88 isBin=False
89 for end in ["Opt","Debug","Prof"]:
90 if f.find(end)>0 and (f.find(end)+len(end))==len(f):
91 isBin=True
92 if isBin:
93 sz=diskUsage(nPath)
94 try:
95 usages[f]+=sz
96 except KeyError:
97 usages[f]=sz
98
99 else:
100 try:
101 for f in listdir(dPath):
102 nPath=path.join(dPath,f)
103 if path.isdir(nPath) and not path.islink(nPath):
104 self.scanDir(nPath,usages)
105 except OSError:
106 self.warning("Can't process",dPath)
107
109 """Report the usages of a OpenFOAM-installation"""
110
111 print_("\nScanning",fName)
112 if path.islink(fName):
113 print_("Symlinked to",path.realpath(fName))
114 if not self.opts.symlinkInstallations:
115 print_("Skipping symlinked installation")
116 return 0
117 usages={}
118 self.scanDir(fName,usages)
119 if len(usages)>0:
120 nameLength=max([len(k) for k in usages.keys()])
121 sizeLength=max([len(str(k)) for k in usages.values()])
122 formatString=" %%%ds - %%%dd (%%s)" % (nameLength,sizeLength)
123 total=0
124 for k in sorted(usages.keys()):
125 v=usages[k]
126 total+=v
127 print_(formatString % (k,v,humanReadableSize(v)))
128 print_("Sum of binaries",humanReadableSize(total))
129 return total
130 else:
131 print_(" No binaries found")
132 return 0
133
144