1#!/usr/bin/env python3 2 3# OpenEmbedded SDK publishing tool 4 5# Copyright (C) 2015-2016 Intel Corporation 6# 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. 19 20import sys 21import os 22import argparse 23import glob 24import re 25import subprocess 26import logging 27import shutil 28import errno 29 30scripts_path = os.path.dirname(os.path.realpath(__file__)) 31lib_path = scripts_path + '/lib' 32sys.path = sys.path + [lib_path] 33import scriptutils 34import argparse_oe 35logger = scriptutils.logger_create('sdktool') 36 37def mkdir(d): 38 try: 39 os.makedirs(d) 40 except OSError as e: 41 if e.errno != errno.EEXIST: 42 raise e 43 44def 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): 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) 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) 66 destdir = destination 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: 98 cmd = "sh %s -p -y -d %s" % (dest_sdk, destination) 99 ret = subprocess.call(cmd, shell=True) 100 if ret == 0: 101 logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination)) 102 os.remove(dest_sdk) 103 else: 104 logger.error('Failed to unpack %s to %s' % (dest_sdk, destination)) 105 return ret 106 else: 107 cmd = "ssh %s 'sh %s -p -y -d %s && rm -f %s'" % (host, dest_sdk, destdir, dest_sdk) 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: 117 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) 118 else: 119 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) 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 128def main(): 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") 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 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).') 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 148if __name__ == "__main__": 149 try: 150 ret = main() 151 except Exception: 152 ret = 1 153 import traceback 154 traceback.print_exc() 155 sys.exit(ret) 156