138 lines
4.8 KiB
Python
Executable File
138 lines
4.8 KiB
Python
Executable File
#!/usr/bin/python3
|
|
#
|
|
# dirpacker.py, Copyright © 2014 Matteo Cypriani <mcy@lm7.fr>
|
|
#
|
|
########################################################################
|
|
# This program is licensed under the terms of the Expat license.
|
|
#
|
|
# Permission is hereby granted, free of charge, to any person obtaining
|
|
# a copy of this software and associated documentation files (the
|
|
# "Software"), to deal in the Software without restriction, including
|
|
# without limitation the rights to use, copy, modify, merge, publish,
|
|
# distribute, sublicense, and/or sell copies of the Software, and to
|
|
# permit persons to whom the Software is furnished to do so, subject to
|
|
# the following conditions:
|
|
#
|
|
# The above copyright notice and this permission notice shall be
|
|
# included in all copies or substantial portions of the Software.
|
|
#
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
########################################################################
|
|
#
|
|
# This script groups input files by volumes of a given size. It is loosely
|
|
# inspired by datapacker but is able to work on directories rather than regular
|
|
# files only.
|
|
|
|
|
|
import argparse
|
|
import os
|
|
from collections import defaultdict
|
|
import math
|
|
|
|
|
|
def du(basepath):
|
|
""" Returns the size of the file of directory `basepath`, in MiB.
|
|
"""
|
|
size = os.path.getsize(basepath)
|
|
for root, dirs, files in os.walk(basepath):
|
|
for name in files + dirs:
|
|
fullname = os.path.join(root, name)
|
|
if not os.path.exists(fullname):
|
|
continue
|
|
size += os.path.getsize(fullname)
|
|
return size / 1024 / 1024
|
|
|
|
|
|
def create_bin(binnumber):
|
|
"""Creates a bin numbered `binnumber` from the global list of files and
|
|
sizes.
|
|
"""
|
|
print("\n### Bin #{} ###\n# List of files:".format(binnumber))
|
|
binsize = 0
|
|
for size in reversed(sorted(sizes.keys())):
|
|
newbinsize = binsize + size
|
|
# Try the next, smaller file if we would exceed the bin's maximum size
|
|
# with this one:
|
|
if newbinsize > options.maxbinsize:
|
|
continue
|
|
|
|
filename = sizes[size].pop()
|
|
print("{} # {:.2f} MiB".format(filename, size))
|
|
# Delete the size from the dictionary if its last file name was just
|
|
# popped:
|
|
if not len(sizes[size]):
|
|
del sizes[size]
|
|
|
|
binsize = newbinsize
|
|
|
|
print("# This bin's size: {:.2f} MiB".format(binsize))
|
|
sizefree = options.maxbinsize - binsize
|
|
print("# Free space: {:.2f} MiB".format(sizefree))
|
|
|
|
|
|
### Parse command-line arguments ###
|
|
|
|
arg_parser = argparse.ArgumentParser(
|
|
description="Packs files and directories into fixed-size volumes",
|
|
epilog="For more information about this program, see the README file \
|
|
provided with the distribution.")
|
|
arg_parser.add_argument("-s", "--size", action="store", dest="maxbinsize",
|
|
type=float, default=703,
|
|
help="maximal size of each volume (bin), in MiB; the "
|
|
+"default is 703 MiB, i.e. the size of a 80-minute "
|
|
+"CD-ROM")
|
|
arg_parser.add_argument("filenames", metavar="file", nargs="+",
|
|
help="files or directories to pack")
|
|
options = arg_parser.parse_args()
|
|
|
|
|
|
### Preliminary statistics ###
|
|
|
|
print("# Maximum size of a bin: {:.2f} MiB".format(options.maxbinsize))
|
|
|
|
# Compute the size of all the files
|
|
sizes = defaultdict(list)
|
|
totalsize = 0
|
|
ignored_files = False
|
|
for filename in options.filenames:
|
|
size = du(filename)
|
|
if (size > options.maxbinsize):
|
|
print('# WARNING! "{}" is {:.2f} MiB, which exceeds the maximum size of'
|
|
' a bin: ignoring.'
|
|
.format(filename, size))
|
|
ignored_files = True
|
|
continue
|
|
sizes[size].append(filename)
|
|
totalsize += size
|
|
|
|
print("# Total size of the input files: {:.2f} MiB".format(totalsize))
|
|
minbins = math.ceil(totalsize / options.maxbinsize)
|
|
print("# Minimal (optimal) number of bins required: ", minbins)
|
|
sizefree = minbins * options.maxbinsize - totalsize
|
|
print("# Total unused space with {} bins: {:.2f} MiB".format(minbins, sizefree))
|
|
|
|
|
|
### Assemble the bins ###
|
|
|
|
binnumber = 1
|
|
while len(sizes):
|
|
create_bin(binnumber)
|
|
binnumber += 1
|
|
binscreated = binnumber - 1
|
|
|
|
|
|
### Final statistics ###
|
|
|
|
sizefree = binscreated * options.maxbinsize - totalsize
|
|
print("\n# Total unused space over the {} bins created: {:.2f} MiB"
|
|
.format(binscreated, sizefree))
|
|
|
|
if (ignored_files):
|
|
print("\n# WARNING! There were ignored files, see at the top of this log.")
|