From 4d00f25d0136e80a92ef5af21fd58e764497389b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Pag=C3=A8s?= Date: Wed, 10 Jun 2015 23:35:33 +0200 Subject: [PATCH] Bug 1164443 - Mozinstall should only install in an empty dir; r=ahal --HG-- extra : commitid : 9jTgPYAbTt0 extra : rebase_source : 89a108164c90062f2534a06d1edd466d4185c711 --- .../mozinstall/mozinstall/mozinstall.py | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/testing/mozbase/mozinstall/mozinstall/mozinstall.py b/testing/mozbase/mozinstall/mozinstall/mozinstall.py index 2360660fe479..27df58ef6069 100755 --- a/testing/mozbase/mozinstall/mozinstall/mozinstall.py +++ b/testing/mozbase/mozinstall/mozinstall/mozinstall.py @@ -10,6 +10,7 @@ import sys import tarfile import time import zipfile +import tempfile import mozfile import mozinfo @@ -93,8 +94,7 @@ def install(src, dest): the installation folder. :param src: Path to the install file - :param dest: Path to install to (to ensure we do not overwrite any existent - files the folder should not exist yet) + :param dest: Path to install to """ src = os.path.realpath(src) dest = os.path.realpath(dest) @@ -102,14 +102,15 @@ def install(src, dest): if not is_installer(src): raise InvalidSource(src + ' is not valid installer file.') - did_we_create = False if not os.path.exists(dest): - did_we_create = True os.makedirs(dest) + else: + # create a subfolder to prevent data loss + dest = tempfile.mkdtemp(prefix="mozinstall", dir=dest) trbk = None + install_dir = None try: - install_dir = None if zipfile.is_zipfile(src) or tarfile.is_tarfile(src): install_dir = mozfile.extract(src, dest)[0] elif src.lower().endswith('.dmg'): @@ -121,17 +122,17 @@ def install(src, dest): except: cls, exc, trbk = sys.exc_info() - if did_we_create: - try: - # try to uninstall this properly - uninstall(dest) - except: - # uninstall may fail, let's just try to clean the folder - # in this case - try: - mozfile.remove(dest) - except: - pass + # try to uninstall this properly + try: + if install_dir: + uninstall(install_dir) + except: + pass + # remove the dest folder anyway + try: + mozfile.remove(dest) + except: + pass if issubclass(cls, Exception): error = InstallError('Failed to install "%s (%s)"' % (src, str(exc))) raise InstallError, error, trbk