From f7b30c3129aabb45384c30591ac66f6a1ddab15e Mon Sep 17 00:00:00 2001 From: thelittlefireman Date: Fri, 28 Jul 2017 15:13:45 +0200 Subject: [PATCH] fix python error when files already exist related to : ``` File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/root/src/app.py", line 189, in run() File "/root/src/app.py", line 178, in run prepare_avd(device, avd_name) File "/root/src/app.py", line 74, in prepare_avd os.symlink(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s)) FileExistsError: [Errno 17] File exists: '/root/devices/skins/galaxy_nexus' -> '/root/platforms/android-25/skins/galaxy_nexus' Traceback (most recent call last): File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main "__main__", mod_spec) File "/usr/lib/python3.5/runpy.py", line 85, in _run_code exec(code, run_globals) File "/root/src/app.py", line 189, in run() File "/root/src/app.py", line 178, in run prepare_avd(device, avd_name) File "/root/src/app.py", line 74, in prepare_avd os.symlink(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s)) FileExistsError: [Errno 17] File exists: '/root/devices/skins/galaxy_nexus' -> '/root/platforms/android-25/skins/galaxy_nexus' ``` --- src/app.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/app.py b/src/app.py index de493a2..13372f4 100644 --- a/src/app.py +++ b/src/app.py @@ -11,6 +11,15 @@ from src import log log.init() logger = logging.getLogger('app') +def symlink_force(target, link_name): + try: + os.symlink(target, link_name) + except OSError, e: + if e.errno == errno.EEXIST: + os.remove(link_name) + os.symlink(target, link_name) + else: + raise e def get_or_raise(env: str) -> str: """ @@ -71,7 +80,7 @@ def prepare_avd(device: str, avd_name: str): skin_dst_path = os.path.join(ANDROID_HOME, 'platforms', 'android-{api}'.format(api=API_LEVEL), 'skins') logger.info('Skin destination path: {dst}'.format(dst=skin_dst_path)) for s in os.listdir(skin_rsc_path): - os.symlink(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s)) + symlink_force(os.path.join(skin_rsc_path, s), os.path.join(skin_dst_path, s)) # Hardware and its skin device_name_bash = device.replace(' ', '\ ') @@ -85,7 +94,7 @@ def prepare_avd(device: str, avd_name: str): profile_src_path = os.path.join(ROOT, 'devices', 'profiles', '{profile}.xml'.format(profile=skin_name)) logger.info('Hardware profile resource path: {rsc}'.format(rsc=profile_src_path)) logger.info('Hardware profile destination path: {dst}'.format(dst=profile_dst_path)) - os.symlink(profile_src_path, profile_dst_path) + symlink_force(profile_src_path, profile_dst_path) # Append command cmd += ' -d {device} -s {skin}'.format(device=device_name_bash, skin=skin_name)