Repository navigation
Expand file tree
/
Copy pathcommon.py
More file actions
67 lines (54 loc) · 1.54 KB
/
Copy pathcommon.py
File metadata and controls
67 lines (54 loc) · 1.54 KB
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
import subprocess
import tempfile
import os
import sys
import shutil
import atexit
import colorama
import logging
from logging import debug, info, warning, error
logging.basicConfig(format=f'%(levelname)s: %(message)s',level=logging.INFO)
def rm(*files):
for f in files:
try:
if os.path.isdir(f):
shutil.rmtree(f)
else:
os.remove(f)
except:
pass
tmp_files = []
def get_tmp(suffix='', prefix=''):
tmp = tempfile.mkstemp(suffix, prefix)[1]
tmp_files.append(tmp)
return tmp
def get_tmp_dir():
tmp = tempfile.mkdtemp()
tmp_files.append(tmp)
return tmp
@atexit.register
def cleanup():
if tmp_files:
info('Cleaning up temporary files: ' + ', '.join(tmp_files))
rm(*tmp_files)
del tmp_files[:]
def call_prog(prog, arguments=[], get_output=False):
cmd = [prog] + arguments
info(' '.join(cmd))
try:
if get_output:
return subprocess.check_output(cmd)
else:
subprocess.check_call(cmd)
except OSError as e:
if e.errno == os.errno.ENOENT:
fatal_error('{} is not in your PATH'.format(prog))
else:
fatal_error('Error running {}: {}'.format(prog, e))
except subprocess.CalledProcessError:
fatal_error(f'Command {prog} failed')
def fatal_error(msg):
error(colorama.Style.BRIGHT + colorama.Fore.RED + msg + colorama.Style.RESET_ALL)
info(f'leaving temporary files: {tmp_files}')
atexit.unregister(cleanup)
sys.exit(1)