Source

iphonify / cleanup.py

#!/usr/bin/python

"""Delete source files based on missing output files"""

import os
import sys

if len(sys.argv) <= 2:
    print "Usage: cleanup.py <indir> <outdir>"
    sys.exit(1)

indir = sys.argv[1]
outdir = sys.argv[2]

for dirpath, dirnames, filenames in os.walk(indir):
    # sort by reverse mtime, so we encode newest entries first
    fullpaths = [os.path.join(dirpath, x) for x in filenames]
    fullpaths.sort(key=os.path.getmtime, reverse=True)

    for fullpath in fullpaths:
        # files to convert
        if os.path.splitext(fullpath)[1] == ".avi":
            infile = fullpath

            outfile = os.path.join(outdir, os.path.splitext(os.path.basename(fullpath))[0]+".m4v")

            if not os.path.exists(outfile):
                res = raw_input("%s - Delete [Y/n]: " % os.path.basename(infile))
                if res.lower() == 'y' or res == '':
                    if not os.path.isdir(infile):
                        os.remove(infile)
                    else:
                        shutil.rmtree(infile)