utils.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright: (c) OpenSpug Organization. https://github.com/openspug/spug
  2. # Copyright: (c) <spug.dev@gmail.com>
  3. # Released under the AGPL-3.0 License.
  4. from django.conf import settings
  5. from apps.app.models import Deploy
  6. from apps.setting.utils import AppSetting
  7. from libs.gitlib import Git
  8. import shutil
  9. import os
  10. def parse_envs(text):
  11. data = {}
  12. if text:
  13. for line in text.split('\n'):
  14. fields = line.split('=', 1)
  15. if len(fields) != 2 or fields[0].strip() == '':
  16. raise Exception(f'解析自定义全局变量{line!r}失败,确认其遵循 key = value 格式')
  17. data[fields[0].strip()] = fields[1].strip()
  18. return data
  19. def fetch_versions(deploy: Deploy):
  20. git_repo = deploy.extend_obj.git_repo
  21. repo_dir = os.path.join(settings.REPOS_DIR, str(deploy.id))
  22. pkey = AppSetting.get_default('private_key')
  23. with Git(git_repo, repo_dir, pkey) as git:
  24. return git.fetch_branches_tags()
  25. def fetch_repo(deploy_id, git_repo):
  26. repo_dir = os.path.join(settings.REPOS_DIR, str(deploy_id))
  27. pkey = AppSetting.get_default('private_key')
  28. with Git(git_repo, repo_dir, pkey) as git:
  29. return git.fetch_branches_tags()
  30. def remove_repo(deploy_id):
  31. shutil.rmtree(os.path.join(settings.REPOS_DIR, str(deploy_id)), True)