Skip to content

Commit

Permalink
feat: Improve version extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
kesara committed Jan 19, 2024
1 parent 1deb26a commit 385f40d
Show file tree
Hide file tree
Showing 6 changed files with 169 additions and 21 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ echo "VERSION = '9.9.9'" >> at/config.py
echo "REQUIRE_AUTH = False" >> at/config.py
echo "DT_LATEST_DRAFT_URL = 'https://datatracker.ietf.org/doc/rfcdiff-latest-json'" >> at/config.py
echo "ALLOWED_DOMAINS = ['ietf.org', 'ietf.org', 'rfc-editor.org']" >> at/config.py
PATH=$PATH:./node_modules/.bin/ python docker/version.py >> at/config.py
```

* Run flask server.
Expand Down
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ RUN arch=$(arch | sed s/aarch64/arm64/ | sed s/x86_64/amd64/) && \
tar zxf mmark_*.tgz -C /bin/ && \
rm mmark_*.tgz

COPY Gemfile Gemfile.lock LICENSE README.md api.yml constraints.txt package-lock.json package.json requirements.txt .
COPY Gemfile Gemfile.lock LICENSE README.md api.yml constraints.txt package-lock.json package.json requirements.txt docker/version.py .
COPY at ./at

# Install JavaScript dependencies
Expand All @@ -86,7 +86,8 @@ RUN mkdir -p tmp && \
echo "VERSION = '${VERSION}'" >> at/config.py && \
echo "REQUIRE_AUTH = False" >> at/config.py && \
echo "DT_LATEST_DRAFT_URL = 'https://datatracker.ietf.org/api/rfcdiff-latest-json'" >> at/config.py && \
echo "ALLOWED_DOMAINS = ['ietf.org', 'rfc-editor.org', 'github.com', 'githubusercontent.com', 'github.io', 'gitlab.com', 'gitlab.io', 'codeberg.page']" >> at/config.py
echo "ALLOWED_DOMAINS = ['ietf.org', 'rfc-editor.org', 'github.com', 'githubusercontent.com', 'github.io', 'gitlab.com', 'gitlab.io', 'codeberg.page']" >> at/config.py && \
python3 version.py >> at/config.py


# COPY required files
Expand Down
19 changes: 1 addition & 18 deletions at/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,8 @@
get_text_id_from_file, get_text_id_from_url, TextProcessingError)
from at.utils.validation import (
idnits as get_idnits, svgcheck as get_svgcheck, validate_draft)
from at.utils.version import (
get_aasvg_version, get_idnits_version, get_id2xml_version,
get_iddiff_version, get_mmark_version, get_kramdown_rfc_version,
get_rfcdiff_version, get_svgcheck_version, get_weasyprint_version,
get_xml2rfc_version)

BAD_REQUEST = 400
VERSION_INFORMATION = {
'xml2rfc': get_xml2rfc_version(),
'kramdown-rfc': get_kramdown_rfc_version(),
'mmark': get_mmark_version(),
'id2xml': get_id2xml_version(),
'weasyprint': get_weasyprint_version(),
'idnits': get_idnits_version(),
'iddiff': get_iddiff_version(),
'aasvg': get_aasvg_version(),
'svgcheck': get_svgcheck_version(),
'rfcdiff': get_rfcdiff_version(),
'bap': '1.4'} # bap does not provide a switch to get version

bp = Blueprint('api', __name__, url_prefix='/api')

Expand Down Expand Up @@ -596,7 +579,7 @@ def version():

logger = current_app.logger
logger.debug('version information request')
versions = VERSION_INFORMATION
versions = current_app.config['VERSION_INFORMATION']
versions['author_tools_api'] = current_app.config['VERSION']

return jsonify(versions=versions)
150 changes: 150 additions & 0 deletions docker/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
from logging import getLogger
from subprocess import run as proc_run, CalledProcessError

from weasyprint import __version__ as weasyprint_version
from xml2rfc import __version__ as xml2rfc_version


def get_kramdown_rfc_version(logger=getLogger()):
'''Return kramdown-rfc version'''

output = proc_run(
args=['kramdown-rfc', '--version'],
capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').replace(
'kramdown-rfc', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('kramdown-rfc error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_mmark_version(logger=getLogger()):
'''Return mmark version'''

output = proc_run(args=['mmark', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').strip()
except CalledProcessError: # pragma: no cover
logger.info('mmark error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_id2xml_version(logger=getLogger()):
'''Return id2xml version'''

output = proc_run(args=['id2xml', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').replace('id2xml', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('id2xml error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_xml2rfc_version():
'''Return xml2rfc version'''

return xml2rfc_version


def get_weasyprint_version():
'''Return Weasyprint version'''

return weasyprint_version


def get_idnits_version(logger=getLogger()):
'''Return idnits version'''

output = proc_run(args=['idnits', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').replace('idnits', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('idnits error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_aasvg_version(logger=getLogger()):
'''Return aasvg version'''

output = proc_run(args=['aasvg', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').replace('aasvg', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('aasvg error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_iddiff_version(logger=getLogger()):
'''Return iddiff version'''

output = proc_run(args=['iddiff', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').replace('iddiff', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('iddiff error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_svgcheck_version(logger=getLogger()):
'''Return svgcheck version'''

output = proc_run(args=['svgcheck', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').split('\n')[0] \
.replace('svgcheck =', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('svgcheck error: {}'.format(
output.stderr.decode('utf-8')))
return None


def get_rfcdiff_version(logger=getLogger()):
'''Return rfcdiff version'''

output = proc_run(args=['rfcdiff', '--version'], capture_output=True)

try:
output.check_returncode()
return output.stdout.decode('utf-8').split('\n')[0] \
.replace('rfcdiff', '').strip()
except CalledProcessError: # pragma: no cover
logger.info('rfcdiff error: {}'.format(
output.stderr.decode('utf-8')))
return None


if __name__ == '__main__':
VERSION_INFORMATION = {
'xml2rfc': get_xml2rfc_version(),
'kramdown-rfc': get_kramdown_rfc_version(),
'mmark': get_mmark_version(),
'id2xml': get_id2xml_version(),
'weasyprint': get_weasyprint_version(),
'idnits': get_idnits_version(),
'iddiff': get_iddiff_version(),
'aasvg': get_aasvg_version(),
'svgcheck': get_svgcheck_version(),
'rfcdiff': get_rfcdiff_version(),
'bap': '1.4'} # bap does not provide a switch to get version
print(f'VERSION_INFORMATION = {VERSION_INFORMATION}')
13 changes: 13 additions & 0 deletions tests/test_api_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
from at import create_app

AUTHOR_TOOLS_API_TEST_VERSION = '0.0.1'
VERSION_INFORMATION = {
'xml2rfc': '3.19.1',
'kramdown-rfc': '1.7.5',
'mmark': '2.2.25',
'id2xml': '1.5.2',
'weasyprint': '60.2',
'idnits': '2.17.00',
'iddiff': '0.4.3',
'aasvg': '0.3.6',
'svgcheck': '0.7.1',
'rfcdiff': '1.48',
'bap': '1.4'}
VERSION_LABELS = (
'author_tools_api',
'xml2rfc',
Expand All @@ -28,6 +40,7 @@ def setUp(self):

config = {
'REQUIRE_AUTH': False,
'VERSION_INFORMATION': VERSION_INFORMATION,
'VERSION': AUTHOR_TOOLS_API_TEST_VERSION}

self.app = create_app(config)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_utils_version.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from unittest import TestCase

from at.utils.version import (
from docker.version import (
get_aasvg_version, get_idnits_version, get_id2xml_version,
get_iddiff_version, get_mmark_version, get_kramdown_rfc_version,
get_rfcdiff_version, get_svgcheck_version, get_weasyprint_version,
Expand Down

0 comments on commit 385f40d

Please sign in to comment.