Skip to content

Commit

Permalink
Add a "status" management command to display Project status information
Browse files Browse the repository at this point in the history
#66

Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Dec 9, 2020
1 parent 0b92b52 commit 899f149
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

### v1.0.6 (unreleased)

- Add a "status" management command to display Project status information
https://github.com/nexB/scancode.io/issues/66

- Fix the env_file location to run commands from outside the root dir
https://github.com/nexB/scancode.io/issues/64

Expand Down
6 changes: 6 additions & 0 deletions docs/scanpipe-command-line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ Optional arguments:
List all the pipelines added of the project named ``PROJECT``.


`$ scanpipe status --project PROJECT`
--------------------------------------------

Display status information about the provided ``PROJECT``.


`$ scanpipe output --project PROJECT <output_file>`
---------------------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions scanpipe/management/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ def handle(self, *args, **options):
raise CommandError(f"Project {project_name} does not exit")


class RunStatusCommandMixin:
def get_run_status_code(self, run):
status = " "
if run.task_succeeded:
status = self.style.SUCCESS("SUCCESS")
elif run.task_exitcode and run.task_exitcode > 0:
status = self.style.ERROR("FAILURE")
elif run.task_start_date:
status = "RUNNING"
return status


def validate_inputs(inputs):
"""
Raise an error if one of the provided `inputs` is not an existing file.
Expand Down
15 changes: 4 additions & 11 deletions scanpipe/management/commands/show-pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,15 @@
# Visit https://github.com/nexB/scancode.io for support and download.

from scanpipe.management.commands import ProjectCommand
from scanpipe.management.commands import RunStatusCommandMixin


class Command(ProjectCommand):
class Command(ProjectCommand, RunStatusCommandMixin):
help = "Show pipelines of a project."

def handle(self, *args, **options):
super().handle(*args, **options)

for run in self.project.runs.all():
status = self.get_run_status_code(run)
self.stdout.write(f" [{status}] {run.pipeline}")

def get_run_status_code(self, run):
status = " "
if run.task_succeeded:
status = self.style.SUCCESS("SUCCESS")
elif run.task_exitcode and run.task_exitcode > 0:
status = self.style.ERROR("FAILURE")
return status
status_code = self.get_run_status_code(run)
self.stdout.write(f" [{status_code}] {run.pipeline}")
55 changes: 55 additions & 0 deletions scanpipe/management/commands/status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 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.

from scanpipe.management.commands import ProjectCommand
from scanpipe.management.commands import RunStatusCommandMixin
from scanpipe.models import CodebaseResource
from scanpipe.models import DiscoveredPackage
from scanpipe.models import ProjectError


class Command(ProjectCommand, RunStatusCommandMixin):
help = "Display status information about the provided project."

def handle(self, *args, **options):
super().handle(*args, **options)

status = [
f"Project: {self.project.name}",
f"Create date: {self.project.created_date.strftime('%b %d %Y %H:%M')}",
f"Work directory: {self.project.work_directory}",
"\nDatabase:",
]

for model_class in [CodebaseResource, DiscoveredPackage, ProjectError]:
object_count = model_class.objects.project(self.project).count()
status.append(f" - {model_class.__name__}: {object_count}")

runs = self.project.runs.all()
if runs:
status.append("\nPipelines:")
for run in runs:
status_code = self.get_run_status_code(run)
status.append(f" [{status_code}] {run.pipeline}")

for line in status:
self.stdout.write(line)
15 changes: 15 additions & 0 deletions scanpipe/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,18 @@ def test_scanpipe_management_command_run_resume(self, mock_resume_pipeline_task)
mock_resume_pipeline_task.assert_called_once()
expected = "Error during scanpipe/pipelines/docker.py execution:"
self.assertIn(expected, err.getvalue())

def test_scanpipe_management_command_status(self):
project = Project.objects.create(name="my_project")
project.add_pipeline(self.pipeline_location)

options = ["--project", project.name, "--no-color"]
out = StringIO()
call_command("status", *options, stdout=out)

output = out.getvalue()
self.assertIn("Project: my_project", output)
self.assertIn("- CodebaseResource: 0", output)
self.assertIn("- DiscoveredPackage: 0", output)
self.assertIn("- ProjectError: 0", output)
self.assertIn("[ ] scanpipe/pipelines/docker.py", output)

0 comments on commit 899f149

Please sign in to comment.