From 3ba80808be7e56162bd6cac853ef1f329d9ccd2e Mon Sep 17 00:00:00 2001 From: trimtab613 Date: Thu, 10 Apr 2014 14:25:23 +0800 Subject: [PATCH 1/5] Replace os.rename with shutil.move Currently the code crashes with `[Errno 18] Invalid cross-device link` and this fixes it, as mentioned in many places online. --- terrarium/terrarium.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/terrarium/terrarium.py b/terrarium/terrarium.py index 0116360..e25e810 100755 --- a/terrarium/terrarium.py +++ b/terrarium/terrarium.py @@ -144,7 +144,7 @@ def restore_previously_backed_up_environment(self): rmtree(target) logger.info('Renaming %s to %s', backup, target) - os.rename(backup, target) + shutil.move(backup, target) return 0 def get_target_location(self): @@ -234,7 +234,7 @@ def install(self): logger.info( 'Backing environment up to %s' % old_target_backup) try: - os.rename(old_target, old_target_backup) + shutil.move(old_target, old_target_backup) except OSError, why: logger.error( 'Failed to move environment out of the way. ' @@ -252,7 +252,7 @@ def install(self): try: # move the new environment into the target's place - os.rename(new_target, old_target) + shutil.move(new_target, old_target) except OSError, why: logger.error( 'Failed to move the new environment into the correct path. ' From 56e67596ff6b49bb86293060f2b3379ad65ca320 Mon Sep 17 00:00:00 2001 From: trimtab613 Date: Thu, 10 Apr 2014 15:16:08 +0800 Subject: [PATCH 2/5] Add logging before file moves --- terrarium/terrarium.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/terrarium/terrarium.py b/terrarium/terrarium.py index e25e810..c053f69 100755 --- a/terrarium/terrarium.py +++ b/terrarium/terrarium.py @@ -234,6 +234,7 @@ def install(self): logger.info( 'Backing environment up to %s' % old_target_backup) try: + logger.info('Renaming %s to %s', old_target, old_target_backup) shutil.move(old_target, old_target_backup) except OSError, why: logger.error( @@ -252,6 +253,7 @@ def install(self): try: # move the new environment into the target's place + logger.info('Renaming %s to %s', new_target, old_target) shutil.move(new_target, old_target) except OSError, why: logger.error( From ffb9881f0d2ddceafda4451453b62de26d2e6f13 Mon Sep 17 00:00:00 2001 From: A Kwiatkowski Date: Wed, 16 Apr 2014 17:36:39 +0800 Subject: [PATCH 3/5] shutil.move does not support identical source and dest paths --- terrarium/terrarium.py | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/terrarium/terrarium.py b/terrarium/terrarium.py index c053f69..cfd26a5 100755 --- a/terrarium/terrarium.py +++ b/terrarium/terrarium.py @@ -144,7 +144,8 @@ def restore_previously_backed_up_environment(self): rmtree(target) logger.info('Renaming %s to %s', backup, target) - shutil.move(backup, target) + if backup != target: + shutil.move(backup, target) return 0 def get_target_location(self): @@ -215,7 +216,8 @@ def install(self): logger.info('Copying bootstrap script to new environment') dest = os.path.join( new_target, 'bin', 'terrarium_bootstrap.py') - shutil.copyfile(bootstrap, dest) + if bootstrap != dest: + shutil.copyfile(bootstrap, dest) os.chmod(dest, 0744) os.close(fd) os.unlink(bootstrap) @@ -233,16 +235,17 @@ def install(self): old_target_backup = os.path.join(old_target_backup, prompt) logger.info( 'Backing environment up to %s' % old_target_backup) - try: - logger.info('Renaming %s to %s', old_target, old_target_backup) - shutil.move(old_target, old_target_backup) - except OSError, why: - logger.error( - 'Failed to move environment out of the way. ' - 'Check that you have the correct permissions. ' - '%s' % why - ) - return 1 + if old_target != old_target_backup: + try: + logger.info('Renaming %s to %s', old_target, old_target_backup) + shutil.move(old_target, old_target_backup) + except OSError, why: + logger.error( + 'Failed to move environment out of the way. ' + 'Check that you have the correct permissions. ' + '%s' % why + ) + return 1 # Fix paths Terrarium.replace_all_in_directory( From 0c7d28cab2869c89a65e19cc4522c415d7fa1594 Mon Sep 17 00:00:00 2001 From: A Kwiatkowski Date: Wed, 16 Apr 2014 17:42:23 +0800 Subject: [PATCH 4/5] shutil.move does not support identical source and dest paths --- terrarium/terrarium.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/terrarium/terrarium.py b/terrarium/terrarium.py index cfd26a5..f7f9cb0 100755 --- a/terrarium/terrarium.py +++ b/terrarium/terrarium.py @@ -216,8 +216,7 @@ def install(self): logger.info('Copying bootstrap script to new environment') dest = os.path.join( new_target, 'bin', 'terrarium_bootstrap.py') - if bootstrap != dest: - shutil.copyfile(bootstrap, dest) + shutil.copyfile(bootstrap, dest) os.chmod(dest, 0744) os.close(fd) os.unlink(bootstrap) @@ -254,17 +253,18 @@ def install(self): old_target, ) - try: - # move the new environment into the target's place - logger.info('Renaming %s to %s', new_target, old_target) - shutil.move(new_target, old_target) - except OSError, why: - logger.error( - 'Failed to move the new environment into the correct path. ' - 'Check that you have the correct permissions. ' - '%s' % why - ) - return 1 + if new_target != old_target: + try: + # move the new environment into the target's place + logger.info('Renaming %s to %s', new_target, old_target) + shutil.move(new_target, old_target) + except OSError, why: + logger.error( + 'Failed to move the new environment into the correct path. ' + 'Check that you have the correct permissions. ' + '%s' % why + ) + return 1 # Do we keep a backup of the old environment around or wipe it? if os.path.isdir(old_target_backup) and not self.args.backup: From abbacebc14f16394325a7577de0a4b5ccc0a885a Mon Sep 17 00:00:00 2001 From: A Kwiatkowski Date: Wed, 16 Apr 2014 18:09:46 +0800 Subject: [PATCH 5/5] pep8 formatting (line length) --- terrarium/terrarium.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/terrarium/terrarium.py b/terrarium/terrarium.py index f7f9cb0..02ce491 100755 --- a/terrarium/terrarium.py +++ b/terrarium/terrarium.py @@ -236,7 +236,8 @@ def install(self): 'Backing environment up to %s' % old_target_backup) if old_target != old_target_backup: try: - logger.info('Renaming %s to %s', old_target, old_target_backup) + msg = 'Renaming %s to %s' + logger.info(msg, old_target, old_target_backup) shutil.move(old_target, old_target_backup) except OSError, why: logger.error( @@ -260,8 +261,8 @@ def install(self): shutil.move(new_target, old_target) except OSError, why: logger.error( - 'Failed to move the new environment into the correct path. ' - 'Check that you have the correct permissions. ' + 'Failed to move the new environment into the correct path.' + ' Check that you have the correct permissions. ' '%s' % why ) return 1