From 5da703c98f1a8fcb2c537e66ab2d69b1282e0c4d Mon Sep 17 00:00:00 2001 From: James McKinney <26463+jpmckinney@users.noreply.github.com> Date: Sun, 24 Sep 2023 18:41:03 -0400 Subject: [PATCH] chore: Avoid creating a temporary file when activating egg 9c06c26 413f510 #494 --- scrapyd/runner.py | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/scrapyd/runner.py b/scrapyd/runner.py index 4e41b4a6..38db9c75 100644 --- a/scrapyd/runner.py +++ b/scrapyd/runner.py @@ -1,7 +1,5 @@ import os -import shutil import sys -import tempfile from contextlib import contextmanager from scrapy.utils.misc import load_object @@ -12,7 +10,6 @@ @contextmanager def project_environment(project): - eggversion = os.environ.get('SCRAPYD_EGG_VERSION', None) config = Config() eggstorage_path = config.get( 'eggstorage', 'scrapyd.eggstorage.FilesystemEggStorage' @@ -20,21 +17,14 @@ def project_environment(project): eggstorage_cls = load_object(eggstorage_path) eggstorage = eggstorage_cls(config) + eggversion = os.environ.get('SCRAPYD_EGG_VERSION', None) version, eggfile = eggstorage.get(project, eggversion) if eggfile: - prefix = '%s-%s-' % (project, version) - f = tempfile.NamedTemporaryFile(suffix='.egg', prefix=prefix, delete=False) - shutil.copyfileobj(eggfile, f) - f.close() - activate_egg(f.name) - else: - f = None - try: - assert 'scrapy.conf' not in sys.modules, "Scrapy settings already loaded" - yield - finally: - if f: - os.remove(f.name) + activate_egg(eggfile.name) + eggfile.close() + + assert 'scrapy.conf' not in sys.modules, "Scrapy settings already loaded" + yield def main():