Last active
December 16, 2015 04:39
-
-
Save piotrpalek/5379055 to your computer and use it in GitHub Desktop.
Create symlinks and recreate directory structure from specified source path to destination. Desciption in file, after creating the files the script creates an "cache" file, which remembers what directories and symlinks it created so you can later remove them with the optional argument "--remove" to remove all created files.
Useful if you want to…
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
#Eg. let's say you have this path | |
#something/abc/123.file | |
#something/ccc | |
#something/bbb.file | |
#running this script with the arguments: | |
#module.py something /user/peter/destination will result in directories: | |
#destination/abc | |
#destination/ccc | |
#To be created, and after that symbolic links will be created: | |
#destination/abc/123.file | |
#destination/bbb.file | |
import os, pickle, sys, argparse | |
def remove_symlinks(cache): | |
for fil in cache['files']: | |
try: | |
os.remove(fil) | |
print "Removing " + fil | |
except OSError: | |
pass | |
dirs = cache['dirs'] | |
dirs.reverse() | |
for dire in dirs: | |
try: | |
os.rmdir(dire) | |
print "Removing " + dire | |
except OSError: | |
pass | |
def path_filter(path, string): | |
pathlist = path.split(os.sep) | |
remainder = [pathelem for pathelem in pathlist if not pathelem in string.split(os.sep)] | |
return os.sep.join(remainder) | |
def handle_path(path, dirs, files, from_path, to_path): | |
tmp_path = os.path.join(to_path, path_filter(path, from_path)) | |
cdirs = [] | |
cfiles = [] | |
if not os.path.exists(tmp_path): | |
os.makedirs(tmp_path) | |
cdirs.append(tmp_path) | |
for afile in files: | |
dest_link = os.path.join(tmp_path, afile) | |
src_file = os.path.join(path, afile) | |
os.symlink(src_file, dest_link) | |
cfiles.append(dest_link) | |
return cdirs, cfiles | |
def make_symlinks(from_path, to_path): | |
i = 0 | |
cache = {'dirs': [], 'files': []} | |
for (path, dirs, files) in os.walk(from_path): | |
tmp = handle_path(path, dirs, files, from_path, to_path) | |
cache['dirs'] += tmp[0] #foldery | |
cache['files'] += tmp[1] #pliki | |
i += 1 | |
if i >= 1000: | |
break | |
return cache | |
def opt_create(src, dest): | |
cache = make_symlinks(src, dest) | |
cache_file = open(os.path.join(dest, 'cache'), 'w') | |
pickle.dump(cache, cache_file) | |
cache_file.close() | |
def opt_remove(dest): | |
cache_path = os.path.join(dest, 'cache') | |
cache_file = open(cache_path, 'r') | |
cache = pickle.load(cache_file) | |
remove_symlinks(cache) | |
cache_file.close() | |
os.remove(cache_path) | |
def main(): | |
parser = argparse.ArgumentParser(description='Recreates the directory structure under path N on path M then creates symlinks to every file from N (and subdirectories) to M') | |
parser.add_argument('src', metavar='N', nargs=1, help='Source directory') | |
parser.add_argument('dest', metavar='M', nargs=1, help='Destination directory') | |
parser.add_argument('--remove', const=True, default=False, action='store_const', help='Remove directories and symlinks') | |
args = parser.parse_args() | |
if(args.remove): | |
print args.dest[0] | |
opt_remove(args.dest[0]) | |
else: | |
opt_create(args.src[0], args.dest[0]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment