__main__.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import sys
  2. import venv
  3. import asyncio
  4. from typing import cast
  5. from pathlib import Path
  6. from shlex import quote
  7. from django.core.management import call_command
  8. from . import const, utils, parser
  9. from .utils.git import Repo
  10. from .utils.files import write_file
  11. from .utils.process import run_cmd
  12. async def exec():
  13. args = parser.parse_args()
  14. project_path = Path(args.dir).absolute()
  15. print('Checking project directory...')
  16. if not project_path.exists():
  17. print('Project directory not found, creating project directory')
  18. project_path.mkdir()
  19. else:
  20. print('Project directory found')
  21. name = args.name
  22. print('Creating Django project...')
  23. call_command('startproject', name, str(project_path))
  24. print('Django project created')
  25. venv_path = project_path / 'venv'
  26. bin_path = venv_path / 'bin'
  27. pip_path = bin_path / 'pip'
  28. requirements_path = project_path / 'requirements.txt'
  29. args.packages.extend(const.REQUIRED_PACKAGES)
  30. print(f'Creating virtual environment at {venv_path}')
  31. if args.python:
  32. await run_cmd([
  33. args.python,
  34. '-m',
  35. 'venv',
  36. str(venv_path)
  37. ])
  38. else:
  39. venv.create(str(venv_path), with_pip=True)
  40. print(f'Created virtual environment at {venv_path}')
  41. tasks = [
  42. write_file(
  43. str(project_path / '.gitignore'),
  44. const.GITIGNORE
  45. ),
  46. write_file(
  47. str(project_path / '.env'),
  48. utils.generate_env(args.debug, args.hosts)
  49. ),
  50. write_file(
  51. str(project_path / 'README.md'),
  52. const.README
  53. ),
  54. write_file(
  55. str(project_path / 'env.example'),
  56. const.ENV_EXAMPLE
  57. ),
  58. utils.write_settings(str(project_path / args.name / 'settings.py')),
  59. utils.install_packages(
  60. str(pip_path),
  61. args.packages,
  62. str(requirements_path),
  63. *(('--no-compile',) if args.no_compile else tuple())
  64. )
  65. ]
  66. repo = cast(Repo, None) # In order to fix linter errors
  67. if args.git:
  68. repo = Repo(project_path)
  69. tasks.append(repo.init())
  70. print('Creating files...')
  71. await asyncio.gather(*tasks)
  72. print('Created files')
  73. tasks = []
  74. if args.git and args.commit:
  75. await repo.setup(args.email, args.username)
  76. await repo.add(('.',), _v=True)
  77. task = repo.commit(args.commit_message, _v=True)
  78. tasks.append(task)
  79. if args.migrate:
  80. python_path = bin_path / 'python'
  81. manage_path = project_path / 'manage.py'
  82. task = run_cmd((
  83. str(python_path),
  84. quote(str(manage_path)),
  85. 'migrate',
  86. '--no-input'
  87. ))
  88. tasks.append(task)
  89. await asyncio.gather(*tasks)
  90. def main():
  91. asyncio.run(exec())
  92. if __name__ == '__main__':
  93. try:
  94. main()
  95. except KeyboardInterrupt:
  96. sys.exit()