oe-publish-sdk 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #!/usr/bin/env python
  2. # OpenEmbedded SDK publishing tool
  3. # oe-publish-sdk publish <ext-sdk> <destination>
  4. # <ext-sdk>: extensible SDK to publish (path to the installer shell script)
  5. # <destination>: local or remote location which servers as an SDK update server
  6. # e.g.
  7. # oe-publish-sdk /path/to/sdk-ext.sh /mnt/poky/sdk-ext
  8. # oe-publish-sdk /path/to/sdk-ext.sh user@host:/opt/poky/sdk-ext
  9. #
  10. import sys
  11. import os
  12. import argparse
  13. import glob
  14. import re
  15. import subprocess
  16. import logging
  17. import shutil
  18. import errno
  19. scripts_path = os.path.dirname(os.path.realpath(__file__))
  20. lib_path = scripts_path + '/lib'
  21. sys.path = sys.path + [lib_path]
  22. import scriptutils
  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("%s doesn't exist" % target_sdk)
  39. return -1
  40. if ':' in destination:
  41. is_remote = True
  42. host, destdir = destination.split(':')
  43. dest_sdk = os.path.join(destdir, sdk_basename)
  44. else:
  45. is_remote = False
  46. dest_sdk = os.path.join(destination, sdk_basename)
  47. # Making sure the directory exists
  48. logger.debug("Making sure the destination directory exists")
  49. if not is_remote:
  50. mkdir(destination)
  51. else:
  52. cmd = "ssh %s 'mkdir -p %s'" % (host, destdir)
  53. ret = subprocess.call(cmd, shell=True)
  54. if ret != 0:
  55. logger.error("Making directory %s on %s failed" % (destdir, host))
  56. return ret
  57. # Copying the SDK to the destination
  58. logger.info("Copying the SDK to destination")
  59. if not is_remote:
  60. if os.path.exists(dest_sdk):
  61. os.remove(dest_sdk)
  62. if (os.stat(target_sdk).st_dev == os.stat(destination).st_dev):
  63. os.link(target_sdk, dest_sdk)
  64. else:
  65. shutil.copy(target_sdk, dest_sdk)
  66. else:
  67. cmd = "scp %s %s" % (target_sdk, destination)
  68. ret = subprocess.call(cmd, shell=True)
  69. if ret != 0:
  70. logger.error("scp %s %s failed" % (target_sdk, destination))
  71. return ret
  72. # Unpack the SDK
  73. logger.info("Unpacking SDK")
  74. if not is_remote:
  75. cmd = "sh %s -n -y -d %s" % (dest_sdk, destination)
  76. ret = subprocess.call(cmd, shell=True)
  77. if ret == 0:
  78. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
  79. else:
  80. logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
  81. return ret
  82. else:
  83. cmd = "ssh %s 'sh %s -n -y -d %s'" % (host, dest_sdk, destdir)
  84. ret = subprocess.call(cmd, shell=True)
  85. if ret == 0:
  86. logger.info('Successfully unpacked %s to %s' % (dest_sdk, destdir))
  87. else:
  88. logger.error('Failed to unpack %s to %s' % (dest_sdk, destdir))
  89. return ret
  90. # Setting up the git repo
  91. if not is_remote:
  92. cmd = 'set -e; mkdir -p %s/layers; cd %s/layers; if [ ! -e .git ]; then git init .; mv .git/hooks/post-update.sample .git/hooks/post-update; fi; git add -A .; git commit -q -m "init repo" || true; git update-server-info' % (destination, destination)
  93. else:
  94. cmd = "ssh %s 'set -e; mkdir-p %s/layers; cd %s/layers; if [ ! -e .git ]; then git init .; mv .git/hooks/post-update.sample .git/hooks/post-update; fi; git add -A .; git commit -q -m \"init repo\" || true; git update-server-info'" % (host, destdir, destdir)
  95. ret = subprocess.call(cmd, shell=True)
  96. if ret == 0:
  97. logger.info('SDK published successfully')
  98. else:
  99. logger.error('Failed to set up layer git repo')
  100. return ret
  101. def main():
  102. parser = argparse.ArgumentParser(description="OpenEmbedded development tool",
  103. epilog="Use %(prog)s <subcommand> --help to get help on a specific command")
  104. parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
  105. parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
  106. parser.add_argument('sdk', help='Extensible SDK to publish')
  107. parser.add_argument('dest', help='Destination to publish SDK to')
  108. parser.set_defaults(func=publish)
  109. args = parser.parse_args()
  110. if args.debug:
  111. logger.setLevel(logging.DEBUG)
  112. elif args.quiet:
  113. logger.setLevel(logging.ERROR)
  114. ret = args.func(args)
  115. return ret
  116. if __name__ == "__main__":
  117. try:
  118. ret = main()
  119. except Exception:
  120. ret = 1
  121. import traceback
  122. traceback.print_exc(5)
  123. sys.exit(ret)