Commit 7ff8c474 authored by Florian Rhiem's avatar Florian Rhiem
Browse files

Initial commit

parents
Pipeline #36168 passed with stages
in 1 minute and 43 seconds
.*
!.gitignore
!.gitlab-ci.yml
_*
!__init__.py
!__main__.py
stages:
- prerun
- test
- deploy
variables:
POSTGRES_HOST: postgres
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ""
SAMPLEDB_SQLALCHEMY_DATABASE_URI: "postgresql+psycopg2://postgres:@postgres:5432/postgres"
POSTGRES_HOST_AUTH_METHOD: trust
tag-type-check:
stage: prerun
image: busybox
only:
- tags
script:
# stop pipelines for lightweight tags
- test `git cat-file -t $CI_COMMIT_TAG` = "tag"
analyze:
stage: test
image: python:3
script:
# install static analysis dependencies
- python3 -m pip install pycodestyle pyflakes
# run static analysis tools
- python3 -m pycodestyle --ignore=E402,E501,W504 qcodes_sampledb_import
- python3 -m pyflakes qcodes_sampledb_import
test:
stage: test
image: ubuntu:18.04
services:
- postgres:12
script:
- apt-get update
# set up Python 3
- apt-get install -y git python3-pip libpangocairo-1.0-0
# install main dependencies
- python3 -m pip install -r requirements.txt
# install additional testing dependencies
- python3 -m pip install pytest sampledb
- export SAMPLEDB_FILE_STORAGE_PATH=${CI_PROJECT_DIR}/test_files/
# run tests and gather coverage data
- python3 -m pytest -x --cov=qcodes_sampledb_import/ --junitxml=pytest.xml tests
artifacts:
reports:
junit: pytest.xml
documentation:
stage: test
image: python:3
script:
# install main dependencies
- python3 -m pip install -r requirements.txt
# install additional documentation dependencies
- python3 -m pip install sphinx sphinxcontrib-httpdomain vcversioner
# build documentation
- python3 -m sphinx -b html docs/ build_documentation/
artifacts:
paths:
- build_documentation
pages:
stage: deploy
image: busybox
only:
- master
- tags
script:
# allow GitLab Pages to deploy the artifacts from the documentation job
- mv build_documentation public
artifacts:
paths:
- public
expire_in: 1 day
deploy-to-pypi:
stage: deploy
image: python:3
only:
- tags@Scientific-IT-Systems/qcodes-sampledb-import
script:
# install packaging dependencies
- python3 -m pip install setuptools wheel twine
# build sdist
- python3 setup.py sdist
# deploy sdist to PyPI for version tags
- if echo "$CI_COMMIT_TAG" | grep -Eq '^v[0-9]+\.[0-9]+\.[0-9]+$'; then
export VERSION=`echo "$CI_COMMIT_TAG" | sed 's/^v//'`;
python3 -m twine upload dist/qcodes-sampledb-import-$VERSION.tar.gz;
fi
MIT License
Copyright (c) 2021 PGI / JCNS Scientific IT-Systems
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.
# QCoDeS SampleDB Import
[![MIT license](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
A tool for importing data from QCoDeS into SampleDB.
Changelog
=========
Version 0.1
-----------
Currently in development.
- Set up basic project structure
\ No newline at end of file
# coding: utf-8
import os
import sys
import vcversioner
import datetime
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, base_dir)
# Project information
project = 'QCoDeS-SampleDB-Import'
description = 'A tool for importing data from QCoDeS into SampleDB.'
copyright = '{}, PGI / JCNS Scientific IT-Systems'.format(datetime.date.today().year)
author = 'Florian Rhiem'
contact_email = 'f.rhiem@fz-juelich.de'
imprint = 'https://pgi-jcns.fz-juelich.de/portal/pages/imprint.html'
# The full version, including alpha/beta/rc tags
release = vcversioner.find_version(root=base_dir).version
# The short X.Y version
version = '.'.join(release.split('.', 2)[:2])
# General configuration
source_suffix = '.rst'
master_doc = 'index'
exclude_patterns = []
language = None
templates_path = ['templates']
pygments_style = None
extensions = [
'sphinx.ext.todo',
'sphinx.ext.autodoc',
'sphinxcontrib.httpdomain',
]
todo_include_todos = False
# Options for HTML output
html_theme = 'alabaster'
html_static_path = ['static']
html_show_sourcelink = False
# Options for Alabaster theme
html_theme_options = {
'logo_text_align': 'center',
'description': description,
'font_family': "'Lato', 'Helvetica Neue', 'Arial', sans-serif",
'font_size': '16px',
'head_font_family': "'Lato', 'Helvetica Neue', 'Arial', sans-serif",
'body_text_align': 'justify',
'show_powered_by': False,
'extra_nav_links': {
'Source Code': 'https://iffgit.fz-juelich.de/Scientific-IT-Systems/qcodes-sampledb-import/',
'Issues': 'https://iffgit.fz-juelich.de/Scientific-IT-Systems/qcodes-sampledb-import/-/issues',
'Contact': 'mailto:{}'.format(contact_email),
'Imprint': imprint,
'PGI/JCNS-TA': 'https://pgi-jcns.fz-juelich.de',
}
}
# Options for other output methods
htmlhelp_basename = '{}doc'.format(project)
latex_elements = {}
latex_documents = [
(master_doc, '{}.tex'.format(project), '{} Documentation'.format(project),
author, 'manual'),
]
man_pages = [
(master_doc, project.lower(), '{} Documentation'.format(project),
[author], 1)
]
texinfo_documents = [
(master_doc, project, '{} Documentation'.format(project),
author, project, description,
'Miscellaneous'),
]
epub_title = project
epub_exclude_files = ['search.html']
def setup(app):
app.add_css_file('css/custom.css')
QCoDeS-SampleDB-Import
======================
.. toctree::
:hidden:
:maxdepth: 2
changelog.rst
# coding: utf-8
"""
A tool for importing research data from QCoDeS into SampleDB
Usage: python -m qcodes_sampledb_import
"""
import sys
import typing
def main(argv: typing.List[str]) -> int:
if len(argv) > 1:
print(__doc__)
return 1
# TODO: implement functionality
return 0
if __name__ == '__main__':
exit(main(sys.argv))
# coding: utf-8
"""
This file should only contain the definition of the __version__ variable.
It may be read by multiple other files, e.g. setup.py.
"""
__version__ = '0.0.0'
pytest
sampledb
sphinx
sphinxcontrib-httpdomain
vcversioner
# coding: utf-8
"""
"""
import importlib.util
from setuptools import setup, find_packages
import os.path
setup_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(setup_directory, 'README.md')) as readme_file:
long_description = readme_file.read()
with open(os.path.join(setup_directory, 'requirements.txt')) as requirements_file:
requirements = requirements_file.readlines()
version_file_path = os.path.join(setup_directory, 'qcodes_sampledb_import', 'version.py')
spec = importlib.util.spec_from_file_location("version", version_file_path)
version = importlib.util.module_from_spec(spec)
spec.loader.exec_module(version)
setup(
name='qcodes_sampledb_import',
version=version.__version__,
description='A tool for importing data from QCoDeS into SampleDB.',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://iffgit.fz-juelich.de/Scientific-IT-Systems/qcodes-sampledb-import',
author='Florian Rhiem',
author_email='f.rhiem@fz-juelich.de',
license='MIT',
classifiers=[
'Development Status :: 3 - Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Topic :: Scientific/Engineering',
],
packages=find_packages(exclude=['tests']),
install_requires=requirements
)
# coding: utf-8
"""
"""
__author__ = 'Florian Rhiem <f.rhiem@fz-juelich.de>'
# coding: utf-8
"""
Setup and configuration for tests
"""
import copy
import logging
import os
import random
import secrets
import threading
import time
import flask
import pytest
import requests
import sqlalchemy
import sampledb
import sampledb.utils
import sampledb.config
# SampleDB configuration
sampledb.config.MAIL_SUPPRESS_SEND = True
sampledb.config.TEMPLATES_AUTO_RELOAD = True
sampledb.config.SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://postgres:@localhost:5432/postgres'
sampledb.config.MAIL_SENDER = 'sampledb@example.com'
sampledb.config.MAIL_SERVER = 'mail.example.com'
sampledb.config.CONTACT_EMAIL = 'sampledb@example.com'
sampledb.config.LDAP_NAME = 'LDAP'
# restore possibly overridden configuration data from environment variables
sampledb.config.use_environment_configuration(env_prefix='SAMPLEDB_')
def create_flask_server(app):
if not getattr(app, 'has_shutdown_route', False):
@app.route('/shutdown', methods=['POST'])
def shutdown():
func = flask.request.environ.get('werkzeug.server.shutdown')
if func is None:
raise RuntimeError('Not running with the Werkzeug Server')
func()
return 'Server shutting down...'
app.has_shutdown_route = True
port = random.randint(10000, 20000)
server_thread = threading.Thread(target=lambda: app.run(port=port, debug=True, use_reloader=False), daemon=True)
server_thread.start()
server_thread.app = app
server_thread.initial_config = copy.deepcopy(server_thread.app.config)
server_thread.base_url = 'http://localhost:{0}/'.format(port)
server_thread.api_url = server_thread.base_url + 'api/'
# short delay to allow the web server to start
time.sleep(0.1)
yield server_thread
# restore initial configuration
server_thread.app.config = server_thread.initial_config
r = requests.post(server_thread.base_url + 'shutdown')
assert r.status_code == 200
server_thread.join()
@pytest.fixture(scope='session')
def flask_server():
app = create_app()
# empty the database first, to ensure all tests rebuild it before use
sampledb.utils.empty_database(sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI), only_delete=True)
yield from create_flask_server(app)
def create_app():
logging.getLogger('flask.app').setLevel(logging.WARNING)
os.environ['FLASK_ENV'] = 'development'
os.environ['FLASK_TESTING'] = 'True'
sampledb.utils.empty_database(sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI), only_delete=True)
sampledb_app = sampledb.create_app()
return sampledb_app
@pytest.fixture
def app(flask_server):
app = flask_server.app
# reset config and database before each test
app.config = copy.deepcopy(flask_server.initial_config)
sampledb.utils.empty_database(sqlalchemy.create_engine(sampledb.config.SQLALCHEMY_DATABASE_URI), only_delete=True)
sampledb.setup_database(app)
return app
@pytest.fixture(autouse=True)
def app_context(app):
with app.app_context():
# yield to keep the app context active until the test is done
yield None
@pytest.fixture
def api_token_user(flask_server):
with flask_server.app.app_context():
user = sampledb.logic.users.create_user(name="Test User", email="example@example.com", type=sampledb.models.UserType.PERSON)
assert user.id is not None
sampledb.logic.authentication.add_other_authentication(user.id, 'username', 'password')
api_token = secrets.token_hex(32)
sampledb.logic.authentication.add_api_token(user.id, api_token, "Test API Token")
return api_token, user
@pytest.fixture
def api_token_headers(api_token_user):
api_token, _ = api_token_user
return {
'Authorization': 'Bearer ' + api_token
}
# coding: utf-8
"""
These tests are examples using the SampleDB API and the CLI.
"""
import requests
import qcodes_sampledb_import.__main__ as entrypoint
def test_sampledb_api(flask_server, api_token_headers):
r = requests.get(
flask_server.base_url + 'api/v1/objects/',
headers=api_token_headers
)
assert r.status_code == 200
assert r.json() == []
def test_cli():
assert entrypoint.main([entrypoint.__file__]) == 0
assert entrypoint.main([entrypoint.__file__, "..."]) == 1
v0.0.0-0-placeholder
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment