Skip to content

Commit 7f2242d

Browse files
committed
do not run sys.exit for scrapy
1 parent f99a9eb commit 7f2242d

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/apify/_actor.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import sys
66
from datetime import timedelta
7+
from importlib.util import find_spec
78
from typing import TYPE_CHECKING, Any, Callable, Literal, TypeVar, cast, overload
89

910
from lazy_object_proxy import Proxy
@@ -266,10 +267,15 @@ async def finalize() -> None:
266267
await asyncio.wait_for(finalize(), cleanup_timeout.total_seconds())
267268
self._is_initialized = False
268269

270+
# Find out if Scrapy is installed.
271+
scrapy_installed = find_spec('scrapy') is not None
272+
269273
if is_running_in_ipython():
270274
self.log.debug(f'Not calling sys.exit({exit_code}) because Actor is running in IPython')
271275
elif os.getenv('PYTEST_CURRENT_TEST', default=False): # noqa: PLW1508
272276
self.log.debug(f'Not calling sys.exit({exit_code}) because Actor is running in an unit test')
277+
elif scrapy_installed:
278+
self.log.debug(f'Not calling sys.exit({exit_code}) because Actor is running in Scrapy')
273279
else:
274280
sys.exit(exit_code)
275281

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
# The test fixture will put the Apify SDK wheel path on the next line
22
APIFY_SDK_WHEEL_PLACEHOLDER
3-
scrapy~=2.12.0

tests/integration/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ def __call__(
198198
main_func: Callable | None = None,
199199
main_py: str | None = None,
200200
source_files: Mapping[str, str | bytes] | None = None,
201+
additional_requirements: list[str] | None = None,
201202
) -> Awaitable[ActorClientAsync]:
202203
"""Create a temporary Actor from the given main function or source files.
203204
@@ -211,6 +212,7 @@ def __call__(
211212
main_func: The main function of the Actor.
212213
main_py: The `src/main.py` file of the Actor.
213214
source_files: A dictionary of the source files of the Actor.
215+
additional_requirements: A list of additional requirements to be added to the `requirements.txt`.
214216
215217
Returns:
216218
A resource client for the created Actor.
@@ -235,6 +237,7 @@ async def _make_actor(
235237
main_func: Callable | None = None,
236238
main_py: str | None = None,
237239
source_files: Mapping[str, str | bytes] | None = None,
240+
additional_requirements: list[str] | None = None,
238241
) -> ActorClientAsync:
239242
if not (main_func or main_py or source_files):
240243
raise TypeError('One of `main_func`, `main_py` or `source_files` arguments must be specified')
@@ -270,6 +273,16 @@ async def _make_actor(
270273
actor_source_files = actor_base_source_files.copy()
271274
actor_source_files.update(source_files)
272275

276+
if additional_requirements:
277+
# Get the current requirements.txt content (as a string).
278+
req_content = actor_source_files.get('requirements.txt', '')
279+
if isinstance(req_content, bytes):
280+
req_content = req_content.decode('utf-8')
281+
# Append the additional requirements, each on a new line.
282+
additional_reqs = '\n'.join(additional_requirements)
283+
req_content = req_content.strip() + '\n' + additional_reqs + '\n'
284+
actor_source_files['requirements.txt'] = req_content
285+
273286
# Reformat the source files in a format that the Apify API understands.
274287
source_files_for_api = []
275288
for file_name, file_contents in actor_source_files.items():

tests/integration/test_actor_scrapy.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ async def test_actor_scrapy_title_spider(
2222
'src/spiders/title.py': read_file('docs/02_guides/code/scrapy_project/src/spiders/title.py'),
2323
}
2424

25-
actor = await make_actor('actor-scrapy-title-spider', source_files=actor_source_files)
25+
actor = await make_actor(
26+
'actor-scrapy-title-spider',
27+
source_files=actor_source_files,
28+
additional_requirements=['scrapy~=2.12.0'],
29+
)
2630
run_result = await run_actor(
2731
actor,
2832
run_input={

0 commit comments

Comments
 (0)