"Fossies" - the Fresh Open Source Software Archive

Member "fslint-2.46/fslint/fstool/dupwaste" (2 Feb 2017, 2223 Bytes) of package /linux/privat/fslint-2.46.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "dupwaste": 2.44_vs_2.46.

    1 #!/usr/bin/env python2
    2 
    3 # This script shows the total space wasted by duplicate files
    4 # as reported by findup (pipe `findup --summary`to this).
    5 #
    6 # This script can take in two arguments.
    7 # --help prints the command usage.
    8 # --human calculates the output size in KB, MB, GB, or TB.
    9 
   10 import sys
   11 import getopt
   12 import locale
   13 
   14 def sizes():
   15     for line in sys.stdin:
   16         (num, mul, size, rest) = line.split(None,3)
   17         num=int(num)
   18         size=int(size)
   19         num -= 1
   20         size *= num
   21         yield size
   22 
   23 def human_num(num, divisor=1, power=""):
   24     num=float(num)
   25     if divisor == 1:
   26         return locale.format("%.f",num,1)
   27     elif divisor == 1000:
   28         powers=[" ","K","M","G","T","P"]
   29     elif divisor == 1024:
   30         powers=["  ","Ki","Mi","Gi","Ti","Pi"]
   31     else:
   32         raise ValueError("Invalid divisor")
   33     if not power: power=powers[0]
   34     while num >= 1000: #4 digits
   35         num /= divisor
   36         power=powers[powers.index(power)+1]
   37     if power.strip():
   38         num = locale.format("%6.1f",num,1)
   39     else:
   40         num = locale.format("%4.f",num,1)+'  '
   41     return "%s%s" % (num,power)
   42 
   43 def Usage():
   44     print "Print the total wasted space given output from `findup --summary`"
   45     print
   46     print "--human  print the total in human readable form."
   47     print "--help   print this information."
   48 
   49 
   50 # The start of the program
   51 # Catching arguments in the order of importance
   52 #
   53 # The help should be the first and only argument parsed and run even if
   54 # the user wants to pass in multiple options. As long as one of these strings
   55 # are passed in, the help function runs.
   56 #
   57 # Using the same function as found in fslint-gui just to maintain code
   58 # similarity throughout the project as best I can.
   59 try:
   60     lOpts, lArgs = getopt.getopt(sys.argv[1:], "", ["help","human"])
   61     if ("--help","") in lOpts:
   62         Usage()
   63         sys.exit(None)
   64     # See the resize function for more details about the output.
   65     elif ("--human","") in lOpts:
   66         print "Total wasted space: %sB" % human_num(sum(sizes()), divisor=1024)
   67     # No options get the default bytes.
   68     else:
   69         print "Total wasted space: %d bytes" % sum(sizes())
   70 except getopt.error, msg:
   71     print msg
   72     Usage()
   73     sys.exit(1)