Skip to content

Commit

Permalink
Fix deps conversion (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandresgf authored Nov 3, 2022
1 parent 05e1f12 commit 7850e0c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
32 changes: 18 additions & 14 deletions shub/deploy.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion tests/test_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7850e0c

Please sign in to comment.