aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/llvm_project.py
blob: 79a6cd2edf110e94902a72253087f05dd44d389c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Module for manipulating llvm-project-copy. Generally intended for tests."""


import datetime
import os
import subprocess
import sys

import get_llvm_hash
import git_llvm_rev


def get_location() -> str:
    """Gets the absolute path for llvm-project-copy."""
    my_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(my_dir, "llvm-project-copy")


def ensure_up_to_date():
    """Ensures that llvm-project-copy is checked out and semi-up-to-date."""

    checkout = get_location()
    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 = subprocess.check_output(
        [
            "git",
            "log",
            "-n1",
            "--format=%ct",
            "origin/" + git_llvm_rev.MAIN_BRANCH,
        ],
        cwd=checkout,
        encoding="utf-8",
    )

    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"], check=False, cwd=checkout
    )
    if result.returncode:
        print(
            "Sync failed somehow; hoping that things are fresh enough, then...",
            file=sys.stderr,
        )