Created
March 16, 2015 23:38
-
-
Save tzwenn/6ee3a808c4494afc182d to your computer and use it in GitHub Desktop.
Hardlink all files in an exported unicode iTunes playlist into a subfolder.
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 | |
import sys, os | |
from Carbon.CoreFoundation import * | |
from Carbon.CF import * | |
def to_posix(hfs_path): | |
if hfs_path: | |
return toCF(hfs_path).CFURLCreateWithFileSystemPath(kCFURLHFSPathStyle, False).CFURLCopyFileSystemPath(kCFURLPOSIXPathStyle).toPython() | |
def hardlink(filename, outfolder): | |
print >> sys.stderr, "Copy", filename | |
folder, fname = os.path.split(filename) | |
if folder[0] == '/': | |
folder = folder[1:] | |
folder = os.path.join(outfolder, folder) | |
try: | |
os.makedirs(folder) | |
except OSError: | |
pass | |
os.link(filename, os.path.join(folder, fname)) | |
def main(outfolder): | |
lines = sys.stdin.read().decode('utf16').split('\r')[1:-1] | |
hfsfiles = (l.split('\t')[-1] for l in lines) | |
posixfiles = map(to_posix, hfsfiles) | |
files = [f for f in posixfiles if f is not None and os.path.exists(f)] | |
broken = [lines[i] for i in xrange(len(lines)) if posixfiles[i] is None or not os.path.exists(posixfiles[i])] | |
for f in files: | |
hardlink(f, outfolder) | |
if broken: | |
print >> sys.stderr, "----------------------------" | |
print >> sys.stderr, " MISSING FILES " | |
print >> sys.stderr, "----------------------------" | |
for f in broken: | |
print f.partition("\t")[0] | |
if __name__ == "__main__": | |
if len(sys.argv) > 1: | |
main(sys.argv[1]) | |
else: | |
print >> sys.stderr, "Usage: %s OUTDIR < inputlist.txt\n" % sys.argv[0] | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment