-
Notifications
You must be signed in to change notification settings - Fork 2
/
log.py
92 lines (78 loc) · 2.66 KB
/
log.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
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
import sys
# Turn logging on or off
enabled = True
def enable ():
global enabled
enabled = True
def disable ():
global enabled
enabled = False
# Turn halting on or off (halting turned off means it will raise an exception
# instead, which you can catch)
halting = True
def set_halting ( on = True ):
global halting
halting = on
# Function for printing section headings in the console output
def heading ( title ):
if not enabled:
return
print()
print()
print( title )
print( '-' * len( title ) )
# Pad text on the left to right-justify it
def right_justify ( text, width ):
width = max( width, 25 )
return ( ' ' * ( width - len( text ) ) ) + text
# Print a table of key-value pairs in helpful output format
def out_str ( **kwargs ):
result = ''
width = max( len( key ) for key in kwargs )
for key, value in kwargs.items():
result += right_justify( key, width ) + ': ' + str( value ) + '\n'
return result
def out ( **kwargs ):
if not enabled:
return
print( out_str( **kwargs ) )
# Print a table of error info and quit the build process
def error ( message, **kwargs ):
global halting
out_arg = { **{ 'Build error' : message }, **kwargs }
if halting:
enable() # To ensure the problem that kills the process gets reported
out( **out_arg )
sys.exit( 1 )
else:
raise Exception( out_str( **out_arg ) )
# Print a table of warning info but do not quit the build process
def warning ( message, **kwargs ):
out( **{ **{ 'Build warning' : message }, **kwargs } )
# Print a table of any kind of info
def info ( message, **kwargs ):
out( **{ **{ 'Build info' : message }, **kwargs } )
# Print info about copying a file
def file_copy ( source, dest, **kwargs ):
out( **{ **{ 'Copied: Source' : source, 'Dest' : dest }, **kwargs } )
# Print info about deleting a file
def file_delete ( filename, **kwargs ):
out( **{ **{ 'Running rm on' : filename }, **kwargs } )
# Print info about a missing file that will cause us to rebuild it
def file_missing ( source, **kwargs ):
out( **{ f"Must rebuild because DNE": source, **kwargs } )
# Print info about building a file from sources
def built ( desc, target, *sources, **kwargs ):
out( **{
f"Built {desc}": target,
**{ f"Newer than source {i+1}": sources[i] for i in range(len(sources)) },
**kwargs
} )
print()
# Print info about not building a file when that's not needed
def not_built ( target, *sources, **kwargs ):
out( **{
"Not rebuilding target": target,
**{ f"Newer than source {i+1}": sources[i] for i in range(len(sources)) },
**kwargs
} )