-
Notifications
You must be signed in to change notification settings - Fork 3
/
toolbox.py
73 lines (58 loc) · 2.08 KB
/
toolbox.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
#!/usr/bin/python3
"""
Useful functions and classes
"""
import os
import pathlib
def enabled_in_env(var, fallback_var=None):
"""Returns True for environment variables with non-zero value."""
val1 = os.environ.get(var)
val2 = os.environ.get(fallback_var) if fallback_var else None
return (val1 and val1 != '0') or (val2 and val2 != '0')
def guess_game_install_dir(directory=None):
"""Return absolute path pointing to game installation directory.
Path will be escaped in a way, that allows it to be injected into
quoted shell commands.
This function assumes current working directory is a subdirectory
of an installation directory.
"""
path = directory or os.getcwd()
posix_path = pathlib.PurePosixPath(path)
assert posix_path.is_absolute()
posix_parts = posix_path.parts
steam_lib_pattern = ('steamapps'.casefold(), 'common'.casefold())
share_games_pattern = ('share', 'games')
lib_pattern_found, pos = False, -1
prev_part_2, prev_part_1, this_part = '', '', ''
for i, part in enumerate(posix_parts):
prev_part_2 = prev_part_1
prev_part_1 = this_part
this_part = part.casefold()
if steam_lib_pattern == (prev_part_2, prev_part_1) or \
share_games_pattern == (prev_part_2, prev_part_1):
lib_pattern_found = True
pos = i
break
if not lib_pattern_found:
return None
found_path = os.path.join(*posix_parts[:pos + 1])
return found_path.replace(' ', r'\ ').replace('&', r'\&')
class PidFile:
"""Helper class to create and remove PID file"""
def __init__(self, file_name):
self.file_name = file_name
def __enter__(self):
with open(self.file_name, 'w') as pid_file:
pid_file.write(str(os.getpid()))
return self
def __exit__(self, exc_type, exc_value, exc_tb):
try:
os.remove(self.file_name)
except FileNotFoundError:
pass
def rm_force(path):
"""Forcefully remove the file."""
try:
os.remove(path)
except FileNotFoundError:
pass