-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyam-block-until-release
executable file
·95 lines (79 loc) · 3.09 KB
/
pyam-block-until-release
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/env python
"""Block until a module is released."""
from __future__ import absolute_import
from __future__ import print_function
import argparse
import os
import subprocess
import sys
import time
def pyam_path():
"""Return path to pyam executable."""
bin_path = os.path.dirname(os.path.realpath(__file__))
sibling_candidate = os.path.join(bin_path, 'pyam')
return sibling_candidate if os.path.exists(sibling_candidate) else 'pyam'
def latest():
"""Return latest module information."""
process = subprocess.Popen([pyam_path(), 'latest'], stdout=subprocess.PIPE)
latest_text = process.communicate()[0]
if process.returncode != 0:
raise OSError('pyam failed\n')
return latest_text
def block(timeout, poll_interval,
shell_command=None, loop_duration=None,
test=False):
"""Block until module release is detected."""
loop_start_time = time.time()
while True:
if not test:
original = latest()
poll_start_time = time.time()
while original.strip() == latest().strip():
if time.time() - poll_start_time > timeout:
print('timed out')
break
time.sleep(poll_interval)
sys.stdout.write('.')
sys.stdout.flush()
if shell_command:
try:
subprocess.check_call(' '.join(shell_command), shell=True)
except subprocess.CalledProcessError as exception:
sys.stderr.write(str(exception) + '\n')
if loop_duration == 0 or not shell_command:
break
elif loop_duration > 0:
if time.time() - loop_start_time > loop_duration:
break
else:
# Negative numbers and None indicate that we should loop forever.
pass
def main():
"""Main."""
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--poll-interval', type=float, default=20.,
help='poll at this interval (in seconds)')
parser.add_argument('--timeout', type=float, default=3. * 60 * 60,
help='timeout after this interval (in seconds)')
parser.add_argument(
'--test', action='store_true',
help='run command instantaneously rather than waiting for release')
parser.add_argument(
'--loop-duration', type=float, default=None,
help='keep looping for this amount of time (in seconds); '
'default is to loop forever unless no shell command is given')
parser.add_argument('command', nargs='*',
help='shell command to execute after blocking')
args = parser.parse_args()
try:
block(timeout=args.timeout,
poll_interval=args.poll_interval,
shell_command=args.command,
loop_duration=args.loop_duration,
test=args.test)
except KeyboardInterrupt:
sys.stderr.write('\nInterrupted by user\n')
if __name__ == '__main__':
sys.exit(main())