xref: /openbmc/openbmc/poky/scripts/oe-publish-sdk (revision 5199d831)
1eb8dc403SDave Cobbley#!/usr/bin/env python3
2c342db35SBrad Bishop#
3eb8dc403SDave Cobbley# OpenEmbedded SDK publishing tool
4c342db35SBrad Bishop#
5eb8dc403SDave Cobbley# Copyright (C) 2015-2016 Intel Corporation
6eb8dc403SDave Cobbley#
7c342db35SBrad Bishop# SPDX-License-Identifier: GPL-2.0-only
8eb8dc403SDave Cobbley#
9eb8dc403SDave Cobbley
10eb8dc403SDave Cobbleyimport sys
11eb8dc403SDave Cobbleyimport os
12eb8dc403SDave Cobbleyimport argparse
13eb8dc403SDave Cobbleyimport glob
14eb8dc403SDave Cobbleyimport re
15eb8dc403SDave Cobbleyimport subprocess
16eb8dc403SDave Cobbleyimport logging
17eb8dc403SDave Cobbleyimport shutil
18eb8dc403SDave Cobbleyimport errno
19eb8dc403SDave Cobbley
20eb8dc403SDave Cobbleyscripts_path = os.path.dirname(os.path.realpath(__file__))
21eb8dc403SDave Cobbleylib_path = scripts_path + '/lib'
22eb8dc403SDave Cobbleysys.path = sys.path + [lib_path]
23eb8dc403SDave Cobbleyimport scriptutils
24eb8dc403SDave Cobbleyimport argparse_oe
25eb8dc403SDave Cobbleylogger = scriptutils.logger_create('sdktool')
26eb8dc403SDave Cobbley
27eb8dc403SDave Cobbleydef mkdir(d):
28eb8dc403SDave Cobbley    try:
29eb8dc403SDave Cobbley        os.makedirs(d)
30eb8dc403SDave Cobbley    except OSError as e:
31eb8dc403SDave Cobbley        if e.errno != errno.EEXIST:
32eb8dc403SDave Cobbley            raise e
33eb8dc403SDave Cobbley
34eb8dc403SDave Cobbleydef publish(args):
35eb8dc403SDave Cobbley    logger.debug("In publish function")
36eb8dc403SDave Cobbley    target_sdk = args.sdk
37eb8dc403SDave Cobbley    destination = args.dest
38eb8dc403SDave Cobbley    logger.debug("target_sdk = %s, update_server = %s" % (target_sdk, destination))
39eb8dc403SDave Cobbley    sdk_basename = os.path.basename(target_sdk)
40eb8dc403SDave Cobbley
41eb8dc403SDave Cobbley    # Ensure the SDK exists
42eb8dc403SDave Cobbley    if not os.path.exists(target_sdk):
43eb8dc403SDave Cobbley        logger.error("Specified SDK %s doesn't exist" % target_sdk)
44eb8dc403SDave Cobbley        return -1
45eb8dc403SDave Cobbley    if os.path.isdir(target_sdk):
46eb8dc403SDave Cobbley        logger.error("%s is a directory - expected path to SDK installer file" % target_sdk)
47eb8dc403SDave Cobbley        return -1
48eb8dc403SDave Cobbley
49eb8dc403SDave Cobbley    if ':' in destination:
50eb8dc403SDave Cobbley        is_remote = True
51eb8dc403SDave Cobbley        host, destdir = destination.split(':')
52eb8dc403SDave Cobbley        dest_sdk = os.path.join(destdir, sdk_basename)
53eb8dc403SDave Cobbley    else:
54eb8dc403SDave Cobbley        is_remote = False
55eb8dc403SDave Cobbley        dest_sdk = os.path.join(destination, sdk_basename)
56eb8dc403SDave Cobbley        destdir = destination
57eb8dc403SDave Cobbley
58eb8dc403SDave Cobbley    # Making sure the directory exists
59eb8dc403SDave Cobbley    logger.debug("Making sure the destination directory exists")
60eb8dc403SDave Cobbley    if not is_remote:
61eb8dc403SDave Cobbley        mkdir(destination)
62eb8dc403SDave Cobbley    else:
63eb8dc403SDave Cobbley        cmd = "ssh %s 'mkdir -p %s'" % (host, destdir)
64eb8dc403SDave Cobbley        ret = subprocess.call(cmd, shell=True)
65eb8dc403SDave Cobbley        if ret != 0:
66eb8dc403SDave Cobbley            logger.error("Making directory %s on %s failed" % (destdir, host))
67eb8dc403SDave Cobbley            return ret
68eb8dc403SDave Cobbley
69eb8dc403SDave Cobbley    # Copying the SDK to the destination
70eb8dc403SDave Cobbley    logger.info("Copying the SDK to destination")
71eb8dc403SDave Cobbley    if not is_remote:
72eb8dc403SDave Cobbley        if os.path.exists(dest_sdk):
73eb8dc403SDave Cobbley            os.remove(dest_sdk)
74eb8dc403SDave Cobbley        if (os.stat(target_sdk).st_dev == os.stat(destination).st_dev):
75eb8dc403SDave Cobbley            os.link(target_sdk, dest_sdk)
76eb8dc403SDave Cobbley        else:
77eb8dc403SDave Cobbley            shutil.copy(target_sdk, dest_sdk)
78eb8dc403SDave Cobbley    else:
79eb8dc403SDave Cobbley        cmd = "scp %s %s" % (target_sdk, destination)
80eb8dc403SDave Cobbley        ret = subprocess.call(cmd, shell=True)
81eb8dc403SDave Cobbley        if ret != 0:
82eb8dc403SDave Cobbley            logger.error("scp %s %s failed" % (target_sdk, destination))
83eb8dc403SDave Cobbley            return ret
84eb8dc403SDave Cobbley
85eb8dc403SDave Cobbley    # Unpack the SDK
86eb8dc403SDave Cobbley    logger.info("Unpacking SDK")
87eb8dc403SDave Cobbley    if not is_remote:
88eb8dc403SDave Cobbley        cmd = "sh %s -p -y -d %s" % (dest_sdk, destination)
89eb8dc403SDave Cobbley        ret = subprocess.call(cmd, shell=True)
90eb8dc403SDave Cobbley        if ret == 0:
91eb8dc403SDave Cobbley            logger.info('Successfully unpacked %s to %s' % (dest_sdk, destination))
92eb8dc403SDave Cobbley            os.remove(dest_sdk)
93eb8dc403SDave Cobbley        else:
94eb8dc403SDave Cobbley            logger.error('Failed to unpack %s to %s' % (dest_sdk, destination))
95eb8dc403SDave Cobbley            return ret
96eb8dc403SDave Cobbley    else:
97c9f7865aSAndrew Geissler        rm_or_not = " && rm -f %s" % dest_sdk
98c9f7865aSAndrew Geissler        if args.keep_orig:
99c9f7865aSAndrew Geissler            rm_or_not = ""
100c9f7865aSAndrew Geissler        cmd = "ssh %s 'sh %s -p -y -d %s%s'" % (host, dest_sdk, destdir, rm_or_not)
101eb8dc403SDave Cobbley        ret = subprocess.call(cmd, shell=True)
102eb8dc403SDave Cobbley        if ret == 0:
103eb8dc403SDave Cobbley            logger.info('Successfully unpacked %s to %s' % (dest_sdk, destdir))
104eb8dc403SDave Cobbley        else:
105eb8dc403SDave Cobbley            logger.error('Failed to unpack %s to %s' % (dest_sdk, destdir))
106eb8dc403SDave Cobbley            return ret
107eb8dc403SDave Cobbley
108eb8dc403SDave Cobbley    # Setting up the git repo
109eb8dc403SDave Cobbley    if not is_remote:
110*5199d831SAndrew Geissler        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 config gc.auto 0; git add -A .; git config user.email "oe@oe.oe" && git config user.name "OE" && git commit -q -m "init repo" || true' % (destination, destination)
111eb8dc403SDave Cobbley    else:
112*5199d831SAndrew Geissler        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' > .gitignore; echo '*.pyo' >> .gitignore; echo 'pyshtables.py' >> .gitignore; fi; git config gc.auto 0; 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)
113eb8dc403SDave Cobbley    ret = subprocess.call(cmd, shell=True)
114eb8dc403SDave Cobbley    if ret == 0:
115eb8dc403SDave Cobbley        logger.info('SDK published successfully')
116eb8dc403SDave Cobbley    else:
117eb8dc403SDave Cobbley        logger.error('Failed to set up layer git repo')
118eb8dc403SDave Cobbley    return ret
119eb8dc403SDave Cobbley
120eb8dc403SDave Cobbley
121eb8dc403SDave Cobbleydef main():
122eb8dc403SDave Cobbley    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")
123eb8dc403SDave Cobbley    parser.add_argument('-d', '--debug', help='Enable debug output', action='store_true')
124eb8dc403SDave Cobbley    parser.add_argument('-q', '--quiet', help='Print only errors', action='store_true')
125c9f7865aSAndrew Geissler    parser.add_argument('-k', '--keep-orig', help='When published to a remote host, the eSDK installer gets deleted by default.', action='store_true')
126eb8dc403SDave Cobbley
127eb8dc403SDave Cobbley    parser.add_argument('sdk', help='Extensible SDK to publish (path to .sh installer file)')
128eb8dc403SDave Cobbley    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).')
129eb8dc403SDave Cobbley
130eb8dc403SDave Cobbley    parser.set_defaults(func=publish)
131eb8dc403SDave Cobbley
132eb8dc403SDave Cobbley    args = parser.parse_args()
133eb8dc403SDave Cobbley
134eb8dc403SDave Cobbley    if args.debug:
135eb8dc403SDave Cobbley        logger.setLevel(logging.DEBUG)
136eb8dc403SDave Cobbley    elif args.quiet:
137eb8dc403SDave Cobbley        logger.setLevel(logging.ERROR)
138eb8dc403SDave Cobbley
139eb8dc403SDave Cobbley    ret = args.func(args)
140eb8dc403SDave Cobbley    return ret
141eb8dc403SDave Cobbley
142eb8dc403SDave Cobbleyif __name__ == "__main__":
143eb8dc403SDave Cobbley    try:
144eb8dc403SDave Cobbley        ret = main()
145eb8dc403SDave Cobbley    except Exception:
146eb8dc403SDave Cobbley        ret = 1
147eb8dc403SDave Cobbley        import traceback
148eb8dc403SDave Cobbley        traceback.print_exc()
149eb8dc403SDave Cobbley    sys.exit(ret)
150