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