license-headers

Added in version 0.1.0.

Description

Add summary of the license to the top of every .py file of the project. By default it takes the content of the LICENSE file from the root directory and adds it to the top of every .py file of the project, prepending it with a comment symbol #.

Configuration

- repo: https://github.com/adrybakov/pre-commit-hooks
  rev: 0.3.1                  # Always use the last available tag
  hooks:
    - id: license-headers     # Required
      args:                   # Optional (as the next lines)
        - --license-template  # If license summary is not in a "LICENSE" file
        - LICENSE.template    # Path to the license summary file

See pre-commit docs for the list of additional parameters.

License template

File with the license template support some dynamic placeholders:

Placeholder

Generated value

{{current-year}}

datetime.now().year

An example template is

MIT License

Copyright (c) 2023 - {{current-year}} Andrey Rybakov (rybakov.ad@icloud.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Standalone usage

The above example will be equivalent to the following code (assuming that you installed it with pip):

license-headers some-filename-1.py some-filename-2.py --license-template LICENSE.template

Implementation details

Text of the license is added via the call of the function:

pre_commit_hooks.license_headers.ensure_license(filenames, license_template, verbose=False)[source]

Insert license text at the top of the file.

Every line at the beginning of the file, which is empty (consists only of spaces and a newline character at the end) or starts with a hash symbol ("#") is removed.

Parameters:
filenameslist of str

List of file to which the license is added.

license_templatelist of str

Path to the file with the template of the license.