archive.py 4.98 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
import nomad_meta_info

27
from nomad import config
28
from nomad.uploads import UploadFiles
29
30
from nomad.utils import get_logger

31
from .app import api
32
33
34
35
from .auth import login_if_available
from .common import calc_route

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


@calc_route(ns, '/logs')
class ArchiveCalcLogResource(Resource):
42
    @api.doc('get_archive_logs')
43
    @api.response(404, 'The upload or calculation does not exist')
44
    @api.response(200, 'Archive data send', headers={'Content-Type': 'application/plain'})
45
46
47
48
49
50
51
52
53
54
    @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:
55
56
57
58
59
60
61
            upload_files = UploadFiles.get(upload_hash)
            with upload_files.archive_log_file(calc_hash, 'rt') as f:
                rv = send_file(
                    f,
                    mimetype='text/plain',
                    as_attachment=True,
                    attachment_filename='%s.log' % archive_id)
62
63
64
65
66
67
68
69
70
71
72
73
74
75

            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):
76
    @api.doc('get_archive_calc')
77
78
79
80
81
82
83
84
85
86
87
88
    @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:
89
90
91
92
93
94
95
96
            upload_file = UploadFiles.get(upload_hash)
            mode = 'rb' if config.files.compress_archive else 'rt'
            with upload_file.archive_file(calc_hash, mode) as f:
                rv = send_file(
                    f,
                    mimetype='application/json',
                    as_attachment=True,
                    attachment_filename='%s.json' % archive_id)
97
98
99
100
101

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

            return rv
102
        except KeyError:
103
104
105
106
107
108
109
            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.')
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139


@ns.route('/metainfo/<string:metainfo_path>')
@api.doc(params=dict(metainfo_path='A path or metainfo definition file name.'))
class MetainfoResource(Resource):
    @api.doc('get_metainfo')
    @api.response(404, 'The metainfo does not exist')
    @api.response(200, 'Metainfo data send')
    def get(self, metainfo_path):
        """
        Get a metainfo definition file.
        """
        try:
            file_dir = os.path.dirname(os.path.abspath(nomad_meta_info.__file__))
            meta_info_path = os.path.normpath(os.path.join(file_dir, metainfo_path.strip()))

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

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