|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2020 Efabless Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +import os |
| 16 | +import subprocess |
| 17 | +from types import SimpleNamespace |
| 18 | + |
| 19 | + |
| 20 | +def export_env_default(key, value): |
| 21 | + with open(os.getenv("GITHUB_ENV"), "a") as f: |
| 22 | + f.write("%s=%s\n" % (key, value)) |
| 23 | + |
| 24 | + |
| 25 | +export_env = export_env_default |
| 26 | + |
| 27 | + |
| 28 | +class Repo(object): |
| 29 | + def __init__(self, name, url, branch_rx=None, extraction_rx=None): |
| 30 | + print("[Repo Object] Initializing repo %s with URL %s…" % (name, url)) |
| 31 | + self.name = name |
| 32 | + self.url = url |
| 33 | + self.commit = None |
| 34 | + self.branch_rx = branch_rx |
| 35 | + self.extraction_rx = extraction_rx |
| 36 | + |
| 37 | + self._latest_commit = None |
| 38 | + self._branches = None |
| 39 | + self._tags = None |
| 40 | + |
| 41 | + @property |
| 42 | + def latest_commit(self): |
| 43 | + if self._latest_commit is None: |
| 44 | + print("[Repo Object] Fetching latest commit for %s…" % self.name) |
| 45 | + p = subprocess.check_output(["git", "ls-remote", self.url]).decode("utf8") |
| 46 | + for line in p.split("\n"): |
| 47 | + if "HEAD" in line: |
| 48 | + self._latest_commit = line[:40] |
| 49 | + return self._latest_commit |
| 50 | + |
| 51 | + @property |
| 52 | + def branches(self): |
| 53 | + if self._branches is None: |
| 54 | + print("[Repo Object] Fetching branches for %s…" % self.name) |
| 55 | + p = subprocess.check_output( |
| 56 | + ["git", "ls-remote", "--heads", self.url] |
| 57 | + ).decode("utf8") |
| 58 | + branches = [] |
| 59 | + for line in p.split("\n"): |
| 60 | + if line.strip() == "": |
| 61 | + continue |
| 62 | + |
| 63 | + match = line.split() |
| 64 | + |
| 65 | + hash = match[0] |
| 66 | + name = match[1] |
| 67 | + |
| 68 | + branches.append((hash, name)) |
| 69 | + self._branches = branches |
| 70 | + return self._branches |
| 71 | + |
| 72 | + @property |
| 73 | + def tags(self): |
| 74 | + if self._tags is None: |
| 75 | + print("[Repo Object] Fetching tags for %s…" % self.name) |
| 76 | + p = subprocess.check_output( |
| 77 | + ["git", "ls-remote", "--tags", "--sort=creatordate", self.url] |
| 78 | + ).decode("utf8") |
| 79 | + |
| 80 | + tags = [] |
| 81 | + for line in p.split("\n"): |
| 82 | + if line.strip() == "": |
| 83 | + continue |
| 84 | + |
| 85 | + match = line.split() |
| 86 | + |
| 87 | + hash = match[0] |
| 88 | + name = match[1].split("/")[2] |
| 89 | + |
| 90 | + tags.append((hash, name)) |
| 91 | + self._tags = tags |
| 92 | + return self._tags |
| 93 | + |
| 94 | + def out_of_date(self): |
| 95 | + return self.commit != self.latest_commit |
| 96 | + |
| 97 | + |
| 98 | +if os.getenv("GITHUB_ACTIONS") != "true": |
| 99 | + dn = os.path.dirname |
| 100 | + git_directory = dn(dn(dn(os.path.realpath(__file__)))) |
| 101 | + |
| 102 | + def git_command(*args): |
| 103 | + return subprocess.check_output(["git"] + list(args), cwd=git_directory).decode( |
| 104 | + "utf-8" |
| 105 | + )[:-1] |
| 106 | + |
| 107 | + repo_url = git_command("remote", "get-url", "origin") |
| 108 | + branch = git_command("branch", "--show-current") |
| 109 | + |
| 110 | + os.environ["REPO_URL"] = repo_url |
| 111 | + os.environ["BRANCH_NAME"] = branch |
| 112 | + os.environ["GITHUB_WORKSPACE"] = git_directory |
| 113 | + os.environ["GITHUB_EVENT_NAME"] = "workspace_dispatch" |
| 114 | + os.environ["GITHUB_RUN_ID"] = "mock_gha_run" |
| 115 | + |
| 116 | + def export_env_alt(key, value): |
| 117 | + os.environ[key] = value |
| 118 | + print("Setting ENV[%s] to %s..." % (key, value)) |
| 119 | + |
| 120 | + export_env = export_env_alt |
| 121 | + |
| 122 | +origin = os.getenv("REPO_URL") |
| 123 | +repo = Repo("parselib", origin) |
| 124 | + |
| 125 | +# public |
| 126 | +gh = SimpleNamespace( |
| 127 | + **{ |
| 128 | + "run_id": os.getenv("GITHUB_RUN_ID"), |
| 129 | + "origin": origin, |
| 130 | + "branch": os.getenv("BRANCH_NAME"), |
| 131 | + "root": os.getenv("GITHUB_WORKSPACE"), |
| 132 | + "pdk": os.getenv("PDK_ROOT"), |
| 133 | + "tool": os.getenv("TOOL"), |
| 134 | + "event": SimpleNamespace(**{"name": os.getenv("GITHUB_EVENT_NAME")}), |
| 135 | + "export_env": export_env, |
| 136 | + "Repo": Repo, |
| 137 | + "parselib": repo, |
| 138 | + } |
| 139 | +) |
0 commit comments