2024-07-11 15:40:45 +00:00
|
|
|
import os
|
|
|
|
import json
|
2024-10-30 14:17:15 +00:00
|
|
|
import logging
|
2024-07-11 15:40:45 +00:00
|
|
|
|
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
from django.contrib.auth import get_user_model
|
2024-10-31 09:14:02 +00:00
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from utils.save_snapshots import move_json, save_in_disk
|
2024-07-30 11:37:08 +00:00
|
|
|
from evidence.parse import Build
|
2024-07-11 15:40:45 +00:00
|
|
|
|
|
|
|
|
2024-10-30 14:17:15 +00:00
|
|
|
logger = logging.getLogger('django')
|
|
|
|
|
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
User = get_user_model()
|
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = "Insert Snapshots"
|
|
|
|
snapshots = []
|
|
|
|
files = []
|
|
|
|
devices = []
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
|
|
|
parser.add_argument('path', type=str, help='Path to snapshots')
|
|
|
|
parser.add_argument('email', type=str, help='Email of user')
|
|
|
|
|
|
|
|
|
|
|
|
def handle(self, *args, **kwargs):
|
|
|
|
path = kwargs['path']
|
|
|
|
email = kwargs['email']
|
|
|
|
self.user = User.objects.get(email=email)
|
|
|
|
|
|
|
|
if os.path.isfile(path):
|
|
|
|
self.open(path)
|
|
|
|
|
|
|
|
elif os.path.isdir(path):
|
|
|
|
self.read_directory(path)
|
|
|
|
|
|
|
|
self.parsing()
|
2024-10-31 09:14:02 +00:00
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
def read_directory(self, directory):
|
|
|
|
for filename in os.listdir(directory):
|
|
|
|
filepath = os.path.join(directory, filename)
|
|
|
|
if os.path.isfile(filepath):
|
|
|
|
self.open(filepath)
|
|
|
|
|
|
|
|
def open(self, filepath):
|
2024-11-09 19:46:01 +00:00
|
|
|
try:
|
|
|
|
with open(filepath, 'r') as file:
|
|
|
|
content = json.loads(file.read())
|
|
|
|
path_name = save_in_disk(content, self.user.institution.name)
|
|
|
|
|
|
|
|
self.snapshots.append((content, path_name))
|
|
|
|
|
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
logger.error("JSON decode error in file %s: %s", filepath, e)
|
2024-11-11 07:06:22 +00:00
|
|
|
raise ValueError(f"Invalid JSON format in file. Check for file integrity.") from e
|
2024-11-09 19:46:01 +00:00
|
|
|
except FileNotFoundError as e:
|
|
|
|
logger.error("File not found: %s", filepath)
|
2024-11-11 07:06:22 +00:00
|
|
|
raise FileNotFoundError(f"File not found") from e
|
2024-11-09 19:46:01 +00:00
|
|
|
#or we cath'em all
|
|
|
|
except Exception as e:
|
|
|
|
logger.exception("Unexpected error when opening file %s: %s", filepath, e)
|
2024-11-11 07:06:22 +00:00
|
|
|
raise Exception(f"Unexpected error when opening file") from e
|
2024-10-31 09:14:02 +00:00
|
|
|
|
2024-07-11 15:40:45 +00:00
|
|
|
def parsing(self):
|
2024-10-31 09:14:02 +00:00
|
|
|
for s, p in self.snapshots:
|
2024-10-30 14:17:15 +00:00
|
|
|
try:
|
|
|
|
self.devices.append(Build(s, self.user))
|
2024-10-31 09:14:02 +00:00
|
|
|
move_json(p, self.user.institution.name)
|
2024-10-30 14:17:15 +00:00
|
|
|
except Exception as err:
|
2024-10-31 09:14:02 +00:00
|
|
|
snapshot_id = s.get("uuid", "")
|
2024-11-12 13:31:46 +00:00
|
|
|
txt = "Could not parse snapshot: %s"
|
2024-10-31 09:14:02 +00:00
|
|
|
logger.error(txt, snapshot_id)
|