From 16c9eb132240ff0bde20fa8292c99f76c5b8b8b1 Mon Sep 17 00:00:00 2001 From: "Jens W. Klein" Date: Thu, 6 Jun 2024 15:32:41 +0200 Subject: [PATCH] purge and more rename --- pyproject.toml | 2 +- src/mxmake/templates/plone-site.py | 36 +++++++++++-------- src/mxmake/tests/test_templates.py | 46 +++++++++++++++---------- src/mxmake/topics/applications/plone.mk | 44 ++++++++++++----------- 4 files changed, 73 insertions(+), 55 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index d2ec4ec..9dc3398 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -89,5 +89,5 @@ python_version = 3.8 [tool.ruff] # Exclude a variety of commonly ignored directories. exclude = [ - "src/mxmake/templates/plone-createsite.py", + "src/mxmake/templates/plone-site.py", ] diff --git a/src/mxmake/templates/plone-site.py b/src/mxmake/templates/plone-site.py index 210182e..7d8278a 100644 --- a/src/mxmake/templates/plone-site.py +++ b/src/mxmake/templates/plone-site.py @@ -22,12 +22,7 @@ def asbool(value: str|bool|None) -> bool: return value.strip().lower() in TRUTHY -DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING")) - - -app = makerequest(globals()["app"]) -admin = app.acl_users.getUserById("admin") -newSecurityManager(None, admin.__of__(app.acl_users)) +PLONE_SITE_PURGE = asbool(os.getenv("PLONE_SITE_PURGE")) config = { {% for key, value in site.items() %} @@ -43,13 +38,26 @@ def asbool(value: str|bool|None) -> bool: {% endfor %} "profile_id": _DEFAULT_PROFILE, } +config["setup_content"] = asbool(config["setup_content"]) + +app = makerequest(globals()["app"]) +admin = app.acl_users.getUserById("admin") +newSecurityManager(None, admin.__of__(app.acl_users)) + +if PLONE_SITE_PURGE: + if config["site_id"] in app.objectIds(): + app.manage_delObjects([config["site_id"]]) + transaction.commit() + app._p_jar.sync() + else: + print(f"Site with id {config['site_id']} does not exist!") + exit(0) + -if config["site_id"] in app.objectIds() and DELETE_EXISTING: - app.manage_delObjects([config["site_id"]]) - transaction.commit() - app._p_jar.sync() +if config["site_id"] in app.objectIds(): + print(f"Site with id {config['site_id']} already exists!") + exit(1) -if config["site_id"] not in app.objectIds(): - site = create(app, "{{ distribution }}", config) - transaction.commit() - app._p_jar.sync() +site = create(app, "{{ distribution }}", config) +transaction.commit() +app._p_jar.sync() diff --git a/src/mxmake/tests/test_templates.py b/src/mxmake/tests/test_templates.py index b42f989..5f1013e 100644 --- a/src/mxmake/tests/test_templates.py +++ b/src/mxmake/tests/test_templates.py @@ -823,7 +823,7 @@ def test_MxIni(self, tempdir): ) @testing.template_directory() - def test_PloneCreateSite_all_defaults(self, tempdir): + def test_PloneSite_all_defaults(self, tempdir): mxini = tempdir / "mx.ini" with mxini.open("w") as fd: fd.write( @@ -859,7 +859,7 @@ def test_PloneCreateSite_all_defaults(self, tempdir): template.write() with (tempdir / "plone-site.py").open() as f: self.checkOutput( - """ + ''' from AccessControl.SecurityManagement import newSecurityManager from plone.distribution.api.site import create from Products.CMFPlone.factory import _DEFAULT_PROFILE @@ -873,10 +873,10 @@ def test_PloneCreateSite_all_defaults(self, tempdir): def asbool(value: str|bool|None) -> bool: - \"\"\"Return the boolean value ``True`` if the case-lowered value of string + """Return the boolean value ``True`` if the case-lowered value of string input ``s`` is a :term:`truthy string`. If ``s`` is already one of the boolean values ``True`` or ``False``, return it. - \"\"\" + """ if value is None: return False if isinstance(value, bool): @@ -884,12 +884,7 @@ def asbool(value: str|bool|None) -> bool: return value.strip().lower() in TRUTHY - DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING")) - - - app = makerequest(globals()["app"]) - admin = app.acl_users.getUserById("admin") - newSecurityManager(None, admin.__of__(app.acl_users)) + PLONE_SITE_PURGE = asbool(os.getenv("PLONE_SITE_PURGE")) config = { "site_id": "Plone", @@ -902,16 +897,29 @@ def asbool(value: str|bool|None) -> bool: ], "profile_id": _DEFAULT_PROFILE, } + config["setup_content"] = asbool(config["setup_content"]) - if config["site_id"] in app.objectIds() and DELETE_EXISTING: - app.manage_delObjects([config["site_id"]]) - transaction.commit() - app._p_jar.sync() + app = makerequest(globals()["app"]) + admin = app.acl_users.getUserById("admin") + newSecurityManager(None, admin.__of__(app.acl_users)) - if config["site_id"] not in app.objectIds(): - site = create(app, "", config) - transaction.commit() - app._p_jar.sync() - """, + if PLONE_SITE_PURGE: + if config["site_id"] in app.objectIds(): + app.manage_delObjects([config["site_id"]]) + transaction.commit() + app._p_jar.sync() + else: + print(f"Site with id {config['site_id']} does not exist!") + exit(0) + + + if config["site_id"] in app.objectIds(): + print(f"Site with id {config['site_id']} already exists!") + exit(1) + + site = create(app, "", config) + transaction.commit() + app._p_jar.sync() + ''', f.read(), ) diff --git a/src/mxmake/topics/applications/plone.mk b/src/mxmake/topics/applications/plone.mk index 38a9c11..2d2ad1e 100644 --- a/src/mxmake/topics/applications/plone.mk +++ b/src/mxmake/topics/applications/plone.mk @@ -2,20 +2,20 @@ #:title = plone #:description = Plone application related #:depends = applications.zope -#:[target.plone-createsite] -#:description = Creates a Plone site using the script provided in `PLONE_CREATESITE_SCRIPT` configuration. +#:[target.plone-site-create] +#:description = Creates a Plone site using the script provided in `PLONE_SITE_SCRIPT` configuration. #: -#:[setting.PLONE_CREATESITE_SCRIPT] -#:description = Path to the script to create a Plone site -#:default = .mxmake/files/plone-createsite.py +#:[setting.PLONE_SITE_SCRIPT] +#:description = Path to the script to create or purge a Plone site +#:default = .mxmake/files/plone-site.py #: -#:[target.plone-createsite-dirty] +#:[target.plone-site-dirty] #:description = Touches the sentinel file to force a rebuild of the Plone instance. This will not touch the database. #: -#:[target.plone-createsite-clean] +#:[target.plone-site-clean] #:description = Removes the sentinel file to force files but keeps Plone database. #: -#:[target.plone-createsite-purge] +#:[target.plone-site-purge] #:description = Removes the Plone instance from the database, but the database itself is kept. @@ -23,20 +23,22 @@ # plone ############################################################################## -PLONE_CREATESITE_SENTINEL:=$(SENTINEL_FOLDER)/plone-createsite.sentinel +PLONE_SITE_TARGET: $(FILES_TARGET) $(ZOPE_RUN_TARGET) -PLONE_CREATESITE_TARGET: $(FILES_TARGET) $(ZOPE_RUN_TARGET) - -.PHONY: plone-createsite -plone-createsite: PLONE_CREATESITE_TARGET +.PHONY: plone-site-create +plone-site-create: PLONE_SITE_TARGET @echo "Creating Plone Site" - @zconsole run $(ZOPE_INSTANCE_FOLDER)/etc/zope.conf $(PLONE_CREATESITE_SCRIPT) - @touch $(PLONE_CREATESITE_SENTINEL) + @zconsole run $(ZOPE_INSTANCE_FOLDER)/etc/zope.conf $(PLONE_SITE_SCRIPT) + +plone-site-purge: PLONE_SITE_TARGET + @echo "Purging Plone Site" + @export PLONE_SITE_PURGE=True + @zconsole run $(ZOPE_INSTANCE_FOLDER)/etc/zope.conf $(PLONE_SITE_SCRIPT) -.PHONY: plone-createsite-dirty -plone-createsite-dirty: - @touch $(PLONE_CREATESITE_SENTINEL) +.PHONY: plone-site-dirty +plone-site-dirty: + @touch $(PLONE_SITE_SENTINEL) -.PHONY: plone-createsite-clean -plone-createsite-clean: - @touch $(PLONE_CREATESITE_SENTINEL) +.PHONY: plone-site-clean +plone-site-clean: + @touch $(PLONE_SITE_SENTINEL)