aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/t9write/3rdparty/t9write/unpack.py
blob: 4fc1f4dc057cc83ed047feca905cc35dc58a8eb4 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python
# -*- coding: utf-8 -*-

#############################################################################
##
## Copyright (C) 2017 The Qt Company Ltd.
## Contact: https://www.qt.io/licensing/
##
## This file is part of the Qt Virtual Keyboard module of the Qt Toolkit.
##
## $QT_BEGIN_LICENSE:GPL$
## Commercial License Usage
## Licensees holding valid commercial Qt licenses may use this file in
## accordance with the commercial license agreement provided with the
## Software or, alternatively, in accordance with the terms contained in
## a written agreement between you and The Qt Company. For licensing terms
## and conditions see https://www.qt.io/terms-conditions. For further
## information use the contact form at https://www.qt.io/contact-us.
##
## GNU General Public License Usage
## Alternatively, this file may be used under the terms of the GNU
## General Public License version 3 or (at your option) any later version
## approved by the KDE Free Qt Foundation. The licenses are as published by
## the Free Software Foundation and appearing in the file LICENSE.GPL3
## included in the packaging of this file. Please review the following
## information to ensure the GNU General Public License requirements will
## be met: https://www.gnu.org/licenses/gpl-3.0.html.
##
## $QT_END_LICENSE$
##
#############################################################################

import os
import sys
import zipfile
import tempfile
import shutil
import fnmatch

#
# This utility script unpacks the T9 Write SDK to appropriate directory
# structure for Qt Virtual Keyboard.
#
# Usage: unpack.py <filename.zip> <target dir>
#        unpack.py <input dir> <target dir>
#
# The script will happily overwrite existing files, so be careful.
#

#
# Unpack rule list
#
# Each list entry is a dictionary consisting of target directory as
# key and matching pattern as value. The dictionary can be defined in
# the following ways:
#
# Note: The rules within the dictionary are executed in arbitrary order.
#       Add a new list entry if the order is significant.
#
# Format:
#   1. { 'target dir 1': [ 'pattern1', 'pattern2', ... ], 'target dir 2': ... }
#       - Each pattern is matched against the zip file contents. The file is
#         copied to target dir if the pattern matches. Each pattern is handled
#         independent of each other.
#
#   2. { 'target dir 1': [ [ 'file group pattern', 'sub pattern1', ... ] ], 'target dir 2': ... }
#       - First the file group pattern is matched against the zip file contents.
#         Then all the sub patterns are matched in the sub directory specified by
#         the first match. If all the sub patterns match, then first match from
#         file group pattern and all the matching files from sub pattterns are copied.
#         The purpose of this option is to copy coupled files, e.g. DLL and LIB
#         files found in the same directory.
#

UNPACK_RULES = [
{ # Header files
'api': [
    '*/decuma_hwr.h',
    '*/decuma_hwr_cjk.h',
    '*/decuma_hwr_types.h',
    '*/decuma_point.h',
    '*/decumaBasicTypes.h',
    '*/decumaBasicTypesMinMax.h',
    '*/decumaCharacterSetType.h',
    '*/decumaCurve.h',
    '*/decumaFunctionalSupport.h',
    '*/decumaFunctionalSupportCheck.h',
    '*/decumaLanguages.h',
    '*/decumaLiteFunctionalSupport.h',
    '*/decumaPlusFunctionalSupport.h',
    '*/decumaRuntimeMallocData.h',
    '*/decumaStatus.h',
    '*/decumaStorageSpecifiers.h',
    '*/decumaSymbolCategories.h',
    '*/decumaUnicodeTypes.h',
    '*/t9write_alpha_version.h',
    '*/t9write_api_version.h',
    '*/t9write_cjk_version.h',
    '*/xxt9wApiOem.h',
    '*/xxt9wOem.h',
],
}, { # Data: Arabic and Hebrew database must be copied first (the file name collides with Alphabetic database)
'data/arabic': [
    '*/Arabic/*_le.bin',
],
'data/hebrew': [
    '*/Hebrew/*_le.bin',
],
'data/thai': [
    '*/*Thai*/*_le.bin',
],
}, { # Data: Alphabetic and CJK databases
'data': [
    '*/*_le.bin',
    '*/*.hdb',
    '*/*.phd',
    '*/*.ldb',
],
}, { # Libraries
'lib/arm/static/alphabetic': [
    '*T9Write_Alpha*/*Android_ARM*/*.a',
    '*T9Write_Alpha*/*Android_ARM*/*.o',
],
'lib/arm/shared/alphabetic': [
    '*T9Write_Alpha*/*Android_ARM*/*.so',
],
'lib/arm/static/cjk': [
    '*T9Write_CJK*/*Android_ARM*/*.a',
    '*T9Write_CJK*/*Android_ARM*/*.o',
],
'lib/arm/shared/cjk': [
    '*T9Write_CJK*/*Android_ARM*/*.so',
],
'lib/linux/static/alphabetic': [
    '*T9Write_Alpha*/*Android_x86*/*.a',
    '*T9Write_Alpha*/*Android_x86*/*.o',
],
'lib/linux/shared/alphabetic': [
    '*T9Write_Alpha*/*Android_x86*/*.so',
],
'lib/linux/static/cjk': [
    '*T9Write_CJK*/*Android_x86*/*.a',
    '*T9Write_CJK*/*Android_x86*/*.o',
],
'lib/linux/shared/cjk': [
    '*T9Write_CJK*/*Android_x86*/*.so',
],
'lib/win32/static/alphabetic': [
    '*T9Write_Alpha*/*.obj',
],
'lib/win32/shared/alphabetic': [
    [ '*T9Write_Alpha*/*.dll', '*.lib' ],
],
'lib/win32/static/cjk': [
    '*T9Write_CJK*/*.obj',
],
'lib/win32/shared/cjk': [
    [ '*T9Write_CJK*/*.dll', '*.lib' ],
],
'lib/win32/shared/alphabetic': [
    [ '*T9Write_Alpha*/*.dll', '*.lib' ],
],
}
]

#
# Blacklist
#
# File matching rules for blacklisted items. Matched before UNPACK_RULES.
#

BLACKLIST_RULES = [
'*__MACOSX*',
'*/.DS_Store',
]

def blacklist(file_list):
    result = []
    for file_name in file_list:
        match = False
        for blacklist_rule in BLACKLIST_RULES:
            match = fnmatch.fnmatch(file_name, blacklist_rule)
            if match:
                break
        if not match:
            result.append(file_name)
    return result

def unzip(zip_file, target_dir):
    zip_list = []
    if os.path.isdir(zip_file):
        base_dir, sdk_dir = os.path.split(zip_file.replace('\\', '/').rstrip('/'))
        base_dir_length = len(base_dir) + 1 if base_dir else 0
        if not 'T9Write' in sdk_dir:
            print("Error: The input directory name '" + sdk_dir + "' does not contain 'T9Write'.")
            print("Please unzip the file to a directory named after the zip file and try again.")
            return zip_list
        for root, dirs, files in os.walk(zip_file):
            for file_name in files:
                sub_dir = root[base_dir_length:]
                dst_dir = os.path.join(target_dir, sub_dir)
                if not os.path.exists(dst_dir):
                    os.makedirs(dst_dir)
                shutil.copy2(os.path.join(root, file_name), dst_dir)
                os.chmod(os.path.join(dst_dir, file_name), 0o644)
                zip_list.append(os.path.join(sub_dir, file_name).replace('\\', '/'))
        return zip_list
    with zipfile.ZipFile(zip_file, 'r') as z:
        zip_list = sorted(blacklist(z.namelist()))
        zip_basename = os.path.splitext(os.path.basename(zip_file))[0]
        if zip_list and zip_basename in zip_list[0]:
            zip_basename = ''
        zip_list = [os.path.join(zip_basename, zip_name).replace('\\', '/') for zip_name in zip_list]
        z.extractall(os.path.join(target_dir, zip_basename))
    return zip_list

def match_file_list(file_list, base_dir, fnpattern):
    return [file_name for file_name in file_list \
        if fnmatch.fnmatch(file_name, fnpattern) and \
            os.path.isfile(os.path.join(base_dir, file_name))]

def unpack(zip_list, zip_dir, out_dir):
    if not zip_list:
        return

    for unpack_rules in UNPACK_RULES:
        process_unpack_rules(zip_list, zip_dir, out_dir, unpack_rules)

def process_unpack_rules(zip_list, zip_dir, out_dir, unpack_rules):
    for (target_dir, match_rules) in unpack_rules.items():
        for match_rule in match_rules:
            # Match
            match_rule_group = match_rule if isinstance(match_rule, list) else [match_rule]
            match_group_candidates = [match_file_list(zip_list, zip_dir, match_rule_group[0])]
            if len(match_rule_group) > 1:
                while len(match_group_candidates[0]) > 0:
                    match_group0_candidate = match_group_candidates[0][0]
                    all_sub_groups_match = True
                    for sub_group_rule in match_rule_group[1:]:
                        fnpattern = os.path.join(os.path.dirname(match_group0_candidate), sub_group_rule).replace('\\', '/')
                        sub_group_candidates = match_file_list(zip_list, zip_dir, fnpattern)
                        if not sub_group_candidates:
                            all_sub_groups_match = False
                            break
                        match_group_candidates.append(sub_group_candidates)
                    if all_sub_groups_match:
                        match_group_candidates[0] = [match_group0_candidate]
                        break
                    else:
                        match_group_candidates = [match_group_candidates[0][1:]]

            # Copy
            if match_group_candidates:
                for match_group_candidate in match_group_candidates:
                    for zip_name in match_group_candidate:
                        dst_dir = os.path.join(out_dir, target_dir)
                        if not os.path.exists(dst_dir):
                            os.makedirs(dst_dir)
                        src = os.path.join(zip_dir, zip_name).replace('\\', '/')
                        dst = os.path.join(dst_dir, os.path.basename(zip_name)).replace('\\', '/')
                        print(zip_name + ' -> ' + dst)
                        shutil.copy2(src, dst)
                        zip_list.remove(zip_name)

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print("Usage: %s <filename.zip> <target dir>" % os.path.basename(__file__))
        exit()

    out_dir = sys.argv[2]
    zip_dir = tempfile.mkdtemp()

    try:
        unpack(unzip(sys.argv[1], zip_dir), zip_dir, out_dir)
    except Exception as e:
        print(e)
    finally:
        shutil.rmtree(zip_dir)