oe-publish-sdk 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python3
  2. # OpenEmbedded SDK publishing tool
  3. # Copyright (C) 2015-2016 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License version 2 as
  7. # published by the Free Software Foundation.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc.,
  16. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. import sys
  18. import os
  19. import argparse
  20. import glob
  21. import re
  22. import subprocess
  23. import logging
  24. import shutil
  25. import errno
  26. scripts_path = os.path.dirname(os.path.realpath(__file__))
  27. lib_path = scripts_path + '/lib'
  28. sys.path = sys.path + [lib_path]
  29. import scriptutils
  30. import argparse_oe
  31. logger = scriptutils.logger_create('sdktool')
  32. def mkdir(d):
  33. try:
  34. os.makedirs(d)
  35. except OSError as e:
  36. if e.errno != errno.EEXIST:
  37. raise e
  38. def publish(args):
  39. logger.debug("In publish function")
  40. target_sdk = args.sdk
  41. destination = args.dest
  42. logger.debug("target_sdk = %s, update_server = %s" % (target_sdk, destination))
  43. sdk_basename = os.path.basename(target_sdk)
  44. # Ensure the SDK exists
  45. if not os.path.exists(target_sdk):
  46. logger.error("Specified SDK %s doesn't exist" % target_sdk)
  47. return -1
  48. if os.path.isdir(target_sdk):
  49. logger.error("%s is a directory - expected path to SDK installer file" % target_sdk)
  50. return -1
  51. if ':' in destination:
  52. is_remote = True
  53. host, destdir = destination.split(':')
  54. dest_sdk = os.path.join(destdir, sdk_basename)
  55. else:
  56. is_remote = False
  57. dest_sdk = os.path.join(destination, sdk_basename)
  58. destdir = destination
  59. # Making sure the directory exists
  60. logger.debug("Making sure the destination directory exists")
  61. if not is_remote:
  62. mkdir(destination)
  63. else:
  64. cmd = "ssh %s 'mkdir -p %s'" % (host, destdir)
  65. ret = subprocess.call(cmd, shell=True)
  66. if ret != 0:
  67. logger.error("Making directory %s on %s failed" % (destdir, host))
  68. return ret
  69. # Copying the SDK to the destination
  70. logger.info("Copying the SDK to destination")
  71. if not is_remote:
  72. if os.path.exists(dest_sdk):
  73. os.remove(dest_sdk)
  74. if (os.stat(target_sdk).st_dev == os.stat(destination).st_dev):
  75. os.link(target_sdk, dest_sdk)
  76. else:
  77. shutil.copy(target_sdk, dest_sdk)
  78. else:
  79. cmd = "scp %s %s" % (target_sdk, destination)
  80. ret = subprocess.call(cmd, shell=True)
  81. if ret != 0:
  82. logger.error("scp %s %s failed" % (target_sdk, destination))
  83. return ret
  84. # Unpack the SDK
  85. logger.info("Unpacking SDK")
  86. if not is_remote:
  87. cmd = "sh %s -p -y -d %s" % (dest_sdk, destination)
  88. ret = subprocess.call(cmd, shell=True)
  89. if ret == 0:
  90. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
  91. os.remove(dest_sdk)
  92. else:
  93. logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
  94. return ret
  95. else:
  96. cmd = "ssh %s 'sh %s -p -y -d %s && rm -f %s'" % (host, dest_sdk, destdir, dest_sdk)
  97. ret = subprocess.call(cmd, shell=True)
  98. if ret == 0:
  99. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destdir))
  100. else:
  101. logger.error('Failed to unpack %s to %s' % (dest_sdk, destdir))
  102. return ret
  103. # Setting up the git repo
  104. if not is_remote:
  105. 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)
  106. else:
  107. 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\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'" % (host, destdir, destdir)
  108. ret = subprocess.call(cmd, shell=True)
  109. if ret == 0:
  110. logger.info('SDK published successfully')
  111. else:
  112. logger.error('Failed to set up layer git repo')
  113. return ret
  114. def main():
  115. 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")
  116. parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
  117. parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
  118. parser.add_argument('sdk', help='Extensible SDK to publish (path to .sh installer file)')
  119. 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).')
  120. parser.set_defaults(func=publish)
  121. args = parser.parse_args()
  122. if args.debug:
  123. logger.setLevel(logging.DEBUG)
  124. elif args.quiet:
  125. logger.setLevel(logging.ERROR)
  126. ret = args.func(args)
  127. return ret
  128. if __name__ == "__main__":
  129. try:
  130. ret = main()
  131. except Exception:
  132. ret = 1
  133. import traceback
  134. traceback.print_exc()
  135. sys.exit(ret)