2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-10 00:37:51 +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 click
import os, shutil, tempfile import os, shutil
import os.path as osp import os.path as osp
import contextlib
import logging import logging
from datetime import datetime from datetime import datetime
@ -83,24 +82,6 @@ bench_command.add_command(remote_urls)
from bench.commands.install import install from bench.commands.install import install
bench_command.add_command(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.command('migrate-env')
@click.argument('python', type = click.Choice(['python2', 'python3'])) @click.argument('python', type = click.Choice(['python2', 'python3']))
@click.option('--no-backup', default = False, help = 'Do not backup the existing Virtual Environment') @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. 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: try:
with tempdir() as dirpath: # This is with the assumption that a bench is set-up within path.
virtualenv = which('virtualenv') path = os.getcwd()
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
))
# I know, bad name for a flag. Thanks, Ameya! :| - <achilles@frappe.io> # I know, bad name for a flag. Thanks, Ameya! :| - <achilles@frappe.io>
if not no_backup: 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') stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
dest = osp.join(path, str(stamp)) 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) os.rename(source, dest)
shutil.move(dest, target) shutil.move(dest, target)
log.debug('Setting up a New Virtual {python} Environment'.format( log.debug('Setting up a New Virtual {python} Environment'.format(
python = python 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( log.debug('Migration Successful to {python}'.format(
python = python python = python