File: import_libcbor.py

package info (click to toggle)
aws-crt-python 0.28.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,428 kB
  • sloc: ansic: 437,955; python: 27,657; makefile: 5,855; sh: 4,289; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (149 lines) | stat: -rwxr-xr-x 4,653 bytes parent folder | download
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
#!/usr/bin/env python3
import os
import tempfile
import shutil
import subprocess
import argparse
import re


def parse_version(version_string):
    match = re.fullmatch(r'v(\d+)\.(\d+)\.(\d+)', version_string)
    if not match:
        raise ValueError("Invalid version string")
    return match.group(1), match.group(2), match.group(3)


argument_parser = argparse.ArgumentParser(
    description="Helper to import libcbor as external dependency.")

argument_parser.add_argument("--version",
                             required=True, help="Version string to import")

args = argument_parser.parse_args()
major_version, minor_version, patch_version = parse_version(args.version)

GENERATED_NOTES = """/**
 * DO NOT DIRECTLY MODIFY THIS FILE:
 *
 * The code in this file is generated from scripts/import_libcbor.py
 * and any modifications should be in there.
 */
"""

CBOR_EXPORT_H = """
#ifndef CBOR_EXPORT_H
#define CBOR_EXPORT_H

/* Don't export anything from libcbor */
#define CBOR_EXPORT

#endif /* CBOR_EXPORT_H */
"""

CONFIGURATION_H = f"""
#ifndef LIBCBOR_CONFIGURATION_H
#define LIBCBOR_CONFIGURATION_H

#define CBOR_MAJOR_VERSION {major_version}
#define CBOR_MINOR_VERSION {minor_version}
#define CBOR_PATCH_VERSION {patch_version}

#define CBOR_BUFFER_GROWTH 2
#define CBOR_MAX_STACK_SIZE 2048
#define CBOR_PRETTY_PRINTER 1

#if defined(_MSC_VER)
#    define CBOR_RESTRICT_SPECIFIER
#else
#    define CBOR_RESTRICT_SPECIFIER restrict
#endif

#define CBOR_INLINE_SPECIFIER

/* Ignore the compiler warnings for libcbor. */
#ifdef _MSC_VER
#    pragma warning(disable : 4028)
#    pragma warning(disable : 4189)
#    pragma warning(disable : 4715)
#    pragma warning(disable : 4232)
#    pragma warning(disable : 4068)
#    pragma warning(disable : 4244)
#    pragma warning(disable : 4701)
#    pragma warning(disable : 4703)
#endif

#ifdef __clang__
#    pragma clang diagnostic ignored "-Wreturn-type"
#elif defined(__GNUC__)
#    pragma GCC diagnostic ignored "-Wreturn-type"
#    pragma GCC diagnostic ignored "-Wunknown-pragmas"
#    pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif

#endif // LIBCBOR_CONFIGURATION_H
"""


# Create a temporary directory for cloning the repository
temp_repo_dir = tempfile.mkdtemp()

try:
    # Clone the repository into the temporary directory
    repo_url = "https://github.com/PJK/libcbor.git"
    clone_command = f"git clone {repo_url} {temp_repo_dir}"
    subprocess.run(clone_command, shell=True, check=True)
    subprocess.run(["git", "checkout", "tags/" + args.version],
                   cwd=temp_repo_dir, check=True)
    version_number = args.version[1:]  # Remove 'v' prefix

    # Create a separate folder for the copied files
    base_dir = os.path.join(os.path.dirname(
        __file__), "..")
    output_dir = os.path.join(
        base_dir, "source", "external", "libcbor")
    shutil.rmtree(output_dir, ignore_errors=True)
    os.makedirs(output_dir, exist_ok=True)

    # Copy files ending with .c and .h from the /src directory
    src_dir = os.path.join(temp_repo_dir, "src")
    for root, dirs, files in os.walk(src_dir):
        dir_name = os.path.basename(root)
        for file in files:
            if file.endswith((".c", ".h")):
                # copy the source code to ../source/external/libcbor
                src_file = os.path.join(root, file)
                rel_path = os.path.relpath(src_file, src_dir)
                dest_file = os.path.join(output_dir, rel_path)
                os.makedirs(os.path.dirname(dest_file), exist_ok=True)
                shutil.copy(src_file, dest_file)

    # Use our customized configurations
    with open(os.path.join(output_dir, "cbor/cbor_export.h"), "w") as file:
        file.write(GENERATED_NOTES)
        file.write(CBOR_EXPORT_H)
    with open(os.path.join(output_dir, "cbor/configuration.h"), "w") as file:
        file.write(GENERATED_NOTES)
        file.write(CONFIGURATION_H)

    # Update the version number in THIRD-PARTY-LICENSES.txt
    license_path = os.path.join(base_dir, "THIRD-PARTY-LICENSES.txt")
    with open(license_path, 'r') as f:
        content = f.read()

    # Update the version number using regex for more accurate replacement
    pattern = r'\*\* libcbor; version \d+\.\d+\.\d+ --'
    replacement = f'** libcbor; version {version_number} --'
    content = re.sub(pattern, replacement, content)

    with open(license_path, 'w') as f:
        f.write(content)

    print(f"Updated libcbor to version {version_number}")

except Exception as e:
    print(f"An error occurred: {e}")

finally:
    # Remove the temporary directory
    shutil.rmtree(temp_repo_dir, ignore_errors=True)