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

Provenance tracking #76

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ceci/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Ceci n'est pas une pipeline"""

from .provenance import Provenance
from .stage import PipelineStage
from .pipeline import Pipeline, MiniPipeline, ParslPipeline, DryRunPipeline
from .handle import BaseIOHandle
from pkg_resources import DistributionNotFound
from pkg_resources import get_distribution

try:
__version__ = get_distribution(__name__).version
except DistributionNotFound: # pragma: no cover
# package is not installed
pass
__version__ = "unknown"
20 changes: 20 additions & 0 deletions ceci/handle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import copy

class BaseIOHandle:
"""
Base class for input and output objects. For now this is pretty empty.
"""
suffix = ""

def __init__(self, provenance):
# If provenance is provided then pull that out
self.provenance = provenance

@property
def provenance(self):
return self._provenance

@provenance.setter
def provenance(self, provenance):
# always copy the provenance
self._provenance = copy.deepcopy(provenance)
2 changes: 2 additions & 0 deletions ceci/provenance/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from .provenance import Provenance
from .errors import *
22 changes: 22 additions & 0 deletions ceci/provenance/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class ProvenanceError(Exception):
pass


class ProvenanceFileTypeUnknown(ProvenanceError):
pass


class ProvenanceFileSchemeUnsupported(ProvenanceError):
pass


class ProvenanceMissingFile(ProvenanceError):
pass


class ProvenanceMissingSection(ProvenanceError):
pass


class ProvenanceMissingItem(ProvenanceError):
pass
81 changes: 81 additions & 0 deletions ceci/provenance/git.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
from .utils import get_caller_directory
import subprocess


def diff(dirname=None, parent_frames=1):
"""
Run git diff in the caller's directory (default) or another specified directory,
and return stdout+stderr
"""
if dirname is None:
dirname = get_caller_directory(parent_frames + 1)

if dirname is None:
return "ERROR_GIT_NO_DIRECTORY"
# We use git diff head because it shows all differences,
# including any that have been staged but not committed.
try:
diff = subprocess.run(
"git diff HEAD".split(),
cwd=dirname,
universal_newlines=True,
timeout=5,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)

# There are lots of different ways this can go wrong.
# Here are some - any others it is probably worth knowing
# about
except subprocess.TimeoutExpired:
return "ERROR_GIT_TIMEOUT"
except UnicodeDecodeError:
return "ERROR_GIT_DECODING"
except subprocess.SubprocessError:
return "ERROR_GIT_OTHER"
except FileNotFoundError:
return "ERROR_GIT_NOT_RUNNABLE"
except OSError:
return "ERROR_GIT_OTHER_OSERROR"
# If for some reason we are running outside the main repo
# this will return an error too
if diff.returncode:
return "ERROR_GIT_FAIL"

return diff.stdout


def current_revision(dirname=None, parent_frames=1):
"""Return the git revision ID in the caller's directory (default) or another
specified directory.
"""
if dirname is None:
dirname = get_caller_directory(parent_frames + 1)

if dirname is None:
return "ERROR_GIT_NO_DIRECTORY"
try:
rev = subprocess.run(
"git rev-parse HEAD".split(),
cwd=dirname,
universal_newlines=True,
timeout=5,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
# Same as git diff above.
except subprocess.TimeoutExpired:
return "ERROR_GIT_TIMEOUT"
except UnicodeDecodeError:
return "ERROR_GIT_DECODING"
except subprocess.SubprocessError:
return "ERROR_GIT_OTHER"
except FileNotFoundError:
return "ERROR_GIT_NOT_RUNNABLE"
except OSError:
return "ERROR_GIT_OTHER_OSERROR"
# If for some reason we are running outside the main repo
# this will return an error too
if rev.returncode:
return "ERROR_GIT_FAIL"
return rev.stdout
Loading