JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
General
Third-Party External

Including Python Components in a CodeSonar Project

This page describes how to extend your CodeSonar-facing build so that your CodeSonar project includes:

We provide built-in subcommand codesonar python_scan.py to simplify this process.

Important: This feature requires Pylint 1.7 or later. If Pylint is not already available on your system, you will need to install and configure it.



Overview

This page describes how to extend your CodeSonar-facing build so that your CodeSonar project includes:

Incorporating Python components follows the standard process for including a tier 3 component. The specializations to that process are summarized in the following table. For detailed instructions, see Including Python Components In Your CodeSonar project, below. We also provide an example Makefile.

Language Tools You will need a Python installation.
Third-Party Analyzer Pylint
Third-Party Analysis Step You do not need to explicitly invoke Pylint: it is invoked in the codesonar python_scan.py subcommand.
Extend CodeSonar-facing Build Invoke codesonar python_scan.py on your Python source files (.py). This will analyze the files with Pylint, then add the Pylint SARIF results and the source files to the CodeSonar project.

Including Python Components In Your CodeSonar Project

Incorporating Python components follows the standard process for including a tier 3 component.

  1. Install any required language tools, such as compilers.
    Make sure you have a Python installation.
  2. Install the third-party analyzer.
    This feature requires Pylint 1.7 or later. If Pylint is not already available on your system, you will need to install and configure it.
    The Pylint installation must be in your PATH.
  3. Install the third-party SARIF converter, if needed.
    There is nothing to do for this step: codesonar python_scan.py will take care of converting the Pylint output to SARIF.
  4. Extend the regular build to invoke the third-party analyzer (and SARIF converter, if needed).
    You do not need a separate analysis step.
  5. Extend the CodeSonar-facing build.
    Extend your CodeSonar-facing build by adding one or more invocations of codesonar python_scan.py.
    All the Python source files must be present before codesonar python_scan.py is invoked. Different build systems have different mechanisms for specifying this requirement. For example, you might be able to specify that the CodeSonar-facing build depends on the Python source files, or that the CodeSonar-facing build can only take place if the steps required to produce the Python source files have already occurred.

    If you have not already set up a CodeSonar-facing build for your regular software project, you will need to create one.

  6. Perform the CodeSonar build/analysis, observing the entire execution of your CodeSonar-facing build.

Diagram: including Python components in a CodeSonar project

The following diagram illustrates the extended software build described above, and its interactions with the CodeSonar build/analysis.

Diagram illustrating the procedure described on this page: extend a build that involves Python source files into a CodeSonar-facing build by adding invocations of 'codesonar python_scan.py'.

Note

In this section (and throughout this manual), $CSONAR indicates the CodeSonar installation directory.

Warning Classes

When a SARIF file is imported into a CodeSonar project, the SARIF importer determines an associated warning class for each rule object in the SARIF rules, creating this warning class if it does not already exist.

The SARIF importer has special handling for warning classes derived from SARIF produced by Pylint. For more information, see Warning Classes Corresponding to Pylint Rules.

codesonar python_scan.py

To analyze Python source files with Pylint and then add the source files and analysis results to a project, CodeSonar observes the execution of a command that incorporates one or more invocations of codesonar python_scan.py.

codesonar python_scan.py [file_or_dir ...] [-C rundir, -directory rundir] \
[-include-sources file_pat] [-exclude-sources file_pat] \
[-Xpylint_options_list] [-pylint-output pylint_raw_fname] [-sarif-output sarif_fname] \
[-source-max-bytes num] [@infile]

where:

[file_or_dir ...] is a space-separated list of Python module source file or package directory paths.
  • Relative paths are interpreted with respect to the directory specified with -C/-directory.
  • For each file path, that file is analyzed and imported into the CodeSonar project.
  • For each directory path, all .py files in the directory tree rooted at that directory are analyzed and imported.

The set of files specified with file_or_dir arguments can be modified with -include-sources and -exclude-sources.

Note: the file_or_dir arguments are not glob patterns (as used for -include-sources and -exclude-sources). However, your shell may expand patterns into file and directory names.

If there are no file_or_dir arguments and no -include-sources arguments, the codesonar python_scan.py subcommand will fail.

[-C rundir]
[-directory rundir]
Pylint will be run in rundir. If -C/-directory is not specified, Pylint will be run in the current working directory.

Any file_or_dir, -include-sources file_pat, and -exclude-sources file_pat arguments are interpreted relative to rundir. For example:

  • codesonar python_scan.py .
    Analyze all .py files in the directory tree rooted at the current working directory.
  • codesonar python_scan.py . -C mymod
    Analyze all .py files in the directory tree rooted at directory mymod.
  • codesonar python_scan.py dirA dirB/dirC -C mymod
    Analyze all .py files in the directory tree rooted at directory mymod/dirA and all .py files in the directory tree rooted at directory mymod/dirB/dirC
[-include-sources file_pat]
[-exclude-sources file_pat]
All Python source files (.py) indicated by the -C/-directory and file_or_dir argument combination will be analyzed with Pylint.
By default, this set of Python source files is then imported into the CodeSonar project. Use -include-sources and -exclude-sources to adjust the imported set by adding or removing files.

file_pat is a recursive glob pattern representing a source file path. It matches all files whose file path matches file_pat. Relative paths are interpreted with respect to the directory specified with -C/-directory.

When a single codesonar python_scan.py invocation contains a combination of -include-sources and -exclude-sources options, the set of imported files is determined by applying the corresponding inclusions and exclusions in the order in which they appear on the command line. Some examples are provided below.

Note:

  • Your shell may be configured to expand patterns into file and directory names. If so, make sure you quote the file_pat appropriately to indicate that the shell should pass it through to codesonar python_scan.py. In most cases, this means using single quotes ('); for Windows cmd, use double quotes (").
[-Xpylint_options_list] The list of options and values in pylint_options_list are passed to the Pylint invocation.
  • pylint_options_list can be comma-separated (,) or plus-separated (+); use the same character to introduce the list.
    For example, the following are equivalent: both specify that Pylint should be invoked with --max-line-length 50.
    codesonar python_scan.py -X,--max-line-length,50 [... remaining python_scan.py options]
    codesonar python_scan.py -X+--max-line-length+50 [... remaining python_scan.py options]
  • You can specify multiple Pylint options in a single pylint_options_list; you can also specify -X multiple times in a single codesonar python_scan.py invocation.
    For example, the following are equivalent: both specify that Pylint should be invoked with --max-line-length 50 and --allow-global-unused-variables n.
    codesonar python_scan.py -X,--max-line-length,50,--allow-global-unused-variables,n [... remaining python_scan.py options]
    codesonar python_scan.py -X,--max-line-length,50 -X,--allow-global-unused-variables,n [... remaining python_scan.py options]
Example notes:
[-pylint-output pylint_raw_fname] Specifies that the raw analysis results from Pylint should be written to file pylint_raw_fname.
The primary use case for this option is in projects for which you wish to retain the raw Pylint results after the CodeSonar build/analysis has finished

If -pylint-output pylint_raw_fname is not specified, codesonar python_scan.py will use a temporary file for the raw Pylint results.

[-sarif-output sarif_fname] Specifies that the SARIF output from Pylint should be written to file sarif_fname.
The primary use case for this option is in projects for which you wish to retain the SARIF results after the CodeSonar build/analysis has finished

If -sarif_output sarif_fname is not specified, codesonar python_scan.py will use a temporary file for the SARIF output.

[-source-max-bytes num] Specifies a maximum size of num bytes for imported Python source files: files larger than this maximum size will be ignored by the importer.
If -source-max-bytes is not specified, the importer will ignore files larger than 500KB.
[@infile] infile is a text file containing a list of -include-sources file_pat and -exclude-sources file_pat entries to be added to the codesonar python_scan.py command line.
  • The file must have one entry per line.
  • The entries will be added to the command line at the location where the @infile argument occurred, in the order in which they appear in infile.

When CodeSonar processes a codesonar python_scan.py command, it does the following.

  1. Compute the set of source files to be analyzed. This will contain every source file F.py that matches the specified combination of -include-sources and -exclude-sources options: those specified directly on the command line and those present in a file specified with @infile.
  2. Execute a single Pylint invocation on this set of files.
  3. Convert the Pylint results to SARIF.
  4. Import the SARIF results into the CodeSonar project, along with every F.py in the set.

-include-sources/-exclude-sources Examples

Importing Python source files with codesonar python_scan.py

The codesonar python_scan.py subcommand imports Python source files into the CodeSonar project using the same mechanism as codesonar import_sarif.py, because CodeSonar does not have a full front end for Python.

There are several consequences when files are directly imported with codesonar import_sarif.py.

Example Makefile

An example Makefile for a Python project is shown below. We also provide an example Makefile for a mixed-language project that has a Python component: see Including Tier 3 Components in a CodeSonar Project: Example Makefile

The Makefile illustrates a case where the Python component is not compiled. The all dependencies are thus all the .py source files in the component, and there are no .pyc targets. If the regular build included a Python compilation step, the CodeSonar-facing build would be the same as shown here.

.PHONY: all clean csonar_facing

all: $(SRCDIR)/python_component.py

# Invoke codesonar python_scan.py to analyze the Python source file with Pylint,
# then import the SARIF results and the source file into the project.
csonar_facing: $(SRCDIR)/python_component.py
	"$(CSONAR)"/codesonar/bin/codesonar python_scan.py -include-sources "$(SRCDIR)/python_component.py"

clean:

Try the Example Makefile

We have provided a minimal sample project so you can experiment with the example Makefile.

  1. Sign in to a machine where CodeSonar is installed.
  2. Select or create a working directory. The working directory location must be outside both your CodeSonar installation directory and your hub directory.
  3. If any of the following are not installed, install them now.
  4. If your Pylint installation is not in your PATH, add it now.
  5. Save the example files to the working directory (right-click on the link and select Save Target As... or equivalent).
  6. cd to the working directory.
  7. Rename the Makefile (this simplifies the CodeSonar analysis command later).
    mv Makefile.Python.txt Makefile
  8. Edit Makefile to add a setting for SRCDIR at the top (above the .PHONY line). If CSONAR isn't defined in your environment, set that as well.
    SRCDIR=.
    
    or
    SRCDIR=.
    CSONAR=path/to/your/codesonar/installation
    # on Windows, typically CSONAR=C:\\Program Files\\CodeSecure\\CodeSonar
    
  9. Build and analyze the CodeSonar project. If your hub is at the default location, your command will be something like:
    codesonar analyze PythonExample make csonar_facing
    For full codesonar analyze command details, see Command Line Build/Analysis.
  10. When the analysis finishes, view the Analysis page in the hub GUI.

Other Targets in the Example Makefile

The clean target has an empty recipe because codesonar python_scan.py cleans up its own intermediate files.

Related Links

 

To report problems with this documentation, please visit https://support.codesecure.com/.