diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..9d4ddf0 --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,50 @@ +name: Build and upload to PyPI + +on: + workflow_dispatch: + pull_request: + push: + branches: + - main + release: + types: + - published + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install Hatch + run: pipx install hatch + + - name: Hatch Build + run: hatch build + + - uses: actions/upload-artifact@v4 + with: + name: hatch-build-sdist-and-wheel + path: ./dist/* + + upload_pypi: + needs: [build] + runs-on: ubuntu-latest + environment: pypi + permissions: + id-token: write + if: github.event_name == 'release' && github.event.action == 'published' + steps: + - uses: actions/download-artifact@v4 + with: + pattern: hatch-build-* + path: dist + merge-multiple: true + + - uses: pypa/gh-action-pypi-publish@release/v1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e2646c2 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,113 @@ +name: test + +on: + push: + branches: + - main + pull_request: + branches: + - main + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }} + cancel-in-progress: true + +env: + STABLE_PYTHON_VERSION: '3.11' + PYTHONUNBUFFERED: "1" + FORCE_COLOR: "1" + +jobs: + run: + name: Python ${{ matrix.python-version }} on ${{ startsWith(matrix.os, 'macos-') && 'macOS' || startsWith(matrix.os, 'windows-') && 'Windows' || 'Linux' }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, windows-latest, macos-latest] + python-version: ['3.8', '3.9', '3.10', '3.11', '3.12'] + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - name: Install Hatch + run: pipx install hatch + + - name: Run pre-commit checks + uses: pre-commit/action@v3.0.1 + + - name: Run static analysis + run: hatch fmt --check + + - name: Check types + run: hatch run types:check + + - name: Run tests + run: hatch test --python ${{ matrix.python-version }} --cover --randomize --parallel + + - name: Disambiguate coverage filename + run: mv .coverage ".coverage.${{ matrix.os }}.${{ matrix.python-version }}" + + - name: Upload coverage data + uses: actions/upload-artifact@v4 + with: + name: coverage-${{ matrix.os }}-${{ matrix.python-version }} + path: .coverage* + + coverage: + name: Report coverage + runs-on: ubuntu-latest + needs: + - run + + steps: + - uses: actions/checkout@v4 + + - name: Set up Python ${{ env.STABLE_PYTHON_VERSION }} + uses: actions/setup-python@v5 + with: + python-version: ${{ env.STABLE_PYTHON_VERSION }} + + - name: Install Hatch + run: pipx install hatch + + - name: Trigger build for auto-generated files + run: hatch build --hooks-only + + - name: Download coverage data + uses: actions/download-artifact@v4 + with: + pattern: coverage-* + merge-multiple: true + + - name: Combine coverage data + run: hatch run coverage:combine + + - name: Export coverage reports + run: | + hatch run coverage:report-xml + hatch run coverage:report-uncovered-html + + - name: Upload uncovered HTML report + uses: actions/upload-artifact@v4 + with: + name: uncovered-html-report + path: htmlcov + + - name: Generate coverage summary + run: hatch run coverage:generate-summary + + - name: Write coverage summary report + if: github.event_name == 'pull_request' + run: hatch run coverage:write-summary-report + + - name: Update coverage pull request comment + if: github.event_name == 'pull_request' + uses: marocchino/sticky-pull-request-comment@v2 + with: + path: coverage-report.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a788b9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,242 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +src/alogamous/__version__.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..99b9ef9 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,35 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-toml + - id: check-json + - id: check-added-large-files + - id: mixed-line-ending + - id: detect-private-key + - id: check-merge-conflict + + - repo: https://github.com/asottile/pyupgrade + rev: v3.15.2 + hooks: + - id: pyupgrade + args: [--py310-plus] + + - repo: https://github.com/PyCQA/autoflake + rev: v2.3.1 + hooks: + - id: autoflake + args: [--in-place] + + - repo: local + hooks: + - id: fmt + name: hatch fmt + entry: hatch fmt + language: system + pass_filenames: false + always_run: true diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8f7698e --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python" +} diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..fc9222b --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,48 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment include: + +* Using welcoming and inclusive language +* Being respectful of differing viewpoints and experiences +* Gracefully accepting constructive criticism +* Focusing on what is best for the community +* Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery and unwelcome sexual attention or advances +* Trolling, insulting/derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or electronic address, without explicit permission +* Other conduct which could reasonably be considered inappropriate in a professional setting + +## Our Responsibilities + +Project maintainers will seek to clarify the standards of acceptable behavior and may take appropriate and fair corrective action in response to any instances of unacceptable behavior. Project maintainers have the right to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies within all project spaces, and it also applies when an individual is representing the project or its community in public spaces. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at opensource@aquatic.com. Complaints will be reviewed and investigated as deemed appropriate and may result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see +https://www.contributor-covenant.org/faq diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..72bac18 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright (c) 2024 Aquatic Technologies LLC + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..5db6e0e --- /dev/null +++ b/README.md @@ -0,0 +1,67 @@ +# alogamous + +[![PyPI - Version](https://img.shields.io/pypi/v/alogamous.svg)](https://pypi.org/project/alogamous) +[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/alogamous.svg)](https://pypi.org/project/alogamous) + +----- + +## Table of Contents + +- [Installation](#installation) +- [License](#license) + +## Installation + +```console +pip install alogamous +``` + +## Development + +1. Open a terminal + +1. Install [hatch](https://hatch.pypa.io/latest/) + + ```bash + curl -Lo hatch-universal.pkg https://github.com/pypa/hatch/releases/latest/download/hatch-universal.pkg + sudo installer -pkg ./hatch-universal.pkg -target / + ``` + +1. Restart your terminal + + Hatch modifies your system PATH variable, and this won't take effect unless you restart the terminal. + +1. Make sure hatch works + + ```bash + hatch --version + ``` + +1. Clone the repo + + ```bash + git clone https://github.com/aquanauts/alogamous.git + cd alogamous + ``` + +1. Install pre-commit hooks + + This will make sure certain checks are run when committing code. + + ```bash + hatch run pre-commit:install + ``` + +1. Run the tests + + ```bash + hatch test + ``` + +1. Setup your IDE + + ??? + +## License + +`alogamous` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license. diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..d0860c5 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,89 @@ +[build-system] +requires = ["hatchling", "hatch-vcs"] +build-backend = "hatchling.build" + +[project] +name = "alogamous" +dynamic = ["version"] +description = "A log anomaly detection framework" +readme = "README.md" +requires-python = ">=3.8" +license = "MIT" +keywords = [] +classifiers = [ + "Development Status :: 4 - Beta", + "Programming Language :: Python", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", +] +dependencies = [ + "pandas", +] + +[project.urls] +Documentation = "https://github.com/aquanauts/alogamous" +Issues = "https://github.com/aquanauts/alogamous/issues" +Source = "https://github.com/aquanauts/alogamous" + +[tool.hatch.version] +source = "vcs" + +[tool.hatch.build.hooks.vcs] +version-file = "src/alogamous/__version__.py" + +[tool.hatch.envs.default] +type = "virtual" +path = ".venv" + +[tool.hatch.envs.types] +extra-dependencies = [ + "mypy>=1.0.0", +] +[tool.hatch.envs.types.scripts] +check = "mypy --install-types --non-interactive {args:src/alogamous tests}" + +[tool.hatch.envs.coverage] +detached = true +dependencies = [ + "coverage[toml]>=6.2", + "lxml", +] +[tool.hatch.envs.coverage.scripts] +combine = "coverage combine {args}" +report-xml = "coverage xml" +report-uncovered-html = "coverage html --skip-covered --skip-empty" +generate-summary = "python scripts/generate_coverage_summary.py" +write-summary-report = "python scripts/write_coverage_summary_report.py" + +[tool.hatch.envs.pre-commit] +detached = true +dependencies = [ + "pre-commit>=3.7.1" +] +[tool.hatch.envs.pre-commit.scripts] +install = "pre-commit install" +run = "pre-commit run -av" + +[tool.coverage.run] +source_pkgs = ["alogamous", "tests"] +branch = true +parallel = true +omit = [ + "src/alogamous/__about__.py", +] + +[tool.coverage.paths] +alogamous = ["src/alogamous", "*/alogamous/src/alogamous"] +tests = ["tests", "*/alogamous/tests"] + +[tool.coverage.report] +exclude_lines = [ + "no cov", + "if __name__ == .__main__.:", + "if TYPE_CHECKING:", +] diff --git a/scripts/generate_coverage_summary.py b/scripts/generate_coverage_summary.py new file mode 100644 index 0000000..424ca8b --- /dev/null +++ b/scripts/generate_coverage_summary.py @@ -0,0 +1,53 @@ +import json +from collections import defaultdict +from pathlib import Path + +from lxml import etree # nosec B410 + +PACKAGES = { + "src/alogamous/": "alogamous", + "tests/": "tests", +} +ROOT = Path(__file__).resolve().parent.parent + + +def main(): + coverage_report = ROOT / "coverage.xml" + root = etree.fromstring(coverage_report.read_text()) # nosec B320 # noqa: S320 + + raw_package_data = defaultdict(lambda: {"hits": 0, "misses": 0}) + for package in root.find("packages"): + for module in package.find("classes"): + filename = module.attrib["filename"] + for relative_path, package_name in PACKAGES.items(): + if filename.startswith(relative_path): + data = raw_package_data[package_name] + break + else: + message = f"unknown package: {module}" + raise ValueError(message) + + for line in module.find("lines"): + if line.attrib["hits"] == "1": + data["hits"] += 1 + else: + data["misses"] += 1 + + total_statements_covered = 0 + total_statements = 0 + coverage_data = {} + for package_name, data in sorted(raw_package_data.items()): + statements_covered = data["hits"] + statements = statements_covered + data["misses"] + total_statements_covered += statements_covered + total_statements += statements + + coverage_data[package_name] = {"statements_covered": statements_covered, "statements": statements} + coverage_data["total"] = {"statements_covered": total_statements_covered, "statements": total_statements} + + coverage_summary = ROOT / "coverage-summary.json" + coverage_summary.write_text(json.dumps(coverage_data, indent=4), encoding="utf-8") + + +if __name__ == "__main__": + main() diff --git a/scripts/write_coverage_summary_report.py b/scripts/write_coverage_summary_report.py new file mode 100644 index 0000000..067ef4e --- /dev/null +++ b/scripts/write_coverage_summary_report.py @@ -0,0 +1,49 @@ +import json +from decimal import ROUND_DOWN, Decimal +from pathlib import Path + +PRECISION = Decimal(".01") + + +def main(): + project_root = Path(__file__).resolve().parent.parent + coverage_summary = project_root / "coverage-summary.json" + + coverage_data = json.loads(coverage_summary.read_text(encoding="utf-8")) + total_data = coverage_data.pop("total") + + lines = [ + "\n", + "Package | Statements\n", + "--- | ---\n", + ] + + for package, data in sorted(coverage_data.items()): + statements_covered = data["statements_covered"] + statements = data["statements"] + + rate = Decimal(statements_covered) / Decimal(statements) * 100 + rate = rate.quantize(PRECISION, rounding=ROUND_DOWN) + lines.append( + f"{package} | {100 if rate == 100 else rate}% ({statements_covered} / {statements})\n" # noqa: PLR2004 + ) + + total_statements_covered = total_data["statements_covered"] + total_statements = total_data["statements"] + total_rate = Decimal(total_statements_covered) / Decimal(total_statements) * 100 + total_rate = total_rate.quantize(PRECISION, rounding=ROUND_DOWN) + color = "ok" if float(total_rate) >= 95 else "critical" # noqa: PLR2004 + lines.insert(0, f"![Code Coverage](https://img.shields.io/badge/coverage-{total_rate}%25-{color}?style=flat)\n") + + lines.append( + f"**Summary** | {100 if total_rate == 100 else total_rate}% " # noqa: PLR2004 + f"({total_statements_covered} / {total_statements})\n" + ) + + coverage_report = project_root / "coverage-report.md" + with coverage_report.open("w", encoding="utf-8") as f: + f.write("".join(lines)) + + +if __name__ == "__main__": + main() diff --git a/src/alogamous/__init__.py b/src/alogamous/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/alogamous/example.py b/src/alogamous/example.py new file mode 100644 index 0000000..788043e --- /dev/null +++ b/src/alogamous/example.py @@ -0,0 +1,2 @@ +def hello_world(): + return "Hello World" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/example_test.py b/tests/example_test.py new file mode 100644 index 0000000..d527371 --- /dev/null +++ b/tests/example_test.py @@ -0,0 +1,5 @@ +import alogamous.example + + +def test_hello_world(): + assert alogamous.example.hello_world() == "Hello World"