-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathhelpers.py
48 lines (38 loc) · 1.73 KB
/
helpers.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
import datetime
import os
import subprocess
import sys
from config import *
# Simple class to get floating-point values printed prettily
class PrettyFloat(float):
def __str__(self):
return "%0.3f" % self
# Executes a single command and returns stdout and stderr
def executeCommand(command, stdin = None, cwd = None):
with subprocess.Popen(command, stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = (subprocess.PIPE if stdin != None else None), cwd = cwd) as process:
stdout, stderr = process.communicate(input = (stdin.encode("utf-8") if stdin != None else None))
print(stderr.decode("utf-8"), file = sys.stderr)
sys.stderr.flush()
if process.returncode != 0:
raise RuntimeError(command[0] + " failed with exit code " + str(process.returncode))
return stdout, stderr
# Convenience function to parse a date from a string
def parseDate(string):
return datetime.datetime.strptime(string, "%Y-%m-%d").date()
# The location of the local working copy of the data repository
def locateDataDirectory():
tmpDirectory = configuration["tmpDirectory"]
return os.path.join(tmpDirectory, "hubble-data")
# Create a local clone of the data repository if not present and update it
def prepareDataDirectory(dataDirectory, fetchChanges = True):
# Create data directory if not existing
if not os.path.exists(dataDirectory):
os.makedirs(dataDirectory)
# Get the data directory up-to-date
if fetchChanges:
# Clone data repository if necessary
if not os.path.exists(os.path.join(dataDirectory, ".git")):
executeCommand(["git", "clone", configuration["repositoryURL"], "."], cwd = dataDirectory)
else:
executeCommand(["git", "fetch"], cwd = dataDirectory)
executeCommand(["git", "reset", "--hard", "origin/master"], cwd = dataDirectory)