blob: ee33acf902c699358b3409c4a24bad3ff86bc24c [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Patrick Williamsc124f4f2015-09-15 14:41:29 -05002
3# OpenEmbedded SDK publishing tool
4
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005# Copyright (C) 2015-2016 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007# 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 Williamsc124f4f2015-09-15 14:41:29 -050019
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050034import argparse_oe
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035logger = 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):
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050053 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 Williamsc124f4f2015-09-15 14:41:29 -050057 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 Williamsd8c66bc2016-06-20 12:57:21 -050066 destdir = destination
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
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 Williamsd8c66bc2016-06-20 12:57:21 -050098 cmd = "sh %s -p -y -d %s" % (dest_sdk, destination)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050099 ret = subprocess.call(cmd, shell=True)
100 if ret == 0:
101 logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500102 os.remove(dest_sdk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500103 else:
104 logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
105 return ret
106 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500107 cmd = "ssh %s 'sh %s -p -y -d %s && rm -f %s'" % (host, dest_sdk, destdir, dest_sdk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500108 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:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117 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 Williamsc124f4f2015-09-15 14:41:29 -0500118 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 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 Williamsc124f4f2015-09-15 14:41:29 -0500120 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():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500129 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 Williamsc124f4f2015-09-15 14:41:29 -0500130 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 Williamsd8c66bc2016-06-20 12:57:21 -0500133 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 Williamsc124f4f2015-09-15 14:41:29 -0500135
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
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500154 traceback.print_exc()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500155 sys.exit(ret)