def upload(self, hostname, log_line_func=lambda val: sys.stdout.write(val + '\n')): try: data = Upload.Encoder().default(self) if self.API_KEY: data['api_key'] = self.API_KEY response = requests.post( '{}{}'.format(hostname, self.UPLOAD_ENDPOINT), headers={'Content-type': 'application/json'}, data=json.dumps(data), verify=False, ) except requests.exceptions.ConnectionError: log_line_func(' ' * 4 + 'Failed to upload to {}, results server not online'. format(hostname)) return False except ValueError as e: log_line_func(' ' * 4 + 'Failed to encode upload data: {}'.format(e)) return False if response.status_code != 200: log_line_func(' ' * 4 + 'Error uploading to {}'.format(hostname)) try: log_line_func(' ' * 8 + response.json().get('description')) except ValueError: for line in response.text.splitlines(): log_line_func(' ' * 8 + line) return False log_line_func(' ' * 4 + 'Uploaded results to {}'.format(hostname)) return True
def upload_archive(self, hostname, archive, log_line_func=lambda val: sys.stdout.write(val + '\n')): try: meta_data = dict( version=self.VERSION, suite=self.suite, configuration=json.dumps(self.configuration or self.create_configuration()), commits=json.dumps(self.commits), ) if self.timestamp: meta_data['timestamp'] = self.timestamp if self.API_KEY: meta_data['api_key'] = self.API_KEY meta_data['Content-type'] = 'application/octet-stream' response = requests.post( '{}{}'.format(hostname, self.ARCHIVE_UPLOAD_ENDPOINT), data=meta_data, files=dict(file=archive), verify=False, ) except requests.exceptions.ConnectionError: log_line_func( ' ' * 4 + 'Failed to upload test archive to {}, results server not online' .format(hostname)) return False except ValueError as e: log_line_func( ' ' * 4 + 'Failed to encode archive reference data: {}'.format(e)) return False # FIXME: <rdar://problem/56154412> do not fail test runs because of 403 errors if response.status_code not in [200, 403, 413]: log_line_func(' ' * 4 + 'Error uploading archive to {}'.format(hostname)) try: log_line_func(' ' * 8 + response.json().get('description')) except ValueError: for line in response.text.splitlines(): log_line_func(' ' * 8 + line) return False if response.status_code == 200: log_line_func(' ' * 4 + 'Uploaded test archive to {}'.format(hostname)) else: log_line_func(' ' * 4 + 'Upload to {} failed:'.format(hostname)) try: log_line_func(' ' * 8 + response.json().get('description')) except ValueError: for line in response.text.splitlines(): log_line_func(' ' * 8 + line) log_line_func(' ' * 4 + 'This error is not fatal, continuing') return True
def upload_archive(self, hostname, archive, log_line_func=lambda val: sys.stdout.write(val + '\n')): try: meta_data = dict( version=self.VERSION, suite=self.suite, configuration=json.dumps(self.configuration or self.create_configuration()), commits=json.dumps(self.commits), ) if self.timestamp: meta_data['timestamp'] = self.timestamp response = requests.post( '{}{}'.format(hostname, self.ARCHIVE_UPLOAD_ENDPOINT), data=meta_data, files=dict(file=archive), ) except requests.exceptions.ConnectionError: log_line_func( ' ' * 4 + 'Failed to upload test archive to {}, results server not online' .format(hostname)) return False except ValueError as e: log_line_func( ' ' * 4 + 'Failed to encode archive reference data: {}'.format(e)) return False if response.status_code != 200: log_line_func(' ' * 4 + 'Error uploading archive to {}'.format(hostname)) log_line_func(' ' * 8 + response.json().get('description')) return False log_line_func(' ' * 4 + 'Uploaded test archive to {}'.format(hostname)) return True
def upload(self, url, log_line_func=lambda val: sys.stdout.write(val + '\n')): try: response = requests.post(url + self.UPLOAD_ENDPOINT, data=json.dumps(self, cls=Upload.Encoder)) except requests.exceptions.ConnectionError: log_line_func(' ' * 4 + 'Failed to upload to {}, results server not online'. format(url)) return False except ValueError as e: log_line_func(' ' * 4 + 'Failed to encode upload data: {}'.format(e)) return False if response.status_code != 200: log_line_func(' ' * 4 + 'Error uploading to {}:'.format(url)) log_line_func(' ' * 8 + response.json()['description']) return False log_line_func(' ' * 4 + 'Uploaded results to {}'.format(url)) return True