aboutsummaryrefslogtreecommitdiff
path: root/push-source.py
blob: d0bc8b92206286a1f407671e0020b11072e05a20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/python
#
# Copyright (C) 2017 Linaro Limited
# Author: Matt Hart <matthew.hart@linaro.org>
#
# This module is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at your option)
# any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import os
import argparse
import requests
from urlparse import urljoin
import time

def do_post_retry(url=None, data=None, headers=None, files=None):
    retry = True
    count = 5
    while retry and count >= 0:
        try:
            response = requests.post(url, data=data, headers=headers, files=files)
            if str(response.status_code)[:1] != "2":
                raise Exception(response.content)
            else:
                return response.content
                retry = False
        except Exception as e:
            print "ERROR: failed to publish"
            print e
            count = count - 1
            time.sleep(10)
    print "Failed to push file"
    exit(1)

parser = argparse.ArgumentParser()
parser.add_argument("--token", help="KernelCI API Token")
parser.add_argument("--tree", help="Kernel tree")
parser.add_argument("--describe", help="Kernel describe", default='')
parser.add_argument("--branch", help="Kernel branch")
parser.add_argument("--file", nargs='+', help="File to upload")
parser.add_argument("--api", help="KernelCI API URL", default="https://api.kernelci.org")
parser.add_argument("--publish_path", help="file path at destination")

args = vars(parser.parse_args())

artifacts = []
headers = {}
build_data = {}
headers['Authorization'] = args.get('token')

publish_path = args.get('publish_path', None)
if not publish_path:
    build_data['job'] = args.get('tree')
    build_data['kernel'] = args.get('describe', '')
    build_data['git_branch'] = args.get('branch')
    publish_path = os.path.join(build_data['job'], build_data['git_branch'], build_data['kernel'])

build_data['path'] = publish_path
build_data['file_server_resource'] = publish_path
file_count = 0
filenames = args.get('file')
for f in filenames:
    artifacts.append(('file%d' % file_count,(f, open(f), 'rb')))
    file_count += 1
    
upload_url = urljoin(args.get('api'), '/upload')
print("pushing %s to %s/%s" % (filenames, upload_url, publish_path))
publish_response = do_post_retry(url=upload_url, data=build_data, headers=headers, files=artifacts)