archive.py 3.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2018 Markus Scheidgen
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an"AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
16
17
18
19
"""
The archive API of the nomad@FAIRDI APIs. This API is about serving processed
(parsed and normalized) calculation data in nomad's *meta-info* format.
"""

20
21
22
import os.path

from flask import send_file
23
from flask_restplus import abort, Resource
24
25
26
27
28

from nomad import config
from nomad.files import ArchiveFile, ArchiveLogFile
from nomad.utils import get_logger

29
from .app import api
30
31
32
33
from .auth import login_if_available
from .common import calc_route

ns = api.namespace(
34
35
    'archive',
    description='Access archive data and archive processing logs.')
36
37
38
39


@calc_route(ns, '/logs')
class ArchiveCalcLogResource(Resource):
40
    @api.doc('get_logs')
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    @api.response(404, 'The upload or calculation does not exist')
    @api.response(200, 'Archive data send')
    @login_if_available
    def get(self, upload_hash, calc_hash):
        """
        Get calculation processing log.

        Calcs are references via *upload_hash*, *calc_hash* pairs.
        """
        archive_id = '%s/%s' % (upload_hash, calc_hash)

        try:
            archive = ArchiveLogFile(archive_id)
            if not archive.exists():
                raise FileNotFoundError()

            archive_path = archive.os_path

            rv = send_file(
                archive_path,
                mimetype='application/text',
                as_attachment=True,
                attachment_filename=os.path.basename(archive_path))

            return rv
        except FileNotFoundError:
            abort(404, message='Archive/calculation %s does not exist.' % archive_id)
        except Exception as e:
            logger = get_logger(
                __name__, endpoint='logs', action='get',
                upload_hash=upload_hash, calc_hash=calc_hash)
            logger.error('Exception on accessing calc proc log', exc_info=e)
            abort(500, message='Could not accessing the logs.')


@calc_route(ns)
class ArchiveCalcResource(Resource):
78
    @api.doc('get_calc')
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
    @api.response(404, 'The upload or calculation does not exist')
    @api.response(200, 'Archive data send')
    @login_if_available
    def get(self, upload_hash, calc_hash):
        """
        Get calculation data in archive form.

        Calcs are references via *upload_hash*, *calc_hash* pairs.
        """
        archive_id = '%s/%s' % (upload_hash, calc_hash)

        try:
            archive = ArchiveFile(archive_id)
            if not archive.exists():
                raise FileNotFoundError()

            archive_path = archive.os_path

            rv = send_file(
                archive_path,
                mimetype='application/json',
                as_attachment=True,
                attachment_filename=os.path.basename(archive_path))

            if config.files.compress_archive:
                rv.headers['Content-Encoding'] = 'gzip'

            return rv
        except FileNotFoundError:
            abort(404, message='Archive %s does not exist.' % archive_id)
        except Exception as e:
            logger = get_logger(
                __name__, endpoint='archive', action='get',
                upload_hash=upload_hash, calc_hash=calc_hash)
            logger.error('Exception on accessing archive', exc_info=e)
            abort(500, message='Could not accessing the archive.')