__main__.py 3.1 KB

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