-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.py
75 lines (54 loc) · 1.89 KB
/
utils.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
#!/usr/bin/env python2
import logging
import hexdump
import subprocess
"""
Several utility functions.
"""
# https://stackoverflow.com/questions/11602386/python-function-for-capping-a-string-to-a-maximum-length
def cap(s, l):
return s if len(s) <= l else s[0:l - 3] + '...'
def filenameWithoutExtension(file_name):
if '.' in file_name:
separator_index = file_name.rindex('.')
base_name = file_name[:separator_index]
return base_name
else:
return file_name
def xstr(s):
if s is None:
return '-'
return str(s)
def shortSeed(s):
if s is None:
return '-'
return s[:4]
def setupSlaveLoggingWithFile(threadId):
f = 'ffw-debug-slave-' + str(threadId) + '.log'
fileh = logging.FileHandler(f, 'a')
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
fileh.setFormatter(formatter)
log = logging.getLogger() # root logger
for hdlr in log.handlers[:]: # remove all old handlers
log.removeHandler(hdlr)
log.addHandler(fileh) # set the new handler
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.WARN)
# set a format which is simpler for console use
formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
# tell the handler to use this format
console.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
def hexdumpc(str):
hexdump.hexdump_ex( bytearray(str) )
def setupTmpfs(config, enable=True):
if enable:
logging.info("Mounting tmpfs")
cmd = [ "/bin/mount", "-t", "tmpfs", "tmpfs", config['temp_dir'] ]
subprocess.call(cmd)
else:
logging.info("UnMounting tmpfs")
cmd = [ "/bin/umount", config['temp_dir'] ]
subprocess.call(cmd)