|
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 retry information in headers and exponential backoff to address rate limiting issues. |
| 15 | + See: https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#handle-rate-limit-errors-appropriately |
| 16 | + """ |
| 17 | + exp_backoff = 60 |
| 18 | + retry_count = 0 |
| 19 | + while True: |
| 20 | + try: |
| 21 | + content = urlopen( |
| 22 | + f"https://api.github.com/repos/ray-project/ray/git/ref/tags/ray-{ver}" |
| 23 | + ).read() |
| 24 | + return json.loads(content)["object"]["sha"] |
| 25 | + except HTTPError as e: |
| 26 | + if retry_count < 10 and e.code == 403 and e.msg == "rate limit exceeded": |
| 27 | + retry_count += 1 |
| 28 | + timeout = exp_backoff |
| 29 | + if "retry-after" in e.hdrs: |
| 30 | + timeout = int(e.hdrs["retry-after"]) |
| 31 | + elif e.hdrs.get("x-ratelimit-remaining", None) == "0": |
| 32 | + timeout = max(int(e.hdrs["x-ratelimit-reset"]) - time.gmtime(), 0) |
| 33 | + else: |
| 34 | + exp_backoff *= 1.5 |
| 35 | + print(f"{e}:, retrying after {timeout} s", file=sys.stderr) |
| 36 | + time.sleep(timeout) |
| 37 | + continue |
| 38 | + raise |
| 39 | + |
7 | 40 |
|
8 | 41 | versionpy = sys.argv[1] |
9 | 42 | ver = sys.argv[2] |
10 | 43 |
|
11 | 44 | assert os.path.exists(versionpy) |
12 | 45 |
|
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: |
| 46 | +with open(versionpy, "r") as fid: |
16 | 47 | txt = fid.read() |
| 48 | + |
| 49 | +sha = get_commit_sha(ver) |
| 50 | + |
17 | 51 | txt = txt.replace("{{RAY_COMMIT_SHA}}", sha) |
18 | 52 | assert f'version = "{ver}"' in txt |
19 | 53 | assert f'commit = "{sha}"' in txt |
20 | | -with open(versionpy, 'w') as fid: |
| 54 | +with open(versionpy, "w") as fid: |
21 | 55 | fid.write(txt) |
0 commit comments