-
Notifications
You must be signed in to change notification settings - Fork 5
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
Implementation for hook to define phases of playback during git commands #122
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9a821e8
First draft of plackback hook logic
Wiseqube eff6d55
First running test
Wiseqube eb70b1d
Put command execution to gitexec decorator
Wiseqube 79e9aae
Add rollback on failure
Wiseqube 5051274
Restore old formatting in zodbsync.py
Wiseqube 9042f9b
Also show output of phase script when not failing
Wiseqube f28008d
Adjust formatting
Wiseqube 48253e7
Add entry in changelog, readme and config.py
Wiseqube 080dbc2
Refactoring of gitexec after review
Wiseqube e41ea0c
Update workflow to use latest major version of actions
Wiseqube File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
import os | ||
|
||
import filelock | ||
import json | ||
|
||
from .helpers import Namespace | ||
|
||
|
@@ -150,12 +151,80 @@ def gitexec(func): | |
- play back changed objects (diff between old and new HEAD) | ||
- unstash | ||
""" | ||
def _playback_paths(self, paths): | ||
paths = self.sync.prepare_paths(paths) | ||
dryrun = self.args.dry_run | ||
|
||
playback_hook = self.config.get('playback_hook', None) | ||
if playback_hook and os.path.isfile(playback_hook): | ||
proc = subprocess.Popen( | ||
playback_hook, stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, | ||
universal_newlines=True) | ||
out, _ = proc.communicate(json.dumps({'paths': paths})) | ||
returncode = proc.returncode | ||
if returncode: | ||
raise AssertionError( | ||
"Error calling playback hook, returncode " | ||
"{}, [[{}]] on {}".format( | ||
returncode, playback_hook, out | ||
) | ||
) | ||
phases = json.loads(out) | ||
else: | ||
phases = [{'name': 'playback', 'paths': paths}] | ||
if self.config.get('run_after_playback', None): | ||
phases[0]['cmd'] = self.config['run_after_playback'] | ||
|
||
for ix, phase in enumerate(phases): | ||
phase_name = phase.get('name') or str(ix) | ||
phase_cmd = phase.get('cmd') | ||
|
||
self.sync.playback_paths( | ||
paths=phase['paths'], | ||
recurse=False, | ||
override=True, | ||
skip_errors=self.args.skip_errors, | ||
dryrun=dryrun, | ||
) | ||
|
||
if not dryrun and phase_cmd and os.path.isfile(phase_cmd): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please turn this around and by using a negated condition and |
||
self.logger.info( | ||
'Calling phase %s, command: %s', phase_name, phase_cmd | ||
) | ||
proc = subprocess.Popen( | ||
phase_cmd, stdin=subprocess.PIPE, | ||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT, | ||
universal_newlines=True) | ||
out, _ = proc.communicate(json.dumps( | ||
{'paths': phase['paths']} | ||
)) | ||
returncode = proc.returncode | ||
|
||
if returncode: | ||
self.logger.error( | ||
"Error during phase command %s, %s", | ||
returncode, out | ||
) | ||
if sys.stdin.isatty(): | ||
print("Enter 'y' to continue, other to rollback") | ||
res = input() | ||
if res == 'y': | ||
continue | ||
|
||
raise AssertionError( | ||
"Unrecoverable error in phase command" | ||
) | ||
else: | ||
self.logger.info(out) | ||
|
||
@SubCommand.with_lock | ||
def wrapper(self, *args, **kwargs): | ||
# Check for unstaged changes | ||
self.check_repo() | ||
|
||
try: | ||
self.paths = [] | ||
func(self, *args, **kwargs) | ||
|
||
# Fail and roll back for any of the markers of an interrupted | ||
|
@@ -173,20 +242,10 @@ def wrapper(self, *args, **kwargs): | |
conflicts = files & set(self.unstaged_changes) | ||
assert not conflicts, "Change in unstaged files, aborting" | ||
|
||
# Strip site name from the start | ||
files = [fname[len(self.sync.site):] for fname in files] | ||
# Strip filename to get the object path | ||
dirs = [fname.rsplit('/', 1)[0] for fname in files] | ||
# Make unique and sort | ||
paths = sorted(set(dirs)) | ||
self.paths = sorted(set(files)) | ||
|
||
self.sync.playback_paths( | ||
paths=paths, | ||
recurse=False, | ||
override=True, | ||
skip_errors=self.args.skip_errors, | ||
dryrun=self.args.dry_run, | ||
) | ||
_playback_paths(self, self.paths) | ||
|
||
if self.args.dry_run: | ||
self.abort() | ||
|
@@ -223,6 +282,17 @@ def wrapper(self, *args, **kwargs): | |
self.logger.exception("Unable to show diff") | ||
|
||
self.abort() | ||
# if we are not in dryrun we can't be sure we havent already | ||
viktordick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# committed some stuff to the data-fs so playback all paths | ||
# abort | ||
if not self.args.dry_run and self.paths: | ||
self.sync.playback_paths( | ||
paths=self.paths, | ||
recurse=False, | ||
override=True, | ||
skip_errors=True, | ||
dryrun=False, | ||
) | ||
raise | ||
|
||
return wrapper | ||
|
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this embedded function? Could we pull this out of
def gitexec
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be a problem, I think this was due to the iterative development. Basically started in the
wrapper
but soon moved it out but kept it contained inside of gitexec