From 0b632fdad2d745eac1d2966c1a076b82b5973c97 Mon Sep 17 00:00:00 2001 From: Moritz Emanuel Beber Date: Tue, 30 Oct 2012 12:16:17 +0100 Subject: [PATCH] some interactivity to prevent stupid overwriting --- nblauncher/genericutils.py | 61 ++++++++++++++++++++++++++++++++++++++ nblauncher/notebooks.py | 18 ++++++----- students.csv | 4 +-- 3 files changed, 74 insertions(+), 9 deletions(-) diff --git a/nblauncher/genericutils.py b/nblauncher/genericutils.py index 783bd40..43ba584 100644 --- a/nblauncher/genericutils.py +++ b/nblauncher/genericutils.py @@ -218,6 +218,67 @@ def tree_copy(src, dst): tree_copy(src_path, os.path.join(dst, name)) # ignoring links and mount points +def interactive_tree_copy(src, dst, all_or_none=[False, False]): + """ + Iteratively copies a folder structure to a destination rooted somewhere + else. + + Parameters + ---------- + src: `str` + Root of the file path hierarchy that is to be copied elsewhere. + dst: `str` + File path to which the source tree is added. + replace_all: `bool` + Whether to replace all existing files without asking. + replace_none: `bool` + Whether to never replace an existing file. + + Notes + ----- + Ignores symlinks and mount points. + """ + + def query(): + choice = raw_input("Do you want to replace the file '{0}'? "\ + "(y/[n]/All/Zero):".format(dst_path)) + choice = choice.lower() + if not choice: + return False + elif choice[0] == "a": + all_or_none[0] = True + return True + elif choice[0] == "z": + all_or_none[1] = True + return False + elif choice[0] == "y": + return True + else: + return False + + if not os.path.exists(dst): + os.makedirs(dst) + for name in os.listdir(src): + src_path = os.path.join(src, name) + dst_path = os.path.join(dst, name) + if os.path.isfile(src_path): + if os.path.exists(dst_path): + if all_or_none[0]: + # copy file to destination + shutil.copy2(src_path, dst) + elif all_or_none[1]: + continue + elif query(): + # copy file to destination + shutil.copy2(src_path, dst) + else: + # copy file to destination + shutil.copy2(src_path, dst) + elif os.path.isdir(src_path): + # continue down the directory + tree_copy(src_path, dst_path, all_or_none) + # ignoring links and mount points + def tree_chown(pw_entry, root): """ Iteratively descends a directory structure and changes the owner of all diff --git a/nblauncher/notebooks.py b/nblauncher/notebooks.py index 187e556..9c7a570 100755 --- a/nblauncher/notebooks.py +++ b/nblauncher/notebooks.py @@ -106,7 +106,7 @@ def create_user_environment(user, config): return err.returncode # location of the ipython directory created with the profile # get_ipython_dir currently returns different results for other users :S - cmd = ["ipython_prfl_dir"] + cmd = ["ipython_dir"] user_ipython_dir = gutil.launch_as(pw_entry, cmd, os.getcwd()).strip() # if the specified profile exists we copy the contents profile = "profile_{0}".format(config["profile"]) @@ -160,6 +160,7 @@ def setup(config, users): LOGGER.warn(u"Failed to setup environment for user '{0}'.".format( usr["username"])) else: + send_out(usr, config) LOGGER.info(u"Setup environment for user '{0}'."\ .format(usr["username"])) @@ -189,7 +190,7 @@ def send_out(user, config): if not material: material = os.path.dirname(config["material dir"]) try: - gutil.tree_copy(config["material dir"], + gutil.interactive_tree_copy(config["material dir"], os.path.join(destination_path, material)) except shutil.Error: raise OSError(errno.ENOENT, @@ -414,11 +415,14 @@ def remove(config, users): A list of dictionaries as parsed from the database describing individual users. """ - for usr in users: - rc = usrt.delete_user(usr["username"]) - if rc == 0: - usr["sys-pass"] = "" - usr["nb-pass"] = "" + choice = raw_input("Do you really want to remove the users '{0}'? (y/[n]):"\ + .format(", ".join(usr["username"] for usr in users))) + if choice.lower() == "y": + for usr in users: + rc = usrt.delete_user(usr["username"]) + if rc == 0: + usr["sys-pass"] = "" + usr["nb-pass"] = "" choice = raw_input("Do you really want to remove the group '{0}'? (y/[n]):"\ .format(config["group"])) if choice.lower() == "y": diff --git a/students.csv b/students.csv index fb29f47..037f9af 100644 --- a/students.csv +++ b/students.csv @@ -1,3 +1,3 @@ username,surname,name,email,port,sys-pass,nb-pass -jdoe375,Doe,John,jdoe375@universe.edu,,WSY0sQOI0Q3O,cfdNocD4kIQQ -cjay645,Christa,Jay,cjay645@universe.edu,,bBzLjxkTePMn,ICdJrNVagALV +jdoe375,Doe,John,jdoe375@universe.edu,,, +cjay645,Christa,Jay,cjay645@universe.edu,,,