diff --git a/shub/deploy.py b/shub/deploy.py index 0170e1f8..cdafabc5 100644 --- a/shub/deploy.py +++ b/shub/deploy.py @@ -1,25 +1,25 @@ from __future__ import absolute_import -import os + import glob +import json +import os import shutil import tempfile -import json -from six.moves.urllib.parse import urljoin +from typing import AnyStr, Optional, Union -import click -import toml # Not used in code but needed in runtime, don't remove! import setuptools import setuptools.msvc # noqa -from shub.config import (list_targets_callback, load_shub_config, - SH_IMAGES_REGISTRY) -from shub.exceptions import (BadParameterException, NotFoundException, - ShubException) +import click +import toml +from six.moves.urllib.parse import urljoin + +from shub.config import SH_IMAGES_REGISTRY, list_targets_callback, load_shub_config +from shub.exceptions import BadParameterException, NotFoundException, ShubException +from shub.image.upload import upload_cmd from shub.utils import (create_default_setup_py, create_scrapinghub_yml_wizard, inside_project, make_deploy_request, run_python) -from shub.image.upload import upload_cmd - HELP = """ Deploy the current folder's Scrapy project to Scrapy Cloud. @@ -193,11 +193,15 @@ def _get_pipfile_requirements(tmpdir=None): return open(_add_sources(convert_deps_to_pip(deps), _sources=sources.encode(), tmpdir=tmpdir), 'rb') -def _add_sources(_reqs_file, _sources, tmpdir=None): +def _add_sources(_requirements: Union[str, list], _sources: bytes, tmpdir: Optional[AnyStr] = None) -> str: tmp = tempfile.NamedTemporaryFile(delete=False, suffix="-requirements.txt", dir=tmpdir) tmp.write(_sources + b'\n') - with open(_reqs_file, 'rb') as f: - tmp.write(f.read()) + # Keep backward compatibility with pipenv<=2022.8.30 + if isinstance(_requirements, list): + tmp.write('\n'.join(_requirements).encode('utf-8')) + else: + with open(_requirements, 'rb') as f: + tmp.write(f.read()) tmp.flush() tmp.close() return tmp.name diff --git a/tests/test_deploy.py b/tests/test_deploy.py index 6ea2ed53..303fda63 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -5,7 +5,7 @@ import os import unittest -from unittest.mock import patch +from unittest.mock import patch, Mock import requests from click.testing import CliRunner @@ -256,6 +256,20 @@ def test_egg_glob_pattern(self): self.assertEqual(len(files_main['eggs']), 4) self.assertIn('main content', files_main['eggs']) + def test_add_sources(self): + convert_deps_to_pip = Mock( + side_effect=[ + './requirements.txt', + ['package==0.0.0', 'hash-package==0.0.1', 'hash-package2==0.0.1'], + ], + ) + _sources = ( + b'-i https://pypi.python.org/simple ' + b'--extra-index-url https://example.external-index.org/simple' + ) + self.assertIsInstance(deploy._add_sources(convert_deps_to_pip(), _sources), str) + self.assertIsInstance(deploy._add_sources(convert_deps_to_pip(), _sources), str) + def pipfile_test(self, req_name): with self.runner.isolated_filesystem(): with open('./main.egg', 'w') as f: