From f525b59483ad8fd1ed039d0f178a91fc081bd93f Mon Sep 17 00:00:00 2001
From: EvaBardou <bardou@teklia.com>
Date: Fri, 8 Mar 2024 18:48:56 +0100
Subject: [PATCH] Bootstrap

---
 .arkindex.yml                    |   9 ++
 .cookiecutter.json               |   8 ++
 .dockerignore                    |   2 +
 .gitignore                       |   3 +
 .gitlab-ci.yml                   | 136 +++++++++++++++++++++++++++++++
 .pre-commit-config.yaml          |  36 ++++++++
 Dockerfile                       |  18 ++++
 MANIFEST.in                      |   1 +
 Makefile                         |  10 +++
 README.md                        |  99 +++++-----------------
 ci/build.sh                      |  22 +++++
 pyproject.toml                   |  69 ++++++++++++++++
 requirements.txt                 |   1 +
 setup.py                         |   4 +
 tests/conftest.py                |  32 ++++++++
 tests/test_worker.py             |  12 +++
 tox.ini                          |  12 +++
 worker_init_elements/__init__.py |   6 ++
 worker_init_elements/worker.py   |  19 +++++
 19 files changed, 419 insertions(+), 80 deletions(-)
 create mode 100644 .arkindex.yml
 create mode 100644 .cookiecutter.json
 create mode 100644 .dockerignore
 create mode 100644 .gitignore
 create mode 100644 .gitlab-ci.yml
 create mode 100644 .pre-commit-config.yaml
 create mode 100644 Dockerfile
 create mode 100644 MANIFEST.in
 create mode 100644 Makefile
 create mode 100755 ci/build.sh
 create mode 100644 pyproject.toml
 create mode 100644 requirements.txt
 create mode 100755 setup.py
 create mode 100644 tests/conftest.py
 create mode 100644 tests/test_worker.py
 create mode 100644 tox.ini
 create mode 100644 worker_init_elements/__init__.py
 create mode 100644 worker_init_elements/worker.py

diff --git a/.arkindex.yml b/.arkindex.yml
new file mode 100644
index 0000000..8e23acd
--- /dev/null
+++ b/.arkindex.yml
@@ -0,0 +1,9 @@
+---
+version: 2
+
+workers:
+  - slug: init-elements
+    name: Init Elements
+    type: extractor
+    docker:
+      build: Dockerfile
diff --git a/.cookiecutter.json b/.cookiecutter.json
new file mode 100644
index 0000000..5ca0c5b
--- /dev/null
+++ b/.cookiecutter.json
@@ -0,0 +1,8 @@
+{
+  "slug": "init-elements",
+  "name": "Init Elements",
+  "description": "Worker to initialize Arkindex elements to process",
+  "worker_type": "extractor",
+  "author": "Teklia",
+  "email": "contact@teklia.com"
+}
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..e64c35d
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,2 @@
+.tox
+.git
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..1287c57
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+*.pyc
+*.egg-info/
+.tox/
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
new file mode 100644
index 0000000..71dd174
--- /dev/null
+++ b/.gitlab-ci.yml
@@ -0,0 +1,136 @@
+stages:
+  - test
+  - build
+  - release
+
+# GitLab provides a template to ensure pipelines run only for branches and tags, not for merge requests
+# This prevents duplicate pipelines in merge requests.
+# See https://docs.gitlab.com/ee/ci/troubleshooting.html#job-may-allow-multiple-pipelines-to-run-for-a-single-action
+include:
+  - template: 'Workflows/Branch-Pipelines.gitlab-ci.yml'
+
+variables:
+  VERSION: commit-$CI_COMMIT_SHORT_SHA
+  DEBIAN_FRONTEND: non-interactive
+
+test:
+  image: python:slim
+
+  stage: test
+  cache:
+    paths:
+      - .cache/pip
+
+  variables:
+    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
+    ARKINDEX_API_SCHEMA_URL: schema.yml
+
+  before_script:
+    - pip install tox
+
+    # Install curl
+    - apt-get update -q -y && apt-get install -q -y --no-install-recommends curl
+
+    # Download OpenAPI schema from last backend build
+    - curl https://assets.teklia.com/arkindex/openapi.yml > schema.yml
+
+  except:
+    - schedules
+
+  script:
+    - tox -- --junitxml=test-report.xml --durations=50
+
+lint:
+  image: python:slim
+
+  cache:
+    paths:
+      - .cache/pip
+      - .cache/pre-commit
+
+  variables:
+    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
+    PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit"
+
+  before_script:
+    - pip install pre-commit
+
+    # Install git
+    - apt-get update -q -y && apt-get install -q -y --no-install-recommends git
+
+  except:
+    - schedules
+
+  script:
+    - pre-commit run -a
+
+docker-build:
+  stage: build
+  image: docker:24.0.6
+  services:
+    - docker:dind
+  variables:
+    DOCKER_DRIVER: overlay2
+    DOCKER_HOST: tcp://docker:2375/
+
+  rules:
+    # Never run on scheduled pipelines
+    - if: '$CI_PIPELINE_SOURCE == "schedule"'
+      when: never
+    # Use commit tag when running on tagged commit
+    - if: $CI_COMMIT_TAG
+      variables:
+        VERSION: $CI_COMMIT_TAG
+    - when: on_success
+
+  script:
+    - ci/build.sh
+
+release-notes:
+  stage: release
+  image: registry.gitlab.teklia.com/infra/devops:latest
+
+  # Only run on tags
+  only:
+    - tags
+
+  script:
+    - devops release-notes
+
+bump-python-deps:
+  stage: release
+  image: registry.gitlab.teklia.com/infra/devops:latest
+
+  only:
+    - schedules
+
+  script:
+    - devops python-deps requirements.txt
+
+publish-worker:
+  stage: release
+  allow_failure: true
+  image: registry.gitlab.teklia.com/arkindex/cli:latest
+
+  script:
+    - arkindex -p "$ARKINDEX_INSTANCE" --gitlab-secure-file arkindex-cli.yaml worker publish "$CI_REGISTRY_IMAGE:$VERSION"
+
+  rules:
+    # Never run on scheduled pipelines
+    - if: '$CI_PIPELINE_SOURCE == "schedule"'
+      when: never
+    # Use commit tag when running on tagged commit
+    - if: $CI_COMMIT_TAG
+      variables:
+        VERSION: $CI_COMMIT_TAG
+    - when: on_success
+
+  parallel:
+    matrix:
+      - ARKINDEX_INSTANCE:
+        # Publish worker on https://preprod.arkindex.teklia.com
+        - preprod
+        # Publish worker on https://demo.arkindex.org
+        - demo
+        # Publish worker on https://arkindex.teklia.com
+        - prod
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
new file mode 100644
index 0000000..8d48f7a
--- /dev/null
+++ b/.pre-commit-config.yaml
@@ -0,0 +1,36 @@
+repos:
+  - repo: https://github.com/astral-sh/ruff-pre-commit
+    # Ruff version.
+    rev: v0.3.1
+    hooks:
+      # Run the linter.
+      - id: ruff
+        args: [--fix, --exit-non-zero-on-fix]
+      # Run the formatter.
+      - id: ruff-format
+  - repo: https://github.com/pre-commit/pre-commit-hooks
+    rev: v4.5.0
+    hooks:
+      - id: check-ast
+      - id: check-docstring-first
+      - id: check-executables-have-shebangs
+      - id: check-merge-conflict
+      - id: check-symlinks
+      - id: debug-statements
+      - id: trailing-whitespace
+      - id: check-yaml
+        args: [--allow-multiple-documents]
+      - id: mixed-line-ending
+      - id: name-tests-test
+        args: ['--django']
+      - id: check-json
+      - id: check-toml
+      - id: requirements-txt-fixer
+  - repo: https://github.com/codespell-project/codespell
+    rev: v2.2.6
+    hooks:
+      - id: codespell
+        args: ['--write-changes']
+  - repo: meta
+    hooks:
+      - id: check-useless-excludes
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..0d03194
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,18 @@
+FROM python:3.11-slim
+
+WORKDIR /src
+
+# Install curl
+ENV DEBIAN_FRONTEND=non-interactive
+RUN apt-get update -q -y && apt-get install -q -y --no-install-recommends curl
+
+# Install worker as a package
+COPY worker_init_elements worker_init_elements
+COPY requirements.txt setup.py pyproject.toml ./
+RUN pip install . --no-cache-dir
+
+# Add archi local CA
+RUN curl https://assets.teklia.com/teklia_dev_ca.pem > /usr/local/share/ca-certificates/arkindex-dev.crt && update-ca-certificates
+ENV REQUESTS_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt
+
+CMD ["worker-init-elements"]
diff --git a/MANIFEST.in b/MANIFEST.in
new file mode 100644
index 0000000..f9bd145
--- /dev/null
+++ b/MANIFEST.in
@@ -0,0 +1 @@
+include requirements.txt
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..f9322fd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,10 @@
+.PHONY: release
+
+release:
+	# Grep the version from pyproject.toml, squeeze multiple spaces, delete double and single quotes, get 3rd val.
+	# This command tolerates multiple whitespace sequences around the version number.
+	$(eval version:=$(shell grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3))
+	echo Releasing version $(version)
+	git commit pyproject.toml -m "Version $(version)"
+	git tag $(version)
+	git push origin master $(version)
diff --git a/README.md b/README.md
index 8ae641e..9350732 100644
--- a/README.md
+++ b/README.md
@@ -1,93 +1,32 @@
 # Init Elements
 
+Worker to initialize Arkindex elements to process
 
+### Development
 
-## Getting started
-
-To make it easy for you to get started with GitLab, here's a list of recommended next steps.
-
-Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)!
-
-## Add your files
-
-- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files
-- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command:
+For development and tests purpose it may be useful to install the worker as a editable package with pip.
 
+```shell
+pip3 install -e .
 ```
-cd existing_repo
-git remote add origin https://gitlab.teklia.com/arkindex/workers/init-elements.git
-git branch -M master
-git push -uf origin master
-```
-
-## Integrate with your tools
-
-- [ ] [Set up project integrations](https://gitlab.teklia.com/arkindex/workers/init-elements/-/settings/integrations)
-
-## Collaborate with your team
-
-- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/)
-- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html)
-- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically)
-- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/)
-- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html)
-
-## Test and Deploy
-
-Use the built-in continuous integration in GitLab.
-
-- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html)
-- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/)
-- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html)
-- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/)
-- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html)
-
-***
 
-# Editing this README
+### Linter
 
-When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template.
+Code syntax is analyzed before submitting the code.\
+To run the linter tools suite you may use pre-commit.
 
-## Suggestions for a good README
-
-Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information.
-
-## Name
-Choose a self-explaining name for your project.
-
-## Description
-Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors.
-
-## Badges
-On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge.
-
-## Visuals
-Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method.
-
-## Installation
-Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection.
-
-## Usage
-Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README.
-
-## Support
-Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc.
-
-## Roadmap
-If you have ideas for releases in the future, it is a good idea to list them in the README.
-
-## Contributing
-State if you are open to contributions and what your requirements are for accepting them.
-
-For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self.
+```shell
+pip install pre-commit
+pre-commit run -a
+```
 
-You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser.
+### Run tests
 
-## Authors and acknowledgment
-Show your appreciation to those who have contributed to the project.
+Tests are executed with tox using [pytest](https://pytest.org).
 
-## License
-For open source projects, say how it is licensed.
+```shell
+pip install tox
+tox
+```
 
-## Project status
-If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers.
+To recreate tox virtual environment (e.g. a dependencies update), you may run `tox -r`
diff --git a/ci/build.sh b/ci/build.sh
new file mode 100755
index 0000000..fe1577d
--- /dev/null
+++ b/ci/build.sh
@@ -0,0 +1,22 @@
+#!/bin/sh -e
+# Build the tasks Docker image.
+# Requires CI_PROJECT_DIR and CI_REGISTRY_IMAGE to be set.
+# Will automatically login to a registry if CI_REGISTRY, CI_REGISTRY_USER and CI_REGISTRY_PASSWORD are set.
+# Will only push an image if $CI_REGISTRY is set.
+
+if [ -z "$VERSION" -o -z "$CI_PROJECT_DIR" -o -z "$CI_REGISTRY_IMAGE" ]; then
+  echo Missing environment variables
+  exit 1
+fi
+
+IMAGE_TAG="$CI_REGISTRY_IMAGE:$VERSION"
+
+cd $CI_PROJECT_DIR
+docker build -f Dockerfile . -t "$IMAGE_TAG"
+
+if [ -n "$CI_REGISTRY" -a -n "$CI_REGISTRY_USER" -a -n "$CI_REGISTRY_PASSWORD" ]; then
+  echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
+  docker push $IMAGE_TAG
+else
+  echo "Missing environment variables to log in to the container registry…"
+fi
diff --git a/pyproject.toml b/pyproject.toml
new file mode 100644
index 0000000..ff8f1c2
--- /dev/null
+++ b/pyproject.toml
@@ -0,0 +1,69 @@
+[build-system]
+requires = ["setuptools >= 61.0"]
+build-backend = "setuptools.build_meta"
+
+[project]
+name = "worker_init_elements"
+version = "0.1.0"
+description = "Worker to initialize Arkindex elements to process"
+dynamic = ["dependencies"]
+authors = [
+    { name = "Teklia", email = "contact@teklia.com" },
+]
+maintainers = [
+    { name = "Teklia", email = "contact@teklia.com" },
+]
+requires-python = ">=3.10"
+readme = { file = "README.md", content-type = "text/markdown" }
+keywords = ["python"]
+classifiers = [
+    # Specify the Python versions you support here.
+    "Programming Language :: Python :: 3 :: Only",
+    "Programming Language :: Python :: 3.10",
+    "Programming Language :: Python :: 3.11",
+]
+
+[project.scripts]
+worker-init-elements = "worker_init_elements.worker:main"
+
+[tool.setuptools.dynamic]
+dependencies = { file = ["requirements.txt"] }
+
+[tool.ruff]
+exclude = [".git", "__pycache__"]
+
+[tool.ruff.lint]
+ignore = ["E501"]
+select = [
+    # pycodestyle
+    "E",
+    "W",
+    # Pyflakes
+    "F",
+    # Flake8 Debugger
+    "T1",
+    # Isort
+    "I",
+    # Implicit Optional
+    "RUF013",
+    # Invalid pyproject.toml
+    "RUF200",
+    # pyupgrade
+    "UP",
+    # flake8-bugbear
+    "B",
+    # flake8-simplify
+    "SIM",
+    # flake8-pytest-style
+    "PT",
+    # flake8-use-pathlib
+    "PTH",
+]
+
+[tool.ruff.lint.per-file-ignores]
+# Ignore `pytest-composite-assertion` rules of `flake8-pytest-style` linter for non-test files
+"worker_init_elements/**/*.py" = ["PT018"]
+
+[tool.ruff.lint.isort]
+known-first-party = ["arkindex", "arkindex_worker"]
+known-third-party = ["pytest", "setuptools"]
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..896b959
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1 @@
+arkindex-base-worker==0.3.6
diff --git a/setup.py b/setup.py
new file mode 100755
index 0000000..ca9ba4a
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+from setuptools import find_packages, setup
+
+setup(packages=find_packages())
diff --git a/tests/conftest.py b/tests/conftest.py
new file mode 100644
index 0000000..47295a4
--- /dev/null
+++ b/tests/conftest.py
@@ -0,0 +1,32 @@
+import os
+
+import pytest
+
+from arkindex.mock import MockApiClient
+from arkindex_worker.worker.base import BaseWorker
+
+
+@pytest.fixture(autouse=True)
+def _setup_environment(responses, monkeypatch) -> None:
+    """Setup needed environment variables"""
+
+    # Allow accessing remote API schemas
+    # defaulting to the prod environment
+    schema_url = os.environ.get(
+        "ARKINDEX_API_SCHEMA_URL",
+        "https://demo.arkindex.org/api/v1/openapi/?format=openapi-json",
+    )
+    responses.add_passthru(schema_url)
+
+    # Set schema url in environment
+    os.environ["ARKINDEX_API_SCHEMA_URL"] = schema_url
+    # Setup a fake worker run ID
+    os.environ["ARKINDEX_WORKER_RUN_ID"] = "1234-init-elements"
+    # Setup a fake corpus ID
+    os.environ["ARKINDEX_CORPUS_ID"] = "1234-corpus-id"
+
+    # Setup a mock api client instead of using a real one
+    def mock_setup_api_client(self):
+        self.api_client = MockApiClient()
+
+    monkeypatch.setattr(BaseWorker, "setup_api_client", mock_setup_api_client)
diff --git a/tests/test_worker.py b/tests/test_worker.py
new file mode 100644
index 0000000..929a2fb
--- /dev/null
+++ b/tests/test_worker.py
@@ -0,0 +1,12 @@
+import importlib
+
+
+def test_dummy():
+    assert True
+
+
+def test_import():
+    """Import our newly created module, through importlib to avoid parsing issues"""
+    worker = importlib.import_module("worker_init_elements.worker")
+    assert hasattr(worker, "Demo")
+    assert hasattr(worker.Demo, "process_element")
diff --git a/tox.ini b/tox.ini
new file mode 100644
index 0000000..3e595bb
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,12 @@
+[tox]
+envlist = worker-init-elements
+
+[testenv]
+passenv = ARKINDEX_API_SCHEMA_URL
+commands =
+  pytest {posargs}
+
+deps =
+  pytest
+  pytest-responses
+  -rrequirements.txt
diff --git a/worker_init_elements/__init__.py b/worker_init_elements/__init__.py
new file mode 100644
index 0000000..75fdb48
--- /dev/null
+++ b/worker_init_elements/__init__.py
@@ -0,0 +1,6 @@
+import logging
+
+logging.basicConfig(
+    level=logging.INFO,
+    format="%(asctime)s %(levelname)s/%(name)s: %(message)s",
+)
diff --git a/worker_init_elements/worker.py b/worker_init_elements/worker.py
new file mode 100644
index 0000000..047866a
--- /dev/null
+++ b/worker_init_elements/worker.py
@@ -0,0 +1,19 @@
+from logging import Logger, getLogger
+
+from arkindex_worker.models import Element
+from arkindex_worker.worker import ElementsWorker
+
+logger: Logger = getLogger(__name__)
+
+
+class Demo(ElementsWorker):
+    def process_element(self, element: Element) -> None:
+        logger.info(f"Demo processing element ({element.id})")
+
+
+def main() -> None:
+    Demo(description="Worker to initialize Arkindex elements to process").run()
+
+
+if __name__ == "__main__":
+    main()
-- 
GitLab