django-simplecms/cms/__main__.py

99 wiersze
2.4 KiB
Python
Czysty Zwykły widok Historia

2024-12-10 22:05:36 +00:00
"""
Main entry point.
"""
2021-07-03 23:59:48 +00:00
import argparse
import os
import re
import shutil
import cms
2021-07-03 23:59:48 +00:00
import example
2021-01-23 09:29:24 +00:00
def main():
2024-12-10 22:05:36 +00:00
"""
Ask the user to confirm, then call create_project().
"""
2021-07-03 23:59:48 +00:00
parser = argparse.ArgumentParser(description="SimpleCMS")
parser.add_argument("project_name", nargs="?", default=".")
2021-01-23 09:29:24 +00:00
project_name = parser.parse_args().project_name
2021-07-03 23:59:48 +00:00
if project_name == ".":
2021-01-23 09:29:24 +00:00
project_dir = os.getcwd()
project_name = os.path.basename(project_dir)
else:
project_dir = os.path.join(os.getcwd(), project_name)
2021-07-03 23:59:48 +00:00
if re.match(r"^\w+$", project_name):
if (
2024-12-10 00:03:13 +00:00
input(
f"Do you want to create a new project in {project_dir}? [yN] "
).lower()
== "y"
2021-07-03 23:59:48 +00:00
):
2021-01-23 09:29:24 +00:00
create_project(project_name, project_dir)
else:
2021-07-03 23:59:48 +00:00
print(f"Invalid project name: {project_name}")
2021-01-23 09:29:24 +00:00
def create_project(project_name, project_dir):
2024-12-10 22:05:36 +00:00
"""
Populate project directory with a minimal, working project.
"""
os.makedirs(project_dir, exist_ok=True)
2021-07-03 23:59:48 +00:00
with open(os.path.join(project_dir, "requirements.txt"), "w") as f:
2024-12-10 22:05:36 +00:00
print(f"django-simplecms=={cms.__version__}", file=f)
example_dir = os.path.dirname(example.__file__)
app_dir = os.path.join(project_dir, project_name)
shutil.copytree(example_dir, app_dir, dirs_exist_ok=True)
2021-07-03 23:59:48 +00:00
with open(
os.open(
os.path.join(project_dir, "manage.py"), os.O_CREAT | os.O_WRONLY, 0o755
),
"w",
) as f:
print(
2024-12-10 22:05:36 +00:00
f"""#!/usr/bin/env python3
import os
import sys
from django.core.management import execute_from_command_line
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{project_name}.settings")
execute_from_command_line(sys.argv)""",
2021-07-03 23:59:48 +00:00
file=f,
)
with open(os.path.join(project_dir, project_name, "wsgi.py"), "w") as f:
print(
f"""import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{project_name}.settings")
application = get_wsgi_application()""",
2021-07-03 23:59:48 +00:00
file=f,
)
with open(os.path.join(project_dir, ".gitignore"), "w") as f:
print("*.pyc\n__pycache__/", file=f)
print(
f"""
2024-12-10 22:05:36 +00:00
Successfully created project "{project_name}"!
Things to do next:
- create a database
- ./manage.py makemigrations
- ./manage.py migrate
- ./manage.py createsuperuser
- ./manage.py runserver
"""
2021-07-03 23:59:48 +00:00
)
if __name__ == "__main__":
main()