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