submodules.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Functions for working with QMK's submodules.
  2. """
  3. from functools import lru_cache
  4. from milc import cli
  5. @lru_cache(maxsize=0)
  6. def status():
  7. """Returns a dictionary of submodules.
  8. Each entry is a dict of the form:
  9. {
  10. 'name': 'submodule_name',
  11. 'status': None/False/True,
  12. 'githash': '<sha-1 hash for the submodule>
  13. }
  14. status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
  15. """
  16. submodules = {}
  17. git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
  18. for line in git_cmd.stdout.split('\n'):
  19. if not line:
  20. continue
  21. status = line[0]
  22. githash, submodule = line[1:].split()[:2]
  23. submodules[submodule] = {'name': submodule, 'githash': githash}
  24. if status == '-':
  25. submodules[submodule]['status'] = None
  26. elif status == '+':
  27. submodules[submodule]['status'] = False
  28. elif status == ' ':
  29. submodules[submodule]['status'] = True
  30. else:
  31. raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
  32. return submodules
  33. def update(submodules=None):
  34. """Update the submodules.
  35. submodules
  36. A string containing a single submodule or a list of submodules.
  37. """
  38. git_sync_cmd = ['git', 'submodule', 'sync']
  39. git_update_cmd = ['git', 'submodule', 'update', '--init']
  40. if submodules is None:
  41. # Update everything
  42. git_sync_cmd.append('--recursive')
  43. git_update_cmd.append('--recursive')
  44. cli.run(git_sync_cmd, check=True)
  45. cli.run(git_update_cmd, check=True)
  46. else:
  47. if isinstance(submodules, str):
  48. # Update only a single submodule
  49. git_sync_cmd.append(submodules)
  50. git_update_cmd.append(submodules)
  51. cli.run(git_sync_cmd, check=True)
  52. cli.run(git_update_cmd, check=True)
  53. else:
  54. # Update submodules in a list
  55. for submodule in submodules:
  56. cli.run([*git_sync_cmd, submodule], check=True)
  57. cli.run([*git_update_cmd, submodule], check=True)