Created
May 16, 2010 01:39
-
-
Save Velrok/402576 to your computer and use it in GitHub Desktop.
cryptic file linking
This file contains hidden or 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/python | |
import sys | |
import re | |
import os | |
def print_help(): | |
print "Checks if the given file is allready linked follwoing a schema." | |
print "" | |
print " -h --help prints this text" | |
print "" | |
print " -src The source files, that should be linked evantualy." | |
print " -dest The destination base folder. Subfolders will be created based on the series name." | |
print " --soft-link Create softlinks insted of hardlinks." | |
# print help if needed | |
if '-h' in sys.argv or '--help' in sys.argv: | |
print_help() | |
sys.exit() | |
# check for required params | |
required_params = ['-src', '-dest'] | |
# we need 4 params for src <filename> dest <dirname> and the script name | |
if len(sys.argv) < 5: | |
print_help() | |
sys.exit() | |
all_present = True | |
for req in required_params: | |
if req not in sys.argv: | |
print_help() | |
sys.exit() | |
# geather paramerters | |
src = sys.argv[sys.argv.index('-src')+1] | |
src = os.path.abspath(src) | |
src_file = os.path.basename(src) | |
src_dir = os.path.dirname(src) | |
dest_dir = sys.argv[sys.argv.index('-dest')+1] | |
if '--soft-link' in sys.argv: | |
create_softlink = True | |
else: | |
create_softlink = False | |
#print "src file = " + src_file | |
#print "src dir = " + src_dir | |
#print "dest dir = " + dest_dir | |
#print "create softlinks = " + str(create_softlink) | |
# find series name, nubmer and episode number | |
pattern = "(.*)\.S(\d\d)E(\d\d).*" | |
result = re.match(pattern, src_file).groups() | |
series = result[0].replace('.', '\ ') | |
seasson = result[1] | |
episode = result[2] | |
#print 'series = ' + series | |
#print 'seasson = ' + str(seasson) | |
#print 'episode = ' + str(episode) | |
# generate path from found data | |
final_dest = os.path.join(dest_dir, series, str('Seasson'+seasson)) | |
#print 'final destination = ' + final_dest | |
# if path not existent create it | |
if not os.path.exists(final_dest): | |
os.makedirs(final_dest) | |
# check if file allready exists | |
link = os.path.join(final_dest, src_file) | |
#print "link = " + link | |
if not os.path.exists(link): | |
if create_softlink: | |
os.symlink(src, link) | |
else: | |
os.link(src, link) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Waaah, Typo! Is Season, not "Seasson". Damn, Waldi :(
And beeing a bit more verbose what it does, would be nice too. But so far, nice one :)