Skip to content

Commit 16ab593

Browse files
committed
Add download archiving system with LocalFilesystem, S3, and SFTP providers
Signed-off-by: Varsha U N <[email protected]>
1 parent d9875ff commit 16ab593

File tree

3 files changed

+584
-3
lines changed

3 files changed

+584
-3
lines changed

scancodeio/settings.py

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
import sys
2424
import tempfile
2525
from pathlib import Path
26-
26+
from venv import logger
2727
import environ
28+
from scanpipe.archiving import LocalFilesystemProvider, S3LikeProvider, SftpProvider
29+
2830

2931
PROJECT_DIR = environ.Path(__file__) - 1
3032
ROOT_DIR = PROJECT_DIR - 1
@@ -371,6 +373,59 @@
371373

372374
CRISPY_TEMPLATE_PACK = "bootstrap3"
373375

376+
# Storing archives locally or in S3 (Package Storage settings)
377+
378+
ENABLE_DOWNLOAD_ARCHIVING = env.bool("ENABLE_DOWNLOAD_ARCHIVING", default=False)
379+
380+
# localstorage, s3, sftp
381+
DOWNLOAD_ARCHIVING_PROVIDER = env.str("DOWNLOAD_ARCHIVING_PROVIDER", default="localstorage")
382+
383+
# For local storage, we would store the root path in that setting
384+
DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION = env.dict("DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION", default=None)
385+
386+
# Initialize the DownloadStore based on provider
387+
388+
download_store = None
389+
if ENABLE_DOWNLOAD_ARCHIVING:
390+
if DOWNLOAD_ARCHIVING_PROVIDER == "localstorage":
391+
config = DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION or {}
392+
root_path = Path(config.get("root_path", "/var/scancodeio/downloads"))
393+
try:
394+
download_store = LocalFilesystemProvider(root_path=root_path)
395+
except Exception as e:
396+
logger.error(f"Failed to initialize LocalFilesystemProvider: {e}")
397+
elif DOWNLOAD_ARCHIVING_PROVIDER == "s3":
398+
config = DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION or {}
399+
required_keys = ["bucket_name", "aws_userid", "aws_apikey"]
400+
if not all(key in config for key in required_keys):
401+
logger.error(f"S3 provider requires {required_keys} in DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION")
402+
else:
403+
try:
404+
download_store = S3LikeProvider(
405+
bucket_name=config.get("bucket_name"),
406+
aws_userid=config.get("aws_userid"),
407+
aws_apikey=config.get("aws_apikey"),
408+
other_aws_credentials=config.get("other_aws_credentials", {}),
409+
)
410+
except Exception as e:
411+
logger.error(f"Failed to initialize S3LikeProvider: {e}")
412+
elif DOWNLOAD_ARCHIVING_PROVIDER == "sftp":
413+
config = DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION or {}
414+
required_keys = ["host", "root_path", "ssh_credentials"]
415+
if not all(key in config for key in required_keys):
416+
logger.error(f"SFTP provider requires {required_keys} in DOWNLOAD_ARCHIVING_PROVIDER_CONFIGURATION")
417+
else:
418+
try:
419+
download_store = SftpProvider(
420+
host=config.get("host"),
421+
root_path=config.get("root_path"),
422+
ssh_credentials=config.get("ssh_credentials", {}),
423+
)
424+
except Exception as e:
425+
logger.error(f"Failed to initialize SftpProvider: {e}")
426+
else:
427+
logger.error(f"Unknown DOWNLOAD_ARCHIVING_PROVIDER: {DOWNLOAD_ARCHIVING_PROVIDER}")
428+
374429
# Job Queue
375430

376431
RQ_QUEUES = {

0 commit comments

Comments
 (0)