Skip to content

Commit

Permalink
Add a createproject management command #13
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Sep 17, 2020
1 parent 6239482 commit 157cef0
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 3 deletions.
8 changes: 6 additions & 2 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

### v1.0.2 (unreleased)

- *2020-09-17* **ScanPipe** -- Always return the Pipeline subclass/implementation
- **ScanPipe** -- Add a createproject management command.
Fix for https://github.com/nexB/scancode.io/issues/13

- **ScanPipe** -- Always return the Pipeline subclass/implementation
from the module inspection. Fix for https://github.com/nexB/scancode.io/issues/11


### v1.0.1 (2020-09-12)

- *2020-09-11* **ScanPipe** -- Do not fail when collecting system packages in
- **ScanPipe** -- Do not fail when collecting system packages in
Ubuntu docker images for layers that do not install packages by updating to a
newer version of ScanCode Toolkit. Fix for https://github.com/nexB/scancode.io/issues/1

Expand Down
90 changes: 90 additions & 0 deletions scanpipe/management/commands/createproject.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# SPDX-License-Identifier: Apache-2.0
#
# http://nexb.com and https://github.com/nexB/scancode.io
# The ScanCode.io software is licensed under the Apache License version 2.0.
# Data generated with ScanCode.io is provided as-is without warranties.
# ScanCode is a trademark of nexB Inc.
#
# You may not use this software except in compliance with the License.
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.
#
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
# for any legal advice.
#
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
# Visit https://github.com/nexB/scancode.io for support and download.

import shutil
import sys
from pathlib import Path

from django.apps import apps
from django.core.exceptions import ValidationError
from django.core.management.base import BaseCommand

from scanpipe.models import Project

scanpipe_app_config = apps.get_app_config("scanpipe")


class Command(BaseCommand):
help = "Create a ScanPipe project."

def add_arguments(self, parser):
parser.add_argument("name", help="Project name.")
parser.add_argument(
"--pipeline",
action="append",
dest="pipelines",
default=list(),
help=(
"Pipelines locations to add on the project. "
"The pipelines are added and ran respecting this provided order."
),
)
parser.add_argument(
"--input",
action="append",
dest="inputs",
default=list(),
help="Input file locations to copy in the input/ work directory.",
)

def handle(self, *args, **options):
name = options["name"]
pipelines = options["pipelines"]
inputs = options["inputs"]

project = Project(name=name)
try:
project.full_clean()
except ValidationError as e:
self.stderr.write(str(e))
sys.exit(1)

for pipeline_location in pipelines:
if not scanpipe_app_config.is_valid(pipeline_location):
self.stderr.write(f"{pipeline_location} is not a valid pipeline")
sys.exit(1)

for input_location in inputs:
input_path = Path(input_location)
if not input_path.is_file():
self.stderr.write(f"{input_location} not found or not a file.")
sys.exit(1)

project.save()
msg = f"Project {name} created with work directory {project.work_directory}"
self.stdout.write(self.style.SUCCESS(msg))

for pipeline_location in pipelines:
project.add_pipeline(pipeline_location)

for input_location in inputs:
shutil.copyfile(input_location, project.input_path)
19 changes: 18 additions & 1 deletion scanpipe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@
from io import StringIO
from pathlib import Path

from django.core.management import CommandError
from django.core.management import call_command
from django.test import TestCase

from scanpipe.models import Project


class ScanPipeManagementCommandTest(TestCase):
pipeline_location = "scanpipe/pipelines/docker.py"
Expand All @@ -35,5 +38,19 @@ def test_scanpipe_management_command_graph(self):
out = StringIO()
temp_dir = tempfile.mkdtemp()
call_command("graph", self.pipeline_location, "--output", temp_dir, stdout=out)
self.assertIn("Graph(s) generated.", out.getvalue())
out_value = out.getvalue()
self.assertIn("Graph(s) generated:", out_value)
self.assertIn("DockerPipeline.png", out_value)
self.assertTrue(Path(f"/{temp_dir}/DockerPipeline.png").exists())

def test_scanpipe_management_command_createproject(self):
out = StringIO()

with self.assertRaises(CommandError) as error:
call_command("createproject", stderr=out)
expected = "Error: the following arguments are required: name"
self.assertEqual(expected, str(error.exception))

call_command("createproject", "my_project", stdout=out)
self.assertIn("Project my_project created", out.getvalue())
self.assertTrue(Project.objects.get(name="my_project"))

0 comments on commit 157cef0

Please sign in to comment.