Skip to content

Commit

Permalink
Merge pull request #38 from scrapinghub/raise-click-exceptions
Browse files Browse the repository at this point in the history
Raise ClickException in case of request errors
  • Loading branch information
pablohoffman committed Jun 21, 2015
2 parents 5e96cfa + a005411 commit c9f9e71
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 31 deletions.
47 changes: 22 additions & 25 deletions shub/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
@click.option("--egg", help="deploy the given egg, instead of building one")
@click.option("--build-egg", help="only build the given egg, don't deploy it")
def cli(target, project, version, list_targets, debug, egg, build_egg):
exitcode = 0
if not inside_project():
log("Error: no Scrapy project found in this location")
sys.exit(1)
Expand All @@ -55,32 +54,30 @@ def cli(target, project, version, list_targets, debug, egg, build_egg):

tmpdir = None

if build_egg:
egg, tmpdir = _build_egg()
log("Writing egg to %s" % build_egg)
shutil.copyfile(egg, build_egg)
else:
target = _get_target(target)
project = _get_project(target, project)
version = _get_version(target, version)
if egg:
log("Using egg: %s" % egg)
egg = egg
else:
log("Packing version %s" % version)
try:
if build_egg:
egg, tmpdir = _build_egg()
if _upload_egg(target, egg, project, version):
click.echo("Run your spiders at: https://dash.scrapinghub.com/p/%s/" % project)
log("Writing egg to %s" % build_egg)
shutil.copyfile(egg, build_egg)
else:
exitcode = 1

if tmpdir:
if debug:
log("Output dir not removed: %s" % tmpdir)
else:
shutil.rmtree(tmpdir)

sys.exit(exitcode)
target = _get_target(target)
project = _get_project(target, project)
version = _get_version(target, version)
if egg:
log("Using egg: %s" % egg)
egg = egg
else:
log("Packing version %s" % version)
egg, tmpdir = _build_egg()

_upload_egg(target, egg, project, version)
click.echo("Run your spiders at: https://dash.scrapinghub.com/p/%s/" % project)
finally:
if tmpdir:
if debug:
log("Output dir not removed: %s" % tmpdir)
else:
shutil.rmtree(tmpdir)


def _get_project(target, project):
Expand Down
7 changes: 7 additions & 0 deletions shub/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from click import ClickException


class AuthException(ClickException):
def __init__(self):
msg = 'Authentication failure. Make sure your API key is valid.'
super(AuthException, self).__init__(msg)
12 changes: 12 additions & 0 deletions shub/fetch_eggs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import click
import requests
from click import ClickException

from shub.utils import find_api_key
from shub.click_utils import log
from shub.exceptions import AuthException


@click.command(help="Download a project's eggs from the Scrapy Cloud")
Expand All @@ -12,6 +14,8 @@ def cli(project_id):
url = "https://dash.scrapinghub.com/api/eggs/bundle.zip?project=%s" % project_id
rsp = requests.get(url=url, auth=auth, stream=True, timeout=300)

assert_response_is_valid(rsp)

destfile = 'eggs-%s.zip' % project_id
log("Downloading eggs to %s" % destfile)

Expand All @@ -20,3 +24,11 @@ def cli(project_id):
if chunk:
f.write(chunk)
f.flush()


def assert_response_is_valid(rsp):
if rsp.status_code == 403:
raise AuthException()
elif rsp.status_code != 200:
msg = 'Eggs could not be fetched. Status: %d' % rsp.status_code
raise ClickException(msg)
16 changes: 10 additions & 6 deletions shub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

import requests

from click import ClickException

from shub.click_utils import log
from shub.exceptions import AuthException

SCRAPY_CFG_FILE = os.path.expanduser("~/.scrapy.cfg")
OS_WIN = True if os.name == 'nt' else False
Expand Down Expand Up @@ -48,7 +51,6 @@ def get_key_netrc():
if key:
return key


def make_deploy_request(url, data, files, auth):
try:
rsp = requests.post(url=url, auth=auth, data=data, files=files,
Expand All @@ -59,12 +61,14 @@ def make_deploy_request(url, data, files, auth):
return True
except requests.HTTPError as exc:
rsp = exc.response
log("Deploy failed ({}):".format(rsp.status_code))
log(rsp.text)
return False

if rsp.status_code == 403:
raise AuthException()

msg = "Deploy failed ({}):\n{}".format(rsp.status_code, rsp.text)
raise ClickException(msg)
except requests.RequestException as exc:
log("Deploy failed: {}".format(exc))
return False
raise ClickException("Deploy failed: {}".format(exc))


def pwd_git_version():
Expand Down
26 changes: 26 additions & 0 deletions tests/test_fetch_eggs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import unittest
import mock
from collections import namedtuple

from click.testing import CliRunner
from shub import tool

FakeResponse = namedtuple('FakeResponse', ['status_code'])

@mock.patch('shub.fetch_eggs.requests', autospec=True)
class FetchEggsTest(unittest.TestCase):

def setUp(self):
self.runner = CliRunner()

def test_raises_auth_exception(self, requests_mock):
fake_response = FakeResponse(403)
requests_mock.get.return_value = fake_response
output = self.runner.invoke(tool.cli, ['fetch-eggs', 'xxx']).output
self.assertTrue('Authentication failure' in output)

def test_raises_exception_if_request_error(self, requests_mock):
fake_response = FakeResponse(400)
requests_mock.get.return_value = fake_response
output = self.runner.invoke(tool.cli, ['fetch-eggs', 'xxx']).output
self.assertTrue('Eggs could not be fetched' in output)

0 comments on commit c9f9e71

Please sign in to comment.