util.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # Copyright (C) 2020 The Android Open Source Project
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Random utility code for release tools."""
  15. import os
  16. import re
  17. import subprocess
  18. import sys
  19. assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
  20. TOPDIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  21. HOMEDIR = os.path.expanduser('~')
  22. # These are the release keys we sign with.
  23. KEYID_DSA = '8BB9AD793E8E6153AF0F9A4416530D5E920F5C65'
  24. KEYID_RSA = 'A34A13BE8E76BFF46A0C022DA2E75A824AAB9624'
  25. KEYID_ECC = 'E1F9040D7A3F6DAFAC897CD3D3B95DA243E48A39'
  26. def cmdstr(cmd):
  27. """Get a nicely quoted shell command."""
  28. ret = []
  29. for arg in cmd:
  30. if not re.match(r'^[a-zA-Z0-9/_.=-]+$', arg):
  31. arg = f'"{arg}"'
  32. ret.append(arg)
  33. return ' '.join(ret)
  34. def run(opts, cmd, check=True, **kwargs):
  35. """Helper around subprocess.run to include logging."""
  36. print('+', cmdstr(cmd))
  37. if opts.dryrun:
  38. cmd = ['true', '--'] + cmd
  39. try:
  40. return subprocess.run(cmd, check=check, **kwargs)
  41. except subprocess.CalledProcessError as e:
  42. print(f'aborting: {e}', file=sys.stderr)
  43. sys.exit(1)
  44. def import_release_key(opts):
  45. """Import the public key of the official release repo signing key."""
  46. # Extract the key from our repo launcher.
  47. launcher = getattr(opts, 'launcher', os.path.join(TOPDIR, 'repo'))
  48. print(f'Importing keys from "{launcher}" launcher script')
  49. with open(launcher, encoding='utf-8') as fp:
  50. data = fp.read()
  51. keys = re.findall(
  52. r'\n-----BEGIN PGP PUBLIC KEY BLOCK-----\n[^-]*'
  53. r'\n-----END PGP PUBLIC KEY BLOCK-----\n', data, flags=re.M)
  54. run(opts, ['gpg', '--import'], input='\n'.join(keys).encode('utf-8'))
  55. print('Marking keys as fully trusted')
  56. run(opts, ['gpg', '--import-ownertrust'],
  57. input=f'{KEYID_DSA}:6:\n'.encode('utf-8'))