2
0
mirror of https://github.com/frappe/bench.git synced 2024-11-11 15:51:03 +00:00

Merge pull request #564 from achillesrasquinha/556

Backup, then Migrate Environments. (And not vice-versa)
This commit is contained in:
Achilles Rasquinha 2018-02-06 10:25:45 +05:30 committed by GitHub
commit 7b7d569c2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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')
@ -108,37 +89,9 @@ 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')
nvenv = 'env'
pvenv = osp.join(dirpath, nvenv)
exec_cmd('{virtualenv} --python {python} {pvenv}'.format(
virtualenv = virtualenv,
python = python,
pvenv = pvenv
), cwd = dirpath)
# TODO: Options
apps_path = osp.join(path, 'apps')
apps = os.listdir(apps_path)
apps = ['frappe'] + [app for app in apps if app!='frappe']
for app in apps:
papp = osp.join(apps_path, 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
))
# 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! :| - <achilles@frappe.io>
if not no_backup:
@ -156,16 +109,44 @@ def migrate_env(python, no_backup = False):
stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
dest = osp.join(path, str(stamp))
# 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
))
source = pvenv
target = path
shutil.move(source, target)
# 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