Skip to content

Commit

Permalink
New checks added: liccheck, copyright_check AEA-1690 (#31)
Browse files Browse the repository at this point in the history
* Liccheck added.

* Copywrite check added.

* Copywrites added.

* Comment updated.

* Makefile updated.

* Copyright check added.

* Makefile updated.

* Misprint fixed.

* Misprint fixed.
  • Loading branch information
panasevychol authored Aug 5, 2021
1 parent 381c686 commit 304db06
Show file tree
Hide file tree
Showing 40 changed files with 856 additions and 5 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ jobs:
run: pipenv run make pylint
- name: Unused code check
run: pipenv run make vulture
- name: License check
run: pipenv run make liccheck
- name: Copyright check
run: pipenv run make copyright-check
- name: Unit tests
run: pipenv run make test
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ build/

# IDEs
.idea/

# Requirements file generated for license check (liccheck)
requirements.txt
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,19 @@ pylint:
test:
python -m unittest discover -s $(PYCOSM_SRC_DIR)

####################
### License and copyright checks
####################

.PHONY: liccheck
liccheck:
pipenv lock -r > requirements.txt
liccheck -s strategy.ini -r requirements.txt -l PARANOID

.PHONY: copyright-check
copyright-check:
python scripts/check_copyright.py

####################
### Combinations
####################
Expand All @@ -142,6 +155,8 @@ check:
make safety
make mypy
make pylint
make liccheck
make copyright-check
make test

debug:
Expand Down
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ isort = "==5.9.3"
darglint = "==1.8.0"
vulture = "==2.3"
pylint = "==2.9.6"
liccheck = "==0.6.2"
flake8-copyright = "==0.2.2"

[packages]
cosm = {editable = true, extras = ["dev", "test"], path = "."}
Expand Down
43 changes: 38 additions & 5 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 98 additions & 0 deletions scripts/check_copyright.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2019 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

"""
This script checks that all the Python files of the repository have:
- (optional) the Python shebang
- the encoding header;
- the copyright notice;
It is assumed the script is run from the repository root.
"""

import itertools
import re
import sys
from pathlib import Path


SUPPORTED_YEARS = ["2019", "2020", "2021"]


HEADER_REGEX = fr"""(#!/usr/bin/env python3
)?# -\*- coding: utf-8 -\*-
# ------------------------------------------------------------------------------
#
# (Copyright 2018-({"|".join(SUPPORTED_YEARS)}) Fetch.AI Limited|Copyright [0-9]{{4}}(-[0-9]{{4}})? [a-zA-Z_]+)
#
# Licensed under the Apache License, Version 2\.0 \(the \"License\"\);
# you may not use this file except in compliance with the License\.
# You may obtain a copy of the License at
#
# http://www\.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\.
#
# ------------------------------------------------------------------------------
"""


def check_copyright(file: Path) -> bool:
"""
Given a file, check if the header stuff is in place.
Return True if the files has the encoding header and the copyright notice,
optionally prefixed by the shebang. Return False otherwise.
:param file: the file to check.
:return: True if the file is compliant with the checks, False otherwise.
"""
content = file.read_text()
header_regex = re.compile(HEADER_REGEX, re.MULTILINE)
return re.match(header_regex, content) is not None


if __name__ == "__main__":
python_files = itertools.chain(
Path("src/cosm").glob("**/*.py"),
[Path("setup.py")],
)

# filter out protobuf files (*_pb2.py)
python_files_filtered = filter(
lambda x: not str(x).endswith("_pb2.py"), python_files
)

bad_files = [
filepath for filepath in python_files_filtered if not check_copyright(filepath)
]

if len(bad_files) > 0:
print("The following files are not well formatted:")
print("\n".join(map(str, bad_files)))
sys.exit(1)
else:
print("OK")
sys.exit(0)
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[flake8]
paths=src
copyright-check = True
select = C
exclude=.md, vulture_whitelist.py
ignore = E501,E701

Expand Down
19 changes: 19 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

from setuptools import setup, find_packages
import pathlib

Expand Down
18 changes: 18 additions & 0 deletions src/cosm/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------
19 changes: 19 additions & 0 deletions src/cosm/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

"""This package contains the Auth modules."""
19 changes: 19 additions & 0 deletions src/cosm/auth/interface.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

"""Interface for the Auth functionality of CosmosSDK."""

from abc import ABC, abstractmethod
Expand Down
19 changes: 19 additions & 0 deletions src/cosm/auth/rest_client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

"""Implementation of Auth interface using REST."""

from google.protobuf.json_format import Parse
Expand Down
19 changes: 19 additions & 0 deletions src/cosm/auth/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2018-2021 Fetch.AI Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.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.
#
# ------------------------------------------------------------------------------

"""Tests for REST implementation of Auth."""

import json
Expand Down
Loading

0 comments on commit 304db06

Please sign in to comment.