Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate license-check from argparse to click #47

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 21 additions & 30 deletions obs_common/license_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
This script checks files for license headers.
"""

import argparse
import pathlib
import subprocess
import sys

import click


DESCRIPTION = (
"Checks files in specified directory for license headers. "
+ "If you don't specify a target, it'll check all files in \"git ls-files\"."
"Check specified target files and directories for license headers. "
+ 'If no targets are specified, check all files in "git ls-files".'
)

# From https://www.mozilla.org/en-US/MPL/2.0/
Expand Down Expand Up @@ -83,29 +84,19 @@ def has_license_header(path: pathlib.Path):
return False


def main(args=None):
parser = argparse.ArgumentParser(description=DESCRIPTION)
parser.add_argument(
"-l", "--file-only", action="store_true", help="print files only"
)
parser.add_argument("--verbose", action="store_true", help="verbose output")
parser.add_argument("target", help="file or directory tree to check", nargs="?")

parsed = parser.parse_args(args)

if parsed.target:
target = pathlib.Path(parsed.target)
if not target.exists():
if not parsed.file_only:
print(f"Not a valid file or directory: {target}")
return 1

if target.is_file():
targets = [target]

elif target.is_dir():
targets = list(target.rglob("*"))

@click.command(help=DESCRIPTION)
@click.argument(
"targets", nargs=-1, type=click.Path(exists=True, path_type=pathlib.Path)
)
@click.option("-l", "--file-only", is_flag=True, help="print files only")
@click.option("--verbose", is_flag=True, help="verbose output")
def main(targets, file_only, verbose):
if targets:
targets = [
target
for path in targets
for target in (path.rglob("*") if path.is_dir() else [path])
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty hard to read and reason about especially with the singular/plural variations of target.

else:
ret = subprocess.check_output(["git", "ls-files"])
targets = [
Expand All @@ -116,25 +107,25 @@ def main(args=None):

# Iterate through all the files in this target directory
for path in targets:
if parsed.verbose:
if verbose:
print(f"Checking {path}")
if is_code_file(path) and not has_license_header(path):
missing_headers += 1
if parsed.file_only:
if file_only:
print(str(path))
else:
print(f"File {path} does not have license header.")

if missing_headers > 0:
if not parsed.file_only:
if not file_only:
print(f"Files with missing headers: {missing_headers}")
print("")
print("Add this:")
print("")
print("\n".join(MPLV2))
return 1

if not parsed.file_only:
if not file_only:
print("No files missing headers.")

return 0
Expand Down
17 changes: 15 additions & 2 deletions tests/test_license_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,33 @@
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

from click.testing import CliRunner

from obs_common import license_check


def test_it_runs():
"""Test whether the module loads and spits out help."""
runner = CliRunner()
result = runner.invoke(license_check.main, ["--help"])
assert result.exit_code == 0


def test_license_present(tmp_path):
target = tmp_path / "target.py"
target.write_text(
"# This Source Code Form is subject to the terms of the Mozilla Public\n"
"# License, v. 2.0. If a copy of the MPL was not distributed with this\n"
"# file, You can obtain one at https://mozilla.org/MPL/2.0/.\n"
)
assert license_check.main([str(target)]) == 0
runner = CliRunner()
result = runner.invoke(license_check.main, [str(target)])
assert result.exit_code == 0


def test_license_missing(tmp_path):
target = tmp_path / "target.py"
target.touch()
assert license_check.main([str(target)]) != 0
runner = CliRunner()
result = runner.invoke(license_check.main, [str(target)])
assert result.exit_code == 0