Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate absolute paths #6

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
"wsgi_max_request_body_size": "1073741824",
"wsgi_clear_untrusted_proxy_headers": false,

"environment": {
"zope_i18n_compile_mo_files": "true",
"CHAMELEON_CACHE": "{{ cookiecutter.location_clienthome }}/cache"
},
"environment_CHAMELEON_CACHE": "{{ cookiecutter.location_clienthome }}/cache",
"environment_zope_i18n_compile_mo_files": "true",

"initial_user_name": "",
"initial_user_password": "",
Expand Down Expand Up @@ -105,6 +103,9 @@
"profile_repoze_discard_first_request": "",
"profile_repoze_path": "",
"profile_repoze_flush_at_shutdown": "",
"profile_repoze_unwind": ""
"profile_repoze_unwind": "",

"_extensions": [
"local_extensions.AbsPathExtension"
]
}
35 changes: 3 additions & 32 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,17 @@
# post generation step 1: replace relative with absolute path
# theres no way to get this path while generating the files

from binascii import b2a_base64
from cookiecutter.utils import work_in
from hashlib import sha1
from pathlib import Path

import os
import random
import re
import string


target = """{{ cookiecutter.target }}"""
cwd = Path.cwd()
basedir = cwd.parent

with work_in(basedir):
for dir, subdirs, files in os.walk(cwd / "etc"):
for filename in files:
infile = Path(dir) / filename
lines = []
try:
with open(infile, "r") as fio:
for line in fio:
mo = re.match(r".*ABSPATH\((.*?)\).*", line)
if mo:
for path in mo.groups():
line = line.replace(
f"ABSPATH({path})", os.path.abspath(path)
)
lines.append(line)

with open(infile, "w") as fio:
fio.truncate()
for line in lines:
fio.writelines(line)
except Exception:
print(f"Can not replace ABSPATH() in file {infile}")


# post generation step 2: generate initial user
# post generation step 1: generate initial user
username = "{{ cookiecutter.initial_user_name }}" or "admin"
password = "{{ cookiecutter.initial_user_password }}"

Expand All @@ -60,12 +31,12 @@
os.chmod(inituser_filename, 0o644)


# post generation step 3: generate directories
# post generation step 2: generate directories
with work_in(basedir):
Path("{{ cookiecutter.location_clienthome }}").mkdir(parents=True, exist_ok=True)
Path("{{ cookiecutter.location_log }}").mkdir(parents=True, exist_ok=True)
Path("{{ cookiecutter.db_blobs_location }}").mkdir(parents=True, exist_ok=True)
Path("{{ cookiecutter.environment['CHAMELEON_CACHE'] }}").mkdir(parents=True, exist_ok=True)
Path("{{ cookiecutter.environment_CHAMELEON_CACHE }}").mkdir(parents=True, exist_ok=True)
if "{{ cookiecutter.db_storage }}" == "direct":
filepath = Path("{{ cookiecutter.db_filestorage_location }}")
filepath.parent.mkdir(parents=True, exist_ok=True)
24 changes: 24 additions & 0 deletions local_extensions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from jinja2.ext import Extension, pass_context
from pathlib import Path


class AbsPathExtension(Extension):

def __init__(self, environment):

super(AbsPathExtension, self).__init__(environment)
environment.filters['abspath'] = AbsPathExtension.abspath

@pass_context
def abspath(context, path, append_path=''):

try:
_context_cookiecutter = context.get('cookiecutter')
if _context_cookiecutter:
_output_dir = _context_cookiecutter.get('_output_dir')
if _output_dir:
new_path = Path(_output_dir, path, append_path).resolve().as_posix()
except Exception as e:
raise Exception(f"Failed to filter {context.name}") from e

return f"{new_path}"
25 changes: 11 additions & 14 deletions {{ cookiecutter.target }}/etc/zope.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# This file was generated by "cookiecutter-zope-instance"
%define INSTANCEHOME ABSPATH({{ cookiecutter.target }})
%define INSTANCEHOME {{ cookiecutter.target | abspath }}
instancehome $INSTANCEHOME

%define CLIENTHOME ABSPATH({{ cookiecutter.location_clienthome }})
%define CLIENTHOME {{ cookiecutter.location_clienthome | abspath }}
clienthome $CLIENTHOME

debug-mode {{ "on" if cookiecutter.debug_mode == "True" else "off" }}
Expand All @@ -11,23 +11,20 @@ verbose-security {{ "on" if cookiecutter.verbose_security =="True" else "off" }}

default-zpublisher-encoding utf-8

{%- if cookiecutter.environment %}
<environment>
{%- for key, value in cookiecutter.environment|dictsort %}
{{ key }} {{ value }}
{%- endfor %}

CHAMELEON_CACHE {{ cookiecutter.environment_CHAMELEON_CACHE | abspath }}
zope_i18n_compile_mo_files {{ cookiecutter.environment_zope_i18n_compile_mo_files }}
Copy link
Member

@jensens jensens Nov 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here a feature is lost, adding arbitrary environment strings. So we should loop over all strings starting with environment_, right?

</environment>
{%- endif %}

{%- endif %}
{%- if cookiecutter.dos_protection_available %}
<dos_protection>
form-memory-limit {{ cookiecutter.dos_protection_form_memory_limit }}
form-disk-limit {{ cookiecutter.dos_protection_form_disk_limit }}
form-memfile-limit {{ cookiecutter.dos_protection_form_memfile_limit }}
</dos_protection>
{%- endif %}

{%- endif %}
# Database
<zodb_db main>
mount-point /
Expand All @@ -44,8 +41,8 @@ default-zpublisher-encoding utf-8
{%- if cookiecutter.db_storage == "direct" %}
# Blob-enabled FileStorage database
<filestorage>
path ABSPATH({{ cookiecutter.db_filestorage_location}})
blob-dir ABSPATH({{ cookiecutter.db_blobs_location }})
path {{ cookiecutter.db_filestorage_location | abspath }}
blob-dir {{ cookiecutter.db_blobs_location | abspath }}
pack-keep-old {{ "true" if cookiecutter.db_filestorage_pack_keep_old else "false" }}
{%- if cookiecutter.db_filestorage_quota %}
quota {{ cookiecutter.db_filestorage_quota }}
Expand All @@ -64,7 +61,7 @@ default-zpublisher-encoding utf-8
<relstorage>
# blobs
shared-blob-dir {{ "true" if cookiecutter.db_blobs_mode == "shared" else "false" }}
blob-dir ABSPATH({{ cookiecutter.db_blobs_location }})
blob-dir {{ cookiecutter.db_blobs_location | abspath }}
# general settings
keep-history {{ cookiecutter.db_relstorage_keep_history|lower }}
read-only {{ cookiecutter.db_relstorage_read_only|lower }}
Expand Down Expand Up @@ -134,7 +131,7 @@ default-zpublisher-encoding utf-8
{%- if cookiecutter.db_relstorage == "sqlite3" %}
<sqlite3>
driver {{ cookiecutter.db_relstorae_sqlite3_driver }}
data_dir ABSPATH({{ cookiecutter.db_relstorae_sqlite3_data_dir }})
data_dir {{ cookiecutter.db_relstorae_sqlite3_data_dir | abspath }}
{%- if cookiecutter.db_relstorae_sqlite3_gevent_yield_interval %}
gevent-yield-interval {{ cookiecutter.db_relstorae_sqlite3_gevent_yield_interval }}
{%- endif %}
Expand All @@ -154,7 +151,7 @@ default-zpublisher-encoding utf-8
<zeoclient>
# blobs
shared-blob-dir {{ "true" if cookiecutter.db_blobs_mode == "shared" else "false" }}
blob-dir ABSPATH({{ cookiecutter.db_blobs_location }})
blob-dir {{ cookiecutter.db_blobs_location | abspath }}
# general settings
server {{ cookiecutter.db_zeo_server }}
name {{ cookiecutter.db_zeo_name }}
Expand Down
4 changes: 2 additions & 2 deletions {{ cookiecutter.target }}/etc/zope.ini
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ formatter = generic

[handler_accesslog]
class = FileHandler
args = (r'{{ cookiecutter.location_log }}/instance-access.log', 'a')
args = (r'{{ cookiecutter.location_log | abspath(append_path="instance-access.log") }}', 'a')
kwargs = {}
level = INFO
formatter = message

[handler_eventlog]
class = FileHandler
args = (r'{{ cookiecutter.location_log }}/instance.log', 'a')
args = (r'{{ cookiecutter.location_log | abspath("instance.log") }}', 'a')
kwargs = {}
level = NOTSET
formatter = generic
Expand Down