keystore_generator.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. import os
  8. from config_data import ConfigData
  9. from threaded_lambda import ThreadedLambda
  10. from subprocess_runner import SubprocessRunner
  11. class KeystoreGenerator(ThreadedLambda):
  12. """
  13. This class knows how to use the keytool JAVA command to create
  14. a new keystore. It also uses o3de.bat/o3de.sh to store configuration
  15. data for the current project.
  16. """
  17. def __init__(self, config: ConfigData):
  18. def _job_func():
  19. self._run_commands()
  20. super().__init__("Keystore Generator", _job_func)
  21. ks = config.keystore_settings
  22. o3de_cmd = config.get_o3de_cmd()
  23. self._keystoreFileConfigureCmd = SubprocessRunner(
  24. [
  25. o3de_cmd,
  26. "android-configure",
  27. "--set-value",
  28. f"signconfig.store.file={ks.keystore_file}",
  29. "--project",
  30. config.project_path,
  31. ],
  32. timeOutSeconds=10,
  33. )
  34. self._keystoreKeyAliasConfigureCmd = SubprocessRunner(
  35. [
  36. o3de_cmd,
  37. "android-configure",
  38. "--set-value",
  39. f"signconfig.key.alias={ks.key_alias}",
  40. "--project",
  41. config.project_path,
  42. ],
  43. timeOutSeconds=10,
  44. )
  45. self._keystoreStorePasswordConfigureCmd = SubprocessRunner(
  46. [
  47. o3de_cmd,
  48. "android-configure",
  49. "--set-value",
  50. f"signconfig.store.password={ks.keystore_password}",
  51. "--project",
  52. config.project_path,
  53. ],
  54. timeOutSeconds=10,
  55. )
  56. self._keystoreKeyPasswordConfigureCmd = SubprocessRunner(
  57. [
  58. o3de_cmd,
  59. "android-configure",
  60. "--set-value",
  61. f"signconfig.key.password={ks.key_password}",
  62. "--project",
  63. config.project_path,
  64. ],
  65. timeOutSeconds=10,
  66. )
  67. self._keystoreCreateCmd = SubprocessRunner(
  68. [
  69. "keytool",
  70. "-genkey",
  71. "-keystore",
  72. ks.keystore_file,
  73. "-storepass",
  74. ks.keystore_password,
  75. "-alias",
  76. ks.key_alias,
  77. "-keypass",
  78. ks.key_password,
  79. "-keyalg",
  80. "RSA",
  81. "-keysize",
  82. ks.key_size,
  83. "-validity",
  84. ks.validity_days,
  85. "-dname",
  86. ks.get_distinguished_name(),
  87. ],
  88. timeOutSeconds=10,
  89. )
  90. def _run_commands(self):
  91. """
  92. This is a "protected" function. It is invoked when super().start() is called.
  93. """
  94. if self._is_cancelled:
  95. self._is_finished = True
  96. self._is_success = False
  97. return
  98. commands = [
  99. self._keystoreFileConfigureCmd,
  100. self._keystoreKeyAliasConfigureCmd,
  101. self._keystoreStorePasswordConfigureCmd,
  102. self._keystoreKeyPasswordConfigureCmd,
  103. self._keystoreCreateCmd,
  104. ]
  105. for command in commands:
  106. if self._is_cancelled:
  107. self._is_finished = True
  108. self._is_success = False
  109. return
  110. if not command.run():
  111. self._record_command_error(command)
  112. return
  113. else:
  114. self._record_command_results(command)
  115. self._is_finished = True
  116. self._is_success = True
  117. self._report_msg += f"Next Steps:\nYou can now generate the android project by pushing the `Generate Project` button.\n"
  118. def _record_command_error(self, command: SubprocessRunner):
  119. self._is_finished = True
  120. self._is_success = False
  121. self._record_command_results(command)
  122. def _record_command_results(self, command: SubprocessRunner):
  123. self._report_msg += f"Command:\n{command.get_command_str()}\nCompleted with status code {command.get_error_code()}.\n"
  124. self._report_msg += command.get_stdall()
  125. # class KeystoreGenerator END
  126. ######################################################