From b8399ba45925bf035b595a4e52f66dd2c7bb65ef Mon Sep 17 00:00:00 2001 From: Adrian Chaves Date: Mon, 28 Sep 2026 16:48:14 +0200 Subject: [PATCH] Allow shub deploy from project subdirectories --- shub/deploy.py | 15 ++++++++++++++- tests/test_deploy.py | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/shub/deploy.py b/shub/deploy.py index 0b756dab..8e417b03 100644 --- a/shub/deploy.py +++ b/shub/deploy.py @@ -16,7 +16,8 @@ from shub.config import SH_IMAGES_REGISTRY, list_targets_callback, load_shub_config from shub.exceptions import BadParameterException, NotFoundException, ShubException, SubcommandException from shub.image.upload import upload_cmd -from shub.utils import (create_default_setup_py, create_scrapinghub_yml_wizard, +from shub.utils import (closest_file, create_default_setup_py, + create_scrapinghub_yml_wizard, inside_project, make_deploy_request, remember_cwd, run_cmd, run_python) @@ -89,6 +90,18 @@ def cli(target, version, debug, egg, build_egg, verbose, keep_log, def deploy_cmd(target, version, debug, egg, build_egg, verbose, keep_log, conf=None): + egg = egg and os.path.abspath(egg) + build_egg = build_egg and os.path.abspath(build_egg) + scrapy_cfg = closest_file('scrapy.cfg') + with remember_cwd(): + if scrapy_cfg: + os.chdir(os.path.dirname(scrapy_cfg)) + _deploy_cmd(target, version, debug, egg, build_egg, verbose, + keep_log, conf) + + +def _deploy_cmd(target, version, debug, egg, build_egg, verbose, keep_log, + conf): tmpdir = None try: if build_egg: diff --git a/tests/test_deploy.py b/tests/test_deploy.py index ea26090a..27ec258f 100644 --- a/tests/test_deploy.py +++ b/tests/test_deploy.py @@ -248,6 +248,28 @@ def test_build_egg_falls_back_without_git_repo(self): shutil.rmtree(tmpdir, ignore_errors=True) self.assertTrue(egg.endswith('.egg')) + def _build_egg_from_subdir(self): + os.chdir('project') + result = self.runner.invoke(deploy.cli, ('--build-egg', 'out.egg')) + self.assertEqual(0, result.exit_code, result.output) + with zipfile.ZipFile('out.egg') as z: + return z.namelist() + + def test_build_egg_from_subdir(self): + with self.runner.isolated_filesystem(): + self._make_git_project() + names = self._build_egg_from_subdir() + self.assertIn('project/__init__.py', names) + + def test_build_egg_from_subdir_without_git_repo(self): + with self.runner.isolated_filesystem(): + with open('scrapy.cfg', 'w') as f: + f.write(VALID_SCRAPY_CFG) + os.mkdir('project') + open(os.path.join('project', '__init__.py'), 'w').close() + names = self._build_egg_from_subdir() + self.assertIn('project/__init__.py', names) + @patch('shub.deploy.make_deploy_request') def test_deploy_default_from_git_repo(self, mock_deploy_req): with self.runner.isolated_filesystem():