Skip to content

Commit

Permalink
Allow for moving snapshot tag to another snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
jfischer committed Mar 17, 2019
1 parent 621eee8 commit a55766a
Showing 1 changed file with 48 additions and 6 deletions.
54 changes: 48 additions & 6 deletions dataworkspaces/commands/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
# Copyright 2018,2019 by MPI-SWS and Data-ken Research. Licensed under Apache 2.0. See LICENSE.txt.

import os
from os.path import join, exists, isdir
from os.path import join, exists, isdir, basename, dirname
import re
import json
import datetime
import getpass
from copy import copy

import click

from dataworkspaces.utils.subprocess_utils import call_subprocess
from dataworkspaces.utils.git_utils import \
is_a_git_hash, is_a_shortened_git_hash, validate_git_fat_in_path_if_needed
is_a_git_hash, is_a_shortened_git_hash, \
validate_git_fat_in_path_if_needed, GIT_EXE_PATH
from dataworkspaces.resources.resource import CurrentResources
from dataworkspaces.resources.snapshot_utils import \
expand_dir_template, validate_template, make_re_pattern_for_dir_template
Expand All @@ -22,7 +25,7 @@
from dataworkspaces.utils.lineage_utils import \
get_current_lineage_dir, get_snapshot_lineage_dir,\
LineageStoreCurrent
from dataworkspaces.errors import ConfigurationError
from dataworkspaces.errors import ConfigurationError, UserAbort

def find_metadata_for_tag(tag, workspace_dir):
"""Return the snapshot metadata for the specified
Expand Down Expand Up @@ -202,6 +205,36 @@ def run(self):
def __str__(self):
return "Write snapshot metadata file to .dataworkspace/snapshot_metadata/HASH_md.json"


class RemoveTagFromOldSnapshot(actions.Action):
@actions.requires_from_ns('snapshot_hash', str)
def __init__(self, ns, verbose, workspace_dir, existing_tag_md, remove_tag):
super().__init__(ns, verbose)
self.remove_tag = remove_tag
self.old_hash_file = join(get_snapshot_metadata_dir_path(workspace_dir),
existing_tag_md['hash'] + '_md.json')
self.snapshot_data = copy(existing_tag_md)
old_len = len(self.snapshot_data['tags'])
self.snapshot_data['tags'] = [tag for tag in self.snapshot_data['tags']
if tag!=remove_tag]
assert old_len!=len(self.snapshot_data['tags']), \
"did not remove tag %s from %s"%(remove_tag, self.snapshot_data['hash'])
self.snapshot_data['updated_timestamp'] = datetime.datetime.now().isoformat()

def run(self):
if self.ns.snapshot_hash==self.snapshot_data['hash']:
print("No need to remove tag %s from snapshot %s, as that is also the new snapshot we are taking"%
(self.remove_tag, self.snapshot_data['hash']))
return
with open(self.old_hash_file, 'w') as f:
json.dump(self.snapshot_data, f, indent=2)
call_subprocess([GIT_EXE_PATH, 'add', basename(self.old_hash_file)],
cwd=dirname(self.old_hash_file), verbose=self.verbose)

def __str__(self):
return "Remove tag %s from old commit %s" % (self.remove_tag, self.old_hash)


class SaveLineageData(actions.Action):
@actions.requires_from_ns('snapshot_hash', str)
@actions.requires_from_ns('map_of_hashes', dict)
Expand Down Expand Up @@ -267,9 +300,15 @@ def snapshot_command(workspace_dir, batch, verbose, tag=None, message=''):
if tag is not None:
existing_tag_md = find_metadata_for_tag(tag, workspace_dir)
if existing_tag_md:
raise ConfigurationError("Tag '%s' already exists for snapshot %s taken %s"%
(tag, existing_tag_md['hash'],
existing_tag_md['timestamp']))
msg = "Tag '%s' already exists for snapshot %s taken %s"%\
(tag, existing_tag_md['hash'],
existing_tag_md['timestamp'])
if batch:
raise ConfigurationError(msg)
elif not click.confirm(msg + ". Remove this tag so we an add it to the new snapshot?"):
raise UserAbort()
else:
existing_tag_md = None
current_resources = CurrentResources.read_current_resources(workspace_dir, batch, verbose)
resource_names = current_resources.by_name.keys()
plan = []
Expand Down Expand Up @@ -308,6 +347,9 @@ def snapshot_command(workspace_dir, batch, verbose, tag=None, message=''):
plan.append(actions.GitAdd(ns, verbose, workspace_dir,
lambda:[ns.snapshot_filename,
ns.snapshot_metadata_file]))
if existing_tag_md is not None:
plan.append(RemoveTagFromOldSnapshot(ns, verbose, workspace_dir,
existing_tag_md, tag))
# see if we need to add lineage files
save_lineage = SaveLineageData(ns, verbose, workspace_dir, resource_names,
results_resources, rel_dest_root)
Expand Down

0 comments on commit a55766a

Please sign in to comment.