81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
#!/usr/bin/env python
|
||
"""
|
||
项目初始化脚本
|
||
用于快速初始化新项目
|
||
"""
|
||
import os
|
||
import shutil
|
||
import secrets
|
||
import sys
|
||
|
||
def generate_secret_key():
|
||
"""生成安全的密钥"""
|
||
return secrets.token_hex(32)
|
||
|
||
def init_project():
|
||
"""初始化项目"""
|
||
print("=" * 50)
|
||
print("Python项目初始化脚本")
|
||
print("=" * 50)
|
||
|
||
# 获取项目名称
|
||
project_name = input("请输入项目名称(用于替换your_app): ").strip()
|
||
if not project_name:
|
||
print("错误: 项目名称不能为空")
|
||
sys.exit(1)
|
||
|
||
# 生成密钥
|
||
secret_key = generate_secret_key()
|
||
print(f"\n生成的SECRET_KEY: {secret_key}")
|
||
|
||
# 创建.env文件
|
||
if not os.path.exists('.env'):
|
||
print("\n创建.env文件...")
|
||
shutil.copy('env.example', '.env')
|
||
|
||
# 替换SECRET_KEY
|
||
with open('.env', 'r') as f:
|
||
content = f.read()
|
||
content = content.replace('your-secret-key-here-change-this-in-production', secret_key)
|
||
with open('.env', 'w') as f:
|
||
f.write(content)
|
||
print("✓ .env文件已创建")
|
||
else:
|
||
print("⚠ .env文件已存在,跳过创建")
|
||
|
||
# 创建必要目录
|
||
directories = ['logs', 'uploads', 'migrations']
|
||
for directory in directories:
|
||
if not os.path.exists(directory):
|
||
os.makedirs(directory)
|
||
print(f"✓ 创建目录: {directory}")
|
||
|
||
# 重命名应用目录(可选)
|
||
rename = input(f"\n是否重命名应用目录 src/your_app 为 src/{project_name}? (y/n): ").strip().lower()
|
||
if rename == 'y':
|
||
old_path = 'src/your_app'
|
||
new_path = f'src/{project_name}'
|
||
if os.path.exists(old_path) and not os.path.exists(new_path):
|
||
shutil.move(old_path, new_path)
|
||
print(f"✓ 已重命名: {old_path} -> {new_path}")
|
||
print(f"⚠ 请手动更新以下文件中的导入路径:")
|
||
print(f" - run_dev.py")
|
||
print(f" - run_production.py")
|
||
print(f" - gunicorn.conf.py")
|
||
print(f" - tests/conftest.py")
|
||
print(f" - 所有测试文件")
|
||
|
||
print("\n" + "=" * 50)
|
||
print("初始化完成!")
|
||
print("=" * 50)
|
||
print("\n下一步:")
|
||
print("1. 编辑.env文件,配置数据库和其他设置")
|
||
print("2. 安装依赖: pip install -r requirements/base.txt -r requirements/development.txt")
|
||
print("3. 初始化数据库: flask db init && flask db migrate -m 'Initial migration' && flask db upgrade")
|
||
print("4. 运行项目: python run_dev.py")
|
||
print("\n")
|
||
|
||
if __name__ == '__main__':
|
||
init_project()
|
||
|