Reformat NBI to standardized format
[osm/NBI.git] / osm_nbi / tests / upload.py
1 #! /usr/bin/python3
2 # -*- coding: utf-8 -*-
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import getopt
18 import sys
19 import requests
20 from os.path import getsize, basename
21 from hashlib import md5
22
23 __author__ = "Alfonso Tierno, alfonso.tiernosepulveda@telefonica.com"
24 __date__ = "$2018-01-01$"
25 __version__ = "0.1"
26 version_date = "Jan 2018"
27
28
29 def usage():
30 print("Usage: ", sys.argv[0], "[options]")
31 print(" --version: prints current version")
32 print(" -f|--file FILE: file to be sent")
33 print(" -h|--help: shows this help")
34 print(" -u|--url URL: complete server URL")
35 print(" -s|--chunk-size SIZE: size of chunks, by default 1000")
36 print(" -t|--token TOKEN: Authorizaton token, previously obtained from server")
37 print(" -v|--verbose print debug information, can be used several times")
38 return
39
40
41 if __name__ == "__main__":
42 try:
43 # load parameters and configuration
44 opts, args = getopt.getopt(
45 sys.argv[1:],
46 "hvu:s:f:t:",
47 ["url=", "help", "version", "verbose", "file=", "chunk-size=", "token="],
48 )
49 url = None
50 chunk_size = 500
51 pkg_file = None
52 verbose = 0
53 token = None
54
55 for o, a in opts:
56 if o == "--version":
57 print("upload version " + __version__ + " " + version_date)
58 sys.exit()
59 elif o in ("-v", "--verbose"):
60 verbose += 1
61 elif o in ("-h", "--help"):
62 usage()
63 sys.exit()
64 elif o in ("-u", "--url"):
65 url = a
66 elif o in ("-s", "--chunk-size"):
67 chunk_size = int(a)
68 elif o in ("-f", "--file"):
69 pkg_file = a
70 elif o in ("-t", "--token"):
71 token = a
72 else:
73 assert False, "Unhandled option"
74 total_size = getsize(pkg_file)
75 index = 0
76 transaction_id = None
77 file_md5 = md5()
78 with open(pkg_file, "rb") as f:
79 headers = {
80 "Content-type": "application/gzip",
81 "Content-Filename": basename(pkg_file),
82 "Accept": "application/json",
83 }
84 if token:
85 headers["Authorization"] = token
86 while index < total_size:
87 chunk_data = f.read(chunk_size)
88 file_md5.update(chunk_data)
89 # payload = {"file_name": pkg_file, "chunk_data": base64.b64encode(chunk_data).decode("utf-8"),
90 # "chunk_size": chunk_size}
91 if transaction_id:
92 headers["Transaction-Id"] = transaction_id
93 if index + len(chunk_data) == total_size:
94 headers["Content-File-MD5"] = file_md5.hexdigest()
95 # payload["id"] = transaction_id
96 headers["Content-range"] = "bytes {}-{}/{}".format(
97 index, index + len(chunk_data) - 1, total_size
98 )
99 # refers to rfc2616: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
100 if verbose:
101 print("TX chunk Headers: {}".format(headers))
102 r = requests.post(url, data=chunk_data, headers=headers, verify=False)
103 if r.status_code not in (200, 201):
104 print("Got {}: {}".format(r.status_code, r.text))
105 exit(1)
106 if verbose > 1:
107 print("RX {}: {}".format(r.status_code, r.text))
108 response = r.json()
109 if not transaction_id:
110 transaction_id = response["id"]
111 index += len(chunk_data)
112 if verbose <= 1:
113 print("RX {}: {}".format(r.status_code, r.text))
114 if "id" in response:
115 print("---\nid: {}".format(response["id"]))
116 except Exception:
117 raise