-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shared.py
61 lines (52 loc) · 1.87 KB
/
Shared.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
import os
import sys
import logging
import subprocess
import datetime
import fnmatch
_default_tmp_dir = 'tmp'
def ensureHtmlElementsFromFile(path):
dom = minidom.parse(path)
ensureHtmlElementsFromDom(dom)
writeXmlSansInstructions(dom, path)
def ensureHtmlElementsFromDom(dom):
# now go through all 'important' tags and ensure they are not empty
for element_name in ['canvas', 'script', 'div', 'a', 'textarea', 'span']:
for element in dom.getElementsByTagName(element_name):
element.appendChild(dom.createTextNode(''))
def writeXmlSansInstructions(dom, file):
with open(file, "w") as fp:
for node in dom.childNodes:
node.writexml(fp)
def remove_if_exists(path):
if os.path.exists(path):
os.remove(path)
def get_tmp_file_name(source_file_name, tmp_dir = _default_tmp_dir):
name = os.path.basename(source_file_name)
safe_now = datetime.datetime.utcnow().isoformat().replace(':','_')
name = "{0}_{1}".format(safe_now, name)
if os.path.exists(tmp_dir) != True:
os.mkdir(tmp_dir)
return os.path.join(tmp_dir, name)
def run_process_file_command(command_func):
args, tmp_file, out_file = command_func()
stdoutdata = run_process(args)
logging.info("Moving temp file to '%s'", out_file)
remove_if_exists(out_file)
os.rename(tmp_file, out_file)
def run_process(args):
logging.basicConfig(format=' * %(message)s', level=logging.INFO)
logging.info('* * * * *')
logging.info('Requested command: %s', ' '.join(args))
proc = subprocess.Popen(args, stdout=sys.stdout)
logging.info('Running...')
proc.communicate()
if(proc.returncode != 0):
logging.error('Command failed')
sys.exit(1)
def find_files(directory, pattern):
for root, dirs, files in os.walk(directory):
for basename in files:
if fnmatch.fnmatch(basename, pattern):
filename = os.path.join(root, basename)
yield filename