From 0de8bf0ea7ef0b5265632d8decd0701a77d7d1fa Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Mon, 5 Feb 2018 18:42:29 +0530 Subject: [PATCH 1/3] updated migrate-env --- bench/commands/__init__.py | 116 +++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index d73aadea..260f8650 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -102,6 +102,17 @@ def tempdir(): with tempchdir(dirpath, cleanup): yield dirpath +def copytree(source, destination, symlinks = False, ignore = None): + for f in os.listdir(source): + + s = os.path.join(source, f) + d = os.path.join(destination, f) + + if os.path.isdir(s): + shutil.copytree(s, d, symlinks, ignore) + else: + shutil.copy2(s, d) + @click.command('migrate-env') @click.argument('python', type = click.Choice(['python2', 'python3'])) @click.option('--no-backup', default = False, help = 'Do not backup the existing Virtual Environment') @@ -109,62 +120,65 @@ def migrate_env(python, no_backup = False): """ Migrate Virtual Environment to desired Python Version. """ - - python = which(python) - - path = os.getcwd() - # This is with the assumption that a bench is set-up within path. try: - with tempdir() as dirpath: - virtualenv = which('virtualenv') + # This is with the assumption that a bench is set-up within path. + path = os.getcwd() + + # I know, bad name for a flag. Thanks, Ameya! :| - + if not no_backup: + # Back, the f*ck up. + parch = osp.join(path, 'archived_envs') + if not osp.exists(parch): + os.mkdir(parch) - nvenv = 'env' - pvenv = osp.join(dirpath, nvenv) + # Simply moving. Thanks, Ameya. + # I'm keen to zip. + source = osp.join(path, 'env') + target = parch - exec_cmd('{virtualenv} --python {python} {pvenv}'.format( - virtualenv = virtualenv, - python = python, - pvenv = pvenv - ), cwd = dirpath) - - # TODO: Options - - papps = osp.join(path, 'apps') - for app in os.listdir(papps): - papp = osp.join(papps, app) - if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): - pip = osp.join(pvenv, 'bin', 'pip') - exec_cmd('{pip} install -e {app}'.format( - pip = pip, app = papp - )) - - # I know, bad name for a flag. Thanks, Ameya! :| - - if not no_backup: - # Back, the f*ck up. - parch = osp.join(path, 'archived_envs') - if not osp.exists(parch): - os.mkdir(parch) - - # Simply moving. Thanks, Ameya. - # I'm keen to zip. - source = osp.join(path, 'env') - target = parch - - log.debug('Backing up Virtual Environment') - stamp = datetime.now().strftime('%Y%m%d_%H%M%S') - dest = osp.join(path, str(stamp)) - - os.rename(source, dest) - shutil.move(dest, target) + log.debug('Backing up Virtual Environment') + stamp = datetime.now().strftime('%Y%m%d_%H%M%S') + dest = osp.join(path, str(stamp)) - log.debug('Setting up a New Virtual {python} Environment'.format( - python = python - )) - source = pvenv - target = path - - shutil.move(source, target) + # WARNING: This is an archive, you might have to use virtualenv --relocate + # That's because virtualenv creates symlinks with shebangs pointing to executables. + + # ...and shutil.copytree is a f*cking mess. + os.rename(source, dest) + shutil.move(dest, target) + log.debug('Setting up a New Virtual {python} Environment'.format( + python = python + )) + + # Path to Python Executable (Basically $PYTHONPTH) + python = which(python) + + + virtualenv = which('virtualenv') + + nvenv = 'env' + pvenv = osp.join(path, nvenv) + + exec_cmd('{virtualenv} --python {python} {pvenv}'.format( + virtualenv = virtualenv, + python = python, + pvenv = pvenv + ), cwd = path) + + # TODO: Options + + papps = osp.join(path, 'apps') + apps = ['frappe'] + [app for app in os.listdir(papps) if app != 'frappe'] + + for app in apps: + papp = osp.join(papps, app) + if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')): + pip = osp.join(pvenv, 'bin', 'pip') + exec_cmd('{pip} install -e {app}'.format( + pip = pip, app = papp + )) + log.debug('Migration Successful to {python}'.format( python = python )) From a3f6c4b3466cfd4f6b934963f3c3c82439a6a017 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Mon, 5 Feb 2018 18:46:41 +0530 Subject: [PATCH 2/3] Removed copytree --- bench/commands/__init__.py | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index 6d104873..a5be0f43 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -101,17 +101,6 @@ def tempdir(): with tempchdir(dirpath, cleanup): yield dirpath -def copytree(source, destination, symlinks = False, ignore = None): - for f in os.listdir(source): - - s = os.path.join(source, f) - d = os.path.join(destination, f) - - if os.path.isdir(s): - shutil.copytree(s, d, symlinks, ignore) - else: - shutil.copy2(s, d) - @click.command('migrate-env') @click.argument('python', type = click.Choice(['python2', 'python3'])) @click.option('--no-backup', default = False, help = 'Do not backup the existing Virtual Environment') From e446848ae05d69265cf97ca0e665a6ab77cbcb03 Mon Sep 17 00:00:00 2001 From: Achilles Rasquinha Date: Mon, 5 Feb 2018 18:54:44 +0530 Subject: [PATCH 3/3] removed tempdir --- bench/commands/__init__.py | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/bench/commands/__init__.py b/bench/commands/__init__.py index a5be0f43..646b7f86 100755 --- a/bench/commands/__init__.py +++ b/bench/commands/__init__.py @@ -1,8 +1,7 @@ import click -import os, shutil, tempfile +import os, shutil import os.path as osp -import contextlib import logging from datetime import datetime @@ -83,24 +82,6 @@ bench_command.add_command(remote_urls) from bench.commands.install import install bench_command.add_command(install) -@contextlib.contextmanager -def tempchdir(dirpath, cleanup): - basedir = os.getcwd() - os.chdir(osp.expanduser(dirpath)) - try: - yield - finally: - os.chdir(basedir) - cleanup() - -@contextlib.contextmanager -def tempdir(): - dirpath = tempfile.mkdtemp() - def cleanup(): - shutil.rmtree(dirpath) - with tempchdir(dirpath, cleanup): - yield dirpath - @click.command('migrate-env') @click.argument('python', type = click.Choice(['python2', 'python3'])) @click.option('--no-backup', default = False, help = 'Do not backup the existing Virtual Environment')