2018-10-13 01:53:05 +08:00
|
|
|
import os, subprocess
|
2019-03-30 01:23:13 +08:00
|
|
|
import argparse
|
2020-11-21 06:56:29 +08:00
|
|
|
from pathlib import Path
|
2018-10-13 01:53:05 +08:00
|
|
|
|
|
|
|
from . import mlog
|
2020-10-10 22:45:34 +08:00
|
|
|
from .mesonlib import quiet_git, verbose_git, GitException, Popen_safe, MesonException, windows_proof_rmtree
|
2020-09-09 22:57:32 +08:00
|
|
|
from .wrap.wrap import API_ROOT, Resolver, WrapException, ALL_TYPES
|
2018-10-13 01:53:05 +08:00
|
|
|
from .wrap import wraptool
|
|
|
|
|
2020-09-12 02:06:01 +08:00
|
|
|
ALL_TYPES_STRING = ', '.join(ALL_TYPES)
|
|
|
|
|
2018-10-13 01:53:05 +08:00
|
|
|
def update_wrapdb_file(wrap, repo_dir, options):
|
|
|
|
patch_url = wrap.get('patch_url')
|
|
|
|
branch, revision = wraptool.parse_patch_url(patch_url)
|
|
|
|
new_branch, new_revision = wraptool.get_latest_version(wrap.name)
|
|
|
|
if new_branch == branch and new_revision == revision:
|
|
|
|
mlog.log(' -> Up to date.')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
wraptool.update_wrap_file(wrap.filename, wrap.name, new_branch, new_revision)
|
|
|
|
msg = [' -> New wrap file downloaded.']
|
|
|
|
# Meson reconfigure won't use the new wrap file as long as the source
|
|
|
|
# directory exists. We don't delete it ourself to avoid data loss in case
|
|
|
|
# user has changes in their copy.
|
|
|
|
if os.path.isdir(repo_dir):
|
|
|
|
msg += ['To use it, delete', mlog.bold(repo_dir), 'and run', mlog.bold('meson --reconfigure')]
|
|
|
|
mlog.log(*msg)
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def update_file(r, wrap, repo_dir, options):
|
2018-10-13 01:53:05 +08:00
|
|
|
patch_url = wrap.values.get('patch_url', '')
|
|
|
|
if patch_url.startswith(API_ROOT):
|
2020-08-07 22:45:11 +08:00
|
|
|
return update_wrapdb_file(wrap, repo_dir, options)
|
2018-10-13 01:53:05 +08:00
|
|
|
elif not os.path.isdir(repo_dir):
|
|
|
|
# The subproject is not needed, or it is a tarball extracted in
|
|
|
|
# 'libfoo-1.0' directory and the version has been bumped and the new
|
|
|
|
# directory is 'libfoo-2.0'. In that case forcing a meson
|
|
|
|
# reconfigure will download and use the new tarball.
|
|
|
|
mlog.log(' -> Subproject has not been checked out. Run', mlog.bold('meson --reconfigure'), 'to fetch it if needed.')
|
|
|
|
else:
|
|
|
|
# The subproject has not changed, or the new source and/or patch
|
|
|
|
# tarballs should be extracted in the same directory than previous
|
|
|
|
# version.
|
|
|
|
mlog.log(' -> Subproject has not changed, or the new source/patch needs to be extracted on the same location.\n' +
|
|
|
|
' In that case, delete', mlog.bold(repo_dir), 'and run', mlog.bold('meson --reconfigure'))
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-01-22 23:33:47 +08:00
|
|
|
def git_output(cmd, workingdir):
|
2020-09-08 23:10:50 +08:00
|
|
|
return quiet_git(cmd, workingdir, check=True)[1]
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-08-08 02:55:08 +08:00
|
|
|
def git_stash(workingdir):
|
2020-09-14 01:44:43 +08:00
|
|
|
# That git command return 1 (failure) when there is something to stash.
|
|
|
|
# We don't want to stash when there is nothing to stash because that would
|
|
|
|
# print spurious "No local changes to save".
|
|
|
|
if not quiet_git(['diff', '--quiet', 'HEAD'], workingdir)[0]:
|
|
|
|
# Don't pipe stdout here because we want the user to see their changes have
|
|
|
|
# been saved.
|
|
|
|
verbose_git(['stash'], workingdir, check=True)
|
2020-08-08 02:55:08 +08:00
|
|
|
|
2018-11-05 11:13:06 +08:00
|
|
|
def git_show(repo_dir):
|
2020-01-22 23:33:47 +08:00
|
|
|
commit_message = git_output(['show', '--quiet', '--pretty=format:%h%n%d%n%s%n[%an]'], repo_dir)
|
2018-11-05 11:13:06 +08:00
|
|
|
parts = [s.strip() for s in commit_message.split('\n')]
|
|
|
|
mlog.log(' ->', mlog.yellow(parts[0]), mlog.red(parts[1]), parts[2], mlog.blue(parts[3]))
|
|
|
|
|
2020-08-08 02:55:08 +08:00
|
|
|
def git_rebase(repo_dir, revision):
|
|
|
|
try:
|
|
|
|
git_output(['-c', 'rebase.autoStash=true', 'rebase', 'FETCH_HEAD'], repo_dir)
|
2020-09-08 23:10:50 +08:00
|
|
|
except GitException as e:
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(' -> Could not rebase', mlog.bold(repo_dir), 'onto', mlog.bold(revision))
|
2020-09-08 23:10:50 +08:00
|
|
|
mlog.log(mlog.red(e.output))
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def git_reset(repo_dir, revision):
|
|
|
|
try:
|
|
|
|
# Stash local changes, commits can always be found back in reflog, to
|
|
|
|
# avoid any data lost by mistake.
|
|
|
|
git_stash(repo_dir)
|
|
|
|
git_output(['reset', '--hard', 'FETCH_HEAD'], repo_dir)
|
2020-09-08 23:10:50 +08:00
|
|
|
except GitException as e:
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(' -> Could not reset', mlog.bold(repo_dir), 'to', mlog.bold(revision))
|
2020-09-08 23:10:50 +08:00
|
|
|
mlog.log(mlog.red(e.output))
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def git_checkout(repo_dir, revision, create=False):
|
2020-12-07 19:36:14 +08:00
|
|
|
cmd = ['checkout', '--ignore-other-worktrees', revision, '--']
|
2020-08-08 02:55:08 +08:00
|
|
|
if create:
|
|
|
|
cmd.insert('-b', 1)
|
|
|
|
try:
|
|
|
|
# Stash local changes, commits can always be found back in reflog, to
|
|
|
|
# avoid any data lost by mistake.
|
|
|
|
git_stash(repo_dir)
|
|
|
|
git_output(cmd, repo_dir)
|
2020-09-08 23:10:50 +08:00
|
|
|
except GitException as e:
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(' -> Could not checkout', mlog.bold(revision), 'in', mlog.bold(repo_dir))
|
2020-09-08 23:10:50 +08:00
|
|
|
mlog.log(mlog.red(e.output))
|
2020-08-08 02:55:08 +08:00
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def git_checkout_and_reset(repo_dir, revision):
|
|
|
|
# revision could be a branch that already exists but is outdated, so we still
|
|
|
|
# have to reset after the checkout.
|
|
|
|
success = git_checkout(repo_dir, revision)
|
|
|
|
if success:
|
|
|
|
success = git_reset(repo_dir, revision)
|
|
|
|
return success
|
|
|
|
|
|
|
|
def git_checkout_and_rebase(repo_dir, revision):
|
|
|
|
# revision could be a branch that already exists but is outdated, so we still
|
|
|
|
# have to rebase after the checkout.
|
|
|
|
success = git_checkout(repo_dir, revision)
|
|
|
|
if success:
|
|
|
|
success = git_rebase(repo_dir, revision)
|
|
|
|
return success
|
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def update_git(r, wrap, repo_dir, options):
|
2018-10-13 01:53:05 +08:00
|
|
|
if not os.path.isdir(repo_dir):
|
|
|
|
mlog.log(' -> Not used.')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2020-10-10 22:45:34 +08:00
|
|
|
if not os.path.exists(os.path.join(repo_dir, '.git')):
|
|
|
|
if options.reset:
|
|
|
|
# Delete existing directory and redownload
|
|
|
|
windows_proof_rmtree(repo_dir)
|
|
|
|
try:
|
|
|
|
r.resolve(wrap.name, 'meson')
|
|
|
|
update_git_done(repo_dir)
|
|
|
|
return True
|
|
|
|
except WrapException as e:
|
|
|
|
mlog.log(' ->', mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
mlog.log(' -> Not a git repository.')
|
|
|
|
mlog.log('Pass --reset option to delete directory and redownload.')
|
|
|
|
return False
|
2020-10-08 22:09:15 +08:00
|
|
|
revision = wrap.values.get('revision')
|
|
|
|
url = wrap.values.get('url')
|
|
|
|
push_url = wrap.values.get('push-url')
|
|
|
|
if not revision or not url:
|
2020-08-07 22:06:33 +08:00
|
|
|
# It could be a detached git submodule for example.
|
2020-10-08 22:09:15 +08:00
|
|
|
mlog.log(' -> No revision or URL specified.')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2020-10-08 22:09:15 +08:00
|
|
|
try:
|
|
|
|
origin_url = git_output(['remote', 'get-url', 'origin'], repo_dir).strip()
|
|
|
|
except GitException as e:
|
|
|
|
mlog.log(' -> Failed to determine current origin URL in', mlog.bold(repo_dir))
|
|
|
|
mlog.log(mlog.red(e.output))
|
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
if options.reset:
|
|
|
|
try:
|
|
|
|
git_output(['remote', 'set-url', 'origin', url], repo_dir)
|
|
|
|
if push_url:
|
|
|
|
git_output(['remote', 'set-url', '--push', 'origin', push_url], repo_dir)
|
|
|
|
except GitException as e:
|
|
|
|
mlog.log(' -> Failed to reset origin URL in', mlog.bold(repo_dir))
|
|
|
|
mlog.log(mlog.red(e.output))
|
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
elif url != origin_url:
|
|
|
|
mlog.log(' -> URL changed from {!r} to {!r}'.format(origin_url, url))
|
|
|
|
return False
|
2020-09-09 22:31:44 +08:00
|
|
|
try:
|
2020-09-16 23:21:04 +08:00
|
|
|
# Same as `git branch --show-current` but compatible with older git version
|
|
|
|
branch = git_output(['rev-parse', '--abbrev-ref', 'HEAD'], repo_dir).strip()
|
|
|
|
branch = branch if branch != 'HEAD' else ''
|
2020-09-09 22:31:44 +08:00
|
|
|
except GitException as e:
|
|
|
|
mlog.log(' -> Failed to determine current branch in', mlog.bold(repo_dir))
|
|
|
|
mlog.log(mlog.red(e.output))
|
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
2020-09-08 23:10:50 +08:00
|
|
|
try:
|
2020-09-12 02:07:00 +08:00
|
|
|
# Fetch only the revision we need, this avoids fetching useless branches.
|
|
|
|
# revision can be either a branch, tag or commit id. In all cases we want
|
|
|
|
# FETCH_HEAD to be set to the desired commit and "git checkout <revision>"
|
|
|
|
# to to either switch to existing/new branch, or detach to tag/commit.
|
|
|
|
# It is more complicated than it first appear, see discussion there:
|
|
|
|
# https://github.com/mesonbuild/meson/pull/7723#discussion_r488816189.
|
|
|
|
heads_refmap = '+refs/heads/*:refs/remotes/origin/*'
|
|
|
|
tags_refmap = '+refs/tags/*:refs/tags/*'
|
|
|
|
git_output(['fetch', '--refmap', heads_refmap, '--refmap', tags_refmap, 'origin', revision], repo_dir)
|
2020-09-08 23:10:50 +08:00
|
|
|
except GitException as e:
|
|
|
|
mlog.log(' -> Could not fetch revision', mlog.bold(revision), 'in', mlog.bold(repo_dir))
|
|
|
|
mlog.log(mlog.red(e.output))
|
|
|
|
mlog.log(mlog.red(str(e)))
|
|
|
|
return False
|
|
|
|
|
2020-08-07 21:32:51 +08:00
|
|
|
if branch == '':
|
2020-08-08 02:55:08 +08:00
|
|
|
# We are currently in detached mode
|
|
|
|
if options.reset:
|
|
|
|
success = git_checkout_and_reset(repo_dir, revision)
|
|
|
|
else:
|
|
|
|
success = git_checkout_and_rebase(repo_dir, revision)
|
2020-08-07 21:32:51 +08:00
|
|
|
elif branch == revision:
|
2020-08-08 02:55:08 +08:00
|
|
|
# We are in the same branch. A reset could still be needed in the case
|
|
|
|
# a force push happened on remote repository.
|
|
|
|
if options.reset:
|
|
|
|
success = git_reset(repo_dir, revision)
|
|
|
|
else:
|
|
|
|
success = git_rebase(repo_dir, revision)
|
2018-10-13 01:53:05 +08:00
|
|
|
else:
|
2020-08-08 02:55:08 +08:00
|
|
|
# We are in another branch, either the user created their own branch and
|
|
|
|
# we should rebase it, or revision changed in the wrap file and we need
|
|
|
|
# to checkout the new branch.
|
|
|
|
if options.reset:
|
|
|
|
success = git_checkout_and_reset(repo_dir, revision)
|
2018-10-13 01:53:05 +08:00
|
|
|
else:
|
2020-08-08 02:55:08 +08:00
|
|
|
success = git_rebase(repo_dir, revision)
|
|
|
|
if success:
|
2020-10-10 22:45:34 +08:00
|
|
|
update_git_done(repo_dir)
|
2020-08-08 02:55:08 +08:00
|
|
|
return success
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def update_git_done(repo_dir):
|
|
|
|
git_output(['submodule', 'update', '--checkout', '--recursive'], repo_dir)
|
|
|
|
git_show(repo_dir)
|
|
|
|
|
|
|
|
def update_hg(r, wrap, repo_dir, options):
|
2018-10-13 01:53:05 +08:00
|
|
|
if not os.path.isdir(repo_dir):
|
|
|
|
mlog.log(' -> Not used.')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
revno = wrap.get('revision')
|
|
|
|
if revno.lower() == 'tip':
|
|
|
|
# Failure to do pull is not a fatal error,
|
|
|
|
# because otherwise you can't develop without
|
|
|
|
# a working net connection.
|
|
|
|
subprocess.call(['hg', 'pull'], cwd=repo_dir)
|
|
|
|
else:
|
|
|
|
if subprocess.call(['hg', 'checkout', revno], cwd=repo_dir) != 0:
|
|
|
|
subprocess.check_call(['hg', 'pull'], cwd=repo_dir)
|
|
|
|
subprocess.check_call(['hg', 'checkout', revno], cwd=repo_dir)
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def update_svn(r, wrap, repo_dir, options):
|
2018-10-13 01:53:05 +08:00
|
|
|
if not os.path.isdir(repo_dir):
|
|
|
|
mlog.log(' -> Not used.')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
revno = wrap.get('revision')
|
2019-06-03 22:58:43 +08:00
|
|
|
p, out, _ = Popen_safe(['svn', 'info', '--show-item', 'revision', repo_dir])
|
2018-10-13 01:53:05 +08:00
|
|
|
current_revno = out
|
|
|
|
if current_revno == revno:
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
if revno.lower() == 'head':
|
|
|
|
# Failure to do pull is not a fatal error,
|
|
|
|
# because otherwise you can't develop without
|
|
|
|
# a working net connection.
|
|
|
|
subprocess.call(['svn', 'update'], cwd=repo_dir)
|
|
|
|
else:
|
|
|
|
subprocess.check_call(['svn', 'update', '-r', revno], cwd=repo_dir)
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-10-13 01:53:05 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def update(r, wrap, repo_dir, options):
|
2020-03-04 03:45:43 +08:00
|
|
|
mlog.log('Updating {}...'.format(wrap.name))
|
2018-11-05 11:13:06 +08:00
|
|
|
if wrap.type == 'file':
|
2020-10-10 22:45:34 +08:00
|
|
|
return update_file(r, wrap, repo_dir, options)
|
2018-11-05 11:13:06 +08:00
|
|
|
elif wrap.type == 'git':
|
2020-10-10 22:45:34 +08:00
|
|
|
return update_git(r, wrap, repo_dir, options)
|
2018-11-05 11:13:06 +08:00
|
|
|
elif wrap.type == 'hg':
|
2020-10-10 22:45:34 +08:00
|
|
|
return update_hg(r, wrap, repo_dir, options)
|
2018-11-05 11:13:06 +08:00
|
|
|
elif wrap.type == 'svn':
|
2020-10-10 22:45:34 +08:00
|
|
|
return update_svn(r, wrap, repo_dir, options)
|
2018-11-05 11:13:06 +08:00
|
|
|
else:
|
|
|
|
mlog.log(' -> Cannot update', wrap.type, 'subproject')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-11-05 11:13:06 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def checkout(r, wrap, repo_dir, options):
|
2018-11-05 11:13:06 +08:00
|
|
|
if wrap.type != 'git' or not os.path.isdir(repo_dir):
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-11-05 11:13:06 +08:00
|
|
|
branch_name = options.branch_name if options.branch_name else wrap.get('revision')
|
2020-08-07 22:06:33 +08:00
|
|
|
if not branch_name:
|
|
|
|
# It could be a detached git submodule for example.
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2020-03-04 03:45:43 +08:00
|
|
|
mlog.log('Checkout {} in {}...'.format(branch_name, wrap.name))
|
2020-08-08 02:55:08 +08:00
|
|
|
if git_checkout(repo_dir, branch_name, create=options.b):
|
2018-11-05 11:13:06 +08:00
|
|
|
git_show(repo_dir)
|
2020-08-08 02:55:08 +08:00
|
|
|
return True
|
|
|
|
return False
|
2018-11-05 11:13:06 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def download(r, wrap, repo_dir, options):
|
2020-03-04 03:45:43 +08:00
|
|
|
mlog.log('Download {}...'.format(wrap.name))
|
2018-11-24 03:55:06 +08:00
|
|
|
if os.path.isdir(repo_dir):
|
|
|
|
mlog.log(' -> Already downloaded')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2018-11-24 03:55:06 +08:00
|
|
|
try:
|
2019-05-28 22:56:45 +08:00
|
|
|
r.resolve(wrap.name, 'meson')
|
2018-11-24 03:55:06 +08:00
|
|
|
mlog.log(' -> done')
|
|
|
|
except WrapException as e:
|
|
|
|
mlog.log(' ->', mlog.red(str(e)))
|
2020-08-07 22:45:11 +08:00
|
|
|
return False
|
|
|
|
return True
|
2018-11-24 03:55:06 +08:00
|
|
|
|
2020-10-10 22:45:34 +08:00
|
|
|
def foreach(r, wrap, repo_dir, options):
|
2020-03-04 03:45:43 +08:00
|
|
|
mlog.log('Executing command in {}'.format(repo_dir))
|
2019-03-30 01:23:13 +08:00
|
|
|
if not os.path.isdir(repo_dir):
|
|
|
|
mlog.log(' -> Not downloaded yet')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2020-09-08 23:10:50 +08:00
|
|
|
cmd = [options.command] + options.args
|
|
|
|
p, out, _ = Popen_safe(cmd, stderr=subprocess.STDOUT, cwd=repo_dir)
|
|
|
|
if p.returncode != 0:
|
|
|
|
err_message = "Command '{}' returned non-zero exit status {}.".format(" ".join(cmd), p.returncode)
|
Fix 'meson subprojects foreach' when command returns non-zero exit code
The 'output' field of the subprocess.CalledProcessError exception is
valid only when subprocess.check_output() is called, trying to access it
after calling subprocess.check_call() results in an unwanted exception
when commands return non-zero exit code, e.g.:
-----------------------------------------------------------------------
$ meson subprojects foreach false
Executing command in ./subprojects/sqlite-amalgamation-3250100
-> Not downloaded yet
Executing command in ./subprojects/gstreamer
Traceback (most recent call last):
File "/home/ao2/meson/meson/mesonbuild/msubprojects.py", line 177, in foreach
subprocess.check_call([options.command] + options.args, cwd=repo_dir)
File "/usr/lib/python3.7/subprocess.py", line 363, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command '['false']' returned non-zero exit status 1.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ao2/meson/meson/mesonbuild/mesonmain.py", line 129, in run
return options.run_func(options)
File "/home/ao2/meson/meson/mesonbuild/msubprojects.py", line 248, in run
options.subprojects_func(wrap, repo_dir, options)
File "/home/ao2/meson/meson/mesonbuild/msubprojects.py", line 180, in foreach
out = e.output.decode().strip()
AttributeError: 'NoneType' object has no attribute 'decode'
-----------------------------------------------------------------------
Use subprocess.check_output() instead and behave more like git commands
in handling stderr.
This makes it possible to actually run commands on all subprojects
allowing them to fail on some subprojects and succeed on others.
Also catch the case of missing commands and print an error message in
this case as well.
2019-11-27 22:56:15 +08:00
|
|
|
mlog.log(' -> ', mlog.red(err_message))
|
|
|
|
mlog.log(out, end='')
|
2020-08-07 22:45:11 +08:00
|
|
|
return False
|
2020-09-08 23:10:50 +08:00
|
|
|
|
|
|
|
mlog.log(out, end='')
|
2020-08-07 22:45:11 +08:00
|
|
|
return True
|
2019-03-30 01:23:13 +08:00
|
|
|
|
2018-11-05 11:13:06 +08:00
|
|
|
def add_common_arguments(p):
|
|
|
|
p.add_argument('--sourcedir', default='.',
|
|
|
|
help='Path to source directory')
|
2020-09-16 23:05:44 +08:00
|
|
|
p.add_argument('--types', default='',
|
2020-09-12 02:06:01 +08:00
|
|
|
help='Comma-separated list of subproject types. Supported types are: {} (default: all)'.format(ALL_TYPES_STRING))
|
2020-08-07 21:28:25 +08:00
|
|
|
|
|
|
|
def add_subprojects_argument(p):
|
2018-11-05 11:13:06 +08:00
|
|
|
p.add_argument('subprojects', nargs='*',
|
|
|
|
help='List of subprojects (default: all)')
|
|
|
|
|
|
|
|
def add_arguments(parser):
|
|
|
|
subparsers = parser.add_subparsers(title='Commands', dest='command')
|
|
|
|
subparsers.required = True
|
|
|
|
|
|
|
|
p = subparsers.add_parser('update', help='Update all subprojects from wrap files')
|
2020-08-08 02:55:08 +08:00
|
|
|
p.add_argument('--rebase', default=True, action='store_true',
|
|
|
|
help='Rebase your branch on top of wrap\'s revision. ' + \
|
|
|
|
'Deprecated, it is now the default behaviour. (git only)')
|
|
|
|
p.add_argument('--reset', default=False, action='store_true',
|
|
|
|
help='Checkout wrap\'s revision and hard reset to that commit. (git only)')
|
2018-11-05 11:13:06 +08:00
|
|
|
add_common_arguments(p)
|
2020-08-07 21:28:25 +08:00
|
|
|
add_subprojects_argument(p)
|
2018-11-05 11:13:06 +08:00
|
|
|
p.set_defaults(subprojects_func=update)
|
|
|
|
|
|
|
|
p = subparsers.add_parser('checkout', help='Checkout a branch (git only)')
|
|
|
|
p.add_argument('-b', default=False, action='store_true',
|
|
|
|
help='Create a new branch')
|
|
|
|
p.add_argument('branch_name', nargs='?',
|
|
|
|
help='Name of the branch to checkout or create (default: revision set in wrap file)')
|
|
|
|
add_common_arguments(p)
|
2020-08-07 21:28:25 +08:00
|
|
|
add_subprojects_argument(p)
|
2018-11-05 11:13:06 +08:00
|
|
|
p.set_defaults(subprojects_func=checkout)
|
|
|
|
|
2018-11-24 03:55:06 +08:00
|
|
|
p = subparsers.add_parser('download', help='Ensure subprojects are fetched, even if not in use. ' +
|
|
|
|
'Already downloaded subprojects are not modified. ' +
|
|
|
|
'This can be used to pre-fetch all subprojects and avoid downloads during configure.')
|
|
|
|
add_common_arguments(p)
|
2020-08-07 21:28:25 +08:00
|
|
|
add_subprojects_argument(p)
|
2018-11-24 03:55:06 +08:00
|
|
|
p.set_defaults(subprojects_func=download)
|
|
|
|
|
2019-03-30 01:23:13 +08:00
|
|
|
p = subparsers.add_parser('foreach', help='Execute a command in each subproject directory.')
|
|
|
|
p.add_argument('command', metavar='command ...',
|
|
|
|
help='Command to execute in each subproject directory')
|
|
|
|
p.add_argument('args', nargs=argparse.REMAINDER,
|
|
|
|
help=argparse.SUPPRESS)
|
2020-08-07 21:28:25 +08:00
|
|
|
add_common_arguments(p)
|
|
|
|
p.set_defaults(subprojects=[])
|
2019-03-30 01:23:13 +08:00
|
|
|
p.set_defaults(subprojects_func=foreach)
|
|
|
|
|
2018-11-05 11:13:06 +08:00
|
|
|
def run(options):
|
2018-10-13 01:53:05 +08:00
|
|
|
src_dir = os.path.relpath(os.path.realpath(options.sourcedir))
|
|
|
|
if not os.path.isfile(os.path.join(src_dir, 'meson.build')):
|
|
|
|
mlog.error('Directory', mlog.bold(src_dir), 'does not seem to be a Meson source directory.')
|
|
|
|
return 1
|
|
|
|
subprojects_dir = os.path.join(src_dir, 'subprojects')
|
|
|
|
if not os.path.isdir(subprojects_dir):
|
|
|
|
mlog.log('Directory', mlog.bold(src_dir), 'does not seem to have subprojects.')
|
|
|
|
return 0
|
2020-04-14 02:35:06 +08:00
|
|
|
r = Resolver(src_dir, 'subprojects')
|
2020-08-07 21:28:25 +08:00
|
|
|
if options.subprojects:
|
|
|
|
wraps = [wrap for name, wrap in r.wraps.items() if name in options.subprojects]
|
|
|
|
else:
|
|
|
|
wraps = r.wraps.values()
|
2020-09-16 23:05:44 +08:00
|
|
|
types = [t.strip() for t in options.types.split(',')] if options.types else []
|
2020-09-09 22:57:32 +08:00
|
|
|
for t in types:
|
|
|
|
if t not in ALL_TYPES:
|
2020-09-12 02:06:01 +08:00
|
|
|
raise MesonException('Unknown subproject type {!r}, supported types are: {}'.format(t, ALL_TYPES_STRING))
|
2020-08-07 22:45:11 +08:00
|
|
|
failures = []
|
2020-08-07 21:28:25 +08:00
|
|
|
for wrap in wraps:
|
2020-09-16 23:05:44 +08:00
|
|
|
if types and wrap.type not in types:
|
2020-08-07 22:35:28 +08:00
|
|
|
continue
|
2020-09-17 02:05:44 +08:00
|
|
|
dirname = Path(subprojects_dir, wrap.directory).as_posix()
|
2020-10-10 22:45:34 +08:00
|
|
|
if not options.subprojects_func(r, wrap, dirname, options):
|
2020-08-07 22:45:11 +08:00
|
|
|
failures.append(wrap.name)
|
|
|
|
if failures:
|
|
|
|
m = 'Please check logs above as command failed in some subprojects which could have been left in conflict state: '
|
|
|
|
m += ', '.join(failures)
|
|
|
|
mlog.warning(m)
|
|
|
|
return len(failures)
|