-
Notifications
You must be signed in to change notification settings - Fork 1
/
host_range_resolver.py
executable file
·43 lines (36 loc) · 1.16 KB
/
host_range_resolver.py
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
#! /usr/bin/env python
import sys
#
# 'foo01~foo03,foo05,foo10' resolved to 'foo01 foo02 foo03 foo05 foo10'
#
def parse_hostname(hostname):
final = len(hostname)
for index in range(len(hostname)):
if hostname[index:].isdigit():
final = index
break
return hostname[:final], hostname[final:]
first = True
result = []
for arg in sys.argv[1:]:
tokens = arg.split(',')
for token in tokens:
hosts = token.split('~')
hosts_len = len(hosts)
if first == True:
first = False
if hosts_len == 1:
result.append(hosts[0])
elif hosts_len == 2:
# hosts should share non-numeric substring
prefix0, postfix0 = parse_hostname(hosts[0])
prefix1, postfix1 = parse_hostname(hosts[1])
if prefix0 != prefix1:
print >> sys.stderr, 'invalid host range string'
sys.exit(1)
for index in range(int(postfix0), int(postfix1) + 1):
result.append(prefix0 + str(index))
else:
print >> sys.stderr, 'invalid host range string'
sys.exit(1)
print " ".join(result)