Skip to content

Commit

Permalink
Consolidate naming convention in multiple places #91
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Druez <tdruez@nexb.com>
  • Loading branch information
tdruez committed Feb 15, 2021
1 parent 7c29986 commit 5d0726b
Show file tree
Hide file tree
Showing 23 changed files with 107 additions and 98 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
- Implement Pipeline registration through distribution entry points.
Pipeline can now be installed as part of external libraries.
With this change pipelines are no longer referenced by the
Python script path, but by their registered name. This is a
breaking command line API change.
Python script path, but by their registered name.
This is a breaking command line API change.
https://github.com/nexB/scancode.io/issues/91

- Add a "Run Pipeline" button in the Pipeline modal of the Project details view
Expand Down
8 changes: 3 additions & 5 deletions docs/scanpipe-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ To get started locally with the API:
From the bottom of this page you can **create a new project**, **upload an input
file** and **add a pipeline** to this project at once.

.. note::
If you add a pipeline, the pipeline starts immediately on project creation.

----

Multiple **views** and **actions** are available to manage projects.
Expand All @@ -25,8 +22,9 @@ From a ``Project Instance`` view:
Add pipeline
------------

Add the selected ``pipeline`` to the ``project``. If the ``start`` value is provided,
the pipeline run will start immediately on pipeline addition.
Add the selected ``pipeline`` to the ``project``.
If the ``execute_now`` value is provided, the pipeline execution will start immediately
on pipeline addition.

Errors
------
Expand Down
14 changes: 7 additions & 7 deletions docs/scanpipe-command-line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Display help for the provided subcommand.
For example::

$ scanpipe create-project --help
usage: scanpipe create-project [--pipeline PIPELINES] [--input INPUTS] name
usage: scanpipe create-project [--pipeline PIPELINES] [--input INPUTS] [--execute] name

Create a ScanPipe project.
Expand All @@ -55,10 +55,10 @@ Optional arguments:
- ``--input INPUTS`` Input file locations to copy in the :guilabel:`input/` workspace
directory.

- ``--run`` Start running the pipelines right after project creation.
- ``--execute`` Execute the pipelines right after project creation.

.. warning::
The pipelines are added and will be running in the order of the provided options.
The pipelines are added and will be executed in the order of the provided options.

`$ scanpipe add-input --project PROJECT <input ...>`
----------------------------------------------------
Expand Down Expand Up @@ -88,10 +88,10 @@ add the docker pipeline to your project::
$ scanpipe add-pipeline --project foo docker


`$ scanpipe run --project PROJECT`
----------------------------------
`$ scanpipe execute --project PROJECT`
--------------------------------------

Run the next pipeline of the project named ``PROJECT`` queue.
Execute the next pipeline of the project named ``PROJECT`` queue.


`$ scanpipe show-pipeline --project PROJECT`
Expand All @@ -101,7 +101,7 @@ List all the pipelines added of the project named ``PROJECT``.


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

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

Expand Down
6 changes: 3 additions & 3 deletions docs/scanpipe-tutorial-1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ Step-by-step
.. note::
The ``inputs`` and ``pipelines`` can be provided directly at once when
calling the ``create-project`` command.
A ``run`` option is also available to start the pipeline execution right
An ``execute`` option is also available to start the pipeline execution right
after the project creation.
For example, the following command will create a project named ``staticbox2``,
copy the test docker image to the project's inputs, add the docker pipeline,
and execute the pipeline run in one operation::
and execute the pipeline in one operation::

$ scanpipe create-project staticbox2 \
--input ~/30-alpine-nickolashkraus-staticbox-latest.tar \
--pipeline docker \
--run
--execute
4 changes: 2 additions & 2 deletions docs/scanpipe-tutorial-2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ Step-by-step

- The following command will create a new project named ``asgiref``,
add the archive as an input for the project,
add the ``scan_codebase`` pipeline, and run its execution::
add the ``scan_codebase`` pipeline, and execute it::

$ scanpipe create-project asgiref \
--input ~/asgiref-3.3.0-py3-none-any.whl \
--pipeline scan_codebase \
--run
--execute

.. note::
The content of the :guilabel:`input/` directory will be copied in the
Expand Down
14 changes: 7 additions & 7 deletions scanpipe/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ProjectSerializer(ExcludeFromListViewMixin, serializers.ModelSerializer):
"Requires an input file."
),
)
start = serializers.BooleanField(write_only=True)
execute_now = serializers.BooleanField(write_only=True)
upload_file = serializers.FileField(write_only=True, required=False)
next_run = serializers.CharField(source="get_next_run", read_only=True)
runs = RunSerializer(many=True, read_only=True)
Expand All @@ -104,7 +104,7 @@ class Meta:
"upload_file",
"created_date",
"pipeline",
"start",
"execute_now",
"input_root",
"output_root",
"next_run",
Expand Down Expand Up @@ -136,19 +136,19 @@ def get_discovered_package_summary(self, project):
def create(self, validated_data):
"""
Create a new `project` with optionally provided `upload_file` and `pipeline`.
The `start` paramter can be provided to start the Pipeline run on creation.
The `execute_now` parameter can be provided to execute the Pipeline on creation.
"""
upload_file = validated_data.pop("upload_file", None)
pipeline = validated_data.pop("pipeline", None)
start = validated_data.pop("start", False)
execute_now = validated_data.pop("execute_now", False)

project = super().create(validated_data)

if upload_file:
project.add_input_file(upload_file)

if pipeline:
project.add_pipeline(pipeline, start)
project.add_pipeline(pipeline, execute_now)

return project

Expand Down Expand Up @@ -197,13 +197,13 @@ class PipelineSerializer(serializers.ModelSerializer):
required=True,
write_only=True,
)
start = serializers.BooleanField(write_only=True)
execute_now = serializers.BooleanField(write_only=True)

class Meta:
model = Run
fields = [
"pipeline",
"start",
"execute_now",
]


Expand Down
6 changes: 3 additions & 3 deletions scanpipe/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ def add_pipeline(self, request, *args, **kwargs):
pipeline = request.data.get("pipeline")
if pipeline:
if pipeline in scanpipe_app_config.pipelines:
start = request.data.get("start")
project.add_pipeline(pipeline, start)
execute_now = request.data.get("execute_now")
project.add_pipeline(pipeline, execute_now)
return Response({"status": "Pipeline added."})

message = {"status": f"{pipeline} is not a valid pipeline."}
Expand Down Expand Up @@ -185,6 +185,6 @@ def start_pipeline(self, request, *args, **kwargs):
message = {"status": "Pipeline already started."}
return Response(message, status=status.HTTP_400_BAD_REQUEST)

transaction.on_commit(run.run_pipeline_task_async)
transaction.on_commit(run.execute_task_async)

return Response({"status": f"Pipeline {run.pipeline_name} started."})
7 changes: 6 additions & 1 deletion scanpipe/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
class ScanPipeConfig(AppConfig):
name = "scanpipe"
verbose_name = _("ScanPipe")
pipelines = {}

def __init__(self, app_name, app_module):
super().__init__(app_name, app_module)

# Mapping of registered pipeline names to pipeline classes.
self.pipelines = {}

def ready(self):
self.load_pipelines()
Expand Down
14 changes: 7 additions & 7 deletions scanpipe/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ class ProjectForm(forms.ModelForm):
choices=get_pipeline_choices(),
required=False,
)
run_pipeline = forms.BooleanField(
label="Run the selected pipeline",
execute_now = forms.BooleanField(
label="Execute pipeline now",
initial=True,
required=False,
)
Expand All @@ -60,21 +60,21 @@ class Meta:
"name",
"inputs",
"pipeline",
"run_pipeline",
"execute_now",
]

def save(self, *args, **kwargs):
project = super().save(*args, **kwargs)

pipeline = self.cleaned_data["pipeline"]
run_pipeline = self.cleaned_data["run_pipeline"]
execute_now = self.cleaned_data["execute_now"]

inputs = self.files.getlist("inputs")
for upload_file in inputs:
project.add_input_file(upload_file)

if pipeline:
project.add_pipeline(pipeline, start=run_pipeline)
project.add_pipeline(pipeline, execute_now)

return project

Expand All @@ -84,8 +84,8 @@ class AddPipelineForm(forms.Form):
choices=get_pipeline_choices(),
required=True,
)
run_pipeline = forms.BooleanField(
label="Run the selected pipeline",
execute_now = forms.BooleanField(
label="Execute pipeline now",
initial=True,
required=False,
)
Expand Down
16 changes: 9 additions & 7 deletions scanpipe/management/commands/create-project.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ def add_arguments(self, parser):
help="Input file locations to copy in the input/ work directory.",
)
parser.add_argument(
"--run",
"--execute",
action="store_true",
help="Start running the pipelines right after project creation.",
help="Execute the pipelines right after project creation.",
)

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

project = Project(name=name)
try:
Expand All @@ -74,8 +74,8 @@ def handle(self, *args, **options):
validate_pipelines(pipeline_names)
validate_inputs(inputs)

if run and not pipeline_names:
raise CommandError("The --run option requires one or more pipelines.")
if execute and not pipeline_names:
raise CommandError("The --execute option requires one or more pipelines.")

project.save()
msg = f"Project {name} created with work directory {project.work_directory}"
Expand All @@ -87,5 +87,7 @@ def handle(self, *args, **options):
for input_location in inputs:
project.copy_input_from(input_location)

if run:
call_command("run", project=project, stderr=self.stderr, stdout=self.stdout)
if execute:
call_command(
"execute", project=project, stderr=self.stderr, stdout=self.stdout
)
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def handle(self, *args, **options):
raise CommandError(f"No pipelines to run on project {self.project}")

self.stdout.write(f"Pipeline {run.pipeline_name} run in progress...")
run.run_pipeline_task_async()
run.execute_task_async()
run.refresh_from_db()

if run.task_succeeded:
Expand Down
19 changes: 10 additions & 9 deletions scanpipe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,11 @@ def clear_tmp_directory(self):

def inputs(self, pattern="**/*"):
"""
Return a generator of all the files and directories path of the input/
directory matching the provided `pattern`.
The default "**" pattern means: "this directory and all subdirectories,
Yield all the files and directories path of the input/ directory matching the
provided `pattern`.
The default "**/*" pattern means: "this directory and all subdirectories,
recursively".
Use the "*" pattern to list the root content only.
"""
return self.input_path.glob(pattern)

Expand Down Expand Up @@ -332,11 +333,11 @@ def move_input_from(self, input_location):

move_inputs([input_location], self.input_path)

def add_pipeline(self, pipeline_name, start=False):
def add_pipeline(self, pipeline_name, execute_now=False):
"""
Create a new Run instance with the provided `pipeline` on this project.
If `start` is True, the pipeline task is created.
If `execute_now` is True, the pipeline task is created.
The on_commit() is used to postpone the task creation after the transaction is
successfully committed.
If there isn’t an active transaction, the callback will be executed immediately.
Expand All @@ -347,8 +348,8 @@ def add_pipeline(self, pipeline_name, start=False):
pipeline_name=pipeline_name,
description=pipeline_class.get_doc(),
)
if start:
transaction.on_commit(run.run_pipeline_task_async)
if execute_now:
transaction.on_commit(run.execute_task_async)
return run

def get_next_run(self):
Expand Down Expand Up @@ -525,8 +526,8 @@ class Meta:
def __str__(self):
return f"{self.pipeline_name}"

def run_pipeline_task_async(self):
tasks.run_pipeline_task.apply_async(args=[self.pk], queue="default")
def execute_task_async(self):
tasks.execute_pipeline_task.apply_async(args=[self.pk], queue="default")

@property
def pipeline_class(self):
Expand Down
5 changes: 4 additions & 1 deletion scanpipe/pipelines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def execute(self):

return 0, ""

def add_error(self, error):
self.project.add_error(error, model=self.pipeline_name)

@contextmanager
def save_errors(self, *exceptions):
"""
Expand All @@ -118,7 +121,7 @@ def save_errors(self, *exceptions):
try:
yield
except exceptions as error:
self.project.add_error(error, model=self.pipeline_name)
self.add_error(error)


def is_pipeline(obj):
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_run_instance(run_pk):


@shared_task(bind=True)
def run_pipeline_task(self, run_pk):
def execute_pipeline_task(self, run_pk):
task_id = self.request.id
info(f"Enter `{self.name}` Task.id={task_id}", run_pk)

Expand Down
4 changes: 2 additions & 2 deletions scanpipe/templates/scanpipe/includes/add_pipeline_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

<div class="field">
<label class="checkbox" for="id_run_pipeline">
{{ add_pipeline_form.run_pipeline }}
Run the selected pipeline
{{ add_pipeline_form.execute_now }}
{{ add_pipeline_form.execute_now.label }}
</label>
</div>
</section>
Expand Down
2 changes: 1 addition & 1 deletion scanpipe/templates/scanpipe/includes/run_modal.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</div>
</div>
{% if not run.task_start_date %}
<a href="{% url 'project_run_pipeline' project.uuid run.uuid %}" class="run-pipeline-link has-text-weight-bold">
<a href="{% url 'project_execute_pipeline' project.uuid run.uuid %}" class="run-pipeline-link has-text-weight-bold">
<i class="fas fa-play-circle"></i>
Run Pipeline
</a>
Expand Down
Loading

0 comments on commit 5d0726b

Please sign in to comment.