1# Copyright 2020 The ChromiumOS Authors 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4 5"""Module for manipulating llvm-project-copy. Generally intended for tests.""" 6 7import datetime 8import os 9import subprocess 10import sys 11 12import get_llvm_hash 13import git_llvm_rev 14 15 16def get_location() -> str: 17 """Gets the absolute path for llvm-project-copy.""" 18 my_dir = os.path.dirname(os.path.abspath(__file__)) 19 return os.path.join(my_dir, "llvm-project-copy") 20 21 22def ensure_up_to_date() -> None: 23 """Ensures that llvm-project-copy is checked out and semi-up-to-date.""" 24 25 checkout = get_location() 26 if not os.path.isdir(checkout): 27 print( 28 "No llvm-project exists locally; syncing it. This takes a while.", 29 file=sys.stderr, 30 ) 31 actual_checkout = get_llvm_hash.GetAndUpdateLLVMProjectInLLVMTools() 32 assert checkout == actual_checkout, "%s != %s" % ( 33 actual_checkout, 34 checkout, 35 ) 36 37 commit_timestamp = subprocess.check_output( 38 [ 39 "git", 40 "log", 41 "-n1", 42 "--format=%ct", 43 "origin/" + git_llvm_rev.MAIN_BRANCH, 44 ], 45 cwd=checkout, 46 encoding="utf-8", 47 ) 48 49 commit_time = datetime.datetime.fromtimestamp(int(commit_timestamp.strip())) 50 now = datetime.datetime.now() 51 52 time_since_last_commit = now - commit_time 53 54 # Arbitrary, but if it's been more than 2d since we've seen a commit, it's 55 # probably best to bring us up-to-date. 56 if time_since_last_commit <= datetime.timedelta(days=2): 57 return 58 59 print( 60 "%d days have elapsed since the last commit to %s; auto-syncing" 61 % (time_since_last_commit.days, checkout), 62 file=sys.stderr, 63 ) 64 65 result = subprocess.run( 66 ["git", "fetch", "origin"], check=False, cwd=checkout 67 ) 68 if result.returncode: 69 print( 70 "Sync failed somehow; hoping that things are fresh enough, then...", 71 file=sys.stderr, 72 ) 73