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

Know about current context in hooks #1983

Open
michael-todorovic opened this issue Nov 23, 2023 · 5 comments
Open

Know about current context in hooks #1983

michael-todorovic opened this issue Nov 23, 2023 · 5 comments
Labels
enhancement This issue/PR relates to a feature request.

Comments

@michael-todorovic
Copy link

  • Cookiecutter version: 2.5.0

Description:

I'm trying to run stuff in hooks depending on I'm replaying or not

What I've run:

I have created some py/sh hooks but I can't find how is run cookiecutter. For example, I'd like to know if I used --replay-file from my hook. I tried an export or echo $@ in bash or dump cookiecutter in python but I didn't anything useful for me.

Would you have an idea?
Thanks!

@michael-todorovic
Copy link
Author

Ah, in fact I can just get my parent cmdline 😄

In bash:

cat /proc/$PPID/cmdline | tr '\0' ' '

and in python:

import psutil
import os

ppid = psutil.Process(os.getppid())
print(ppid.cmdline())

and work this output from there 🚀

If there's a more native way, I'll be happy to use it 🙏

@ericof ericof added the enhancement This issue/PR relates to a feature request. label Nov 27, 2023
@ericof
Copy link
Member

ericof commented Nov 27, 2023

Hello @michael-todorovic, Could you expand on your use case a bit more? I would love to know what do you plan to use this for.

@michael-todorovic
Copy link
Author

Hello,
We're starting to replace a in-house tool by cookiecutter in order to render skeletons in infra provisioning repos. I'm missing a skeleton instantiation feature but as I'm in a POC, I don't have time (yet) to contribute with a PR, doing quick&dirty code to make sure it's the right tool for us 😄

A skeleton can be instantiated multiple time on a single repo: think about a postgresql skeleton and that we can have several postgresql clusters per repo. This should create multiple replay files as answers will differ from one cluster to another.

Currently, the replay file is named against the skeleton name. In my cookiecutter.json, I have a subcomponent key used to build the cluster name. I use this subcomponent to rename the replay file with

src_path = ".cookiecutter-replay/{{ cookiecutter.__project_slug }}.json"
dest_path = ".cookiecutter-replay/{{ cookiecutter.__replay_name }}.json"
if os.path.exists(src_path):
    print(f"Post-hook: renaming {src_path} to {dest_path}")
    os.rename(src_path, dest_path)

in post-hook. In my cookiecutter.json, I have

"__replay_name": "{{ cookiecutter.__project_slug }}_{{ cookiecutter.subcomponent }}"

In my repo, this results in:

├── .cookiecutter-replay
│   ├── patroni_cookie.json
│   └── patroni_cutter.json

so anyone from my team can replay an upgraded skeleton later with the same answers. I wanted to know in the hook whether I'm in a replay mode or not in order to avoid renaming the json file (and likely some other things I didn't think of).

I'm pretty sure that "runmode" awareness can be useful in some other cases 😄
Also, I think I'll get time on Q4/Q1 to implement a skeleton instantiation feature if you don't already have it in the v3 roadmap 🙏

@igor-lobanov-maersk
Copy link

igor-lobanov-maersk commented Nov 28, 2023

I've got a similar challenge, but in a different context.

I use cruft to manage template drift. In my cookiecutter template I use post-hooks to initialise some gitignored .env files for local directory. Post-hook asks for Personal Access Tokens and does some access validation. When 'cruft update' runs, it runs the template twice (old and new versions) to generate the diff to apply, and I don't want post-hook to run during that period. If I could reliably detect the replay condition from the hook, I could shortcut it.

I am conscious that this looks more like a workaround for cruft and the point of replay is to reproduce the behaviour like for like. However, I think this feature would be useful for anyone who uses post-hooks for validation or environment setup, which does not make sense to repeat during replay.

@michael-todorovic
Copy link
Author

I use cruft to manage template drift.

@igor-lobanov-maersk thanks for linking that tool, I wasn't aware of it 😄 👀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue/PR relates to a feature request.
Projects
None yet
Development

No branches or pull requests

3 participants