Skip to content

Commit

Permalink
Merge pull request #194 from NTIA/copy_driver_files
Browse files Browse the repository at this point in the history
Add feature to copy required driver files to specified locations in the api container
  • Loading branch information
jhazentia authored Apr 20, 2021
2 parents 62f8494 + 3b71a24 commit 6db8d3c
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ services:
expose:
- '8000'
volumes:
- ${REPO_ROOT}/drivers:/drivers
- ${REPO_ROOT}/drivers:/drivers:ro
cap_add:
- SYS_PTRACE
devices:
Expand Down
35 changes: 31 additions & 4 deletions drivers/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
# SCOS Sensor Drivers Directory

This directory is mounted as a Docker volume to `/drivers` in the SCOS sensor Docker
This directory is mounted as a Docker volume to `/drivers` in the scos-sensor Docker
container.

Some sensors/SDRs require drivers which cannot be packaged with their respective SCOS
plugins. Those drivers can be placed here manually, allowing them to be referenced from
within the SCOS sensor Docker container.
Some signal analyzers require drivers which cannot be packaged with their respective
SCOS plugins. Those drivers can be placed here manually, allowing them to be referenced
from within the scos-sensor Docker container.

A json file can be used to copy files in this directory to a required destination in
the Docker container.

Below is a sample json file. The `"source_path"` must be relative to the `drivers`
directory. The `"dest_path"` can be anywhere in the Docker container. If they do not
exist, the destination directory and parent directories will automatically be created.

```json
{
"scos_files": [
{
"source_path": "test_drivers1/test1.sh",
"dest_path": "/test_drivers/test_drivers1/new_name.sh"
},
{
"source_path": "test_drivers2/test2.sh",
"dest_path": "/test_drivers/test_drivers2/test.sh"
}
]
}
```

A json file for configuring the copying of files can be placed anywhere inside the
`drivers` directory. Multiple json configuration files can be added. Any json files that
do not have `"scos_files"` will be ignored as a configuration file, but can be copied
if specified in `"source_path"`. Copying directories is not currently supported.
3 changes: 3 additions & 0 deletions src/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import pkgutil

from sensor import settings
from sensor.utils import copy_driver_files

from . import logger as logger_action

logger = logging.getLogger(__name__)

copy_driver_files() # copy driver files before loading plugins

discovered_plugins = {
name: importlib.import_module(name)
for finder, name, ispkg in pkgutil.iter_modules()
Expand Down
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
coreapi==2.3.3
cryptography~=3.3.2
Django~=3.0.12
django-debug-toolbar==2.2
django-debug-toolbar==2.2.1
django-extensions==2.2.6
django-filter==2.2.0
djangorestframework==3.11.2
Expand Down
1 change: 1 addition & 0 deletions src/sensor/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
OPENAPI_FILE = path.join(REPO_ROOT, "docs", "openapi.json")

CONFIG_DIR = path.join(REPO_ROOT, "configs")
DRIVERS_DIR = path.join(REPO_ROOT, "drivers")

# JSON configs
# TODO remove calibration files, add instructions to set these in scos-usrp
Expand Down
40 changes: 38 additions & 2 deletions src/sensor/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import json
import logging
import os
import shutil
from datetime import datetime

import numpy as np
from django.conf import settings

from .settings import DATETIME_FORMAT
logger = logging.getLogger(__name__)


class FindNearestDict(dict):
Expand Down Expand Up @@ -61,4 +66,35 @@ def get_timestamp_from_datetime(dt):


def parse_datetime_str(d):
return datetime.strptime(d, DATETIME_FORMAT)
return datetime.strptime(d, settings.DATETIME_FORMAT)


def copy_driver_files():
"""Copy driver files where they need to go"""
for root, dirs, files in os.walk(settings.DRIVERS_DIR):
for filename in files:
name_without_ext, ext = os.path.splitext(filename)
if ext.lower() == ".json":
json_data = {}
file_path = os.path.join(root, filename)
with open(file_path) as json_file:
json_data = json.load(json_file)
if type(json_data) == dict and "scos_files" in json_data:
scos_files = json_data["scos_files"]
for scos_file in scos_files:
source_path = os.path.join(
settings.DRIVERS_DIR, scos_file["source_path"]
)
if not os.path.isfile(source_path):
logger.error(f"Unable to find file at {source_path}")
continue
dest_path = scos_file["dest_path"]
dest_dir = os.path.dirname(dest_path)
try:
if not os.path.isdir(dest_dir):
os.makedirs(dest_dir)
logger.debug(f"copying {source_path} to {dest_path}")
shutil.copyfile(source_path, dest_path)
except Exception as e:
logger.error(f"Failed to copy {source_path} to {dest_path}")
logger.error(e)

0 comments on commit 6db8d3c

Please sign in to comment.