forked from DevTable/gantryd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
50 lines (38 loc) · 1.45 KB
/
util.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
43
44
45
46
47
48
49
50
import docker
import datetime
import socket
from termcolor import colored, cprint
def enum(*sequential, **named):
""" Represent an enumeration. Originally from: http://stackoverflow.com/questions/36932/whats-the-best-way-to-implement-an-enum-in-python """
enums = dict(zip(sequential, range(len(sequential))), **named)
return type('Enum', (), enums)
ReportLevels = enum(BACKGROUND=-2, EXTRA=-1, NORMAL=0, IMPORTANT=1)
client = docker.Client(version='auto')
def pickUnusedPort():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('localhost', 0))
addr, port = s.getsockname()
s.close()
return port
def report(msg, level=ReportLevels.NORMAL, project=None, component=None):
""" Reports a message to the console. """
print_string = ''
if component:
print_string += colored('[' + component.getName() + '] ', 'green')
color = 'white'
attrs = []
if level == ReportLevels.BACKGROUND or level == ReportLevels.EXTRA:
attrs.append('dark')
elif level == ReportLevels.IMPORTANT:
attrs.append('bold')
if level == ReportLevels.BACKGROUND:
dtstring = datetime.datetime.now().strftime("%d/%m/%Y %I:%M%p")
msg = dtstring + ' | ' + msg
print_string += colored(msg, color, attrs=attrs)
print print_string
def fail(reason, project=None, component=None, exception=None):
""" Fails due to some unexpected error. """
raise Exception(reason)
def getDockerClient():
""" Returns the docker client. """
return client