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