Skip to content

How To: Add a post scan plugin

John M. Horan edited this page Oct 8, 2019 · 11 revisions

Introduction

Automatic vs. Manual Installation

Automatic

Some post-scan plugins are installed when ScanCode itself is installed, e.g., the License Policy plugin, whose code is located here:

https://github.com/nexB/scancode-toolkit/blob/develop/src/licensedcode/plugin_license_policy.py

These plugins do not require any additional installation steps and can be used as soon as ScanCode is up and running.

Manual

ScanCode is also designed to use post-scan plugins that must be installed separately from the installation of ScanCode. The code for this sort of plugin is located here:

https://github.com/nexB/scancode-toolkit/tree/develop/plugins

This wiki page will focus on manually-installed post-scan plugins.

Example Plugin: Hello ScanCode

To illustrate the creation of a simple post-scan plugin, we'll create a hypothetical plugin named Hello ScanCode.

The top-level folder

(1) In the /scancode-toolkit/plugins/ directory, add a folder with a relevant name, e.g., hello-scancode. This folder will hold all of your plugin code.

(2) Inside the /hello-scancode/ folder you'll need to add a new folder named src and 7 files.

  • /src/ -- This folder will contain your primary Python code and is discussed in more detail in the following section.
  • .gitignore -- x
  • apache-2.0.LICENSE -- x
  • MANIFEST.in -- x
  • NOTICE -- x
  • README.md -- x
  • setup.cfg -- x
  • setup.py -- x

The /src/ folder

(1) Add an __init__.py file inside the src folder. This file can be empty, and is used to indicate that the folder should be treated as a Python package directory.

  • [No longer required when ScanCode moves from Python 2.7 to 3.5+?]

(2) x

  • x

The /hello_scancode/ folder

(1) Add an __init__.py file inside the hello_scancode folder.

(2) Add a hello_scancode.py file.

Notice at the top of the file

#
# Copyright (c) 2019 nexB Inc. and others. All rights reserved.
# http://nexb.com and https://github.com/nexB/scancode-toolkit/
# The ScanCode software is licensed under the Apache License version 2.0.
# Data generated with ScanCode require an acknowledgment.
# 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.
#
# When you publish or redistribute any data created with ScanCode or any ScanCode
# derivative work, you must accompany this data with the following acknowledgment:
#
#  Generated with ScanCode and provided on an "AS IS" BASIS, WITHOUT WARRANTIES
#  OR CONDITIONS OF ANY KIND, either express or implied. No content created from
#  ScanCode should be considered or used as legal advice. Consult an Attorney
#  for any legal advice.
#  ScanCode is a free software code scanning tool from nexB Inc. and others.
#  Visit https://github.com/nexB/scancode-toolkit/ for support and download.

Imports

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from plugincode.post_scan import PostScanPlugin
from plugincode.post_scan import post_scan_impl
from scancode import CommandLineOption
from scancode import POST_SCAN_GROUP

Create a PostScanPlugin class

@post_scan_impl
class SayHello(PostScanPlugin):
    """
    Illustrate a simple "Hello World" post-scan plugin.
    """

    options = [
        CommandLineOption(('--hello',),
                                        is_flag=True, default=False,
                                        help='Generate a simple "Hello ScanCode" greeting in the terminal.',
                                        help_group=POST_SCAN_GROUP)
    ]

    def is_enabled(self, hello, **kwargs):
        return hello

    def process_codebase(self, codebase, hello, **kwargs):
        """
        Say hello.
        """
        if not self.is_enabled(hello):
            return

        print('\nHello ScanCode!!\n')

[More to come.]

Clone this wiki locally