File: pip-install-with-retry.py

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (39 lines) | stat: -rw-r--r-- 1,151 bytes parent folder | download | duplicates (3)
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
import time
import sys
import subprocess

DOCS = """Given cmdline args, executes: python3 -m pip install [args...]
Keeps retrying until the new version becomes available in pypi (or we time out)"""
if len(sys.argv) < 2:
    sys.exit(DOCS)

RETRY_INTERVAL_SECS = 10
GIVE_UP_AFTER_SECS = 60 * 15

pip_install_args = [sys.executable, '-m', 'pip', 'install'] + sys.argv[1:]

start_time = time.time()
while True:
    print(subprocess.list2cmdline(pip_install_args))
    result = subprocess.run(pip_install_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)

    stdout = result.stdout.decode().strip()
    if stdout:
        print(stdout)

    if result.returncode == 0:
        # success
        sys.exit(0)

    if "could not find a version" in stdout.lower():
        elapsed_secs = time.time() - start_time
        if elapsed_secs < GIVE_UP_AFTER_SECS:
            # try again
            print("Retrying in", RETRY_INTERVAL_SECS, "secs...")
            time.sleep(RETRY_INTERVAL_SECS)
            continue
        else:
            print("Giving up on retries after", int(elapsed_secs), "total secs.")

    # fail
    sys.exit(result.returncode)