Skip to content

Commit

Permalink
Fix building egg for projects with custom setup.py and invalid scrapy…
Browse files Browse the repository at this point in the history
….cfg (#284)
  • Loading branch information
jdemaeyer authored Jun 1, 2017
1 parent fcac89f commit fd7a053
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
6 changes: 2 additions & 4 deletions shub/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
from shub.exceptions import (BadParameterException, NotFoundException,
ShubException)
from shub.utils import (create_default_setup_py, create_scrapinghub_yml_wizard,
get_config, inside_project, make_deploy_request,
run_python)
inside_project, make_deploy_request, run_python)
from shub.image.upload import upload_cmd


Expand Down Expand Up @@ -158,8 +157,7 @@ def _upload_egg(endpoint, eggpath, project, version, auth, verbose, keep_log,
def _build_egg():
if not inside_project():
raise NotFoundException("No Scrapy project found in this location.")
settings = get_config().get('settings', 'default')
create_default_setup_py(settings=settings)
create_default_setup_py()
d = tempfile.mkdtemp(prefix="shub-deploy-")
run_python(['setup.py', 'clean', '-a', 'bdist_egg', '-d', d])
egg = glob.glob(os.path.join(d, '*.egg'))[0]
Expand Down
2 changes: 2 additions & 0 deletions shub/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ def create_default_setup_py(**kwargs):
with remember_cwd():
os.chdir(os.path.dirname(closest))
if not os.path.exists('setup.py'):
if 'settings' not in kwargs:
kwargs['settings'] = get_config().get('settings', 'default')
with open('setup.py', 'w') as f:
f.write(_SETUP_PY_TEMPLATE % kwargs)
click.echo("Created setup.py at {}".format(os.getcwd()))
Expand Down
21 changes: 16 additions & 5 deletions tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@
from __future__ import absolute_import

import unittest
import os

import pytest
from click.testing import CliRunner
from mock import patch

from shub import deploy
from shub.config import ShubConfig
from shub.exceptions import InvalidAuthException, NotFoundException, \
ShubException, BadParameterException
from shub.exceptions import (NotFoundException, ShubException,
BadParameterException)
from shub.utils import create_default_setup_py

from .utils import AssertInvokeRaisesMixin, mock_conf

Expand Down Expand Up @@ -106,6 +104,19 @@ def test_custom_deploy_bad_registry(self):
self._make_project()
self.assertInvokeRaises(BadParameterException, deploy.cli, ('custom3',))

@patch('shub.deploy.make_deploy_request')
def test_deploy_with_custom_setup_py(self, mock_deploy_req):
with self.runner.isolated_filesystem():
# This scrapy.cfg contains no "settings" section, so creating a
# default setup.py would fail (because we can't find the settings
# module)
open('scrapy.cfg', 'w').close()
# However, we already have a setup.py...
create_default_setup_py(settings='some_module')
# ... so shub should not fail while trying to create one
result = self.runner.invoke(deploy.cli)
self.assertEqual(result.exit_code, 0)


class DeployFilesTest(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit fd7a053

Please sign in to comment.