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:
commit
7b7d569c2d
@ -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,64 +89,64 @@ 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'
|
# I know, bad name for a flag. Thanks, Ameya! :| - <achilles@frappe.io>
|
||||||
pvenv = osp.join(dirpath, nvenv)
|
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(
|
log.debug('Backing up Virtual Environment')
|
||||||
virtualenv = virtualenv,
|
stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||||
python = python,
|
dest = osp.join(path, str(stamp))
|
||||||
pvenv = pvenv
|
|
||||||
), cwd = dirpath)
|
# 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')
|
virtualenv = which('virtualenv')
|
||||||
apps = os.listdir(apps_path)
|
|
||||||
apps = ['frappe'] + [app for app in apps if app!='frappe']
|
|
||||||
|
|
||||||
for app in apps:
|
nvenv = 'env'
|
||||||
papp = osp.join(apps_path, app)
|
pvenv = osp.join(path, nvenv)
|
||||||
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>
|
exec_cmd('{virtualenv} --python {python} {pvenv}'.format(
|
||||||
if not no_backup:
|
virtualenv = virtualenv,
|
||||||
# Back, the f*ck up.
|
python = python,
|
||||||
parch = osp.join(path, 'archived_envs')
|
pvenv = pvenv
|
||||||
if not osp.exists(parch):
|
), cwd = path)
|
||||||
os.mkdir(parch)
|
|
||||||
|
|
||||||
# Simply moving. Thanks, Ameya.
|
# TODO: Options
|
||||||
# I'm keen to zip.
|
|
||||||
source = osp.join(path, 'env')
|
|
||||||
target = parch
|
|
||||||
|
|
||||||
log.debug('Backing up Virtual Environment')
|
papps = osp.join(path, 'apps')
|
||||||
stamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
apps = ['frappe'] + [app for app in os.listdir(papps) if app != 'frappe']
|
||||||
dest = osp.join(path, str(stamp))
|
|
||||||
|
|
||||||
os.rename(source, dest)
|
for app in apps:
|
||||||
shutil.move(dest, target)
|
papp = osp.join(papps, app)
|
||||||
|
if osp.isdir(papp) and osp.exists(osp.join(papp, 'setup.py')):
|
||||||
log.debug('Setting up a New Virtual {python} Environment'.format(
|
pip = osp.join(pvenv, 'bin', 'pip')
|
||||||
python = python
|
exec_cmd('{pip} install -e {app}'.format(
|
||||||
))
|
pip = pip, app = papp
|
||||||
source = pvenv
|
))
|
||||||
target = path
|
|
||||||
|
|
||||||
shutil.move(source, target)
|
|
||||||
|
|
||||||
log.debug('Migration Successful to {python}'.format(
|
log.debug('Migration Successful to {python}'.format(
|
||||||
python = python
|
python = python
|
||||||
|
Loading…
Reference in New Issue
Block a user