/
utilities.py
70 lines (50 loc) · 1.87 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import os
import platform
from subprocess import Popen, PIPE
def check_output_name(filename):
''' Checks for the existance of a given filename and creates a new and unused one if file
already exists.
@param filename String filename (abspath)
@retun filenmae String possibly altered filename (basename)
'''
suffix = 1
while os.path.exists(filename):
base = os.path.basename(filename)
base = base.split('.')
if base[0].endswith('_{0}'.format(suffix - 1)):
new_base = base[0].replace('_{0}'.format(suffix - 1), '_{0}'.format(suffix)) + '.' + base[1]
else:
new_base = base[0] + '_{0}'.format(suffix) + '.' + base[1]
filename = os.path.join(os.path.dirname(filename), new_base)
suffix += 1
return filename
def get_system_info():
''' Function to retrieve system related information.
@return list of system variables
'''
sys_info = []
sys_info.append({'Report time': datetime.datetime.now().isoformat()})
sys_info.append({'Uname': platform.uname()})
if platform.system() == 'Linux':
sys_info.append({'Version': platform.linux_distribution()})
else:
sys_info.append({'Version': platform.win32_ver()})
return sys_info
def get_zonation_info():
''' Function to retrieve Zonation version info.
NOTE: Zonation must be in PATH.
@return tuple Zonation version number
'''
version = Popen(['zig3', '-v'], stdout=PIPE)
version = version.communicate()[0]
version = version.split('\n')[0].strip()
version = version.split(':')[1].strip()
version = tuple(version.split('.'))
return version
def pad_header(msg, print_width):
# - 4 is for 2 leading stars and 2 whitespaces
nstars = print_width - len(msg) - 4
return '\n** ' + msg + ' ' + '*' * nstars