Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 2 | |
| 3 | # Sends an error report (if the report-error class was enabled) to a |
| 4 | # remote server. |
| 5 | # |
| 6 | # Copyright (C) 2013 Intel Corporation |
| 7 | # Author: Andreea Proca <andreea.b.proca@intel.com> |
| 8 | # Author: Michael Wood <michael.g.wood@intel.com> |
| 9 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 10 | import urllib.request, urllib.error |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 11 | import sys |
| 12 | import json |
| 13 | import os |
| 14 | import subprocess |
| 15 | import argparse |
| 16 | import logging |
| 17 | |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 18 | scripts_lib_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib') |
| 19 | sys.path.insert(0, scripts_lib_path) |
| 20 | import argparse_oe |
| 21 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 22 | version = "0.3" |
| 23 | |
| 24 | log = logging.getLogger("send-error-report") |
| 25 | logging.basicConfig(format='%(levelname)s: %(message)s') |
| 26 | |
| 27 | def getPayloadLimit(url): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 28 | req = urllib.request.Request(url, None) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 29 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 30 | response = urllib.request.urlopen(req) |
| 31 | except urllib.error.URLError as e: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 32 | # Use this opportunity to bail out if we can't even contact the server |
| 33 | log.error("Could not contact server: " + url) |
| 34 | log.error(e.reason) |
| 35 | sys.exit(1) |
| 36 | try: |
| 37 | ret = json.loads(response.read()) |
| 38 | max_log_size = ret.get('max_log_size', 0) |
| 39 | return int(max_log_size) |
| 40 | except: |
| 41 | pass |
| 42 | |
| 43 | return 0 |
| 44 | |
| 45 | def ask_for_contactdetails(): |
| 46 | print("Please enter your name and your email (optionally), they'll be saved in the file you send.") |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 47 | username = input("Name (required): ") |
| 48 | email = input("E-mail (not required): ") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 49 | return username, email |
| 50 | |
| 51 | def edit_content(json_file_path): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 52 | edit = input("Review information before sending? (y/n): ") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | if 'y' in edit or 'Y' in edit: |
| 54 | editor = os.environ.get('EDITOR', None) |
| 55 | if editor: |
| 56 | subprocess.check_call([editor, json_file_path]) |
| 57 | else: |
| 58 | log.error("Please set your EDITOR value") |
| 59 | sys.exit(1) |
| 60 | return True |
| 61 | return False |
| 62 | |
| 63 | def prepare_data(args): |
| 64 | # attempt to get the max_log_size from the server's settings |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 65 | max_log_size = getPayloadLimit("https://"+args.server+"/ClientPost/JSON") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 66 | |
| 67 | if not os.path.isfile(args.error_file): |
| 68 | log.error("No data file found.") |
| 69 | sys.exit(1) |
| 70 | |
| 71 | home = os.path.expanduser("~") |
| 72 | userfile = os.path.join(home, ".oe-send-error") |
| 73 | |
| 74 | try: |
| 75 | with open(userfile, 'r') as userfile_fp: |
| 76 | if len(args.name) == 0: |
| 77 | args.name = userfile_fp.readline() |
| 78 | else: |
| 79 | #use empty readline to increment the fp |
| 80 | userfile_fp.readline() |
| 81 | |
| 82 | if len(args.email) == 0: |
| 83 | args.email = userfile_fp.readline() |
| 84 | except: |
| 85 | pass |
| 86 | |
| 87 | if args.assume_yes == True and len(args.name) == 0: |
| 88 | log.error("Name needs to be provided either via "+userfile+" or as an argument (-n).") |
| 89 | sys.exit(1) |
| 90 | |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 91 | while len(args.name) <= 0 or len(args.name) > 50: |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 92 | print("\nName needs to be given and must not more than 50 characters.") |
| 93 | args.name, args.email = ask_for_contactdetails() |
| 94 | |
| 95 | with open(userfile, 'w') as userfile_fp: |
| 96 | userfile_fp.write(args.name.strip() + "\n") |
| 97 | userfile_fp.write(args.email.strip() + "\n") |
| 98 | |
| 99 | with open(args.error_file, 'r') as json_fp: |
| 100 | data = json_fp.read() |
| 101 | |
| 102 | jsondata = json.loads(data) |
| 103 | jsondata['username'] = args.name.strip() |
| 104 | jsondata['email'] = args.email.strip() |
| 105 | jsondata['link_back'] = args.link_back.strip() |
| 106 | # If we got a max_log_size then use this to truncate to get the last |
| 107 | # max_log_size bytes from the end |
| 108 | if max_log_size != 0: |
| 109 | for fail in jsondata['failures']: |
| 110 | if len(fail['log']) > max_log_size: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 111 | print("Truncating log to allow for upload") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 112 | fail['log'] = fail['log'][-max_log_size:] |
| 113 | |
| 114 | data = json.dumps(jsondata, indent=4, sort_keys=True) |
| 115 | |
| 116 | # Write back the result which will contain all fields filled in and |
| 117 | # any post processing done on the log data |
| 118 | with open(args.error_file, "w") as json_fp: |
| 119 | if data: |
| 120 | json_fp.write(data) |
| 121 | |
| 122 | |
| 123 | if args.assume_yes == False and edit_content(args.error_file): |
| 124 | #We'll need to re-read the content if we edited it |
| 125 | with open(args.error_file, 'r') as json_fp: |
| 126 | data = json_fp.read() |
| 127 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 128 | return data.encode('utf-8') |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def send_data(data, args): |
| 132 | headers={'Content-type': 'application/json', 'User-Agent': "send-error-report/"+version} |
| 133 | |
| 134 | if args.json: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 135 | url = "https://"+args.server+"/ClientPost/JSON/" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 136 | else: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 137 | url = "https://"+args.server+"/ClientPost/" |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 138 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 139 | req = urllib.request.Request(url, data=data, headers=headers) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 140 | try: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 141 | response = urllib.request.urlopen(req) |
| 142 | except urllib.error.HTTPError as e: |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 143 | logging.error(str(e)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 144 | sys.exit(1) |
| 145 | |
Brad Bishop | d5ae7d9 | 2018-06-14 09:52:03 -0700 | [diff] [blame] | 146 | print(response.read().decode('utf-8')) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 147 | |
| 148 | |
| 149 | if __name__ == '__main__': |
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 150 | arg_parse = argparse_oe.ArgumentParser(description="This scripts will send an error report to your specified error-report-web server.") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 151 | |
| 152 | arg_parse.add_argument("error_file", |
| 153 | help="Generated error report file location", |
| 154 | type=str) |
| 155 | |
| 156 | arg_parse.add_argument("-y", |
| 157 | "--assume-yes", |
| 158 | help="Assume yes to all queries and do not prompt", |
| 159 | action="store_true") |
| 160 | |
| 161 | arg_parse.add_argument("-s", |
| 162 | "--server", |
| 163 | help="Server to send error report to", |
| 164 | type=str, |
| 165 | default="errors.yoctoproject.org") |
| 166 | |
| 167 | arg_parse.add_argument("-e", |
| 168 | "--email", |
| 169 | help="Email address to be used for contact", |
| 170 | type=str, |
| 171 | default="") |
| 172 | |
| 173 | arg_parse.add_argument("-n", |
| 174 | "--name", |
| 175 | help="Submitter name used to identify your error report", |
| 176 | type=str, |
| 177 | default="") |
| 178 | |
| 179 | arg_parse.add_argument("-l", |
| 180 | "--link-back", |
| 181 | help="A url to link back to this build from the error report server", |
| 182 | type=str, |
| 183 | default="") |
| 184 | |
| 185 | arg_parse.add_argument("-j", |
| 186 | "--json", |
| 187 | help="Return the result in json format, silences all other output", |
| 188 | action="store_true") |
| 189 | |
| 190 | |
| 191 | |
| 192 | args = arg_parse.parse_args() |
| 193 | |
| 194 | if (args.json == False): |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame] | 195 | print("Preparing to send errors to: "+args.server) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 196 | |
| 197 | data = prepare_data(args) |
| 198 | send_data(data, args) |
| 199 | |
| 200 | sys.exit(0) |