Skip to content

Commit

Permalink
chore: Avoid creating a temporary file when activating egg 9c06c26 41…
Browse files Browse the repository at this point in the history
  • Loading branch information
jpmckinney committed Sep 24, 2023
1 parent a149a5e commit 5da703c
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions scrapyd/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import os
import shutil
import sys
import tempfile
from contextlib import contextmanager

from scrapy.utils.misc import load_object
Expand All @@ -12,29 +10,21 @@

@contextmanager
def project_environment(project):
eggversion = os.environ.get('SCRAPYD_EGG_VERSION', None)
config = Config()
eggstorage_path = config.get(
'eggstorage', 'scrapyd.eggstorage.FilesystemEggStorage'
)
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():
Expand Down

0 comments on commit 5da703c

Please sign in to comment.