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

Add GitHub workflows to lint code scripts and copyright #5

Merged
merged 10 commits into from
Sep 23, 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
42 changes: 42 additions & 0 deletions .github/check_license.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os

# Define the license header you are looking for
license_header_first = [
"Copyright (c) MONAI Consortium",
"limitations under the License."
]

# List of file extensions to check
file_extensions = ['.py', '.sh', '.ipynb', '.slurm']

def check_license_in_file(file_path):
"""Check if the file contains the license header"""
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
for license_header in license_header_first:
if license_header not in content:
return False
return True

def check_license_in_directory(directory):
"""Check for missing license headers in all files in the directory"""
files_without_license = []

for root, dirs, files in os.walk(directory):
for file in files:
if any(file.endswith(ext) for ext in file_extensions):
file_path = os.path.join(root, file)
if not check_license_in_file(file_path):
files_without_license.append(file_path)

return files_without_license


if __name__ == '__main__':
# Change this to the directory you want to check
directory_to_check = '.'

missing_license_files = check_license_in_directory(directory_to_check)

if missing_license_files:
raise FileNotFoundError(f"Copyright is missing in the following files:\n {'\n'.join(missing_license_files)}")
23 changes: 23 additions & 0 deletions .github/workflows/copyright.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Copyright Check

on:
pull_request:
branches:
- main

jobs:
copyright:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Run copyright check
run: |
python .github/check_license.py
37 changes: 37 additions & 0 deletions .github/workflows/general-linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: General Linting

on:
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-ci.txt

- name: Run isort
run: |
isort --check-only --diff .

- name: Run black
run: |
black --diff .


- name: Run ruff
run: |
ruff check .
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,26 @@

# MONAI-VILA

monai_vila2d
monai_vila2d

## Contributing

To lint the code, please install these packages:

```bash
pip install -r requirements-ci.txt
```

Then run the following command:

```bash
isort --check-only --diff . # using the configuration in pyproject.toml
black . --check # using the configuration in pyproject.toml
ruff check . # using the configuration in ruff.toml
```

To auto-format the code, run the following command:

```bash
isort . && black . && ruff format .
```
30 changes: 30 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[tool.black]
line-length = 122
include = '\.pyi?$'
exclude = '''
/(
build
| docs
| setup\.py
| venv.*
| .*test_.*
| \.mypy_cache
| \.vscode
)/
'''

[tool.isort]
profile = "black"
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
line_length = 122
skip_glob = [
"build/*",
"docs/*",
"setup.py",
"venv*/",
"*test_*",
"./.vscode/*",
]
3 changes: 3 additions & 0 deletions requirements-ci.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
isort==5.13.2
black==24.8.0
ruff==0.6.3
13 changes: 13 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
line-length = 122

exclude = [
"build/*",
"docs/*",
"setup.py",
"venv*/",
"*test_*",
"./.vscode/*",
]

[lint]
select = ["D1"]
Loading