Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
 
import argparse
import glob
import os
import re
import shutil
import subprocess
import sys
import zipfile  # Available Python 3.2 or higher
 
linux_gpu_package_libraries = [
    "libonnxruntime_providers_shared.so",
    "libonnxruntime_providers_cuda.so",
    "libonnxruntime_providers_tensorrt.so",
]
win_gpu_package_libraries = [
    "onnxruntime_providers_shared.lib",
    "onnxruntime_providers_shared.dll",
    "onnxruntime_providers_cuda.lib",
    "onnxruntime_providers_cuda.dll",
    "onnxruntime_providers_tensorrt.lib",
    "onnxruntime_providers_tensorrt.dll",
]
gpu_related_header_files = [
    "cpu_provider_factory.h",
    "onnxruntime_c_api.h",
    "onnxruntime_cxx_api.h",
    "onnxruntime_float16.h",
    "onnxruntime_cxx_inline.h",
    "onnxruntime_experimental_c_api.h",
    "onnxruntime_experimental_c_api.inc",
]
dmlep_related_header_files = [
    "cpu_provider_factory.h",
    "onnxruntime_c_api.h",
    "onnxruntime_cxx_api.h",
    "onnxruntime_float16.h",
    "onnxruntime_cxx_inline.h",
    "dml_provider_factory.h",
    "onnxruntime_experimental_c_api.h",
    "onnxruntime_experimental_c_api.inc",
]
training_related_header_files = [
    "onnxruntime_c_api.h",
    "onnxruntime_float16.h",
    "onnxruntime_cxx_api.h",
    "onnxruntime_cxx_inline.h",
    "onnxruntime_training_c_api.h",
    "onnxruntime_training_cxx_api.h",
    "onnxruntime_training_cxx_inline.h",
    "onnxruntime_experimental_c_api.h",
    "onnxruntime_experimental_c_api.inc",
]
 
 
def parse_arguments():
    parser = argparse.ArgumentParser(
        description="Validate ONNX Runtime native nuget containing native shared library artifacts spec script",
        usage="",
    )
    # Main arguments
    parser.add_argument("--package_type", required=True, help="Specify nuget, tarball or zip.")
    parser.add_argument("--package_name", required=True, help="Package name to be validated.")
    parser.add_argument(
        "--package_path",
        required=True,
        help="Path containing the package to be validated. Must only contain only one package within this.",
    )
    parser.add_argument(
        "--platforms_supported", required=True, help="Comma separated list (no space). Ex: linux-x64,win-x86,osx-x64"
    )
    parser.add_argument(
        "--verify_nuget_signing",
        help="Flag indicating if Nuget package signing is to be verified. Only accepts 'true' or 'false'",
    )
    parser.add_argument(
        "--is_release_build",
        help="Flag indicating if validating a release build or dev build. Only accepts 'true' or 'false'",
    )
 
    return parser.parse_args()
 
 
def check_exists(path):
    return os.path.exists(path)
 
 
def remove_residual_files(path):
    if check_exists(path):
        os.remove(path)
 
 
def is_windows():
    return sys.platform.startswith("win")
 
 
def check_if_headers_are_present(header_files, header_folder, file_list_in_package, platform):
    for header in header_files:
        path = header_folder + "/" + header
        print("Checking path: " + path)
        if path not in file_list_in_package:
            print(header + " not found for " + platform)
            raise Exception(header + " not found for " + platform)
 
 
def check_if_dlls_are_present(
    package_type,
    is_windows_ai_package,
    is_gpu_package,
    is_dml_package,
    is_training_package,
    platforms_supported,
    zip_file,
    package_path,
    is_gpu_dependent_package=False,  # only used for nuget packages
):
    platforms = platforms_supported.strip().split(",")
    if package_type == "tarball":
        file_list_in_package = []
        for dirpath, _dirnames, filenames in os.walk(package_path):
            file_list_in_package += [os.path.join(dirpath, file) for file in filenames]
    else:
        file_list_in_package = zip_file.namelist()
    print(file_list_in_package)
    # In Nuget GPU package, onnxruntime.dll is in dependent package.
    package_contains_library = not bool(package_type == "nuget" and is_gpu_package)
    # In Nuget GPU package, gpu header files are not in dependent package.
    package_contains_headers = bool(
        (is_gpu_package and package_type != "nuget") or (package_type == "nuget" and not is_gpu_package)
    )
    # In Nuget GPU package, cuda ep and tensorrt ep dlls are in dependent package
    package_contains_cuda_binaries = bool((is_gpu_package and package_type != "nuget") or is_gpu_dependent_package)
    for platform in platforms:
        if platform.startswith("win"):
            native_folder = "_native" if is_windows_ai_package else "native"
 
            if package_type == "nuget":
                folder = "runtimes/" + platform + "/" + native_folder
                build_dir = "buildTransitive" if is_gpu_dependent_package else "build"
                header_folder = f"{build_dir}/native/include"
            else:  # zip package
                folder = package_path + "/lib"
                header_folder = package_path + "/include"
 
            # In Nuget GPU package, onnxruntime.dll is in dependent package.
            if package_contains_library:
                path = folder + "/" + "onnxruntime.dll"
                print("Checking path: " + path)
                if path not in file_list_in_package:
                    print("onnxruntime.dll not found for " + platform)
                    raise Exception("onnxruntime.dll not found for " + platform)
 
            if package_contains_cuda_binaries:
                for dll in win_gpu_package_libraries:
                    path = folder + "/" + dll
                    print("Checking path: " + path)
                    if path not in file_list_in_package:
                        print(dll + " not found for " + platform)
                        raise Exception(dll + " not found for " + platform)
 
            if package_contains_headers:
                check_if_headers_are_present(gpu_related_header_files, header_folder, file_list_in_package, platform)
 
            if is_dml_package:
                check_if_headers_are_present(dmlep_related_header_files, header_folder, file_list_in_package, platform)
 
            if is_training_package:
                check_if_headers_are_present(
                    training_related_header_files, header_folder, file_list_in_package, platform
                )
 
        elif platform.startswith("linux"):
            if package_type == "nuget":
                folder = "runtimes/" + platform + "/native"
                build_dir = "buildTransitive" if is_gpu_dependent_package else "build"
                header_folder = f"{build_dir}/native/include"
            else:  # tarball package
                folder = package_path + "/lib"
                header_folder = package_path + "/include"
 
            if package_contains_library:
                path = folder + "/" + "libonnxruntime.so"
                print("Checking path: " + path)
                if path not in file_list_in_package:
                    print("libonnxruntime.so not found for " + platform)
                    raise Exception("libonnxruntime.so not found for " + platform)
 
            if package_contains_cuda_binaries:
                for so in linux_gpu_package_libraries:
                    path = folder + "/" + so
                    print("Checking path: " + path)
                    if path not in file_list_in_package:
                        print(so + " not found for " + platform)
                        raise Exception(so + " not found for " + platform)
 
            if package_contains_headers:
                for header in gpu_related_header_files:
                    path = header_folder + "/" + header
                    print("Checking path: " + path)
                    if path not in file_list_in_package:
                        print(header + " not found for " + platform)
                        raise Exception(header + " not found for " + platform)
 
        elif platform.startswith("osx"):
            path = "runtimes/" + platform + "/native/libonnxruntime.dylib"
            print("Checking path: " + path)
            if path not in file_list_in_package:
                print("libonnxruntime.dylib not found for " + platform)
                raise Exception("libonnxruntime.dylib not found for " + platform)
 
        else:
            raise Exception("Unsupported platform: " + platform)
 
 
def check_if_nuget_is_signed(nuget_path):
    code_sign_summary_file = glob.glob(os.path.join(nuget_path, "*.md"))
    if len(code_sign_summary_file) != 1:
        print("CodeSignSummary files found in path: ")
        print(code_sign_summary_file)
        raise Exception("No CodeSignSummary files / more than one CodeSignSummary files found in the given path.")
 
    print("CodeSignSummary file: " + code_sign_summary_file[0])
 
    with open(code_sign_summary_file[0]) as f:
        contents = f.read()
        return "Pass" in contents
 
    return False
 
 
def validate_tarball(args):
    files = glob.glob(os.path.join(args.package_path, args.package_name))
    if len(files) != 1:
        print("packages found in path: ")
        print(files)
        raise Exception("No packages / more than one packages found in the given path.")
 
    package_name = args.package_name
    is_gpu_package = "-gpu_cuda" in package_name.lower()
 
    package_folder = re.search("(.*)[.].*", package_name).group(1)
 
    print("tar zxvf " + package_name)
    subprocess.run(["tar", "zxvf", package_name], check=True)
 
    is_windows_ai_package = False
    zip_file = None
    is_dml_package = False
    is_training_package = False
    check_if_dlls_are_present(
        args.package_type,
        is_windows_ai_package,
        is_gpu_package,
        is_dml_package,
        is_training_package,
        args.platforms_supported,
        zip_file,
        package_folder,
    )
 
 
def validate_zip(args):
    files = glob.glob(os.path.join(args.package_path, args.package_name))
    if len(files) != 1:
        print("packages found in path: ")
        print(files)
        raise Exception("No packages / more than one packages found in the given path.")
 
    package_name = args.package_name
    is_gpu_package = "-gpu_cuda" in package_name.lower()
 
    package_folder = re.search("(.*)[.].*", package_name).group(1)
 
    is_windows_ai_package = False
    is_dml_package = False
    is_training_package = False
    zip_file = zipfile.ZipFile(package_name)
    check_if_dlls_are_present(
        args.package_type,
        is_windows_ai_package,
        is_gpu_package,
        is_dml_package,
        is_training_package,
        args.platforms_supported,
        zip_file,
        package_folder,
    )
 
 
def validate_nuget(args):
    files = glob.glob(os.path.join(args.package_path, args.package_name))
    is_release_build = args.is_release_build and args.is_release_build.lower() == "true"
    nuget_packages_found_in_path = [
        i
        for i in files
        if i.endswith(".nupkg")
        and "Managed" not in i
        and ((is_release_build and "-dev" not in i) or (not is_release_build and "-dev" in i))
    ]
    if len(nuget_packages_found_in_path) != 1:
        print("Nuget packages found in path: ")
        print(nuget_packages_found_in_path)
        raise Exception("No Nuget packages / more than one Nuget packages found in the given path.")
    nuget_file_name = nuget_packages_found_in_path[0]
    full_nuget_path = os.path.join(args.package_path, nuget_file_name)
 
    is_gpu_package = bool("microsoft.ml.onnxruntime.gpu.1" in args.package_name.lower())
    is_gpu_dependent_package = bool(
        "microsoft.ml.onnxruntime.gpu.windows" in args.package_name.lower()
        or "microsoft.ml.onnxruntime.gpu.linux" in args.package_name.lower()
    )
    if "directml" in nuget_file_name.lower():
        is_dml_package = True
    else:
        is_dml_package = False
 
    if "Training" in nuget_file_name:
        is_training_package = True
    else:
        is_training_package = False
 
    exit_code = 0
 
    nupkg_copy_name = "NugetCopy.nupkg"
    zip_copy_name = "NugetCopy.zip"
    zip_file = None
 
    # Remove any residual files
    remove_residual_files(nupkg_copy_name)
    remove_residual_files(zip_copy_name)
 
    # Do all validations here
    try:
        if not is_windows():
            raise Exception("Nuget validation is currently supported only on Windows")
 
        # Make a copy of the Nuget package
        print("Copying [" + full_nuget_path + "] -> [" + nupkg_copy_name + "], and extracting its contents")
        shutil.copy2(full_nuget_path, nupkg_copy_name)
 
        # Convert nupkg to zip
        os.rename(nupkg_copy_name, zip_copy_name)
        zip_file = zipfile.ZipFile(zip_copy_name)
 
        # Check if the relevant dlls are present in the Nuget/Zip
        print("Checking if the Nuget contains relevant dlls")
        is_windows_ai_package = os.path.basename(full_nuget_path).startswith("Microsoft.AI.MachineLearning")
        check_if_dlls_are_present(
            args.package_type,
            is_windows_ai_package,
            is_gpu_package,
            is_dml_package,
            is_training_package,
            args.platforms_supported,
            zip_file,
            None,
            is_gpu_dependent_package,
        )
 
        verify_nuget_signing = args.verify_nuget_signing.lower()
        # Check if the Nuget has been signed
        if verify_nuget_signing != "true" and verify_nuget_signing != "false":
            raise Exception("Parameter verify_nuget_signing accepts only true or false as an argument")
 
        if verify_nuget_signing == "true":
            print("Verifying if Nuget has been signed")
            if not check_if_nuget_is_signed(args.package_path):
                print("Nuget signing verification failed")
                raise Exception("Nuget signing verification failed")
 
    except Exception as e:
        print(e)
        exit_code = 1
 
    finally:
        print("Cleaning up after Nuget validation")
 
        if zip_file is not None:
            zip_file.close()
 
        if check_exists(zip_copy_name):
            os.remove(zip_copy_name)
 
        if exit_code == 0:
            print("Nuget validation was successful")
        else:
            raise Exception("Nuget validation was unsuccessful")
 
 
def main():
    args = parse_arguments()
 
    if args.package_type == "nuget":
        validate_nuget(args)
    elif args.package_type == "tarball":
        validate_tarball(args)
    elif args.package_type == "zip":
        validate_zip(args)
    else:
        print(f"Package type {args.package_type} is not supported")
 
 
if __name__ == "__main__":
    sys.exit(main())