blob: 8b65e58c9b58d0df2463b8e6df60810d253f2b91 (
plain)
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
|
import contextlib, subprocess, tempfile, shutil, re, os
class UserError(Exception):
pass
@contextlib.contextmanager
def TemporaryDirectory():
dir = tempfile.mkdtemp()
try:
yield dir
finally:
shutil.rmtree(dir)
def command(args):
process = subprocess.Popen(args)
process.wait()
if process.returncode:
raise UserError('Command failed: {}'.format(' '.join(args)))
def bash_escape_string(string):
return "'{}'".format(re.sub("'", "'\"'\"'", string))
def write_file(path, data):
temp_path = path + '~'
with open(temp_path, 'wb') as file:
file.write(data)
os.rename(temp_path, path)
def read_file(path):
with open(path, 'rb') as file:
return file.read()
|