__main__.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import sys
  2. import venv
  3. import asyncio
  4. from pathlib import Path
  5. from shlex import quote
  6. from django.core.management import call_command
  7. from . import const, utils, parser
  8. from .utils.git import Repo
  9. from .utils.files import write_file
  10. from .utils.process import run_cmd
  11. async def exec():
  12. args = parser.parse_args()
  13. project_path = Path(args.dir).absolute()
  14. print('Checking project directory...')
  15. if not project_path.exists():
  16. print('Project directory not found, creating project directory')
  17. project_path.mkdir()
  18. else:
  19. print('Project directory found')
  20. repo = Repo(project_path)
  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. if args.git:
  67. tasks.append(repo.init())
  68. print('Creating files...')
  69. await asyncio.gather(*tasks)
  70. print('Created files')
  71. tasks = []
  72. if args.git and args.commit:
  73. await repo.add(('.',), _v=True)
  74. task = repo.commit(args.commit_message, _v=True)
  75. tasks.append(task)
  76. if args.migrate:
  77. python_path = bin_path / 'python'
  78. manage_path = project_path / 'manage.py'
  79. task = run_cmd((
  80. str(python_path),
  81. quote(str(manage_path)),
  82. 'migrate',
  83. '--no-input'
  84. ))
  85. tasks.append(task)
  86. await asyncio.gather(*tasks)
  87. def main():
  88. asyncio.run(exec())
  89. if __name__ == '__main__':
  90. try:
  91. main()
  92. except KeyboardInterrupt:
  93. sys.exit()