aboutsummaryrefslogtreecommitdiff
path: root/src/hydrilla/proxy/web_ui/items_import.py
blob: f94768f0d8f601961316c2c8082bde965a4e3876 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
78
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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# SPDX-License-Identifier: GPL-3.0-or-later

# Proxy web UI packages loading.
#
# This file is part of Hydrilla&Haketilo.
#
# Copyright (C) 2022 Wojtek Kosior
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
#
# I, Wojtek Kosior, thereby promise not to sue for violation of this
# file's license. Although I request that you do not make use of this
# code in a proprietary program, I am not going to enforce this in
# court.

"""
.....
"""

import tempfile
import zipfile
import re
import json
import typing as t

from pathlib import Path

import flask
import werkzeug

from ...url_patterns import normalize_pattern
from ...builder import build
from ... import versions
from .. import state as st
from . import _app


bp = flask.Blueprint('import', __package__)

@bp.route('/import', methods=['GET'])
def items_import(errors: t.Mapping[str, bool] = {}) -> werkzeug.Response:
    pattern = flask.request.args.get('pattern')
    if pattern is None:
        extra_args = {}
    else:
        extra_args = {'pattern': normalize_pattern(pattern)}

    html = flask.render_template('import.html.jinja', **errors, **extra_args)
    return flask.make_response(html, 200)

def items_import_from_file() -> werkzeug.Response:
    zip_file_storage = flask.request.files.get('items_zipfile')
    if zip_file_storage is None:
        return items_import()

    with tempfile.TemporaryDirectory() as tmpdir_str:
        tmpdir = Path(tmpdir_str)
        tmpdir_child = tmpdir / 'childdir'
        tmpdir_child.mkdir()

        try:
            with zipfile.ZipFile(zip_file_storage) as zip_file:
                zip_file.extractall(tmpdir_child)
        except:
            return items_import({'uploaded_file_not_zip': True})

        extracted_top_level_files = tuple(tmpdir_child.iterdir())
        if extracted_top_level_files == ():
            return items_import({'invalid_uploaded_malcontent': True})

        if len(extracted_top_level_files) == 1 and \
           extracted_top_level_files[0].is_dir():
            malcontent_dir_path = extracted_top_level_files[0]
        else:
            malcontent_dir_path = tmpdir_child

        try:
            _app.get_haketilo_state().import_items(malcontent_dir_path)
        except:
            return items_import({'invalid_uploaded_malcontent': True})

    return flask.redirect(flask.url_for('items.packages'))

identifier_re = re.compile(r'^[-0-9a-z.]+$')

def item_import_ad_hoc() -> werkzeug.Response:
    form = flask.request.form
    def get_as_str(field_name: str) -> str:
        value = form[field_name]
        assert isinstance(value, str)
        return value.strip()

    try:
        identifier = get_as_str('identifier')
        assert identifier
        assert identifier_re.match(identifier)
    except:
        return items_import({'invalid_ad_hoc_identifier': True})

    long_name = get_as_str('long_name') or identifier

    resource_ref = {'identifier': identifier}

    try:
        ver = versions.parse(get_as_str('version') or '1')
    except:
        return items_import({'invalid_ad_hoc_version': True})

    try:
        pat_str = get_as_str('patterns')
        patterns = [
            normalize_pattern(p.strip())
            for p in pat_str.split('\n')
            if p and not p.isspace()
        ]
        assert patterns
    except:
        return items_import({'invalid_ad_hoc_patterns': True})

    common_definition_fields: t.Mapping[str, t.Any] = {
        'identifier':   identifier,
        'long_name':    long_name,
        'version':      ver,
        'description':  get_as_str('description')
    }

    schema_url = \
        'https://hydrilla.koszko.org/schemas/package_source-1.schema.json'

    package_index_json = {
        '$schema':      schema_url,
        'source_name':  'haketilo-ad-hoc-package',
        'copyright':    [],
        'upstream_url': '<local ad hoc package>',
        'definitions': [{
            **common_definition_fields,
            'type':     'mapping',
            'payloads': dict((p, resource_ref) for p in patterns)
        }, {
            **common_definition_fields,
            'type':         'resource',
            'revision':     1,
            'dependencies': [],
            'scripts':      [{'file': 'script.js'}]
        }]
    }

    with tempfile.TemporaryDirectory() as tmpdir_str:
        tmpdir = Path(tmpdir_str)

        source_dir = tmpdir / 'src'
        source_dir.mkdir()

        malcontent_dir = tmpdir / 'malcontent'
        malcontent_dir.mkdir()

        license_text = get_as_str('license_text')
        if license_text:
            package_index_json['copyright'] = [{'file': 'COPYING'}]
            (source_dir / 'COPYING').write_text(license_text)

        (source_dir / 'script.js').write_text(get_as_str('script_text'))

        (source_dir / 'index.json').write_text(json.dumps(package_index_json))

        try:
            builder_args = ['-s', str(source_dir), '-d', str(malcontent_dir)]
            build.perform(builder_args, standalone_mode=False)
            _app.get_haketilo_state().import_items(malcontent_dir)
        except:
            import traceback
            traceback.print_exc()
            return items_import({'invalid_ad_hoc_package': True})

    return flask.redirect(flask.url_for('items.packages'))

@bp.route('/import', methods=['POST'])
def items_import_post() -> werkzeug.Response:
    action = flask.request.form['action']

    if action == 'import_from_file':
        return items_import_from_file()
    elif action == 'import_ad_hoc':
        return item_import_ad_hoc()
    else:
        raise ValueError()