Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Alzpeta committed Jun 4, 2024
0 parents commit 9caad08
Show file tree
Hide file tree
Showing 25 changed files with 728 additions and 0 deletions.
75 changes: 75 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Build and test

on:
workflow_call:
inputs:
oarepo:
description: OARepo version (11, 12, ...)
required: true
default: 11
type: string

env:
OAREPO_VERSION: ${{ inputs.oarepo }}

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v3
with:
python-version: "3.10"
- name: Cache pip
uses: actions/cache@v3
with:
# This path is specific to Ubuntu
path: ~/.cache/pip
# Look to see if there is a cache hit for the corresponding requirements file
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
${{ runner.os }}-
- name: Configure sysctl limits
run: |
sudo swapoff -a
sudo sysctl -w vm.swappiness=1
sudo sysctl -w fs.file-max=262144
sudo sysctl -w vm.max_map_count=262144
- name: Runs Opensearch
uses: ankane/setup-opensearch@v1
with:
plugins: analysis-icu

- name: Start Redis
uses: supercharge/[email protected]
with:
redis-version: ${{ matrix.redis-version }}

- name: Run tests
run: |
./run-tests.sh
- name: Build package to publish
run: |
.venv-builder/bin/python setup.py sdist bdist_wheel
- name: Freeze packages
run: |
.venv-builder/bin/pip freeze > requirements.txt
.venv-tests/bin/pip freeze >>requirements.txt
- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: dist
path: dist

- name: Archive production artifacts
uses: actions/upload-artifact@v3
with:
name: requirements.txt
path: requirements.txt
15 changes: 15 additions & 0 deletions .github/workflows/manual.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Dispatch

on:
workflow_dispatch:
inputs:
oarepo:
description: OARepo version (11, 12, ...)
required: true
default: 11

jobs:
build:
uses: ./.github/workflows/build.yaml
with:
oarepo: ${{ github.event.inputs.oarepo }}
41 changes: 41 additions & 0 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Build, test and publish

on: push

permissions:
id-token: write
contents: read

jobs:
build11:
uses: ./.github/workflows/build.yaml
with:
oarepo: 11

build12:
uses: ./.github/workflows/build.yaml
with:
oarepo: 12

publish:
runs-on: ubuntu-latest
needs: build11
steps:
- name: Use built artifacts
uses: actions/download-artifact@v3
with:
name: dist
path: dist

- name: List files
run: |
ls -la
ls -la dist
- name: Publish package
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
skip_existing: true
user: __token__
password: ${{ secrets.PYPI_PASSWORD }}
93 changes: 93 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]

# Idea software family
.idea/

# C extensions
*.so

# Distribution / packaging
.Python
env/
venv/
.venv/
build/
develop-eggs/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
*.egg-info/
.installed.cfg
*.egg

# 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/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*,cover

# Translations
*.mo

# Django stuff:
*.log

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Vim swapfiles
.*.sw?

tests/test.db

.venv*
.direnv

docs/migration/data

.env
.envrc
/.python-version
/poetry.lock
example/data
.DS_Store

test-model

# Testing
sample/

tests/test-sample-app
tests/test-sample-site

example_document/
dist/

.model_venv/
.vscode

thesis
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (C) 2021 CESNET.

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.
3 changes: 3 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
recursive-include oarepo_doi *
prune tests
prune thesis
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# OARepo DOI
3 changes: 3 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
black oarepo_requests tests --target-version py310
autoflake --in-place --remove-all-unused-imports --recursive oarepo_requests tests
isort oarepo_requests tests --profile black
Empty file added oarepo_doi/__init__.py
Empty file.
Empty file added oarepo_doi/actions/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions oarepo_doi/actions/doi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from invenio_requests.customizations import SubmitAction
from invenio_requests.resolvers.registry import ResolverRegistry
from invenio_records_resources.proxies import current_service_registry

from oarepo_doi.api import create_doi


class DoiDraftAction(SubmitAction):
def execute(self, identity, uow):
topic = self.request.topic.resolve()
# create_doi(topic, event=None)
super().execute(identity, uow)
59 changes: 59 additions & 0 deletions oarepo_doi/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import requests
import json
from invenio_base.utils import obj_or_import_string
from flask import current_app


def create_doi(service, record, data, event = None ):
""" if event = None, doi will be created as a draft."""

mapping = obj_or_import_string(service.mapping[record.schema])()
errors = mapping.metadata_check(data)
if len(errors) > 0 and event:
return #todo: dois can not be published with missing mandatory values

request_metadata = mapping.create_datacite_payload(data)

if event:
request_metadata["data"]["attributes"]["event"] = event

request_metadata["data"]["attributes"]["prefix"] = service.prefix

request = requests.post(url=service.url, json=request_metadata, headers={'Content-type': 'application/vnd.api+json'},
auth=(service.username, service.password)
)

if request.status_code != 201:
raise requests.ConnectionError("Expected status code 201, but got {}".format(request.status_code))

content = request.content.decode('utf-8')
json_content = json.loads(content)
doi_value = json_content['data']['id']
mapping.add_doi(record, data, doi_value)


def edit_doi(service, record, event = None):
""" edit existing draft """

mapping = obj_or_import_string(service.mapping[record.schema])()
errors = mapping.metadata_check(record)
if len(errors) > 0 and event:
return #todo: dois can not be published with missing mandatory values
doi_value = mapping.get_doi(record)
if doi_value:
if not service.url.endswith('/'):
url = service.url + '/'
else:
url = service.url
url = url + doi_value.replace("/", "%2F")

request_metadata = mapping.create_datacite_payload(record)
if event:
request_metadata["data"]["attributes"]["event"] = event

request = requests.put(url=url, json=request_metadata, headers={'Content-type': 'application/vnd.api+json'},
auth=(service.username, service.password))

if request.status_code != 200:
raise requests.ConnectionError("Expected status code 200, but got {}".format(request.status_code))

20 changes: 20 additions & 0 deletions oarepo_doi/ext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class OARepoDOI(object):
"""OARepo DOI extension."""

def __init__(self, app=None):
"""Extension initialization."""
if app:
self.init_app(app)

def init_app(self, app):
"""Flask application initialization."""
self.init_config(app)
app.extensions["oarepo-doi"] = self

def init_config(self, app):
"""Initialize configuration."""
if "DATACITE_URL" not in app.config:
app.config["DATACITE_URL"] = 'https://api.datacite.org/dois'
if "DATACITE_MODE" not in app.config:
app.config["DATACITE_MODE"] = "ON_EVENT"
Empty file added oarepo_doi/services/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions oarepo_doi/services/components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import json
import requests
from invenio_records_resources.services.records.components import ServiceComponent
from flask import current_app
from invenio_base.utils import obj_or_import_string

from oarepo_doi.api import create_doi, edit_doi


class DoiComponent(ServiceComponent):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.mode = current_app.config.get("DATACITE_MODE")
self.username = current_app.config.get("DATACITE_USERNAME")
self.password = current_app.config.get("DATACITE_PASSWORD")
self.url = current_app.config.get("DATACITE_URL")
self.prefix = current_app.config.get("DATACITE_PREFIX")
self.mapping = current_app.config.get("DATACITE_MAPPING")

def create(self, identity, data=None, record=None, **kwargs):
if self.mode == "AUTOMATIC_DRAFT":
create_doi(self, record,data, None)

def update_draft(self, identity, data=None, record=None, **kwargs):
if self.mode == "AUTOMATIC_DRAFT":
edit_doi(self, record)

def update(self, identity, data=None, record=None, **kwargs):
if self.mode == "AUTOMATIC_DRAFT" or self.mode == "AUTOMATIC":
edit_doi(self, record)

def publish(self, identity, data=None, record=None, **kwargs):
if self.mode == "AUTOMATIC":
create_doi(self, record, data, "publish")
if self.mode == "AUTOMATIC_DRAFT":
edit_doi(self, record, "publish")
Empty file added oarepo_doi/types/__init__.py
Empty file.
Loading

0 comments on commit 9caad08

Please sign in to comment.