2
0
mirror of https://github.com/frappe/bench.git synced 2025-01-10 00:37:51 +00:00

Merge pull request #561 from achillesrasquinha/556

Migrate from one Python Version to Another
This commit is contained in:
Achilles Rasquinha 2018-02-05 11:08:17 +05:30 committed by GitHub
commit de1edbe667
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 103 additions and 2 deletions

View File

@ -1,5 +1,17 @@
import click
import os, shutil, tempfile
import os.path as osp
import contextlib
import logging
from datetime import datetime
from bench.utils import which, exec_cmd
log = logging.getLogger(__name__)
log.setLevel(logging.ERROR)
def print_bench_version(ctx, param, value):
"""Prints current bench version"""
if not value or ctx.resilient_parsing:
@ -70,4 +82,94 @@ bench_command.add_command(remote_reset_url)
bench_command.add_command(remote_urls)
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.argument('python', type = click.Choice(['python2', 'python3']))
@click.option('--no-backup', default = False, help = 'Do not backup the existing Virtual Environment')
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
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! :| - <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
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('Setting up a New Virtual {python} Environment'.format(
python = python
))
source = pvenv
target = path
shutil.move(source, target)
log.debug('Migration Successful to {python}'.format(
python = python
))
except:
log.debug('Migration Error')
raise
bench_command.add_command(migrate_env)

View File

@ -45,7 +45,6 @@ def setup_fonts():
from bench.utils import setup_fonts
setup_fonts()
@click.command('production')
@click.argument('user')
@click.option('--yes', help='Yes to regeneration config', is_flag=True, default=False)