|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | +This script uses the GitHub API to construct http_archive elements to be |
| 4 | +inserted into the WORKSPACE file. |
| 5 | +
|
| 6 | +It is a fork of: |
| 7 | +https://github.com/abseil/federation-hello/blob/0c1ef91b9cc5aabf15a3ff15761837d1c8da93a6/head_sync.py |
| 8 | +''' |
| 9 | +import hashlib |
| 10 | +import json |
| 11 | +import urllib3 |
| 12 | + |
| 13 | +HTTP_ARCHIVE_TEMPLATE = """http_archive( |
| 14 | + name = "{}", |
| 15 | + urls = ["{}"], # {} |
| 16 | + strip_prefix = "{}-{}", |
| 17 | + sha256 = "{}", |
| 18 | +)""" |
| 19 | + |
| 20 | +http = urllib3.PoolManager() |
| 21 | + |
| 22 | + |
| 23 | +class ExternalDependency(object): |
| 24 | + def workspace_rule(self): |
| 25 | + raise NotImplementedError('must implement workspace_rule()') |
| 26 | + |
| 27 | + |
| 28 | +class GitHubProject(ExternalDependency): |
| 29 | + def __init__(self, name, owner, repo): |
| 30 | + self.name = name |
| 31 | + self.owner = owner |
| 32 | + self.repo = repo |
| 33 | + |
| 34 | + def workspace_rule(self): |
| 35 | + # https://developer.github.com/v3/repos/commits/ |
| 36 | + request = http.request( |
| 37 | + 'GET', 'https://api.github.com/repos/{}/{}/commits'.format( |
| 38 | + project.owner, project.repo), |
| 39 | + headers = { 'User-Agent': 'Workspace Updater' }) |
| 40 | + response = json.loads(request.data.decode('utf-8')) |
| 41 | + commit = response[0]["sha"] |
| 42 | + date = response[0]["commit"]["committer"]["date"] |
| 43 | + url = 'https://github.com/{}/{}/archive/{}.zip'.format( |
| 44 | + project.owner, project.repo, commit) |
| 45 | + request = http.request('GET', url, |
| 46 | + headers = { 'User-Agent': 'Workspace Updater' }) |
| 47 | + sha256 = hashlib.sha256(request.data).hexdigest() |
| 48 | + return HTTP_ARCHIVE_TEMPLATE.format(project.name, url, date, project.repo, |
| 49 | + commit, sha256) |
| 50 | + |
| 51 | + |
| 52 | +PROJECTS = [ |
| 53 | + GitHubProject('com_google_absl', 'abseil', 'abseil-cpp'), |
| 54 | + GitHubProject('com_google_googletest', 'google', 'googletest'), |
| 55 | + GitHubProject('com_github_google_benchmark', 'google', 'benchmark'), |
| 56 | + GitHubProject('com_github_grpc_grpc', 'grpc', 'grpc'), |
| 57 | + GitHubProject('com_github_jupp0r_prometheus_cpp', 'jupp0r', 'prometheus-cpp'), |
| 58 | + GitHubProject('com_github_curl', 'curl', 'curl'), |
| 59 | + GitHubProject('com_github_tencent_rapidjson', 'Tencent', 'rapidjson'), |
| 60 | +] |
| 61 | + |
| 62 | +for project in PROJECTS: |
| 63 | + print(project.workspace_rule()) |
0 commit comments