Created
January 4, 2017 12:13
-
-
Save jstrube/80eb96beea3654020c945c1b7e6afc4b to your computer and use it in GitHub Desktop.
ILCDIRAC script to print the usage at a given StorageElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" Prints the storage usage at a given storage element. | |
Recursively traverses the starting path up to a given depth (default 2) | |
""" | |
from DIRAC.Resources.Catalog.FileCatalogClient import FileCatalogClient | |
from DIRAC.Core.Base import Script | |
import sys | |
import Queue | |
import threading | |
import subprocess | |
import argparse | |
from os import path | |
import re | |
Script.initialize() | |
fileCatalog = FileCatalogClient() | |
def sizeof_fmt(num, suffix='B'): | |
""" from http://stackoverflow.com/questions/1094841/reusable-library-to-get-human-readable-version-of-file-size | |
""" | |
for unit in ['','Ki','Mi','Gi','Ti','Pi','Ei','Zi']: | |
if abs(num) < 1024.0: | |
return "%3.1f %s%s" % (num, unit, suffix) | |
num /= 1024.0 | |
return "%.1f %s%s" % (num, 'Yi', suffix) | |
def getDirSize(dirname, seName, depth, maxDepth=2): | |
if depth >= maxDepth: | |
return | |
diracDir = fileCatalog.listDirectory(dirname, verbose=True) | |
if not diracDir['OK']: | |
print 'Could not get contents of', dirname | |
sys.exit(-1) | |
if dirname in diracDir['Value']['Failed']: | |
print 'Error! Dirac reports:', | |
print diracDir['Value']['Failed'][dirname] | |
sys.exit(-2) | |
contents = diracDir['Value']['Successful'][dirname] | |
for c in contents['SubDirs']: | |
size = fileCatalog.getDirectorySize(c, True) | |
if not size["OK"]: | |
print "Could not get directory size" | |
sys.exit(-3) | |
for s, key in size["Value"]["Successful"].items(): | |
dirsize = key["PhysicalSize"].get(seName, {"Size": 0})["Size"] | |
if dirsize > 0: | |
print "\t"*depth, s, sizeof_fmt(dirsize) | |
getDirSize(s, seName, depth+1) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Access a directory in the dirac file catalog') | |
parser.add_argument('dirname', help='A directory in the DIRAC file catalog') | |
parser.add_argument('storage', help='The name of a DIRAC storage element') | |
args = parser.parse_args() | |
if sys.argv[0] == 'dirac-get': | |
args.download = true | |
# we only allow one directory on the command line | |
dirname = path.normpath(args.dirname) | |
getDirSize(dirname, args.storage, 0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment