2
0
mirror of https://github.com/frappe/bench.git synced 2024-09-23 04:29:02 +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,64 +89,64 @@ 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()
nvenv = 'env'
pvenv = osp.join(dirpath, nvenv)
# I know, bad name for a flag. Thanks, Ameya! :| - <achilles@frappe.io>
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
exec_cmd('{virtualenv} --python {python} {pvenv}'.format(
virtualenv = virtualenv,
python = python,
pvenv = pvenv
), cwd = dirpath)
log.debug('Backing up Virtual Environment')
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
))
# Path to Python Executable (Basically $PYTHONPTH)
python = which(python)
# TODO: Options
apps_path = osp.join(path, 'apps')
apps = os.listdir(apps_path)
apps = ['frappe'] + [app for app in apps if app!='frappe']
virtualenv = which('virtualenv')
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
))
nvenv = 'env'
pvenv = osp.join(path, nvenv)
# I know, bad name for a flag. Thanks, Ameya! :| - <achilles@frappe.io>
if not no_backup:
# Back, the f*ck up.
parch = osp.join(path, 'archived_envs')
if not osp.exists(parch):
os.mkdir(parch)
exec_cmd('{virtualenv} --python {python} {pvenv}'.format(
virtualenv = virtualenv,
python = python,
pvenv = pvenv
), cwd = path)
# Simply moving. Thanks, Ameya.
# I'm keen to zip.
source = osp.join(path, 'env')
target = parch
# TODO: Options
log.debug('Backing up Virtual Environment')
stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
dest = osp.join(path, str(stamp))
papps = osp.join(path, 'apps')
apps = ['frappe'] + [app for app in os.listdir(papps) if app != 'frappe']
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)
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