MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / do_upload

Function do_upload

tools/tasks.py:717–748  ·  view source on GitHub ↗

Uploads file path to a URL and returns exit code for the program.

(path, url, proxy)

Source from the content-addressed store, hash-verified

715
716
717def do_upload(path, url, proxy):
718 """
719 Uploads file path to a URL and returns exit code for the program.
720 """
721
722 with open(path, "rb") as f:
723 # Get proxies from environment/system
724 proxy_handler = urllib.request.ProxyHandler(urllib.request.getproxies())
725 if proxy != "":
726 # unless a proxy is explicitly passed, then use that instead
727 proxy_handler = urllib.request.ProxyHandler({"https": proxy, "http": proxy})
728
729 opener = urllib.request.build_opener(proxy_handler)
730 request = urllib.request.Request(url, data=f, method="PUT")
731 request.add_header(str("Content-Type"), str("application/zip"))
732 request.add_header("Content-Length", os.fstat(f.fileno()).st_size)
733
734 try:
735 url = opener.open(request)
736 if url.getcode() == 200:
737 log("Done uploading")
738 else:
739 raise Exception(
740 "Error uploading, expected status code 200, got status code: {0}".format(
741 url.getcode()
742 )
743 )
744 except Exception:
745 log(traceback.format_exc())
746 return 1
747
748 return 0
749
750
751def parse_host(host):

Callers 1

mainFunction · 0.90

Calls 3

logFunction · 0.85
getcodeMethod · 0.80
openMethod · 0.45

Tested by

no test coverage detected