plone / cookiecutter-zope-instance

It bakes configuration for Zope 5
BSD 3-Clause "New" or "Revised" License
7 stars 4 forks source link

Generate absolute paths #5

Closed me-kell closed 7 months ago

me-kell commented 1 year ago

When creating a structure like the following (see below) we must run cookiecutter-zope-instance in the parts directory. Having previously run mkzeoinstance from ${PLONE_HOME} which creates the parts directory.

Some paths (CHAMELEON_CACHE, args in the sections handler_accesslog and handler_eventlog) are generated relative to the directory cookiecutter-zope-instance was called from from.

Starting runwsgi with the generated ${PLONE_HOME}/parts/client/etc/zope.ini from another directory than parts will not work.

${PLONE_HOME}/parts
├── blobstorage
├── client
│   ├── etc
│   │   ├── site.zcml
│   │   ├── zope.conf
│   │   └── zope.ini
│   ├── inituser
│   └── var
│       ├── cache
│       └── log
└── zeoserver
    ├── bin
    │   ├── runzeo
    │   └── zeoctl
    ├── etc
    │   └── zeo.conf
    ├── log
    └── var

I'd like to propose the following two steps as a solution:

  1. Add ABSPATH() to args in the sections handler_accesslog and handler_eventlog:
patch \{\{\ cookiecutter.target\ \}\}/etc/zope.ini << 'EOF'
@@ -84 +84 @@ class = FileHandler
-args = (r'{{ cookiecutter.location_log }}/instance-access.log', 'a')
+args = (r'ABSPATH({{ cookiecutter.location_log }}/instance-access.log)', 'a')
@@ -91 +91 @@ class = FileHandler
-args = (r'{{ cookiecutter.location_log }}/instance.log', 'a')
+args = (r'ABSPATH({{ cookiecutter.location_log }}/instance.log)', 'a')
EOF
  1. Add ABSPATH() to CHAMELEON_CACHE.

This is not trivial since the template zope.conf assumes a dictionary and is not aware of its items types.

On the other side, adding ABSPATH() to CHAMELEON_CACHE in cookiecutter.json is not a solution because this variable is used in post generation step 3: generate directories in post_gen_project.py.

post generation step 3 also assumes that cookiecutter.environment['CHAMELEON_CACHE'] exists to generate the cache's directory.

One could then remove this variable from the environment dictionary in cookiecutter.json and adding the CHAMELEON_CACHE as extra variable. This way the variable could be inserted in the template with ABSPATH. The conditional if cookiecutter.environmentt would be redundant.

patch cookiecutter.json << 'EOF'
@@ -11,0 +12 @@
+    "environment_CHAMELEON_CACHE": "{{ cookiecutter.location_clienthome }}/cache",
@@ -13,2 +14 @@
-        "zope_i18n_compile_mo_files": "true",
-        "CHAMELEON_CACHE": "{{ cookiecutter.location_clienthome }}/cache"
+        "zope_i18n_compile_mo_files": "true"
EOF

patch \{\{\ cookiecutter.target\ \}\}/etc/zope.conf << 'EOF'
@@ -14 +13,0 @@ default-zpublisher-encoding utf-8
-{%- if cookiecutter.environment %}
@@ -15,0 +15 @@ default-zpublisher-encoding utf-8
+    CHAMELEON_CACHE ABSPATH({{ cookiecutter.environment_CHAMELEON_CACHE }})
@@ -21 +20,0 @@ default-zpublisher-encoding utf-8
-{%- endif %}
EOF

patch hooks/post_gen_project.py << 'EOF'
@@ -68 +68 @@ with work_in(basedir):
-    Path("{{ cookiecutter.environment['CHAMELEON_CACHE'] }}").mkdir(parents=True, exist_ok=True)
+    Path("{{ cookiecutter.environment_CHAMELEON_CACHE }}").mkdir(parents=True, exist_ok=True)
EOF

This way we could start runwsgi from any directory.

jensens commented 1 year ago

-1 this would result in non-moveable projects. I tend to skip this here and go for generating all this with the other cookiecutter-zope-instance while running make

me-kell commented 1 year ago

this would result in non-moveable projects

I'm not sure I understand. Which part do you mean? The CHAMELEON_CACHE part or the replacing of your ABSPATH by the abspath filter?

The proposed extraction of CHAMELEON_CACHE from <environment> is of course opinionated. But on the other side I cannot imagine how to generate a project with a directory structure like the one described above (e.g. in a parts directory) and call runwsgi from another directory.

Regarding the absolute paths: AFAICS the projects generated are already "not-moveable". Or do I get something wrong? There are already a couple of ABSPATH (see etc/zope.conf). Here I simple replace your ABSPATH by an abspath filter which do exactly the same and takes into account OS specifics (e.g. slashes).

The intention behind was simply to offer some help after reading the comment on hooks/post_gen_project.py#L1f:

# post generation step 1: replace relative with absolute path # theres no way to get this path while generating the files

jensens commented 1 year ago

You are right. But: All this features are dup here, already covered in the other cookie cutter. I tend to start working on moving in the right direction instead of adding duct tape here.

me-kell commented 1 year ago

Now I'm really confused.

Which other cookie cutter do you mean? I'm here in the "wrong movie"?

jensens commented 1 year ago

yes, I am confused and been in the wrong movie. Sorry. m( Go on, nothing to see.

jensens commented 1 year ago

ok, all fine, I need more coffee in the morning.