blob: 4b70f436b1a1a4b946e445d74d16a441d20895eb [file] [log] [blame]
Patrick Williamsc0f7c042017-02-23 20:41:17 -06001#!/usr/bin/env python3
Brad Bishopc342db32019-05-15 21:57:59 -04002#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05003# OpenEmbedded SDK publishing tool
Brad Bishopc342db32019-05-15 21:57:59 -04004#
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005# Copyright (C) 2015-2016 Intel Corporation
Patrick Williamsc124f4f2015-09-15 14:41:29 -05006#
Brad Bishopc342db32019-05-15 21:57:59 -04007# SPDX-License-Identifier: GPL-2.0-only
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05008#
Patrick Williamsc124f4f2015-09-15 14:41:29 -05009
10import sys
11import os
12import argparse
13import glob
14import re
15import subprocess
16import logging
17import shutil
18import errno
19
20scripts_path = os.path.dirname(os.path.realpath(__file__))
21lib_path = scripts_path + '/lib'
22sys.path = sys.path + [lib_path]
23import scriptutils
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050024import argparse_oe
Patrick Williamsc124f4f2015-09-15 14:41:29 -050025logger = scriptutils.logger_create('sdktool')
26
27def mkdir(d):
28 try:
29 os.makedirs(d)
30 except OSError as e:
31 if e.errno != errno.EEXIST:
32 raise e
33
34def 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 Williamsd8c66bc2016-06-20 12:57:21 -050043 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 Williamsc124f4f2015-09-15 14:41:29 -050047 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 Williamsd8c66bc2016-06-20 12:57:21 -050056 destdir = destination
Patrick Williamsc124f4f2015-09-15 14:41:29 -050057
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 Williamsd8c66bc2016-06-20 12:57:21 -050088 cmd = "sh %s -p -y -d %s" % (dest_sdk, destination)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050089 ret = subprocess.call(cmd, shell=True)
90 if ret == 0:
91 logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050092 os.remove(dest_sdk)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050093 else:
94 logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
95 return ret
96 else:
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050097 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 -050098 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 Bishopd7bf8c12018-02-25 22:55:05 -0500107 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 -0500108 else:
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500109 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 -0500110 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
118def main():
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500119 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 -0500120 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 Williamsd8c66bc2016-06-20 12:57:21 -0500123 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 Williamsc124f4f2015-09-15 14:41:29 -0500125
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
138if __name__ == "__main__":
139 try:
140 ret = main()
141 except Exception:
142 ret = 1
143 import traceback
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500144 traceback.print_exc()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500145 sys.exit(ret)