Skip to content

Commit

Permalink
shamelessly steal better create-site script from plone.edu
Browse files Browse the repository at this point in the history
  • Loading branch information
pbauer committed Mar 9, 2024
1 parent 5c8a13a commit 15c22c5
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 136 deletions.
8 changes: 6 additions & 2 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ GREEN=`tput setaf 2`
RESET=`tput sgr0`
YELLOW=`tput setaf 3`

# Set distributions still in development
DISTRIBUTIONS="voltodemo"
ALLOWED_DISTRIBUTIONS="voltodemo"

PLONE_VERSION=6.0.10.1

BACKEND_FOLDER=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
Expand Down Expand Up @@ -150,8 +154,8 @@ test_quiet: ## run tests removing deprecation warnings
PYTHONWARNINGS=ignore ./bin/zope-testrunner --auto-color --auto-progress --test-path src/plone6demo/src/

.PHONY: create-site
create-site: instance/etc/zope.ini ## Create a new site from scratch
PYTHONWARNINGS=ignore ./bin/zconsole run instance/etc/zope.conf ./scripts/create_site.py
create-site: ## Create a new site using default distribution and default answers
DEVELOP_DISTRIBUTIONS=$(DISTRIBUTIONS) ALLOWED_DISTRIBUTIONS=$(DISTRIBUTIONS) PYTHONWARNINGS=ignore ./bin/zconsole run instance/etc/zope.conf scripts/create-site.py

.PHONY: start
start: ## Start a Plone instance on localhost:8080
Expand Down
98 changes: 98 additions & 0 deletions backend/scripts/create-site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from AccessControl.SecurityManagement import newSecurityManager
from pathlib import Path
from plone.distribution.api import site as site_api
from Testing.makerequest import makerequest

import json
import logging
import os
import transaction


logging.basicConfig(format="%(message)s")

# Silence some loggers
for logger_name in [
"GenericSetup.componentregistry",
"Products.MimetypesRegistry.MimeTypesRegistry",
]:
logging.getLogger(logger_name).setLevel(logging.ERROR)

logger = logging.getLogger("Plone Site Creation")
logger.setLevel(logging.DEBUG)

SCRIPT_DIR = Path().cwd() / "scripts"

truthy = frozenset(("t", "true", "y", "yes", "on", "1"))


def asbool(s):
"""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 s is None:
return False
if isinstance(s, bool):
return s
s = str(s).strip()
return s.lower() in truthy


app = makerequest(globals()["app"])

request = app.REQUEST

admin = app.acl_users.getUserById("admin")
admin = admin.__of__(app.acl_users)
newSecurityManager(None, admin)


def get_answers_file() -> Path:
filename = f"{ANSWERS}.json"
return SCRIPT_DIR / filename


def parse_answers(answers_file: Path, site_id: str = "") -> dict:
answers = json.loads(answers_file.read_text())
if "distribution" not in answers:
# This is a bug in plone.distribution and should be fixed there
answers["distribution"] = DISTRIBUTION
if site_id:
answers["site_id"] = site_id
return answers


# VARS
DISTRIBUTION = os.getenv("DISTRIBUTION", "voltodemo")
SITE_ID = os.getenv("SITE_ID") # if set, this overrides the value in ANSWERS
ANSWERS = os.getenv("ANSWERS", "default")
DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING"))

# Load site creation parameters
answers_file = get_answers_file()
answers = parse_answers(answers_file, SITE_ID)
site_id = answers["site_id"]


logger.info(f"Creating a new Plone site @ {site_id}")
logger.info(f" - Using the {DISTRIBUTION} distribution and answers from {answers_file}")


if site_id in app.objectIds() and DELETE_EXISTING:
app.manage_delObjects([site_id])
transaction.commit()
app._p_jar.sync()
logger.info(f" - Deleted existing site with id {site_id}")
else:
logger.info(
f" - Stopping site creation, as there is already a site with id {site_id} at the instance. "
"Set DELETE_EXISTING=1 to delete the existing site before creating a new one."
)

if site_id not in app.objectIds():
site = site_api._create_site(
context=app, distribution_name=DISTRIBUTION, answers=answers
)
transaction.commit()
app._p_jar.sync()
logger.info(" - Site created!")
66 changes: 0 additions & 66 deletions backend/scripts/create_site.py

This file was deleted.

8 changes: 8 additions & 0 deletions backend/scripts/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"site_id": "Plone",
"title": "Welcome to Plone 6",
"description": "Site created with a new Plone Distribution",
"default_language": "en",
"portal_timezone": "Europe/Berlin",
"setup_content": true
}
8 changes: 6 additions & 2 deletions classic/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ GREEN=`tput setaf 2`
RESET=`tput sgr0`
YELLOW=`tput setaf 3`

# Set distributions still in development
DISTRIBUTIONS="classicdemo"
ALLOWED_DISTRIBUTIONS="classicdemo"

PLONE_VERSION=6.0.10.1

BACKEND_FOLDER=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
Expand Down Expand Up @@ -150,8 +154,8 @@ test_quiet: ## run tests removing deprecation warnings
PYTHONWARNINGS=ignore ./bin/zope-testrunner --auto-color --auto-progress --test-path src/plonedemo.site/src/

.PHONY: create-site
create-site: instance/etc/zope.ini ## Create a new site from scratch
PYTHONWARNINGS=ignore ./bin/zconsole run instance/etc/zope.conf ./scripts/create_site.py
create-site: ## Create a new site using default distribution and default answers
DEVELOP_DISTRIBUTIONS=$(DISTRIBUTIONS) ALLOWED_DISTRIBUTIONS=$(DISTRIBUTIONS) PYTHONWARNINGS=ignore ./bin/zconsole run instance/etc/zope.conf scripts/create-site.py

.PHONY: start
start: ## Start a Plone instance on localhost:8080
Expand Down
98 changes: 98 additions & 0 deletions classic/scripts/create-site.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
from AccessControl.SecurityManagement import newSecurityManager
from pathlib import Path
from plone.distribution.api import site as site_api
from Testing.makerequest import makerequest

import json
import logging
import os
import transaction


logging.basicConfig(format="%(message)s")

# Silence some loggers
for logger_name in [
"GenericSetup.componentregistry",
"Products.MimetypesRegistry.MimeTypesRegistry",
]:
logging.getLogger(logger_name).setLevel(logging.ERROR)

logger = logging.getLogger("Plone Site Creation")
logger.setLevel(logging.DEBUG)

SCRIPT_DIR = Path().cwd() / "scripts"

truthy = frozenset(("t", "true", "y", "yes", "on", "1"))


def asbool(s):
"""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 s is None:
return False
if isinstance(s, bool):
return s
s = str(s).strip()
return s.lower() in truthy


app = makerequest(globals()["app"])

request = app.REQUEST

admin = app.acl_users.getUserById("admin")
admin = admin.__of__(app.acl_users)
newSecurityManager(None, admin)


def get_answers_file() -> Path:
filename = f"{ANSWERS}.json"
return SCRIPT_DIR / filename


def parse_answers(answers_file: Path, site_id: str = "") -> dict:
answers = json.loads(answers_file.read_text())
if "distribution" not in answers:
# This is a bug in plone.distribution and should be fixed there
answers["distribution"] = DISTRIBUTION
if site_id:
answers["site_id"] = site_id
return answers


# VARS
DISTRIBUTION = os.getenv("DISTRIBUTION", "classicdemo")
SITE_ID = os.getenv("SITE_ID") # if set, this overrides the value in ANSWERS
ANSWERS = os.getenv("ANSWERS", "default")
DELETE_EXISTING = asbool(os.getenv("DELETE_EXISTING"))

# Load site creation parameters
answers_file = get_answers_file()
answers = parse_answers(answers_file, SITE_ID)
site_id = answers["site_id"]


logger.info(f"Creating a new Plone site @ {site_id}")
logger.info(f" - Using the {DISTRIBUTION} distribution and answers from {answers_file}")


if site_id in app.objectIds() and DELETE_EXISTING:
app.manage_delObjects([site_id])
transaction.commit()
app._p_jar.sync()
logger.info(f" - Deleted existing site with id {site_id}")
else:
logger.info(
f" - Stopping site creation, as there is already a site with id {site_id} at the instance. "
"Set DELETE_EXISTING=1 to delete the existing site before creating a new one."
)

if site_id not in app.objectIds():
site = site_api._create_site(
context=app, distribution_name=DISTRIBUTION, answers=answers
)
transaction.commit()
app._p_jar.sync()
logger.info(" - Site created!")
66 changes: 0 additions & 66 deletions classic/scripts/create_site.py

This file was deleted.

8 changes: 8 additions & 0 deletions classic/scripts/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"site_id": "Plone",
"title": "Welcome to Plone 6",
"description": "Site created with a new Plone Distribution",
"default_language": "en",
"portal_timezone": "Europe/Berlin",
"setup_content": true
}

0 comments on commit 15c22c5

Please sign in to comment.