Skip to content

Commit

Permalink
Init layers and add --freeze
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Dick committed Jan 31, 2024
1 parent 22d0157 commit 960f3e0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
7 changes: 6 additions & 1 deletion perfact/zodbsync/commands/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def add_args(parser):
help="Skip failed objects and continue",
default=False
)
parser.add_argument(
'--freeze', action='store_true', default=False,
help="Mark given paths as frozen",
)
parser.add_argument(
'path', type=str, nargs='*',
help='Sub-Path in Data.fs to be recorded',
Expand Down Expand Up @@ -118,7 +122,8 @@ def run(self):
for path in paths:
try:
self.sync.record(path=path, recurse=recurse,
skip_errors=self.args.skip_errors)
skip_errors=self.args.skip_errors,
freeze=self.args.freeze)
except AttributeError:
self.sync.logger.exception('Unable to record path ' + path)
pass
Expand Down
4 changes: 2 additions & 2 deletions perfact/zodbsync/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ def prop_dict(data):
return props


def load_config(filename, name='config'):
def load_config(filename):
'''Load the module at "filename" as module "name". Return the contents
as a dictionary. Skips contents starting with '_'.
'''
loader = importlib.machinery.SourceFileLoader(name, filename)
loader = importlib.machinery.SourceFileLoader('config', filename)
spec = importlib.util.spec_from_loader(loader.name, loader)
mod = importlib.util.module_from_spec(spec)
loader.exec_module(mod)
Expand Down
5 changes: 2 additions & 3 deletions perfact/zodbsync/tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1637,7 +1637,6 @@ def test_playback_hook_failed(self):
assert 'NewFolder' not in self.app.objectIds()
assert 'NewFolder2' not in self.app.objectIds()

@pytest.mark.xfail
def test_layer_record_freeze(self):
"""
Create a folder, copy it into an additional fixed layer and record
Expand All @@ -1656,9 +1655,9 @@ def test_layer_record_freeze(self):
f.write('path = "{}"'.format(layer))
with self.appendtoconf('layers = "{}"'.format(path)):
self.run('record', '--freeze', '/')
for fname in ['__meta__', '__frozen__']:
for fname in ['__meta__', '__frozen__', 'Test/__meta__']:
assert os.path.exists(
'{}/__root__/Test/{}'.format(self.repo.path, fname)
'{}/__root__/{}'.format(self.repo.path, fname)
)

os.remove(path + '/00-base.py')
Expand Down
20 changes: 19 additions & 1 deletion perfact/zodbsync/zodbsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# Plugins for handling different object types
from .object_types import object_handlers, mod_implemented_handlers
from .helpers import StrRepr, to_string, literal_eval, remove_redundant_paths
from .helpers import load_config


# Monkey patch ZRDB not to connect to databases immediately.
Expand Down Expand Up @@ -244,6 +245,17 @@ def __init__(self, config, logger, site='__root__'):
root = db.open(self.tm).root
self.app = root.Application

# Initialize layers
layerdir = self.config.get('layers', None)
self.layers = []
if layerdir and os.path.isdir(layerdir):
fnames = sorted(os.listdir(layerdir))
for fname in fnames:
self.layers.append(
load_config('{}/{}'.format(layerdir, fname))
| {'ident': fname}
)

# Make sure the manager user exists
if self.config.get('create_manager_user', False):
self.create_manager_user()
Expand Down Expand Up @@ -445,7 +457,7 @@ def fs_contents(self, path):
filenames = os.listdir(self.fs_path(path))
return sorted([f for f in filenames if not f.startswith('__')])

def record(self, path='/', recurse=True, skip_errors=False):
def record(self, path='/', recurse=True, skip_errors=False, freeze=False):
'''Record Zope objects from the given path into the local
filesystem.'''
if not path:
Expand All @@ -455,6 +467,12 @@ def record(self, path='/', recurse=True, skip_errors=False):
for part in path.split('/'):
if part:
obj = getattr(obj, part)
if freeze:
fullpath = self.fs_path(path)
os.makedirs(fullpath, exist_ok=True)
with open('{}/__frozen__'.format(fullpath), 'w'):
pass

self.record_obj(obj, path, recurse=recurse, skip_errors=skip_errors)

def record_obj(self, obj, path, recurse=True, skip_errors=False):
Expand Down

0 comments on commit 960f3e0

Please sign in to comment.