diff options
author | George Burgess IV <gbiv@google.com> | 2020-01-10 15:39:07 -0800 |
---|---|---|
committer | George Burgess <gbiv@chromium.org> | 2020-01-11 19:14:16 +0000 |
commit | 6c213eaaa629554cbfdc14a2bc88aa7dc6c5caf5 (patch) | |
tree | b63c455ee65323df9fbcf8e1c1b1cc8e3cb03702 | |
parent | f414196425a688d7e37b49d47a7a5b4cbdee9148 (diff) | |
download | toolchain-utils-6c213eaaa629554cbfdc14a2bc88aa7dc6c5caf5.tar.gz |
git_llvm_rev: auto-update llvm-project-copy in tests
My local llvm-project-copy was out-of-date. It's nice if we can
automatically handle that.
This also fixes lints and such that weren't able to fire when this was
originally landed.
BUG=chromium:1041016
TEST=Wiped out my LLVM tree; it synced without issue. Tried syncing with
a timedelta of seconds=2; everything WAI.
Change-Id: I4a4811bb1500db0b00c924eec0be12dc57499fc6
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/third_party/toolchain-utils/+/1995316
Reviewed-by: Manoj Gupta <manojgupta@chromium.org>
Tested-by: George Burgess <gbiv@chromium.org>
-rwxr-xr-x | llvm_tools/git_llvm_rev.py | 7 | ||||
-rwxr-xr-x | llvm_tools/git_llvm_rev_test.py | 51 |
2 files changed, 47 insertions, 11 deletions
diff --git a/llvm_tools/git_llvm_rev.py b/llvm_tools/git_llvm_rev.py index 793b56f9..8eefcdce 100755 --- a/llvm_tools/git_llvm_rev.py +++ b/llvm_tools/git_llvm_rev.py @@ -10,6 +10,8 @@ Revision numbers are all of the form '(branch_name, r1234)'. As a shorthand, r1234 is parsed as '(master, 1234)'. """ +from __future__ import print_function + import argparse import re import subprocess @@ -184,8 +186,7 @@ def translate_sha_to_rev(llvm_config: LLVMConfig, sha_or_ref: str) -> Rev: f'Ambiguity: multiple branches from {llvm_config.remote} have {sha}: ' f'{sorted(candidates)}') - branch, = candidates - return Rev(branch=branch, number=revision_number) + return Rev(branch=candidates[0], number=revision_number) def parse_git_commit_messages(stream: t.Iterable[str], @@ -208,6 +209,8 @@ def parse_git_commit_messages(stream: t.Iterable[str], lines = iter(stream) while True: + # Looks like a potential bug in pylint? crbug.com/1041148 + # pylint: disable=stop-iteration-return sha = next(lines, None) if sha is None: return diff --git a/llvm_tools/git_llvm_rev_test.py b/llvm_tools/git_llvm_rev_test.py index 06b83cd1..c7d59b27 100755 --- a/llvm_tools/git_llvm_rev_test.py +++ b/llvm_tools/git_llvm_rev_test.py @@ -8,10 +8,13 @@ from __future__ import print_function +import datetime import os +import subprocess import sys import unittest +import get_llvm_hash import git_llvm_rev @@ -20,6 +23,44 @@ def get_llvm_checkout() -> str: return os.path.join(my_dir, 'llvm-project-copy') +def ensure_llvm_project_up_to_date(): + checkout = get_llvm_checkout() + if not os.path.isdir(checkout): + print( + 'No llvm-project exists locally; syncing it. This takes a while.', + file=sys.stderr) + actual_checkout = get_llvm_hash.GetAndUpdateLLVMProjectInLLVMTools() + assert checkout == actual_checkout, '%s != %s' % (actual_checkout, checkout) + + commit_timestamp = git_llvm_rev.check_output( + ['git', 'log', '-n1', '--format=%ct', 'origin/master'], cwd=checkout) + + commit_time = datetime.datetime.fromtimestamp(int(commit_timestamp.strip())) + now = datetime.datetime.now() + + time_since_last_commit = now - commit_time + + # Arbitrary, but if it's been more than 2d since we've seen a commit, it's + # probably best to bring us up-to-date. + if time_since_last_commit <= datetime.timedelta(days=2): + return + + print( + '%d days have elapsed since the last commit to %s; auto-syncing' % + (time_since_last_commit.days, checkout), + file=sys.stderr) + + result = subprocess.run( + ['git', 'fetch', 'origin'], + cwd=checkout, + check=True, + ) + if result.returncode: + print( + 'Sync failed somehow; hoping that things are fresh enough, then...', + file=sys.stderr) + + def get_llvm_config() -> git_llvm_rev.LLVMConfig: return git_llvm_rev.LLVMConfig(dir=get_llvm_checkout(), remote='origin') @@ -131,13 +172,5 @@ class Test(unittest.TestCase): # back to the base commit for. if __name__ == '__main__': - # We have exactly one concrete target that we often have checked out anyway. - # Rather than building tests that hopefully match that target, use it - # directly. - if not os.path.isdir(get_llvm_checkout()): - print( - 'Please checkout llvm-project-copy to run these tests. A simple way ' - 'to do that is running `./get_llvm_hash.py --llvm_version 370000`', - file=sys.stderr) - sys.exit(1) + ensure_llvm_project_up_to_date() unittest.main() |