Last active
March 7, 2020 12:35
-
-
Save pbamotra/cf761dfa1005fe031ca96bbc0c9a0a16 to your computer and use it in GitHub Desktop.
Save code snapshot to tar file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://github.com/opencv/openvino_training_extensions/blob/develop/pytorch_toolkit/nncf/examples/common/utils.py#L86 | |
import tarfile | |
from pathlib import Path | |
def create_code_snapshot(root, dst_path, extensions=(".py", ".json", ".cpp", ".cu")): | |
"""Creates tarball with the source code""" | |
with tarfile.open(str(dst_path), "w:gz") as tar: | |
for path in Path(root).rglob("*"): | |
if '.git' in path.parts: | |
continue | |
if path.suffix.lower() in extensions: | |
tar.add(path.as_posix(), arcname=path.relative_to(root).as_posix(), recursive=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Source: https://github.com/opencv/openvino_training_extensions/blob/5a6acb36937614ff70507998bde082501e7fee52/pytorch_toolkit/nncf/examples/common/utils.py#L122 | |
import sys | |
class TeedStream: | |
"""Copy stdout to the file""" | |
def __init__(self, fname, mode='w'): | |
self.file = open(str(fname), mode) | |
self.stdout = sys.stdout | |
sys.stdout = self | |
def __del__(self): | |
sys.stdout = self.stdout | |
self.file.close() | |
def write(self, data): | |
self.file.write(data) | |
self.stdout.write(data) | |
def flush(self): | |
self.file.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment