| 1 |
|
| 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 |
|
| 19 |
temp_dir = tempfile.mkdtemp() |
| 20 |
print(f"Created temporary directory: {temp_dir}") |
| 21 |
|
| 22 |
try: |
| 23 |
|
| 24 |
_, _ = run_command( |
| 25 |
f"git clone https://github.com/DaveGamble/cJSON.git {temp_dir}") |
| 26 |
|
| 27 |
|
| 28 |
tags_output, _ = run_command("git tag -l", cwd=temp_dir) |
| 29 |
tags = tags_output.split('\n') |
| 30 |
|
| 31 |
version_tags = [tag for tag in tags if re.match( |
| 32 |
r'^v\d+\.\d+\.\d+$', tag)] |
| 33 |
|
| 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:] |
| 37 |
|
| 38 |
print(f"Latest cJSON version: {latest_version}") |
| 39 |
|
| 40 |
|
| 41 |
_, _ = run_command(f"git checkout {latest_tag}", cwd=temp_dir) |
| 42 |
|
| 43 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 100 |
shutil.rmtree(temp_dir) |
| 101 |
print(f"Removed temporary directory: {temp_dir}") |
| 102 |
|
| 103 |
|
| 104 |
if __name__ == "__main__": |
| 105 |
main() |