Example #1
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        self.owner = models.User.objects.create(username="******", email="*****@*****.**")
        self.owner.set_password("testing")
        self.owner.save()

        self.project = auth.create_project(user=self.owner, name="Test project",
                                           privacy=models.Project.PUBLIC, uid="testing")
        data = auth.create_data(project=self.project, path=__file__)
        analysis = auth.create_analysis(project=self.project, json_text='{}', template="")
        self.job = auth.create_job(analysis=analysis)

        self.proj_params = dict(uid=self.project.uid)
        self.analysis_params = dict(uid=analysis.uid)
        self.data_params = dict(uid=data.uid)
        self.job_params = dict(uid=self.job.uid)
Example #2
0
    def setUp(self):
        logger.setLevel(logging.WARNING)

        # Set up generic owner
        self.owner = models.User.objects.create_user(username="******",
                                                     email="*****@*****.**")
        self.owner.set_password("test")

        self.project = auth.create_project(user=self.owner,
                                           name="test",
                                           text="Text",
                                           summary="summary",
                                           uid="testing")

        self.recipe = auth.create_analysis(project=self.project,
                                           json_text="{}",
                                           template="",
                                           security=models.Analysis.AUTHORIZED)

        self.job = auth.create_job(analysis=self.recipe, user=self.owner)
        self.job.save()
Example #3
0
    def handle(self, *args, **options):

        json = options['json']
        pid = options['id']
        template_fname = options['template']
        jobs = options['jobs']

        verbosity = int(options['verbosity'])

        if verbosity > 1:
            logger.setLevel(logging.DEBUG)
            logger.info(f"level={verbosity}")

        # Require JSON and templatates to exist.
        if not (json and template_fname):
            logger.error(
                "This command requires --json and a --template to be set")
            return

        # Get the target project.
        project = Project.objects.filter(id=pid).first()

        # Invalid project specified.
        if not project:
            logger.error(f'No project with id={pid}')
            return

        # JSON file does not exist.
        if not os.path.isfile(json):
            logger.error(f'No file found for --json={json}')
            return

        # Template file does not exist.
        if not os.path.isfile(template_fname):
            logger.error(f'No file found for --template={template_fname}')
            return

        try:
            # Parse the json_text into json_data
            json_text = open(json).read()
            json_path = os.path.dirname(json)
            json_data = hjson.loads(json_text)
        except Exception as exc:
            logger.exception(f"JSON exception in file: {json}\n{exc}")
            return

        try:
            # Read the specification
            template = open(template_fname).read()
        except Exception as exc:
            logger.exception(f"Template exception: {exc}")
            return

        try:
            name = json_data.get("settings", {}).get("name", "No name")
            text = json_data.get("settings", {}).get("help", "No help")
            uid = json_data.get("settings", {}).get("uid", "")
            image = json_data.get("settings", {}).get("image", "")
            text = textwrap.dedent(text)
            summary = json_data.get("settings",
                                    {}).get("summary", "No summary")

            # Create the analysis
            analysis = auth.create_analysis(project=project,
                                            uid=uid,
                                            json_text=json_text,
                                            summary=summary,
                                            template=template,
                                            name=name,
                                            text=text,
                                            security=Analysis.AUTHORIZED)

            # Load the image if specified.
            if image:
                image_path = os.path.join(json_path, image)
                if os.path.isfile(image_path):
                    stream = open(image_path, 'rb')
                    analysis.image.save(image, stream, save=True)
                    logger.info(f"Image path: {image_path}")
                else:
                    logger.error(f"Skipping invalid image path: {image_path}")

            if jobs:
                # When creating a job automatically for data in projects
                # it will try to match the value of the parameter to the data name.
                missing_name = ''
                for key, obj in json_data.items():
                    if obj.get("source") != "PROJECT":
                        continue

                    name = obj.get('value', '')
                    data = Data.objects.filter(project=project,
                                               name=name).first()

                    if not data:
                        missing_name = name
                        break

                    data.fill_dict(obj)

                if missing_name:
                    logger.error(
                        f"Job not created! Missing data:{missing_name} in analysis:{analysis.name}"
                    )
                else:
                    auth.create_job(analysis=analysis, json_data=json_data)

        except Exception as exc:
            logger.exception(f"Error: {exc}")
            return