-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_task_logger.py
38 lines (28 loc) · 1.04 KB
/
file_task_logger.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
import os
from datetime import datetime
from pathlib import Path
import git
import yaml
class NullFileTaskLogger:
def __init__(self, *args, **kwargs):
pass
def write_metadata_file(self, *args, **kwargs):
pass
def new_file(self, *args, **kwargs):
return Path(os.devnull)
class FileTaskLogger:
def __init__(self, parent_directory, task):
self.task = task
self.directory = Path(parent_directory).joinpath(self.task.name)
self.directory.mkdir(parents=True, exist_ok=True)
self.write_metadata_file()
def write_metadata_file(self):
repo = git.Repo(search_parent_directories=True)
sha = repo.head.object.hexsha
metadata = {**self.task.configuration, **{'git-sha': sha, 'timestamp': datetime.now()}}
with self.directory.joinpath('metadata.yaml').open('w') as metadata_file:
yaml.dump(metadata, metadata_file)
def new_file(self, filename):
new_file = self.directory.joinpath(filename)
new_file.touch()
return new_file