diff --git a/file_utils/README b/file_utils/README index 44d940d..897d1e4 100644 --- a/file_utils/README +++ b/file_utils/README @@ -16,6 +16,10 @@ called a bin. By default, dirpacker displays a list of bins and the files they would contain, along with the size of each file and some statistics. +With the option --machine-readable, this list will be printed in a +machine-readable format: each line contains a bin's name, then a +tabulation, then a file that belongs to this bin. + With the option --move, a directory will be created for each volume and the files will be moved to the corresponding volume. diff --git a/file_utils/dirpacker.py b/file_utils/dirpacker.py index 821be08..d030754 100755 --- a/file_utils/dirpacker.py +++ b/file_utils/dirpacker.py @@ -53,12 +53,18 @@ class Bin: def print(self): """Displays the contents of the bin. """ - print("\n### {} ###\nList of files:".format(self.name())) + if not options.machine_readable: + print("\n### {} ###\nList of files:".format(self.name())) for filename, size in sorted(self.files.items()): - print("{:12.2f} MiB\t{}".format(size, filename)) - sizefree = options.maxbinsize - self.size - print("This bin's size: {:.2f} MiB".format(self.size)) - print("Unused: {:.2f} MiB".format(sizefree)) + if options.machine_readable: + print(self.name(), end="") + else: + print("{:12.2f} MiB".format(size)) + print("\t{}".format(filename)) + if not options.machine_readable: + sizefree = options.maxbinsize - self.size + print("This bin's size: {:.2f} MiB".format(self.size)) + print("Unused: {:.2f} MiB".format(sizefree)) def name(self): """Returns the bin's name' @@ -136,6 +142,9 @@ arg_parser.add_argument("--prefix", action="store", default="bin_", help="prefix of a bin's name (default: \"bin_\")") arg_parser.add_argument("--move", action="store_true", help="move input to per-volume directories") +arg_parser.add_argument("-m", "--machine-readable", action="store_true", + help="print the list of volumes in a machine-readable" + " format; implies --quiet") arg_parser.add_argument("-v", "--verbose", action="store_true", default=True, help="be verbose (this is the default)") arg_parser.add_argument("-q", "--quiet", action="store_false", dest="verbose", @@ -144,6 +153,9 @@ arg_parser.add_argument("filenames", metavar="file", nargs="+", help="files or directories to pack") options = arg_parser.parse_args() +if options.machine_readable: + options.verbose = False + ### Preliminary statistics ### @@ -198,7 +210,7 @@ Actual unused space over the {} bins created: {:.2f} MiB""" # Print the contents of the bins -if options.verbose: +if options.verbose or options.machine_readable: for b in bins: b.print()