Skip to content

Instantly share code, notes, and snippets.

@wimleers
Created August 10, 2011 21:59
Show Gist options
  • Save wimleers/1138393 to your computer and use it in GitHub Desktop.
Save wimleers/1138393 to your computer and use it in GitHub Desktop.
Calculates the size of the PHP codebase of all sites.
#!/usr/bin/env python
# Calculates the size of the PHP codebase of all sites.
import os
import optparse
import subprocess
def calculate(http_root_dir):
sites = list_sites(http_root_dir)
sites_with_sizes = []
total_size = 0
for site in sites:
size = int(get_php_size(site))
sites_with_sizes.append([site, size])
total_size += size
sites_with_sizes.append(["TOTAL", total_size])
# Sort descendingly on the second item in each element: the size.
sites_with_sizes.sort(key=lambda a: a[1], reverse=True)
# Output.
for (site, size) in sites_with_sizes:
print "%-45s %5.2f MiB" % (site, size / 1024.0 / 1024.0)
def list_sites(http_root_dir):
(stdout, stderr) = cmd('find %s -mindepth 1 -maxdepth 1 -type d -not -iname "%s"' % (http_root_dir, "aaaaaa"))
sites = stdout.split("\n")
return sites
def get_php_size(dir):
"""get size in bytes of all PHP code in this directory"""
# Fast.
(stdout, stderr) = cmd('find -H %s \( -iname "*.inc" -or -iname "*.php" -or -iname "*.module" \) -exec du -bcs "{}" + | tail -1 | awk \'{ print $1 }\' 2>&1 | grep -v "permission denied"' % (dir))
# Slow.
# (stdout, stderr) = cmd('find -H %s \( -iname "*.inc" -or -iname "*.php" -or -iname "*.module" \) -exec echo -n -e {}"\\0" \; | du -bcs --files0-from=- | tail -1 | awk \'{ print $1 }\' 2>&1 | grep -v "permission denied"' % (dir))
if len(stdout) == 0:
return 0
else:
return stdout
def cmd(command):
"""run a command and get (stdout, stderr) back"""
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(stdout, stderr) = p.communicate()
(stdout, stderr) = (stdout.rstrip(), stderr.rstrip())
return (stdout, stderr)
def main():
p = optparse.OptionParser(description="Calculates the size of the PHP codebase of all sites.")
options, arguments = p.parse_args()
if len(arguments) == 1:
calculate(arguments[0])
else:
p.print_help()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment