create_project.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on 2018-08-28 17:38:43
  4. ---------
  5. @summary: 创建项目
  6. ---------
  7. @author: Boris
  8. @email: boris_liu@foxmail.com
  9. """
  10. import getpass
  11. import os
  12. import shutil
  13. import feapder.utils.tools as tools
  14. def deal_file_info(file):
  15. file = file.replace("{DATE}", tools.get_current_date())
  16. file = file.replace("{USER}", getpass.getuser())
  17. return file
  18. class CreateProject:
  19. def copy_callback(self, src, dst, *, follow_symlinks=True):
  20. if src.endswith(".py"):
  21. with open(src, "r", encoding="utf-8") as src_file, open(
  22. dst, "w", encoding="utf8"
  23. ) as dst_file:
  24. content = src_file.read()
  25. content = deal_file_info(content)
  26. dst_file.write(content)
  27. else:
  28. shutil.copy2(src, dst, follow_symlinks=follow_symlinks)
  29. def create(self, project_name):
  30. if os.path.exists(project_name):
  31. print("%s 项目已经存在" % project_name)
  32. else:
  33. template_path = os.path.abspath(
  34. os.path.join(__file__, "../../../templates/project_template")
  35. )
  36. shutil.copytree(
  37. template_path, project_name, copy_function=self.copy_callback
  38. )
  39. print("\n%s 项目生成成功" % project_name)