-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
73 lines (64 loc) · 2.48 KB
/
utility.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
import os
import re
import subprocess
import unicodedata
def subprocess_popen(cmd):
try:
env_path = dict()
env_path['PATH'] = os.environ['PATH']
env_path['PATH'] = '/usr/local/bin:' + env_path['PATH']
return subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env_path, shell=False)
except FileNotFoundError as file_not_found_error:
print('something went wrong when trying to run {}'.format(cmd))
print(file_not_found_error)
exit(-1)
def slugify(value, allow_unicode=False):
"""
Taken from https://github.com/django/django/blob/master/django/utils/text.py
Convert to ASCII if 'allow_unicode' is False. Convert spaces or repeated
dashes to single dashes. Remove characters that aren't alphanumerics,
underscores, or hyphens. Convert to lowercase. Also strip leading and
trailing whitespace, dashes, and underscores.
"""
value = str(value)
if allow_unicode:
value = unicodedata.normalize('NFKC', value)
else:
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub(r'[^\w\s-]', '', value.lower())
return re.sub(r'[-\s]+', '-', value).strip('-_')
def check_tools(config):
# check other tools
for tool in config.other_tools.values():
proc = subprocess_popen(['which',tool[0]])
proc.wait()
if len(proc.stdout.readlines()) == 0:
print('{} is missing and is required'.format(tool[0]))
return False
# check decode tools
is_any_tool_installed = False
for tool in config.decode_tools.values():
proc = subprocess_popen(['which',tool[0]])
proc.wait()
is_any_tool_installed |= (len(proc.stdout.readlines()) > 0)
if not is_any_tool_installed:
error_msg = 'neither of '
for tool in config.decode_tools.values():
error_msg += tool[0] + ' '
error_msg += 'is installed'
print(error_msg)
return False
# check encode tools
is_any_tool_installed = False
for tool in config.encode_tools.values():
proc = subprocess_popen(['which',tool[0]])
proc.wait()
is_any_tool_installed |= (len(proc.stdout.readlines()) > 0)
if not is_any_tool_installed:
error_msg = 'neither of '
for tool in config.encode_tools.values():
error_msg += tool[0] + ' '
error_msg += 'are installed'
print(error_msg)
return False
return True