Debian Codegraph @

continuous-delivery/pip-install-with-retry.py

raw symbols SCIP
1 import time
2 import sys
3 import subprocess
4
5 DOCS = """Given cmdline args, executes: python3 -m pip install [args...]
6 Keeps retrying until the new version becomes available in pypi (or we time out)"""
7 if len(sys.argv) < 2:
8 sys.exit(DOCS)
9
10 RETRY_INTERVAL_SECS = 10
11 GIVE_UP_AFTER_SECS = 60 * 15
12
13 pip_install_args = [sys.executable, '-m', 'pip', 'install'] + sys.argv[1:]
14
15 start_time = time.time()
16 while True:
17 print(subprocess.list2cmdline(pip_install_args))
18 result = subprocess.run(pip_install_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
19
20 stdout = result.stdout.decode().strip()
21 if stdout:
22 print(stdout)
23
24 if result.returncode == 0:
25 # success
26 sys.exit(0)
27
28 if "could not find a version" in stdout.lower():
29 elapsed_secs = time.time() - start_time
30 if elapsed_secs < GIVE_UP_AFTER_SECS:
31 # try again
32 print("Retrying in", RETRY_INTERVAL_SECS, "secs...")
33 time.sleep(RETRY_INTERVAL_SECS)
34 continue
35 else:
36 print("Giving up on retries after", int(elapsed_secs), "total secs.")
37
38 # fail
39 sys.exit(result.returncode)