def test_env(monkeypatch): monkeypatch.setenv("HTTP_PROXY", "http://this") monkeypatch.setenv("HTTPS_PROXY", "http://that") session = new_requests_session() assert session.proxies == {"http": "http://this", "https": "http://that"} session = new_requests_session(proxies=False) assert not session.proxies
def __init__(self, args, file=None): self._tw = py.io.TerminalWriter(file=file) self.args = args self.cwd = py.path.local() self.quiet = False self._last_http_status = None self.http = new_requests_session(agent=("client", client_version))
def devpiserver_on_upload_sync(log, application_url, stage, project, version): slack_hook = stage.ixconfig.get("slack_hook") if not slack_hook: return session = new_requests_session(agent=("devpi-slack", __version__)) try: r = session.post( slack_hook, data={ 'payload': json.dumps({ "text": "Uploaded {}=={} to {}".format( project, version, application_url ), "icon_url": "http://doc.devpi.net/latest/_static/devpicat.jpg", "username": "******", }) }) except session.Errors: raise RuntimeError("%s: failed to send Slack notification %s", project, slack_hook) if 200 <= r.status_code < 300: log.info("successfully sent Slack notification: %s", slack_hook) else: log.error("%s: failed to send Slack notification: %s", r.status_code, slack_hook) log.debug(r.content) raise RuntimeError("%s: failed to send Slack notification: %s", project, slack_hook)
def trigger_jenkins(stage, jenkinurl, testspec): baseurl = get_outside_url(request.headers, stage.xom.config.args.outside_url) source = render_string("devpibootstrap.py", INDEXURL=baseurl + stage.name, VIRTUALENVTARURL= (baseurl + "root/pypi/+f/3a04aa2b32c76c83725ed4d9918e362e/" "virtualenv-1.10.1.tar.gz"), TESTSPEC=testspec, DEVPI_INSTALL_INDEX = baseurl + stage.name + "/+simple/" ) inputfile = py.io.BytesIO(source.encode("ascii")) req = new_requests_session(agent=("server", server_version)) try: r = req.post(jenkinurl, data={ "Submit": "Build", "name": "jobscript.py", "json": json.dumps( {"parameter": {"name": "jobscript.py", "file": "file0"}}), }, files={"file0": ("file0", inputfile)}) except req.RequestException: log.error("%s: failed to connect to jenkins at %s", testspec, jenkinurl) return -1 if 200 <= r.status_code < 300: log.info("successfully triggered jenkins: %s", jenkinurl) else: log.error("%s: failed to trigger jenkins at %s", r.status_code, jenkinurl) return -1
def test_env(monkeypatch, max_retries, retry_counter): monkeypatch.setenv("HTTP_PROXY", "http://this") monkeypatch.setenv("HTTPS_PROXY", "http://that") session = new_requests_session(max_retries=max_retries) with pytest.raises(requests.exceptions.RequestException): session.get("http://example.com") assert retry_counter.count == (max_retries or 0)
def __init__(self, args, file=None, pm=None): self._tw = py.io.TerminalWriter(file=file) self.args = args self.cwd = py.path.local() self.quiet = False self._last_http_stati = [] self.http = new_requests_session(agent=("client", client_version)) if pm is None: self.pm = get_pluginmanager() else: self.pm = pm
def _waitup(self, url, count=500): # try for 20 seconds to start devpi-server (which remotely # receives a serials list which may take a while) req = new_requests_session(proxies=False) while count > 0: try: req.get(url) except req.ConnectionError: time.sleep(0.1) count -= 1 else: return True return False
def _waitup(self, url, count=1800): # try to start devpi-server (which remotely # receives a serials list which may take a while) session = new_requests_session(proxies=False) while count > 0: try: session.get(url) except session.Errors: time.sleep(0.1) count -= 1 else: return True return False
def _waitup(self, url, count=1800): # try to start devpi-server (which remotely # receives a serials list which may take a while) session = new_requests_session() with no_proxy(urlparse(url).netloc): while count > 0: if not self.xproc.getinfo("devpi-server").isrunning(): return False try: session.get(url) except session.Errors: time.sleep(0.1) count -= 1 else: return True return False
def test_env(monkeypatch, max_retries): from urllib3.util.retry import Retry monkeypatch.setenv("HTTP_PROXY", "http://this") monkeypatch.setenv("HTTPS_PROXY", "http://that") orig_increment = Retry.increment increment_retry_totals = [] def increment(self, *args, **kwargs): increment_retry_totals.append(self.total) return orig_increment(self, *args, **kwargs) monkeypatch.setattr(Retry, "increment", increment) session = new_requests_session(max_retries=max_retries) with pytest.raises(requests.exceptions.RequestException): session.get("http://example.com") assert tuple(increment_retry_totals) in ((0, ), (2, 1, 0))
def trigger_jenkins(request, stage, jenkinurl, testspec): log = request.log baseurl = get_outside_url(request, stage.xom.config.args.outside_url) + "/" source = render_string( "devpibootstrap.py", INDEXURL=baseurl + "/" + stage.name, VIRTUALENVTARURL=(baseurl + "root/pypi/+f/d3d/915836c1ada1be731ccaa12412b98/" "virtualenv-1.11.2.tar.gz", ), TESTSPEC=testspec, DEVPI_INSTALL_INDEX=baseurl + stage.name + "/+simple/") inputfile = py.io.BytesIO(source.encode("ascii")) req = new_requests_session(agent=("server", server_version)) try: r = req.post(jenkinurl, data={ "Submit": "Build", "name": "jobscript.py", "json": json.dumps({ "parameter": { "name": "jobscript.py", "file": "file0" } }), }, files={"file0": ("file0", inputfile)}) except req.RequestException: log.error("%s: failed to connect to jenkins at %s", testspec, jenkinurl) return -1 if 200 <= r.status_code < 300: log.info("successfully triggered jenkins: %s", jenkinurl) else: log.error("%s: failed to trigger jenkins at %s", r.status_code, jenkinurl) log.debug(r.content) return -1
def devpiserver_on_upload_sync(log, application_url, stage, project, version): slack_hook = stage.ixconfig.get("slack_hook") or os.getenv("SLACK_HOOK") slack_icon = stage.ixconfig.get("slack_icon") or os.getenv( "SLACK_ICON", "http://doc.devpi.net/latest/_static/devpicat.jpg") slack_user = stage.ixconfig.get("slack_user") or os.getenv( "SLACK_USER", "devpi") if not slack_hook: return message = "Uploaded {}=={} to {}".format(project, version, application_url) global last_message if message == last_message: log.debug("skipping duplicate Slack notification: %s", message) return session = new_requests_session(agent=("devpi-slack", __version__)) try: r = session.post(slack_hook, data={ 'payload': json.dumps({ "text": message, "icon_url": slack_icon, "username": slack_user, }) }) except session.Errors: raise RuntimeError("%s: failed to send Slack notification %s", project, slack_hook) if 200 <= r.status_code < 300: log.info("successfully sent Slack notification: %s", slack_hook) last_message = message else: log.error("%s: failed to send Slack notification: %s", r.status_code, slack_hook) log.debug(r.content.decode('utf-8')) raise RuntimeError("%s: failed to send Slack notification: %s", project, slack_hook)
def devpiserver_on_upload_sync(log, application_url, stage, project, version): jenkinsurl = stage.ixconfig.get("uploadtrigger_jenkins") if not jenkinsurl: return jenkinsurl = jenkinsurl.format(pkgname=project, pkgversion=version) source = render_string( "devpibootstrap.py", INDEXURL=application_url + "/" + stage.name, VIRTUALENVTARURL=( application_url + "/root/pypi/+f/f61/cdd983d2c4e6a/" "virtualenv-1.11.6.tar.gz"), TESTSPEC='%s==%s' % (project, version), DEVPI_INSTALL_INDEX=application_url + "/" + stage.name + "/+simple/") inputfile = py.io.BytesIO(source.encode("ascii")) session = new_requests_session(agent=("devpi-jenkins", __version__)) try: r = session.post( jenkinsurl, data={ "Submit": "Build", "name": "jobscript.py", "json": json.dumps({ "parameter": {"name": "jobscript.py", "file": "file0"}})}, files={"file0": ("file0", inputfile)}) except session.Errors: raise RuntimeError("%s: failed to connect to jenkins at %s", project, jenkinsurl) if 200 <= r.status_code < 300: log.info("successfully triggered jenkins: %s", jenkinsurl) else: log.error("%s: failed to trigger jenkins at %s", r.status_code, jenkinsurl) log.debug(r.content) raise RuntimeError("%s: failed to trigger jenkins at %s", project, jenkinsurl)
def devpiserver_on_upload_sync(log, application_url, stage, projectname, version): jenkinsurl = stage.ixconfig.get("uploadtrigger_jenkins") if not jenkinsurl: return jenkinsurl = jenkinsurl.format(pkgname=projectname, pkgversion=version) source = render_string( "devpibootstrap.py", INDEXURL=application_url + "/" + stage.name, VIRTUALENVTARURL=( application_url + "/root/pypi/+f/f61/cdd983d2c4e6a/" "virtualenv-1.11.6.tar.gz"), TESTSPEC=projectname, DEVPI_INSTALL_INDEX=application_url + "/" + stage.name + "/+simple/") inputfile = py.io.BytesIO(source.encode("ascii")) session = new_requests_session(agent=("devpi-jenkins", __version__)) try: r = session.post( jenkinsurl, data={ "Submit": "Build", "name": "jobscript.py", "json": json.dumps({ "parameter": {"name": "jobscript.py", "file": "file0"}})}, files={"file0": ("file0", inputfile)}) except session.Errors: raise RuntimeError("%s: failed to connect to jenkins at %s", projectname, jenkinsurl) if 200 <= r.status_code < 300: log.info("successfully triggered jenkins: %s", jenkinsurl) else: log.error("%s: failed to trigger jenkins at %s", r.status_code, jenkinsurl) log.debug(r.content) raise RuntimeError("%s: failed to trigger jenkins at %s", projectname, jenkinsurl)
def _httpsession(self): session = new_requests_session(agent=("server", server_version)) return session
def test_useragent(): s = new_requests_session(agent=("hello", "1.2")) ua = s.headers["user-agent"] assert "devpi-hello/1.2" in ua assert sys.version.split()[0] in ua assert "*" not in ua
def test_env(monkeypatch): monkeypatch.setenv("HTTP_PROXY", "http://this") monkeypatch.setenv("HTTPS_PROXY", "http://that") session = new_requests_session() with pytest.raises(requests.exceptions.ProxyError): session.get("http://example.com")
def pushrelease(self): request = self.request stage = self.context.stage pushdata = getjson(request) try: name = pushdata["name"] version = pushdata["version"] except KeyError: apireturn(400, message="no name/version specified in json") # first, get all the links related to the source "to push" release try: linkstore = stage.get_linkstore_perstage(name, version) except stage.MissesRegistration: apireturn( 400, "there are no files for %s-%s on stage %s" % (name, version, stage.name)) links = dict([(rel, linkstore.get_links(rel=rel)) for rel in ('releasefile', 'doczip', 'toxresult')]) if not links["releasefile"]: self.log.info("%s: no release files for version %s-%s" % (stage.name, name, version)) apireturn(404, message="no release/files found for %s-%s" % (name, version)) metadata = get_pure_metadata(linkstore.verdata) # prepare metadata for submission metadata[":action"] = "submit" results = [] targetindex = pushdata.get("targetindex", None) if targetindex is not None: # in-server push parts = targetindex.split("/") if len(parts) != 2: apireturn(400, message="targetindex not in format user/index") target_stage = self.context.getstage(*parts) auth_user = request.authenticated_userid self.log.debug("targetindex %r, auth_user %r", targetindex, auth_user) if not target_stage.can_upload(auth_user): apireturn(401, message="user %r cannot upload to %r" % (auth_user, targetindex)) self._set_versiondata_dict(target_stage, metadata) results.append( (200, "register", name, version, "->", target_stage.name)) results.extend(self._push_links(links, target_stage, name, version)) apireturn(200, result=results, type="actionlog") else: posturl = pushdata["posturl"] username = pushdata["username"] password = pushdata["password"] pypiauth = (username, password) self.log.info("registering %s-%s to %s", name, version, posturl) session = new_requests_session(agent=("server", server_version)) r = session.post(posturl, data=metadata, auth=pypiauth) self.log.debug("register returned: %s", r.status_code) ok_codes = (200, 201) results.append((r.status_code, "register", name, version)) if r.status_code in ok_codes: for link in links["releasefile"]: entry = link.entry file_metadata = metadata.copy() file_metadata[":action"] = "file_upload" basename = link.basename pyver, filetype = get_pyversion_filetype(basename) file_metadata["filetype"] = filetype file_metadata["pyversion"] = pyver content = entry.file_get_content() self.log.info("sending %s to %s, metadata %s", basename, posturl, file_metadata) r = session.post(posturl, data=file_metadata, auth=pypiauth, files={"content": (basename, content)}) self.log.debug("send finished, status: %s", r.status_code) results.append( (r.status_code, "upload", entry.relpath, r.text)) if links["doczip"]: doc_metadata = metadata.copy() doc_metadata[":action"] = "doc_upload" doczip = links["doczip"][0].entry.file_get_content() r = session.post( posturl, data=doc_metadata, auth=pypiauth, files={"content": (name + ".zip", doczip)}) self.log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "docfile", name)) # if r.status_code in ok_codes: apireturn(200, result=results, type="actionlog") else: apireturn(502, result=results, type="actionlog")
from __future__ import print_function from __future__ import unicode_literals import os from requests.exceptions import HTTPError from devpi_server.log import threadlog import devpi_server from devpi_common.request import new_requests_session from devpi_server.extpypi import IndexParser, URL server_version = devpi_server.__version__ session = new_requests_session(agent=("server", server_version)) def devpiserver_get_projectname(stage, name): print('*** devpiserver_get_projectname: stage.name=%r; name=%r' % (stage.name, name)) try: cheeseprism_data = get_data_from_cheeseprism(name) except HTTPError: # Try querying on normalized (lowercased) name try: cheeseprism_data = get_data_from_cheeseprism(name.lower()) except HTTPError: return None else: return cheeseprism_data[0]['name'] else: return cheeseprism_data[0]['name']
def new_http_session(self, component_name): session = new_requests_session(agent=(component_name, server_version)) session.cert = self.config.args.replica_cert return session
def pushrelease(self, user, index): stage = self.getstage(user, index) pushdata = getjson(request) try: name = pushdata["name"] version = pushdata["version"] except KeyError: apireturn(400, message="no name/version specified in json") projectconfig = stage.get_projectconfig(name) matches = [] if projectconfig: verdata = projectconfig.get(version) if verdata: files = verdata.get("+files") for basename, relpath in files.items(): entry = stage.xom.filestore.getentry(relpath) if not entry.iscached(): abort(400, "cannot push non-cached files") matches.append(entry) metadata = get_pure_metadata(verdata) if not matches: log.info("%s: no release files %s-%s" %(stage.name, name, version)) apireturn(404, message="no release/files found for %s-%s" %( name, version)) doczip = stage.get_doczip(name, version) # prepare metadata for submission metadata[":action"] = "submit" results = [] targetindex = pushdata.get("targetindex", None) if targetindex is not None: parts = targetindex.split("/") if len(parts) != 2: apireturn(400, message="targetindex not in format user/index") target_stage = self.getstage(*parts) auth_user = self.auth.get_auth_user(request.auth, raising=False) log.debug("targetindex %r, auth_user %r", targetindex, auth_user) if not target_stage.can_upload(auth_user): apireturn(401, message="user %r cannot upload to %r" %(auth_user, targetindex)) #results = stage.copy_release(metadata, target_stage) #results.append((r.status_code, "upload", entry.relpath)) #apireturn(200, results=results, type="actionlog") if not target_stage.get_metadata(name, version): self._register_metadata_dict(target_stage, metadata) results.append((200, "register", name, version, "->", target_stage.name)) for entry in matches: res = target_stage.store_releasefile( entry.basename, entry.FILE.filepath.read(mode="rb")) if not isinstance(res, int): res = 200 results.append((res, "store_releasefile", entry.basename, "->", target_stage.name)) if doczip: target_stage.store_doczip(name, version, doczip) results.append((200, "uploaded documentation", name, "->", target_stage.name)) apireturn(200, result=results, type="actionlog") else: posturl = pushdata["posturl"] username = pushdata["username"] password = pushdata["password"] pypiauth = (username, password) log.info("registering %s-%s to %s", name, version, posturl) session = new_requests_session(agent=("server", server_version)) r = session.post(posturl, data=metadata, auth=pypiauth) log.debug("register returned: %s", r.status_code) ok_codes = (200, 201) results.append((r.status_code, "register", name, version)) if r.status_code in ok_codes: for entry in matches: file_metadata = metadata.copy() file_metadata[":action"] = "file_upload" basename = entry.basename pyver, filetype = get_pyversion_filetype(basename) file_metadata["filetype"] = filetype file_metadata["pyversion"] = pyver openfile = entry.FILE.filepath.open("rb") log.info("sending %s to %s, metadata %s", basename, posturl, file_metadata) r = session.post(posturl, data=file_metadata, auth=pypiauth, files={"content": (basename, openfile)}) log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "upload", entry.relpath, r.content)) if doczip: doc_metadata = metadata.copy() doc_metadata[":action"] = "doc_upload" r = session.post(posturl, data=doc_metadata, auth=pypiauth, files={"content": (name + ".zip", doczip)}) log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "docfile", name)) # if r.status_code in ok_codes: apireturn(200, result=results, type="actionlog") else: apireturn(502, result=results, type="actionlog")
def __init__(self, url): self._url = url self._session = new_requests_session(agent=("server", server_version)) self._session.headers["content-type"] = "text/xml" self._session.headers["Accept"] = "text/xml"
def pushrelease(self, user, index): stage = self.getstage(user, index) pushdata = getjson(request) try: name = pushdata["name"] version = pushdata["version"] except KeyError: apireturn(400, message="no name/version specified in json") projectconfig = stage.get_projectconfig(name) matches = [] if projectconfig: verdata = projectconfig.get(version) if verdata: files = verdata.get("+files") for basename, relpath in files.items(): entry = stage.xom.filestore.getentry(relpath) if not entry.iscached(): abort(400, "cannot push non-cached files") matches.append(entry) metadata = get_pure_metadata(verdata) if not matches: log.info("%s: no release files %s-%s" % (stage.name, name, version)) apireturn(404, message="no release/files found for %s-%s" % (name, version)) doczip = stage.get_doczip(name, version) # prepare metadata for submission metadata[":action"] = "submit" results = [] targetindex = pushdata.get("targetindex", None) if targetindex is not None: parts = targetindex.split("/") if len(parts) != 2: apireturn(400, message="targetindex not in format user/index") target_stage = self.getstage(*parts) auth_user = self.auth.get_auth_user(request.auth, raising=False) log.debug("targetindex %r, auth_user %r", targetindex, auth_user) if not target_stage.can_upload(auth_user): apireturn(401, message="user %r cannot upload to %r" % (auth_user, targetindex)) #results = stage.copy_release(metadata, target_stage) #results.append((r.status_code, "upload", entry.relpath)) #apireturn(200, results=results, type="actionlog") if not target_stage.get_metadata(name, version): self._register_metadata_dict(target_stage, metadata) results.append( (200, "register", name, version, "->", target_stage.name)) for entry in matches: res = target_stage.store_releasefile( entry.basename, entry.FILE.filepath.read(mode="rb")) if not isinstance(res, int): res = 200 results.append((res, "store_releasefile", entry.basename, "->", target_stage.name)) if doczip: target_stage.store_doczip(name, version, doczip) results.append((200, "uploaded documentation", name, "->", target_stage.name)) apireturn(200, result=results, type="actionlog") else: posturl = pushdata["posturl"] username = pushdata["username"] password = pushdata["password"] pypiauth = (username, password) log.info("registering %s-%s to %s", name, version, posturl) session = new_requests_session(agent=("server", server_version)) r = session.post(posturl, data=metadata, auth=pypiauth) log.debug("register returned: %s", r.status_code) ok_codes = (200, 201) results.append((r.status_code, "register", name, version)) if r.status_code in ok_codes: for entry in matches: file_metadata = metadata.copy() file_metadata[":action"] = "file_upload" basename = entry.basename pyver, filetype = get_pyversion_filetype(basename) file_metadata["filetype"] = filetype file_metadata["pyversion"] = pyver openfile = entry.FILE.filepath.open("rb") log.info("sending %s to %s, metadata %s", basename, posturl, file_metadata) r = session.post(posturl, data=file_metadata, auth=pypiauth, files={"content": (basename, openfile)}) log.debug("send finished, status: %s", r.status_code) results.append( (r.status_code, "upload", entry.relpath, r.content)) if doczip: doc_metadata = metadata.copy() doc_metadata[":action"] = "doc_upload" r = session.post( posturl, data=doc_metadata, auth=pypiauth, files={"content": (name + ".zip", doczip)}) log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "docfile", name)) # if r.status_code in ok_codes: apireturn(200, result=results, type="actionlog") else: apireturn(502, result=results, type="actionlog")
def pushrelease(self): request = self.request stage = self.context.stage pushdata = getjson(request) try: name = pushdata["name"] version = pushdata["version"] except KeyError: apireturn(400, message="no name/version specified in json") # first, get all the links related to the source "to push" release try: linkstore = stage.get_linkstore_perstage(name, version) except stage.MissesRegistration: apireturn(400, "there are no files for %s-%s on stage %s" %( name, version, stage.name)) links = dict([(rel, linkstore.get_links(rel=rel)) for rel in ('releasefile', 'doczip', 'toxresult')]) if not links["releasefile"]: self.log.info("%s: no release files for version %s-%s" % (stage.name, name, version)) apireturn(404, message="no release/files found for %s-%s" %( name, version)) metadata = get_pure_metadata(linkstore.verdata) results = [] targetindex = pushdata.get("targetindex", None) if targetindex is not None: # in-server push parts = targetindex.split("/") if len(parts) != 2: apireturn(400, message="targetindex not in format user/index") target_stage = self.context.getstage(*parts) auth_user = request.authenticated_userid self.log.debug("targetindex %r, auth_user %r", targetindex, auth_user) if not request.has_permission("pypi_submit", context=target_stage): apireturn(401, message="user %r cannot upload to %r" %(auth_user, targetindex)) self._set_versiondata_dict(target_stage, metadata) results.append((200, "register", name, version, "->", target_stage.name)) try: results.extend(self._push_links(links, target_stage, name, version)) except target_stage.NonVolatile as e: apireturn(409, "%s already exists in non-volatile index" % ( e.link.basename,)) apireturn(200, result=results, type="actionlog") else: posturl = pushdata["posturl"] username = pushdata["username"] password = pushdata["password"] pypiauth = (username, password) # prepare metadata for submission metadata[":action"] = "submit" self.log.info("registering %s-%s to %s", name, version, posturl) session = new_requests_session(agent=("server", server_version)) r = session.post(posturl, data=metadata, auth=pypiauth) self.log.debug("register returned: %s", r.status_code) ok_codes = (200, 201) results.append((r.status_code, "register", name, version)) if r.status_code in ok_codes: for link in links["releasefile"]: entry = link.entry file_metadata = metadata.copy() file_metadata[":action"] = "file_upload" basename = link.basename pyver, filetype = get_pyversion_filetype(basename) file_metadata["filetype"] = filetype file_metadata["pyversion"] = pyver content = entry.file_get_content() self.log.info("sending %s to %s, metadata %s", basename, posturl, file_metadata) r = session.post(posturl, data=file_metadata, auth=pypiauth, files={"content": (basename, content)}) self.log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "upload", entry.relpath, r.text)) if links["doczip"]: doc_metadata = metadata.copy() doc_metadata[":action"] = "doc_upload" doczip = links["doczip"][0].entry.file_get_content() r = session.post(posturl, data=doc_metadata, auth=pypiauth, files={"content": (name + ".zip", doczip)}) self.log.debug("send finished, status: %s", r.status_code) results.append((r.status_code, "docfile", name)) # if r.status_code in ok_codes: apireturn(200, result=results, type="actionlog") else: apireturn(502, result=results, type="actionlog")
def test_exception_attributes(): session = new_requests_session() assert isinstance(session.Errors, tuple)
def __init__(self, current): self.current = current self.requests = new_requests_session(agent=("client", client_version))
def new_http_session(self, component_name, max_retries=None): session = new_requests_session(agent=(component_name, server_version), max_retries=max_retries) session.cert = self.config.replica_cert return session