logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

submodules.py (3406B)


  1. """Functions for working with QMK's submodules.
  2. """
  3. from milc import cli
  4. def status():
  5. """Returns a dictionary of submodules.
  6. Each entry is a dict of the form:
  7. {
  8. 'name': 'submodule_name',
  9. 'status': None/False/True,
  10. 'githash': '<sha-1 hash for the submodule>'
  11. 'shorthash': '<short hash for the submodule>'
  12. 'describe': '<output of `git describe --tags`>'
  13. 'last_log_message': 'log message'
  14. 'last_log_timestamp': 'timestamp'
  15. }
  16. status is None when the submodule doesn't exist, False when it's out of date, and True when it's current
  17. """
  18. submodules = {}
  19. gitmodule_config = cli.run(['git', 'config', '-f', '.gitmodules', '-l'], timeout=30)
  20. for line in gitmodule_config.stdout.splitlines():
  21. key, value = line.split('=', maxsplit=2)
  22. if key.endswith('.path'):
  23. submodules[value] = {'name': value, 'status': None}
  24. git_cmd = cli.run(['git', 'submodule', 'status'], timeout=30)
  25. for line in git_cmd.stdout.splitlines():
  26. status = line[0]
  27. githash, submodule = line[1:].split()[:2]
  28. submodules[submodule]['githash'] = githash
  29. if status == '-':
  30. submodules[submodule]['status'] = None
  31. elif status == '+':
  32. submodules[submodule]['status'] = False
  33. elif status == ' ':
  34. submodules[submodule]['status'] = True
  35. else:
  36. raise ValueError('Unknown `git submodule status` sha-1 prefix character: "%s"' % status)
  37. submodule_logs = cli.run(['git', 'submodule', '-q', 'foreach', 'git --no-pager log --no-show-signature --pretty=format:"$sm_path%x01%h%x01%ad%x01%s%x0A" --date=iso -n1'])
  38. for log_line in submodule_logs.stdout.splitlines():
  39. r = log_line.split('\x01')
  40. submodule = r[0]
  41. submodules[submodule]['shorthash'] = r[1] if len(r) > 1 else ''
  42. submodules[submodule]['last_log_timestamp'] = r[2] if len(r) > 2 else ''
  43. submodules[submodule]['last_log_message'] = r[3] if len(r) > 3 else ''
  44. submodule_tags = cli.run(['git', 'submodule', '-q', 'foreach', '\'echo $sm_path `git describe --tags`\''])
  45. for log_line in submodule_tags.stdout.splitlines():
  46. r = log_line.split()
  47. submodule = r[0]
  48. submodules[submodule]['describe'] = r[1] if len(r) > 1 else ''
  49. return submodules
  50. def update(submodules=None):
  51. """Update the submodules.
  52. submodules
  53. A string containing a single submodule or a list of submodules.
  54. """
  55. git_sync_cmd = ['git', 'submodule', 'sync']
  56. git_update_cmd = ['git', 'submodule', 'update', '--init']
  57. if submodules is None:
  58. # Update everything
  59. git_sync_cmd.append('--recursive')
  60. git_update_cmd.append('--recursive')
  61. cli.run(git_sync_cmd, check=True)
  62. cli.run(git_update_cmd, check=True)
  63. else:
  64. if isinstance(submodules, str):
  65. # Update only a single submodule
  66. git_sync_cmd.append(submodules)
  67. git_update_cmd.append(submodules)
  68. cli.run(git_sync_cmd, check=True)
  69. cli.run(git_update_cmd, check=True)
  70. else:
  71. # Update submodules in a list
  72. for submodule in submodules:
  73. cli.run([*git_sync_cmd, submodule], check=True)
  74. cli.run([*git_update_cmd, submodule], check=True)