PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/check-git-repo-availability.py

https://gitlab.com/onitake/fdroiddata
Python | 74 lines | 66 code | 6 blank | 2 comment | 12 complexity | a8c4b751eb3a484dfa593fc5a96b126c MD5 | raw file
  1. #!/usr/bin/env python3
  2. import glob
  3. import re
  4. import subprocess
  5. import sys
  6. import yaml
  7. if len(sys.argv) > 1:
  8. files = sys.argv[1:]
  9. else:
  10. files = sorted(glob.glob('metadata/*.yml'))
  11. errors = dict()
  12. for f in files:
  13. if not f.endswith('.yml'):
  14. print('\n' + f + ':\nThis only runs on YAML files (.yml), ignoring.')
  15. continue
  16. with open(f) as fp:
  17. data = yaml.load(fp)
  18. url = data.get('Repo')
  19. if not url or 'NoSourceSince' in data.keys():
  20. continue
  21. if data['RepoType'] != 'git':
  22. continue
  23. # from class vcs_git() in fdroidserver/common.py
  24. git_config = [
  25. '-c', 'core.askpass=/bin/true',
  26. '-c', 'core.sshCommand=/bin/false',
  27. '-c', 'url.https://.insteadOf=ssh://',
  28. ]
  29. for domain in ('bitbucket.org', 'github.com', 'gitlab.com'):
  30. git_config.append('-c')
  31. git_config.append('url.https://u:p@' + domain + '/.insteadOf=git@' + domain + ':')
  32. git_config.append('-c')
  33. git_config.append('url.https://u:p@' + domain + '.insteadOf=git://' + domain)
  34. git_config.append('-c')
  35. git_config.append('url.https://u:p@' + domain + '.insteadOf=https://' + domain)
  36. env = {
  37. 'GIT_TERMINAL_PROMPT': '0',
  38. 'GIT_ASKPASS': '/bin/true',
  39. 'SSH_ASKPASS': '/bin/true',
  40. 'GIT_SSH': '/bin/false', # for git < 2.3
  41. }
  42. p = subprocess.run(['git', ] + git_config + ['ls-remote', '--exit-code', '-h', url],
  43. env=env,
  44. capture_output=True)
  45. if p.returncode != 0:
  46. with open(f) as fp:
  47. raw = fp.read()
  48. with open(f, 'w') as fp:
  49. fp.write(re.sub(r'(Repo|RepoType):.*\n{1,2}', r'', raw))
  50. builds = data.get('Builds')
  51. if builds:
  52. versionName = str(builds[-1]['versionName'])
  53. # if YAML will think its a float, quote it
  54. try:
  55. float(versionName)
  56. fp.write("\nNoSourceSince: '" + versionName + "'")
  57. except ValueError:
  58. fp.write("\nNoSourceSince: " + versionName)
  59. fp.write('\n')
  60. print('\n' + f + ':')
  61. print(p.stderr.decode())
  62. errors[f] = p.stderr
  63. errorcount = len(errors)
  64. if errorcount > 0:
  65. print('\nFound', errorcount, 'errors.')
  66. sys.exit(errorcount)