oe-publish-sdk 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/usr/bin/env python3
  2. #
  3. # OpenEmbedded SDK publishing tool
  4. #
  5. # Copyright (C) 2015-2016 Intel Corporation
  6. #
  7. # SPDX-License-Identifier: GPL-2.0-only
  8. #
  9. import sys
  10. import os
  11. import argparse
  12. import glob
  13. import re
  14. import subprocess
  15. import logging
  16. import shutil
  17. import errno
  18. scripts_path = os.path.dirname(os.path.realpath(__file__))
  19. lib_path = scripts_path + '/lib'
  20. sys.path = sys.path + [lib_path]
  21. import scriptutils
  22. import argparse_oe
  23. logger = scriptutils.logger_create('sdktool')
  24. def mkdir(d):
  25. try:
  26. os.makedirs(d)
  27. except OSError as e:
  28. if e.errno != errno.EEXIST:
  29. raise e
  30. def publish(args):
  31. logger.debug("In publish function")
  32. target_sdk = args.sdk
  33. destination = args.dest
  34. logger.debug("target_sdk = %s, update_server = %s" % (target_sdk, destination))
  35. sdk_basename = os.path.basename(target_sdk)
  36. # Ensure the SDK exists
  37. if not os.path.exists(target_sdk):
  38. logger.error("Specified SDK %s doesn't exist" % target_sdk)
  39. return -1
  40. if os.path.isdir(target_sdk):
  41. logger.error("%s is a directory - expected path to SDK installer file" % target_sdk)
  42. return -1
  43. if ':' in destination:
  44. is_remote = True
  45. host, destdir = destination.split(':')
  46. dest_sdk = os.path.join(destdir, sdk_basename)
  47. else:
  48. is_remote = False
  49. dest_sdk = os.path.join(destination, sdk_basename)
  50. destdir = destination
  51. # Making sure the directory exists
  52. logger.debug("Making sure the destination directory exists")
  53. if not is_remote:
  54. mkdir(destination)
  55. else:
  56. cmd = "ssh %s 'mkdir -p %s'" % (host, destdir)
  57. ret = subprocess.call(cmd, shell=True)
  58. if ret != 0:
  59. logger.error("Making directory %s on %s failed" % (destdir, host))
  60. return ret
  61. # Copying the SDK to the destination
  62. logger.info("Copying the SDK to destination")
  63. if not is_remote:
  64. if os.path.exists(dest_sdk):
  65. os.remove(dest_sdk)
  66. if (os.stat(target_sdk).st_dev == os.stat(destination).st_dev):
  67. os.link(target_sdk, dest_sdk)
  68. else:
  69. shutil.copy(target_sdk, dest_sdk)
  70. else:
  71. cmd = "scp %s %s" % (target_sdk, destination)
  72. ret = subprocess.call(cmd, shell=True)
  73. if ret != 0:
  74. logger.error("scp %s %s failed" % (target_sdk, destination))
  75. return ret
  76. # Unpack the SDK
  77. logger.info("Unpacking SDK")
  78. if not is_remote:
  79. cmd = "sh %s -p -y -d %s" % (dest_sdk, destination)
  80. ret = subprocess.call(cmd, shell=True)
  81. if ret == 0:
  82. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
  83. os.remove(dest_sdk)
  84. else:
  85. logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
  86. return ret
  87. else:
  88. rm_or_not = " && rm -f %s" % dest_sdk
  89. if args.keep_orig:
  90. rm_or_not = ""
  91. cmd = "ssh %s 'sh %s -p -y -d %s%s'" % (host, dest_sdk, destdir, rm_or_not)
  92. ret = subprocess.call(cmd, shell=True)
  93. if ret == 0:
  94. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destdir))
  95. else:
  96. logger.error('Failed to unpack %s to %s' % (dest_sdk, destdir))
  97. return ret
  98. # Setting up the git repo
  99. if not is_remote:
  100. cmd = 'set -e; mkdir -p %s/layers; cd %s/layers; if [ ! -e .git ]; then git init .; cp .git/hooks/post-update.sample .git/hooks/post-commit; echo "*.pyc\n*.pyo\npyshtables.py" > .gitignore; fi; git add -A .; git config user.email "oe@oe.oe" && git config user.name "OE" && git commit -q -m "init repo" || true' % (destination, destination)
  101. else:
  102. cmd = "ssh %s 'set -e; mkdir -p %s/layers; cd %s/layers; if [ ! -e .git ]; then git init .; cp .git/hooks/post-update.sample .git/hooks/post-commit; echo '*.pyc' > .gitignore; echo '*.pyo' >> .gitignore; echo 'pyshtables.py' >> .gitignore; fi; git add -A .; git config user.email 'oe@oe.oe' && git config user.name 'OE' && git commit -q -m \"init repo\" || true'" % (host, destdir, destdir)
  103. ret = subprocess.call(cmd, shell=True)
  104. if ret == 0:
  105. logger.info('SDK published successfully')
  106. else:
  107. logger.error('Failed to set up layer git repo')
  108. return ret
  109. def main():
  110. parser = argparse_oe.ArgumentParser(description="OpenEmbedded extensible SDK publishing tool - writes server-side data to support the extensible SDK update process to a specified location")
  111. parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
  112. parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
  113. parser.add_argument('-k', '--keep-orig', help='When published to a remote host, the eSDK installer gets deleted by default.', action='store_true')
  114. parser.add_argument('sdk', help='Extensible SDK to publish (path to .sh installer file)')
  115. parser.add_argument('dest', help='Destination to publish SDK to; can be local path or remote in the form of user@host:/path (in the latter case ssh/scp will be used).')
  116. parser.set_defaults(func=publish)
  117. args = parser.parse_args()
  118. if args.debug:
  119. logger.setLevel(logging.DEBUG)
  120. elif args.quiet:
  121. logger.setLevel(logging.ERROR)
  122. ret = args.func(args)
  123. return ret
  124. if __name__ == "__main__":
  125. try:
  126. ret = main()
  127. except Exception:
  128. ret = 1
  129. import traceback
  130. traceback.print_exc()
  131. sys.exit(ret)