|
1 | 1 | # Call like python3 __file__ python/ray/_version.py 2.49.2 |
2 | 2 |
|
3 | | -import json; |
| 3 | +import json |
4 | 4 | import os |
5 | 5 | import sys |
6 | | -from urllib.request import urlopen; |
| 6 | +import time |
| 7 | +from urllib.request import HTTPError, urlopen |
| 8 | + |
| 9 | + |
| 10 | +def get_commit_sha(ver: str) -> str: |
| 11 | + """ |
| 12 | + Return sha from version tag commit |
| 13 | + |
| 14 | + Use exponential backoff to address rate limiting issues. |
| 15 | + """ |
| 16 | + timeout = 16 |
| 17 | + while True: |
| 18 | + try: |
| 19 | + content = urlopen( |
| 20 | + f"https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}" |
| 21 | + ).read() |
| 22 | + return json.loads(content)["object"]["sha"] |
| 23 | + except HTTPError as e: |
| 24 | + if e.code == 403 and e.msg == "rate limit exceeded" and timeout <= 128: |
| 25 | + print(f"{e}, retrying after {timeout} s", file=sys.stderr) |
| 26 | + print("headers", e.hdrs, file=sys.stderr) |
| 27 | + print("env keys", list(os.environ)) |
| 28 | + time.sleep(timeout) |
| 29 | + timeout *= 2 |
| 30 | + continue |
| 31 | + raise |
| 32 | + |
7 | 33 |
|
8 | 34 | versionpy = sys.argv[1] |
9 | 35 | ver = sys.argv[2] |
10 | 36 |
|
11 | 37 | assert os.path.exists(versionpy) |
12 | 38 |
|
13 | | -content = urlopen(f'https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}').read() |
14 | | -sha = json.loads(content)['object']['sha'] |
15 | | -with open(versionpy, 'r') as fid: |
| 39 | +with open(versionpy, "r") as fid: |
16 | 40 | txt = fid.read() |
| 41 | + |
| 42 | +sha = get_commit_sha(ver) |
| 43 | + |
17 | 44 | txt = txt.replace("{{RAY_COMMIT_SHA}}", sha) |
18 | 45 | assert f'version = "{ver}"' in txt |
19 | 46 | assert f'commit = "{sha}"' in txt |
20 | | -with open(versionpy, 'w') as fid: |
| 47 | +with open(versionpy, "w") as fid: |
21 | 48 | fid.write(txt) |
0 commit comments