Skip to content

Commit

Permalink
* remove state provider .. this the responsibility of workflow engine…
Browse files Browse the repository at this point in the history
…s, not packtivity itself

  as it relates to orchestrating / genearting multiple states of multiple packtivities
  • Loading branch information
lukasheinrich committed Nov 20, 2017
1 parent 7ad7b4d commit b319928
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 91 deletions.
22 changes: 1 addition & 21 deletions packtivity/statecontexts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import importlib
import logging
from .posixfs_context import LocalFSState,LocalFSProvider
from .posixfs_context import LocalFSState

log = logging.getLogger(__name__)

Expand All @@ -22,23 +22,3 @@ def load_state(jsondata,deserialization_opts = None):
if jsondata['state_type'] == 'localfs':
return LocalFSState.fromJSON(jsondata)
raise TypeError('unknown state type {}'.format(jsondata['state_type']))

def load_provider(jsondata,deserialization_opts = None):
log.debug('load_provider opts %s', deserialization_opts)
deserialization_opts = deserialization_opts or {}
if jsondata == None:
return None
if 'state_provider' in deserialization_opts:
providerstring = deserialization_opts.get('state_provider','')
if providerstring.startswith('py:'):
_, module, providerclass = providerstring.split(':')
module = importlib.import_module(module)
providerclass = getattr(module,providerclass)
provideropts = {}
return providerclass.fromJSON(jsondata,**provideropts)
if 'PACKTIVITY_STATEPROVIDER' in os.environ:
module = importlib.import_module(os.environ['PACKTIVITY_STATEPROVIDER'])
return module.load_provider(jsondata)
if jsondata['state_provider_type'] == 'localfs_provider':
return LocalFSProvider.fromJSON(jsondata)
raise TypeError('unknown provider type {}'.format(jsondata['state_provider_type']))
70 changes: 0 additions & 70 deletions packtivity/statecontexts/posixfs_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,73 +95,3 @@ def fromJSON(cls,jsondata):
identifier = jsondata['identifier'],
dependencies = [LocalFSState.fromJSON(x) for x in jsondata['dependencies']]
)

def _merge_states(lhs,rhs):
return LocalFSState(lhs.readwrite + rhs.readwrite,lhs.readonly + rhs.readonly)

class LocalFSProvider(object):
def __init__(self, *base_states, **kwargs):
base_states = list(base_states)
self.nest = kwargs.get('nest', True)
self.ensure = kwargs.get('ensure', None)

first = base_states.pop()
assert first

self.base = first

while base_states:
next_state = base_states.pop()
if not next_state:
continue
self.base = _merge_states(self.base,next_state)

def new_provider(self,name):
new_base_ro = self.base.readwrite + self.base.readonly
new_base_rw = [os.path.join(self.base.readwrite[0],name)]
return LocalFSProvider(LocalFSState(new_base_rw,new_base_ro), nest = self.nest, ensure = self.ensure)


def new_state(self,name):
'''
creates a new context from an existing context.
if subdir is True it declares a new read-write nested under the old
context's read-write and adds all read-write and read-only locations
of the old context as read-only. This is recommended as it makes rolling
back changes to the global state made in this context easy.
else the same readwrite/readonly configuration as the parent context is used
'''

if self.base is None:
new_readwrites = [os.path.abspath(name)]
else:
new_readwrites = ['{}/{}'.format(self.base.readwrite[0],name)] if self.nest else self.base.readwrite

if self.nest:
# for nested directories, we want to have at lease read access to all data in parent context
new_readonlies = [ro for ro in itertools.chain(self.base.readonly,self.base.readwrite)] if self.base else []
else:
new_readonlies = self.base.readonly if self.base else []

log.debug('new context is: rw: %s, ro: ', new_readwrites, new_readonlies)
new_identifier = name.replace('/','_') # replace in case name is nested path
newstate = LocalFSState(readwrite = new_readwrites, readonly = new_readonlies, identifier = new_identifier)

if self.ensure:
newstate.ensure()
return newstate

def json(self):
return {
'state_provider_type': 'localfs_provider',
'base_state': self.base.json(),
'nest': self.nest,
'ensure': self.ensure
}

@classmethod
def fromJSON(cls,jsondata):
return cls(LocalFSState.fromJSON(jsondata['base_state']), nest = jsondata['nest'], ensure = jsondata['ensure'])

0 comments on commit b319928

Please sign in to comment.