-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
51 lines (43 loc) · 1.82 KB
/
utilities.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
#!/python3/bin/python3
import subprocess
## Function: run_command
# Description: call the command in the os and return the stdout and stderr,
# or error message in case an exception is caught
# Parameters: command
# command: command to run in the os, it should be a list
# (the command and its parameters) E.g.: ['ps', 'aux']
def run_command(command):
# status of running the command (0= sucess, 1= fail)
status = 0
# call the command and catch the output in using the comminicate()
# method for Popen
try:
process = subprocess.Popen(command,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = process.communicate()
except:
stdout = stderr = 'There was an exception running ' + str(command)
status = 1
return status, stdout, stderr
## Function: format_string
# Description: give special format to lines for content of sections
# Parameters: title, string
# title: title for the line (appended at the beggining)
# string: content of the line (appended at the end)
def format_string(title, string):
# the subprocess module returns byte types, so we need to decode to utf-8
# And add some details to the string
string = string.decode('utf-8')
formatted_string = title + ': ' + string
return formatted_string
## Function: format_section
# Description: give special format to sections
# Parameters: section_title, section_content
# section_title: title for the section (appended at the begginig)
# section_content: content of the section (appended at the end)
def format_section(section_title, section_content):
# Format: Line1: section title, Line2: content, end: two blank lines
section = section_title + '\n'
section += str(section_content)
section += '\n\n'
return section