summaryrefslogtreecommitdiff
path: root/support/lib/util.py
blob: 3c5117de7da529ca94c7d4f15a466f0080ea14ab (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import contextlib, subprocess, tempfile, shutil, re, os


class UserError(Exception):
	pass


def _temp_dir_is_on_same_mount_point():
	tempdir_stat = os.stat(tempfile.gettempdir())
	working_dir_stat = os.stat('.')
	
	return tempdir_stat.st_dev == working_dir_stat.st_dev


@contextlib.contextmanager
def TemporaryDirectory():
	if _temp_dir_is_on_same_mount_point():
		dir = None
	else:
		dir = '.'
	
	dir = tempfile.mkdtemp(dir = dir, prefix = '.tmp_')
	
	try:
		yield dir
	finally:
		shutil.rmtree(dir)


def command(args, remove_env = None):
	if remove_env is None:
		env = None
	else:
		env = dict(os.environ)
		
		for i in remove_env:
			if i in env:
				del env[i]
	
	process = subprocess.Popen(args, env = env)
	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()