mirror of
https://github.com/frappe/bench.git
synced 2024-11-12 00:06:36 +00:00
Merge branch 'vjFaLk-switch-to-branch' into develop
This commit is contained in:
commit
f21139c8f1
@ -7,6 +7,7 @@ python:
|
||||
|
||||
install:
|
||||
- sudo apt-get purge -y mysql-common mysql-server mysql-client
|
||||
- sudo apt-get install --only-upgrade -y git
|
||||
# - sudo python $TRAVIS_BUILD_DIR/installer/install.py --user travis --skip-bench-setup
|
||||
- sudo bash $TRAVIS_BUILD_DIR/install_scripts/setup_frappe.sh --skip-install-bench --mysql-root-password travis
|
||||
- mkdir -p ~/bench-repo
|
||||
|
67
bench/app.py
67
bench/app.py
@ -1,6 +1,6 @@
|
||||
import os
|
||||
from .utils import (exec_cmd, get_frappe, check_git_for_shallow_clone, build_assets,
|
||||
restart_supervisor_processes, get_cmd_output, run_frappe_cmd)
|
||||
restart_supervisor_processes, get_cmd_output, run_frappe_cmd, CommandFailedError)
|
||||
from .config.common_site_config import get_config
|
||||
|
||||
import logging
|
||||
@ -13,6 +13,9 @@ import subprocess
|
||||
logging.basicConfig(level="DEBUG")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class InvalidBranchException(Exception): pass
|
||||
class InvalidRemoteException(Exception): pass
|
||||
|
||||
class MajorVersionUpgradeException(Exception):
|
||||
def __init__(self, message, upstream_version, local_version):
|
||||
super(MajorVersionUpgradeException, self).__init__(message)
|
||||
@ -61,7 +64,7 @@ def get_app(git_url, branch=None, bench='.', build_asset_files=True, verbose=Fal
|
||||
app_name = re.search(r'name\s*=\s*[\'"](.*)[\'"]', f.read().decode('utf-8')).group(1)
|
||||
if repo_name != app_name:
|
||||
apps_path = os.path.join(os.path.abspath(bench), 'apps')
|
||||
os.rename(os.path.join(apps_path, repo_name), os.path.join(apps_path, app_name))
|
||||
os.rename(os.path.join(apps_path, repo_name), os.path.join(apps_path, app_name))
|
||||
|
||||
print 'installing', app_name
|
||||
install_app(app=app_name, bench=bench, verbose=verbose)
|
||||
@ -102,19 +105,21 @@ def pull_all_apps(bench='.'):
|
||||
if os.path.exists(os.path.join(app_dir, '.git')):
|
||||
logger.info('pulling {0}'.format(app))
|
||||
exec_cmd("git pull {rebase} upstream {branch}".format(rebase=rebase, branch=get_current_branch(app, bench=bench)), cwd=app_dir)
|
||||
|
||||
# remove pyc files
|
||||
exec_cmd('find . -name "*.pyc" -delete', cwd=app_dir)
|
||||
|
||||
|
||||
def is_version_upgrade(bench='.', branch=None):
|
||||
fetch_upstream('frappe', bench=bench)
|
||||
upstream_version = get_upstream_version('frappe', bench=bench, branch=branch)
|
||||
def is_version_upgrade(app='frappe', bench='.', branch=None):
|
||||
try:
|
||||
fetch_upstream(app, bench=bench)
|
||||
except CommandFailedError, e:
|
||||
raise InvalidRemoteException("No remote named upstream for {0}".format(app))
|
||||
|
||||
upstream_version = get_upstream_version(app=app, branch=branch, bench=bench)
|
||||
|
||||
if not upstream_version:
|
||||
raise Exception("Current branch of 'frappe' not in upstream")
|
||||
raise InvalidBranchException("Specified branch of app {0} is not in upstream".format(app))
|
||||
|
||||
local_version = get_major_version(get_current_version('frappe', bench=bench))
|
||||
local_version = get_major_version(get_current_version(app, bench=bench))
|
||||
upstream_version = get_major_version(upstream_version)
|
||||
|
||||
if upstream_version - local_version > 0:
|
||||
@ -139,7 +144,7 @@ def use_rq(bench_path):
|
||||
|
||||
def fetch_upstream(app, bench='.'):
|
||||
repo_dir = get_repo_dir(app, bench=bench)
|
||||
return exec_cmd("git fetch upstream", cwd=repo_dir)
|
||||
return subprocess.call(["git", "fetch", "upstream"], cwd=repo_dir)
|
||||
|
||||
def get_current_version(app, bench='.'):
|
||||
repo_dir = get_repo_dir(app, bench=bench)
|
||||
@ -152,6 +157,7 @@ def get_upstream_version(app, branch=None, bench='.'):
|
||||
branch = get_current_branch(app, bench=bench)
|
||||
try:
|
||||
contents = subprocess.check_output(['git', 'show', 'upstream/{branch}:setup.py'.format(branch=branch)], cwd=repo_dir, stderr=subprocess.STDOUT)
|
||||
print contents
|
||||
except subprocess.CalledProcessError, e:
|
||||
if "Invalid object" in e.output:
|
||||
return None
|
||||
@ -166,28 +172,44 @@ def get_upstream_url(app, bench='.'):
|
||||
def get_repo_dir(app, bench='.'):
|
||||
return os.path.join(bench, 'apps', app)
|
||||
|
||||
def switch_branch(branch, apps=None, bench='.', upgrade=False):
|
||||
def switch_branch(branch, apps=None, bench='.', upgrade=False, check_upgrade=True):
|
||||
from .utils import update_requirements, backup_all_sites, patch_sites, build_assets, pre_upgrade, post_upgrade
|
||||
import utils
|
||||
apps_dir = os.path.join(bench, 'apps')
|
||||
version_upgrade = is_version_upgrade(bench=bench, branch=branch)
|
||||
if version_upgrade[0] and not upgrade:
|
||||
raise MajorVersionUpgradeException("Switching to {0} will cause upgrade from {1} to {2}. Pass --upgrade to confirm".format(branch, version_upgrade[1], version_upgrade[2]), version_upgrade[1], version_upgrade[2])
|
||||
version_upgrade = (False,)
|
||||
switched_apps = []
|
||||
|
||||
if not apps:
|
||||
apps = ['frappe', 'erpnext']
|
||||
apps = [name for name in os.listdir(apps_dir)
|
||||
if os.path.isdir(os.path.join(apps_dir, name))]
|
||||
if branch=="v4.x.x":
|
||||
apps.append('shopping_cart')
|
||||
|
||||
for app in apps:
|
||||
app_dir = os.path.join(apps_dir, app)
|
||||
if os.path.exists(app_dir):
|
||||
unshallow = "--unshallow" if os.path.exists(os.path.join(app_dir, ".git", "shallow")) else ""
|
||||
exec_cmd("git config --unset-all remote.upstream.fetch", cwd=app_dir)
|
||||
exec_cmd("git config --add remote.upstream.fetch '+refs/heads/*:refs/remotes/upstream/*'", cwd=app_dir)
|
||||
exec_cmd("git fetch upstream {unshallow}".format(unshallow=unshallow), cwd=app_dir)
|
||||
exec_cmd("git checkout {branch}".format(branch=branch), cwd=app_dir)
|
||||
exec_cmd("git merge upstream/{branch}".format(branch=branch), cwd=app_dir)
|
||||
try:
|
||||
if check_upgrade:
|
||||
version_upgrade = is_version_upgrade(app=app, bench=bench, branch=branch)
|
||||
if version_upgrade[0] and not upgrade:
|
||||
raise MajorVersionUpgradeException("Switching to {0} will cause upgrade from {1} to {2}. Pass --upgrade to confirm".format(branch, version_upgrade[1], version_upgrade[2]), version_upgrade[1], version_upgrade[2])
|
||||
print "Switching for "+app
|
||||
unshallow = "--unshallow" if os.path.exists(os.path.join(app_dir, ".git", "shallow")) else ""
|
||||
exec_cmd("git config --unset-all remote.upstream.fetch", cwd=app_dir)
|
||||
exec_cmd("git config --add remote.upstream.fetch '+refs/heads/*:refs/remotes/upstream/*'", cwd=app_dir)
|
||||
exec_cmd("git fetch upstream {unshallow}".format(unshallow=unshallow), cwd=app_dir)
|
||||
exec_cmd("git checkout {branch}".format(branch=branch), cwd=app_dir)
|
||||
exec_cmd("git merge upstream/{branch}".format(branch=branch), cwd=app_dir)
|
||||
switched_apps.append(app)
|
||||
except CommandFailedError:
|
||||
print "Error switching to branch {0} for {1}".format(branch, app)
|
||||
except InvalidRemoteException:
|
||||
print "Remote does not exist for app "+app
|
||||
except InvalidBranchException:
|
||||
print "Branch {0} does not exist in Upstream for {1}".format(branch, app)
|
||||
|
||||
if switched_apps:
|
||||
print "Successfully switched branches for:\n" + "\n".join(switched_apps)
|
||||
|
||||
if version_upgrade[0] and upgrade:
|
||||
update_requirements()
|
||||
@ -198,6 +220,9 @@ def switch_branch(branch, apps=None, bench='.', upgrade=False):
|
||||
build_assets()
|
||||
post_upgrade(version_upgrade[1], version_upgrade[2])
|
||||
|
||||
def switch_to_branch(branch=None, apps=None, bench='.', upgrade=False):
|
||||
switch_branch(branch, apps=apps, bench=bench, upgrade=upgrade)
|
||||
|
||||
def switch_to_master(apps=None, bench='.', upgrade=False):
|
||||
switch_branch('master', apps=apps, bench=bench, upgrade=upgrade)
|
||||
|
||||
|
@ -20,9 +20,10 @@ bench_command.add_command(new_app)
|
||||
bench_command.add_command(new_site)
|
||||
|
||||
|
||||
from bench.commands.update import update, retry_upgrade, switch_to_master, switch_to_develop, switch_to_v4, switch_to_v5
|
||||
from bench.commands.update import update, retry_upgrade, switch_to_branch, switch_to_master, switch_to_develop, switch_to_v4, switch_to_v5
|
||||
bench_command.add_command(update)
|
||||
bench_command.add_command(retry_upgrade)
|
||||
bench_command.add_command(switch_to_branch)
|
||||
bench_command.add_command(switch_to_master)
|
||||
bench_command.add_command(switch_to_develop)
|
||||
bench_command.add_command(switch_to_v4)
|
||||
|
16
bench/commands/update.py
Normal file → Executable file
16
bench/commands/update.py
Normal file → Executable file
@ -115,12 +115,24 @@ def restart_update(kwargs):
|
||||
os.execv(sys.argv[0], sys.argv[:2] + args)
|
||||
|
||||
|
||||
@click.command('switch-to-branch')
|
||||
@click.argument('branch')
|
||||
@click.argument('apps', nargs=-1)
|
||||
@click.option('--upgrade',is_flag=True)
|
||||
def switch_to_branch(branch, apps, upgrade=False):
|
||||
"Switch all apps to specified branch, or specify apps separated by space"
|
||||
from bench.app import switch_to_branch
|
||||
switch_to_branch(branch=branch, apps=list(apps), upgrade=upgrade)
|
||||
print 'Switched to ' + branch
|
||||
print 'Please run `bench update --patch` to be safe from any differences in database schema'
|
||||
|
||||
|
||||
@click.command('switch-to-master')
|
||||
@click.option('--upgrade',is_flag=True)
|
||||
def switch_to_master(upgrade=False):
|
||||
"Switch frappe and erpnext to master branch"
|
||||
from bench.app import switch_to_master
|
||||
switch_to_master(upgrade=upgrade)
|
||||
switch_to_master(upgrade=upgrade, apps=['frappe', 'erpnext'])
|
||||
print
|
||||
print 'Switched to master'
|
||||
print 'Please run `bench update --patch` to be safe from any differences in database schema'
|
||||
@ -131,7 +143,7 @@ def switch_to_master(upgrade=False):
|
||||
def switch_to_develop(upgrade=False):
|
||||
"Switch frappe and erpnext to develop branch"
|
||||
from bench.app import switch_to_develop
|
||||
switch_to_develop(upgrade=upgrade)
|
||||
switch_to_develop(upgrade=upgrade, apps=['frappe', 'erpnext'])
|
||||
print
|
||||
print 'Switched to develop'
|
||||
print 'Please run `bench update --patch` to be safe from any differences in database schema'
|
||||
|
@ -20,8 +20,6 @@ class TestBenchInit(unittest.TestCase):
|
||||
if os.path.exists(bench_path):
|
||||
shutil.rmtree(bench_path, ignore_errors=True)
|
||||
|
||||
|
||||
|
||||
def test_init(self, bench_name="test-bench", **kwargs):
|
||||
self.init_bench(bench_name, **kwargs)
|
||||
|
||||
@ -90,17 +88,17 @@ class TestBenchInit(unittest.TestCase):
|
||||
def test_get_app(self):
|
||||
site_name = "test-site-2.dev"
|
||||
self.init_bench('test-bench')
|
||||
|
||||
|
||||
self.new_site(site_name)
|
||||
bench_path = os.path.join(self.benches_path, "test-bench")
|
||||
|
||||
|
||||
bench.app.get_app("https://github.com/frappe/frappe-client", bench=bench_path)
|
||||
self.assertTrue(os.path.exists(os.path.join(bench_path, "apps", "frappeclient")))
|
||||
|
||||
def test_install_app(self):
|
||||
site_name = "test-site-3.dev"
|
||||
self.init_bench('test-bench')
|
||||
|
||||
|
||||
self.new_site(site_name)
|
||||
bench_path = os.path.join(self.benches_path, "test-bench")
|
||||
|
||||
@ -118,6 +116,21 @@ class TestBenchInit(unittest.TestCase):
|
||||
out = subprocess.check_output(["bench", "--site", site_name, "list-apps"], cwd=bench_path)
|
||||
self.assertTrue("erpnext" in out)
|
||||
|
||||
def test_switch_to_branch(self):
|
||||
self.init_bench('test-bench')
|
||||
|
||||
bench_path = os.path.join(self.benches_path, "test-bench")
|
||||
app_path = os.path.join(bench_path, "apps", "frappe")
|
||||
|
||||
bench.app.switch_branch(branch="master", apps=["frappe"], bench=bench_path, check_upgrade=False)
|
||||
out = subprocess.check_output(['git', 'status'], cwd=app_path)
|
||||
self.assertTrue("master" in out)
|
||||
|
||||
# bring it back to develop!
|
||||
bench.app.switch_branch(branch="develop", apps=["frappe"], bench=bench_path, check_upgrade=False)
|
||||
out = subprocess.check_output(['git', 'status'], cwd=app_path)
|
||||
self.assertTrue("develop" in out)
|
||||
|
||||
def init_bench(self, bench_name, **kwargs):
|
||||
self.benches.append(bench_name)
|
||||
bench.utils.init(bench_name, **kwargs)
|
||||
|
Loading…
Reference in New Issue
Block a user