python - Cookiecutter 将一个文件夹复制到多个不同名称的文件夹
问题描述
我的 Cookiecutter 项目的结构是这样的
├── project
│ ├── {{Cookiecutter.client_name}}
│ │ ├── {{Cookiecutter.account_name}}
│ │ │ │── some-folder
│ │ │ ├── {{Cookiecutter.account_name}}.py
cookiecutter.json
看起来像这样
{
"client_name": "client",
"account_name": "account"
}
现在,代替一个account_name
。我想添加多个 account_names 以便 Cookiecutter 生成项目时{{Cookiecutter.client_name}}
应该有多个文件夹,如 account1、account2、account3 等。
我浏览了 Cookiecutter 文档,找不到任何有意义的内容或如何去做。
解决方案
AFAIK 如果帐户数量未提前固定,则使用 cookiecutter 是不可能的。但是你可以用一个钩子来达到同样的效果。像这样的东西:(nb未经测试)
├── project
│ ├── {{ client_name }}
│ │ ├── account_name
│ │ │ │── some-folder
│ │ │ ├── account_name.py
| |-- hooks
| | |-- post_gen_project.py
# post_gen_project.py
# runs from the *generated* project
from pathlib import Path
from shutil import copy, rmtree
accounts = [x.strip() for x in {{ accounts }}.split(",")] # cookiecutter will render this for you
client_name = {{ client_name }}
src = Path(client_name)
for account in accounts:
copy(src / "account_name", src/account)
fn = src/ f"account/account_name.py"
fn.rename(fn.with_name(f"{account}.py"))
rmtree(src)
如果您有很多事情要做,最好自己编写部署脚本并避免使用 cookiecutter。
我假设您的帐户是,
分开输入的:我不记得是否有任何方法可以在 cookiecutter 中获取原始列表类型。
另一种选择是使用嵌套的 cookiecutters——具体来说,account
从 post_gen 钩子中调用一个 cookiecutter for,在一个循环中,直到用户不再想要添加帐户。像这样的东西(再次未经测试,作为起点提供):
├── project
│ ├── {{ client_name }}
│ │ ├── account_template
| | | |-- {{ account_name }}
│ | │ │ │── some-folder
│ | │ │ ├── {{ account_name }}.py
| |-- hooks
| | |-- post_gen_project.py
# cookiecutter_project.json
...
"_copy_without_render": ["account_template/"]
这将我们的内部 cookiecutter 部署到我们想要的位置,而无需渲染它。然后在内部 cookiecutter 中:
# account_template/cookiecutter.json
{ "account_name": "default account", "client_name": "default client"}
最后,在外部 cookiecutter 的钩子中:
# hooks/post_gen_project.py
from shutil import rmtree
from cookiecutter.main import cookiecutter
client_name = {{ client_name }}
cont = True
while cont:
account_name = input("Enter account name: ")
cookiecutter(
"template_account",
no_input=True,
extra_content=dict(
account_name=account_name,
client_name=client_name
)
)
cont = not input("Press enter to continue").strip()
rmtree("template_account")
如果预先确定了帐户数量,这很容易——只需命名一组account1
,下一组,account2
依此类推。
参考:
https://cookiecutter.readthedocs.io/en/1.7.3/advanced/hooks.html https://cookiecutter.readthedocs.io/en/1.7.3/advanced/suppressing_prompts.html#suppressing-command-line-prompts
推荐阅读
- jmeter - JMeter- 从 JSON 响应中提取多个变量并将其保存在 CSV 文件中
- reactjs - React-router-dom NavLink 不渲染但进入路径
- mysql - MySQL查询从不同列中查找数据并在线离线检查
- node.js - 猫鼬查询以增加项目数组中的字段(如果存在),否则将其添加到文档中(很难措辞这个标题)
- google-apps-script - 将 GET 发送到 space.members.list 时出现“无效参数”
- node.js - 从 MongoDB 获取数据以获取 dc.js 图表
- wordpress - Ngnix/Wordpress 将子文件夹无缝重定向到具有代理传递的子目录
- ruby-on-rails - 未初始化的常量 Ahoy::QueryMethods - Ahoy Gem
- amazon-web-services - 定价:AWS Dynamo db 与 AWS Cloudwatch
- javascript - 第二个 Javascript 函数未运行