diff --git a/init_homepath b/init_homepath index 28e58a0..bdfef11 100755 --- a/init_homepath +++ b/init_homepath @@ -1,12 +1,25 @@ #!/usr/bin/python3 # +import os +import shutil import subprocess import sys -def proceed(): +class bcolors: + HEADER = '\033[95m' + OKBLUE = '\033[94m' + OKCYAN = '\033[96m' + OKGREEN = '\033[92m' + WARNING = '\033[93m' + FAIL = '\033[91m' + ENDC = '\033[0m' + BOLD = '\033[1m' + UNDERLINE = '\033[4m' + +def proceed(ask="Proceed?"): while True: - uf = input("\nProceed? [Y/n/q]") + uf = input("\n%s [Y/n/q]" % ask) if len(uf) == 0: uf = "y" if uf.lower() in ["y", "n", "q"]: @@ -14,7 +27,17 @@ def proceed(): if uf == "q": sys.exit(1) else: - return uf == "y" + return uf.lower() == "y" + +def delete_destination(): + return proceed("/!\\ Delete Destination including ALL files? /!\\") + +def failed(error_msg): + print(" " + bcolors.WARNING + error_msg.rstrip("\n") + bcolors.ENDC) + print(" [" + bcolors.FAIL + " FAILED " + bcolors.ENDC + "]") + +def success(): + print(" [" + bcolors.OKGREEN + " SUCCESS " + bcolors.ENDC + "]") BASE_GIT_URL = "https://git.mount-mockery.de/dirk" @@ -29,14 +52,13 @@ for data in REPOS: if proceed(): for url, target in REPOS: command = "git clone %s %s" % (url, target) - sys.stdout.write("Cloning %s" % url) + print("Cloning %s" % url) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process.wait() if process.returncode == 0: - print("\t\t[ SUCCESS ]") + success() else: - print("\t\t[ FAILED ]") - print(" ", process.stderr.read().decode("utf-8")) + failed(process.stderr.read().decode("utf-8")) CONFIG_COMMANDS = ( "GS=`grep .bash/enabled ~/.bashrc`; [ ${#GS} -ne 0 ] || cat ~/.bash/BASHRC_ADDON >> ~/.bashrc", @@ -50,41 +72,46 @@ for command in CONFIG_COMMANDS: print(" ", command) if proceed(): for command in CONFIG_COMMANDS: - sys.stdout.write("Executing %s" % command) + print("Executing %s" % command) process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) process.wait() if process.returncode == 0: - print("\t\t[ SUCCESS ]") + success() else: - print("\t\t[ FAILED ]") - print(" ", process.stderr.read().decode("utf-8")) - -#BASE_FOLDERS="Downloads media_images Videos C64" -#VIP_FOLDERS="prj prj/Arduino Schreibtisch" - - -#echo The following folders will be deleted: -#for folder in $BASE_FOLDERS; do -# echo -e "* $HOME/\033[1m$folder\033[0m" -#done -#for folder in $VIP_FOLDERS; do -# echo -e "* $HOME/\033[1m$folder\033[0m" -#done -#echo -#read -r -p "Are you sure? [y/N] " response -#case "$response" in -# [yY][eE][sS]|[yY]) -# for folder in $BASE_FOLDERS; do -# rm -rf $HOME/`basename $folder`; ln -s /usr/data/$USER/local/$folder $HOME -# done -# rm -rf $HOME/data; ln -s /usr/data/$USER/data data -# for folder in $VIP_FOLDERS; do -# rm -rf $HOME/`basename $folder`; ln -s /usr/data/$USER/data/$folder $HOME -# done -# ;; -# *) -# echo No folder initialisation! -# ;; -#esac + failed(process.stderr.read().decode("utf-8")) +DATA_LINKS = ( + ('~/data', 'prj'), + ('~/data', 'prj/Arduino'), + ('~/data', 'Schreibtisch'), + ('/usr/data/dirk/local', 'C64'), + ('/usr/data/dirk/local', 'Downloads'), + ('/usr/data/dirk/local', 'media_images'), + ('/usr/data/dirk/local', 'Videos'), +) +print("\n\n\nAdding the ~/data softlinks to your home directory") +for src_path, path in DATA_LINKS: + print(" %s -> %s" % (os.path.join(src_path, path), os.path.join('~', os.path.basename(path)))) +if proceed(): + delete_dest = delete_destination() + for src_path, path in DATA_LINKS: + src = os.path.expanduser(os.path.join(src_path, path)) + dest = os.path.expanduser(os.path.join("~", os.path.basename(path))) + print(" Creating symlink: %s -> %s" % (src, dest)) + # Check existance of dest + if not os.path.exists(src): + failed("Source %s does not exists." % src) + continue + if os.path.exists(dest): + if delete_dest: + try: + os.remove(dest) + except IsADirectoryError: + shutil.rmtree(dest) + else: + failed("Destination %s already exists." % dest) + continue + # Create Symbolic Link + os.symlink(src, dest) + success()