| Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 2 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 3 | # OpenEmbedded SDK publishing tool | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 5 | # Copyright (C) 2015-2016 Intel Corporation | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 6 | # | 
| Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 7 | # SPDX-License-Identifier: GPL-2.0-only | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 8 | # | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 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 |  | 
 | 20 | scripts_path = os.path.dirname(os.path.realpath(__file__)) | 
 | 21 | lib_path = scripts_path + '/lib' | 
 | 22 | sys.path = sys.path + [lib_path] | 
 | 23 | import scriptutils | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 24 | import argparse_oe | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 25 | logger = scriptutils.logger_create('sdktool') | 
 | 26 |  | 
 | 27 | def mkdir(d): | 
 | 28 |     try: | 
 | 29 |         os.makedirs(d) | 
 | 30 |     except OSError as e: | 
 | 31 |         if e.errno != errno.EEXIST: | 
 | 32 |             raise e | 
 | 33 |  | 
 | 34 | def publish(args): | 
 | 35 |     logger.debug("In publish function") | 
 | 36 |     target_sdk = args.sdk | 
 | 37 |     destination = args.dest | 
 | 38 |     logger.debug("target_sdk = %s, update_server = %s" % (target_sdk, destination)) | 
 | 39 |     sdk_basename = os.path.basename(target_sdk) | 
 | 40 |  | 
 | 41 |     # Ensure the SDK exists | 
 | 42 |     if not os.path.exists(target_sdk): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 43 |         logger.error("Specified SDK %s doesn't exist" % target_sdk) | 
 | 44 |         return -1 | 
 | 45 |     if os.path.isdir(target_sdk): | 
 | 46 |         logger.error("%s is a directory - expected path to SDK installer file" % target_sdk) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 47 |         return -1 | 
 | 48 |  | 
 | 49 |     if ':' in destination: | 
 | 50 |         is_remote = True | 
 | 51 |         host, destdir = destination.split(':') | 
 | 52 |         dest_sdk = os.path.join(destdir, sdk_basename) | 
 | 53 |     else: | 
 | 54 |         is_remote = False | 
 | 55 |         dest_sdk = os.path.join(destination, sdk_basename) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 56 |         destdir = destination | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 57 |  | 
 | 58 |     # Making sure the directory exists | 
 | 59 |     logger.debug("Making sure the destination directory exists") | 
 | 60 |     if not is_remote: | 
 | 61 |         mkdir(destination) | 
 | 62 |     else: | 
 | 63 |         cmd = "ssh %s 'mkdir -p %s'" % (host, destdir) | 
 | 64 |         ret = subprocess.call(cmd, shell=True) | 
 | 65 |         if ret != 0: | 
 | 66 |             logger.error("Making directory %s on %s failed" % (destdir, host)) | 
 | 67 |             return ret | 
 | 68 |  | 
 | 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 |  | 
 | 85 |     # Unpack the SDK | 
 | 86 |     logger.info("Unpacking SDK") | 
 | 87 |     if not is_remote: | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 88 |         cmd = "sh %s -p -y -d %s" % (dest_sdk, destination) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 89 |         ret = subprocess.call(cmd, shell=True) | 
 | 90 |         if ret == 0: | 
 | 91 |             logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination)) | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 92 |             os.remove(dest_sdk) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 93 |         else: | 
 | 94 |             logger.error('Failed to unpack %s to %s' % (dest_sdk, destination)) | 
 | 95 |             return ret | 
 | 96 |     else: | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 97 |         cmd = "ssh %s 'sh %s -p -y -d %s && rm -f %s'" % (host, dest_sdk, destdir, dest_sdk) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 98 |         ret = subprocess.call(cmd, shell=True) | 
 | 99 |         if ret == 0: | 
 | 100 |             logger.info('Successfully unpacked %s to %s' % (dest_sdk, destdir)) | 
 | 101 |         else: | 
 | 102 |             logger.error('Failed to unpack %s to %s' % (dest_sdk, destdir)) | 
 | 103 |             return ret | 
 | 104 |  | 
 | 105 |     # Setting up the git repo | 
 | 106 |     if not is_remote: | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 107 |         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) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 108 |     else: | 
| Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 109 |         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) | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 110 |     ret = subprocess.call(cmd, shell=True) | 
 | 111 |     if ret == 0: | 
 | 112 |         logger.info('SDK published successfully') | 
 | 113 |     else: | 
 | 114 |         logger.error('Failed to set up layer git repo') | 
 | 115 |     return ret | 
 | 116 |  | 
 | 117 |  | 
 | 118 | def main(): | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 119 |     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") | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 120 |     parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true') | 
 | 121 |     parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true') | 
 | 122 |  | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 123 |     parser.add_argument('sdk', help='Extensible SDK to publish (path to .sh installer file)') | 
 | 124 |     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).') | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 125 |  | 
 | 126 |     parser.set_defaults(func=publish) | 
 | 127 |  | 
 | 128 |     args = parser.parse_args() | 
 | 129 |  | 
 | 130 |     if args.debug: | 
 | 131 |         logger.setLevel(logging.DEBUG) | 
 | 132 |     elif args.quiet: | 
 | 133 |         logger.setLevel(logging.ERROR) | 
 | 134 |  | 
 | 135 |     ret = args.func(args) | 
 | 136 |     return ret | 
 | 137 |  | 
 | 138 | if __name__ == "__main__": | 
 | 139 |     try: | 
 | 140 |         ret = main() | 
 | 141 |     except Exception: | 
 | 142 |         ret = 1 | 
 | 143 |         import traceback | 
| Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 144 |         traceback.print_exc() | 
| Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 145 |     sys.exit(ret) |