-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
26 lines (24 loc) · 908 Bytes
/
utils.py
File metadata and controls
26 lines (24 loc) · 908 Bytes
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
import shutil
import re
import subprocess
################################################################################
# Helper Functions
################################################################################
def run(cmd, stdout=None):
"""Run a shell command and optionally redirect stdout."""
if stdout:
with open(stdout, 'w') as f:
subprocess.run(cmd, shell=True, stdout=f, stderr=subprocess.STDOUT)
else:
subprocess.run(cmd, shell=True, check=True)
def can_run_command(command):
"""Check if a command is available on the system."""
return shutil.which(command) is not None
def grep_stat(filename, pattern, group=1):
"""Extract a number from a file using a regex pattern."""
with open(filename) as f:
for line in f:
m = re.search(pattern, line)
if m:
return m.group(group)
return ""