Skip to content

Commit

Permalink
some interactivity to prevent stupid overwriting
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Emanuel Beber committed Oct 30, 2012
1 parent 6f049e4 commit 0b632fd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 9 deletions.
61 changes: 61 additions & 0 deletions nblauncher/genericutils.py
Expand Up @@ -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
Expand Down
18 changes: 11 additions & 7 deletions nblauncher/notebooks.py
Expand Up @@ -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"])
Expand Down Expand Up @@ -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"]))

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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":
Expand Down
4 changes: 2 additions & 2 deletions 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,,,

0 comments on commit 0b632fd

Please sign in to comment.