Skip to content

Instantly share code, notes, and snippets.

@pbamotra
Last active March 7, 2020 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbamotra/cf761dfa1005fe031ca96bbc0c9a0a16 to your computer and use it in GitHub Desktop.
Save pbamotra/cf761dfa1005fe031ca96bbc0c9a0a16 to your computer and use it in GitHub Desktop.
Save code snapshot to tar file
# 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)
# 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