Skip to content

Commit b54e189

Browse files
authored
Add a python script to help pick up the latest cjson and libcbor (#1211)
1 parent a99c7d7 commit b54e189

36 files changed

+914
-726
lines changed

THIRD-PARTY-LICENSES.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3434

3535
------
3636

37-
** cJSON; version 1.7.16 -- https://github.com/DaveGamble/cJSON
37+
** cJSON; version 1.7.18 -- https://github.com/DaveGamble/cJSON
3838
Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
3939

4040
Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -57,7 +57,7 @@ THE SOFTWARE.
5757

5858
------
5959

60-
** libcbor; version 0.11.0 -- https://github.com/PJK/libcbor
60+
** libcbor; version 0.12.0 -- https://github.com/PJK/libcbor
6161
Copyright (c) 2014-2017 Pavel Kalvoda
6262

6363
MIT License

scripts/import_libcbor.py

100644100755
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
import os
23
import tempfile
34
import shutil
@@ -93,10 +94,13 @@ def parse_version(version_string):
9394
subprocess.run(clone_command, shell=True, check=True)
9495
subprocess.run(["git", "checkout", "tags/" + args.version],
9596
cwd=temp_repo_dir, check=True)
97+
version_number = args.version[1:] # Remove 'v' prefix
9698

9799
# Create a separate folder for the copied files
100+
base_dir = os.path.join(os.path.dirname(
101+
__file__), "..")
98102
output_dir = os.path.join(
99-
os.path.dirname(__file__), "..", "source", "external", "libcbor")
103+
base_dir, "source", "external", "libcbor")
100104
shutil.rmtree(output_dir, ignore_errors=True)
101105
os.makedirs(output_dir, exist_ok=True)
102106

@@ -121,6 +125,21 @@ def parse_version(version_string):
121125
file.write(GENERATED_NOTES)
122126
file.write(CONFIGURATION_H)
123127

128+
# Update the version number in THIRD-PARTY-LICENSES.txt
129+
license_path = os.path.join(base_dir, "THIRD-PARTY-LICENSES.txt")
130+
with open(license_path, 'r') as f:
131+
content = f.read()
132+
133+
# Update the version number using regex for more accurate replacement
134+
pattern = r'\*\* libcbor; version \d+\.\d+\.\d+ --'
135+
replacement = f'** libcbor; version {version_number} --'
136+
content = re.sub(pattern, replacement, content)
137+
138+
with open(license_path, 'w') as f:
139+
f.write(content)
140+
141+
print(f"Updated libcbor to version {version_number}")
142+
124143
except Exception as e:
125144
print(f"An error occurred: {e}")
126145

scripts/update_cjson.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import shutil
4+
import tempfile
5+
import subprocess
6+
import re
7+
8+
9+
def run_command(cmd, cwd=None, check=True):
10+
"""Run a command and return its output."""
11+
print(f"Running: {cmd}")
12+
result = subprocess.run(cmd, shell=True, check=check, text=True,
13+
stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
14+
return result.stdout.strip(), result.returncode
15+
16+
17+
def main():
18+
# Create a temporary directory
19+
temp_dir = tempfile.mkdtemp()
20+
print(f"Created temporary directory: {temp_dir}")
21+
22+
try:
23+
# 1. Clone the cJSON repository
24+
_, _ = run_command(
25+
f"git clone https://github.com/DaveGamble/cJSON.git {temp_dir}")
26+
27+
# 2. Get the latest release tag
28+
tags_output, _ = run_command("git tag -l", cwd=temp_dir)
29+
tags = tags_output.split('\n')
30+
# Filter for version tags (e.g., v1.7.15)
31+
version_tags = [tag for tag in tags if re.match(
32+
r'^v\d+\.\d+\.\d+$', tag)]
33+
# Sort by version number
34+
version_tags.sort(key=lambda v: [int(x) for x in v[1:].split('.')])
35+
latest_tag = version_tags[-1]
36+
latest_version = latest_tag[1:] # Remove 'v' prefix
37+
38+
print(f"Latest cJSON version: {latest_version}")
39+
40+
# Checkout the latest tag
41+
_, _ = run_command(f"git checkout {latest_tag}", cwd=temp_dir)
42+
43+
# 3. Copy cJSON.h and cJSON.c to ./source/external/
44+
base_dir = os.path.join(os.path.dirname(
45+
__file__), "..")
46+
source_dir = os.path.join(base_dir, "source", "external")
47+
shutil.copy(os.path.join(temp_dir, "cJSON.h"),
48+
os.path.join(source_dir, "cJSON.h"))
49+
shutil.copy(os.path.join(temp_dir, "cJSON.c"),
50+
os.path.join(source_dir, "cJSON.c"))
51+
52+
print(f"Copied cJSON.h and cJSON.c to {source_dir}")
53+
54+
# 4. Update the version number in THIRD-PARTY-LICENSES.txt
55+
license_path = os.path.join(base_dir, "THIRD-PARTY-LICENSES.txt")
56+
with open(license_path, 'r') as f:
57+
content = f.read()
58+
59+
# Update the version number using regex for more accurate replacement
60+
pattern = r'\*\* cJSON; version \d+\.\d+\.\d+ --'
61+
replacement = f'** cJSON; version {latest_version} --'
62+
content = re.sub(pattern, replacement, content)
63+
64+
with open(license_path, 'w') as f:
65+
f.write(content)
66+
67+
print(f"Updated cJSON to version {latest_version}")
68+
69+
# 5. Apply the changes from commit f005fdc06c34fdd663031661eff5c5575843e998
70+
# https://github.com/awslabs/aws-c-common/pull/1211/commits/f005fdc06c34fdd663031661eff5c5575843e998
71+
# it applies the amazon edit to cJSON.h and cJSON.c
72+
_, returncode = run_command(
73+
f"git add *", cwd=source_dir, check=False)
74+
if returncode == 0:
75+
_, returncode = run_command(
76+
f"git commit -m \"update cjson to {latest_version}\"", cwd=base_dir, check=False)
77+
if returncode == 0:
78+
_, returncode = run_command(
79+
f"git cherry-pick f005fdc06c34fdd663031661eff5c5575843e998", cwd=base_dir, check=False)
80+
if returncode == 0:
81+
print(
82+
"Successfully applied Amazon modifications from commit f005fdc06c34fdd663031661eff5c5575843e998")
83+
else:
84+
print(
85+
"\nWARNING: Failed to apply changes from commit f005fdc06c34fdd663031661eff5c5575843e998")
86+
print(
87+
"You will need to manually apply these changes. Please run the following command:")
88+
print(f" git cherry-pick f005fdc06c34fdd663031661eff5c5575843e998")
89+
print("If there are conflicts, resolve them and then run:")
90+
print(" git cherry-pick --continue")
91+
else:
92+
print("\nWARNING: Failed to commit the updated cJSON files")
93+
print("You will need to manually commit and then apply the changes from commit f005fdc06c34fdd663031661eff5c5575843e998")
94+
else:
95+
print("\nWARNING: Failed to add the updated cJSON files to git")
96+
print("You will need to manually add, commit, and then apply the changes from commit f005fdc06c34fdd663031661eff5c5575843e998")
97+
98+
finally:
99+
# Clean up the temporary directory
100+
shutil.rmtree(temp_dir)
101+
print(f"Removed temporary directory: {temp_dir}")
102+
103+
104+
if __name__ == "__main__":
105+
main()

source/external/cJSON.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);
264264
CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * const b, const cJSON_bool case_sensitive);
265265

266266
/* Minify a strings, remove blank characters(such as ' ', '\t', '\r', '\n') from strings.
267-
* The input pointer json cannot point to a read-only address area, such as a string constant,
267+
* The input pointer json cannot point to a read-only address area, such as a string constant,
268268
* but should point to a readable and writable address area. */
269269
CJSON_PUBLIC(void) cJSON_Minify(char *json);
270270

source/external/libcbor/cbor.c

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
#include "cbor/internal/builder_callbacks.h"
1010
#include "cbor/internal/loaders.h"
1111

12-
#pragma clang diagnostic push
13-
cbor_item_t *cbor_load(cbor_data source, size_t source_size,
14-
struct cbor_load_result *result) {
12+
cbor_item_t* cbor_load(cbor_data source, size_t source_size,
13+
struct cbor_load_result* result) {
1514
/* Context stack */
1615
static struct cbor_callbacks callbacks = {
1716
.uint8 = &cbor_builder_uint8_callback,
@@ -115,8 +114,8 @@ cbor_item_t *cbor_load(cbor_data source, size_t source_size,
115114
return NULL;
116115
}
117116

118-
static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
119-
cbor_item_t *res;
117+
static cbor_item_t* _cbor_copy_int(cbor_item_t* item, bool negative) {
118+
cbor_item_t* res = NULL;
120119
switch (cbor_int_get_width(item)) {
121120
case CBOR_INT_8:
122121
res = cbor_build_uint8(cbor_get_uint8(item));
@@ -137,8 +136,7 @@ static cbor_item_t *_cbor_copy_int(cbor_item_t *item, bool negative) {
137136
return res;
138137
}
139138

140-
static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
141-
// cppcheck-suppress missingReturn
139+
static cbor_item_t* _cbor_copy_float_ctrl(cbor_item_t* item) {
142140
switch (cbor_float_get_width(item)) {
143141
case CBOR_FLOAT_0:
144142
return cbor_build_ctrl(cbor_ctrl_value(item));
@@ -148,11 +146,13 @@ static cbor_item_t *_cbor_copy_float_ctrl(cbor_item_t *item) {
148146
return cbor_build_float4(cbor_float_get_float4(item));
149147
case CBOR_FLOAT_64:
150148
return cbor_build_float8(cbor_float_get_float8(item));
149+
default:
150+
_CBOR_UNREACHABLE;
151+
return NULL;
151152
}
152153
}
153154

154-
cbor_item_t *cbor_copy(cbor_item_t *item) {
155-
// cppcheck-suppress missingReturn
155+
cbor_item_t* cbor_copy(cbor_item_t* item) {
156156
switch (cbor_typeof(item)) {
157157
case CBOR_TYPE_UINT:
158158
return _cbor_copy_int(item, false);
@@ -163,13 +163,13 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
163163
return cbor_build_bytestring(cbor_bytestring_handle(item),
164164
cbor_bytestring_length(item));
165165
} else {
166-
cbor_item_t *res = cbor_new_indefinite_bytestring();
166+
cbor_item_t* res = cbor_new_indefinite_bytestring();
167167
if (res == NULL) {
168168
return NULL;
169169
}
170170

171171
for (size_t i = 0; i < cbor_bytestring_chunk_count(item); i++) {
172-
cbor_item_t *chunk_copy =
172+
cbor_item_t* chunk_copy =
173173
cbor_copy(cbor_bytestring_chunks_handle(item)[i]);
174174
if (chunk_copy == NULL) {
175175
cbor_decref(&res);
@@ -186,16 +186,16 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
186186
}
187187
case CBOR_TYPE_STRING:
188188
if (cbor_string_is_definite(item)) {
189-
return cbor_build_stringn((const char *)cbor_string_handle(item),
189+
return cbor_build_stringn((const char*)cbor_string_handle(item),
190190
cbor_string_length(item));
191191
} else {
192-
cbor_item_t *res = cbor_new_indefinite_string();
192+
cbor_item_t* res = cbor_new_indefinite_string();
193193
if (res == NULL) {
194194
return NULL;
195195
}
196196

197197
for (size_t i = 0; i < cbor_string_chunk_count(item); i++) {
198-
cbor_item_t *chunk_copy =
198+
cbor_item_t* chunk_copy =
199199
cbor_copy(cbor_string_chunks_handle(item)[i]);
200200
if (chunk_copy == NULL) {
201201
cbor_decref(&res);
@@ -211,7 +211,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
211211
return res;
212212
}
213213
case CBOR_TYPE_ARRAY: {
214-
cbor_item_t *res;
214+
cbor_item_t* res;
215215
if (cbor_array_is_definite(item)) {
216216
res = cbor_new_definite_array(cbor_array_size(item));
217217
} else {
@@ -222,7 +222,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
222222
}
223223

224224
for (size_t i = 0; i < cbor_array_size(item); i++) {
225-
cbor_item_t *entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
225+
cbor_item_t* entry_copy = cbor_copy(cbor_move(cbor_array_get(item, i)));
226226
if (entry_copy == NULL) {
227227
cbor_decref(&res);
228228
return NULL;
@@ -237,7 +237,7 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
237237
return res;
238238
}
239239
case CBOR_TYPE_MAP: {
240-
cbor_item_t *res;
240+
cbor_item_t* res;
241241
if (cbor_map_is_definite(item)) {
242242
res = cbor_new_definite_map(cbor_map_size(item));
243243
} else {
@@ -247,14 +247,14 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
247247
return NULL;
248248
}
249249

250-
struct cbor_pair *it = cbor_map_handle(item);
250+
struct cbor_pair* it = cbor_map_handle(item);
251251
for (size_t i = 0; i < cbor_map_size(item); i++) {
252-
cbor_item_t *key_copy = cbor_copy(it[i].key);
252+
cbor_item_t* key_copy = cbor_copy(it[i].key);
253253
if (key_copy == NULL) {
254254
cbor_decref(&res);
255255
return NULL;
256256
}
257-
cbor_item_t *value_copy = cbor_copy(it[i].value);
257+
cbor_item_t* value_copy = cbor_copy(it[i].value);
258258
if (value_copy == NULL) {
259259
cbor_decref(&res);
260260
cbor_decref(&key_copy);
@@ -273,16 +273,19 @@ cbor_item_t *cbor_copy(cbor_item_t *item) {
273273
return res;
274274
}
275275
case CBOR_TYPE_TAG: {
276-
cbor_item_t *item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
276+
cbor_item_t* item_copy = cbor_copy(cbor_move(cbor_tag_item(item)));
277277
if (item_copy == NULL) {
278278
return NULL;
279279
}
280-
cbor_item_t *tag = cbor_build_tag(cbor_tag_value(item), item_copy);
280+
cbor_item_t* tag = cbor_build_tag(cbor_tag_value(item), item_copy);
281281
cbor_decref(&item_copy);
282282
return tag;
283283
}
284284
case CBOR_TYPE_FLOAT_CTRL:
285285
return _cbor_copy_float_ctrl(item);
286+
default:
287+
_CBOR_UNREACHABLE;
288+
return NULL;
286289
}
287290
}
288291

@@ -301,11 +304,11 @@ static int _pow(int b, int ex) {
301304
return res;
302305
}
303306

304-
static void _cbor_type_marquee(FILE *out, char *label, int indent) {
307+
static void _cbor_type_marquee(FILE* out, char* label, int indent) {
305308
fprintf(out, "%*.*s[%s] ", indent, indent, " ", label);
306309
}
307310

308-
static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
311+
static void _cbor_nested_describe(cbor_item_t* item, FILE* out, int indent) {
309312
const int indent_offset = 4;
310313
switch (cbor_typeof(item)) {
311314
case CBOR_TYPE_UINT: {
@@ -329,7 +332,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
329332
_cbor_nested_describe(cbor_bytestring_chunks_handle(item)[i], out,
330333
indent + indent_offset);
331334
} else {
332-
const unsigned char *data = cbor_bytestring_handle(item);
335+
const unsigned char* data = cbor_bytestring_handle(item);
333336
fprintf(out, "Definite, Length: %zuB, Data:\n",
334337
cbor_bytestring_length(item));
335338
fprintf(out, "%*s", indent + indent_offset, " ");
@@ -418,7 +421,7 @@ static void _cbor_nested_describe(cbor_item_t *item, FILE *out, int indent) {
418421
}
419422
}
420423

421-
void cbor_describe(cbor_item_t *item, FILE *out) {
424+
void cbor_describe(cbor_item_t* item, FILE* out) {
422425
_cbor_nested_describe(item, out, 0);
423426
}
424427

0 commit comments

Comments
 (0)