Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding SnapshotManager #1598

Merged
merged 10 commits into from
Jan 15, 2021
1 change: 1 addition & 0 deletions submitit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
from .local.local import LocalJob as LocalJob
from .slurm.slurm import SlurmExecutor as SlurmExecutor
from .slurm.slurm import SlurmJob as SlurmJob
from .snapshot import SnapshotManager

__version__ = "1.1.5"
34 changes: 34 additions & 0 deletions submitit/snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import fnmatch
import os
import shutil
from subprocess import check_output
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved
from tempfile import NamedTemporaryFile

def run_cmd(str_args, **kwargs):
return check_output(str_args, shell=True, **kwargs).decode("utf-8").strip()
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved


class SnapshotManager:
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved
def __init__(
self, snapshot_dir: str, with_submodules: bool = False, exclude=None,
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved
):
self.snapshot_dir = snapshot_dir
self.original_dir = os.getcwd()
self.with_submodules = with_submodules
self.exclude = exclude or []

def __enter__(self):
self.original_dir = os.getcwd()
os.makedirs(self.snapshot_dir, exist_ok=True)
# Get the repository root
root_dir = run_cmd("git rev-parse --show-toplevel")
sub = "--recurse-submodules" if self.with_submodules else "-s"
# Get a list of all the checked in files that we can pass to rsync
with NamedTemporaryFile() as tfile:
# https://stackoverflow.com/a/51689219/4876946
files = run_cmd(f"git ls-files {sub} | grep -v ^16 | cut -f2- > {tfile.name}", cwd=root_dir).split("\n")
run_cmd(f"rsync -a --files-from={tfile.name} {root_dir} {self.snapshot_dir}")
os.chdir(self.snapshot_dir)

def __exit__(self, *args):
os.chdir(self.original_dir)
13 changes: 13 additions & 0 deletions submitit/test_snapshot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import os
from tempfile import TemporaryDirectory

from .snapshot import SnapshotManager


def test_snapshot():
cwd = os.getcwd()
with TemporaryDirectory() as tdir:
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved
with SnapshotManager(tdir):
assert os.getcwd() == tdir
assert os.path.exists(os.path.join(tdir, "submitit/test_snapshot.py"))
assert os.getcwd() == cwd
lematt1991 marked this conversation as resolved.
Show resolved Hide resolved