File: patch_manifest.py

package info (click to toggle)
privacybadger 2025.12.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 12,700 kB
  • sloc: javascript: 56,159; python: 2,225; sh: 403; makefile: 57; xml: 6
file content (51 lines) | stat: -rwxr-xr-x 1,607 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

import argparse
import json

from collections import OrderedDict

def update_manifest(manifest_path, operation, key, value, json_value):
    with open(manifest_path, 'r', encoding="utf-8") as f:
        manifest_data = json.load(f, object_pairs_hook=OrderedDict)

    if operation == 'del':
        data = manifest_data
        while '.' in key: # handle nested keys ("x.y.z")
            idx, _, key = key.partition('.')
            data = data[idx]
        del data[key]

    elif operation == 'set':
        if json_value:
            try:
                value = json.loads(value)
            except json.decoder.JSONDecodeError as err:
                print("Failed to parse", value)
                raise err

        data = manifest_data
        while '.' in key: # handle nested keys ("x.y.z")
            idx, _, key = key.partition('.')
            if idx not in data:
                data[idx] = {}
            data = data[idx]
        data[key] = value

    with open(manifest_path, 'w', encoding="utf-8") as f:
        json.dump(manifest_data, f, sort_keys=False, indent=2, separators=(',', ': '))
        f.write('\n')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('manifest_path')
    parser.add_argument('operation', choices=('set', 'del'))
    parser.add_argument('key')
    parser.add_argument('value', nargs='?')
    parser.add_argument('--json-value', action='store_true', default=False)

    args = parser.parse_args()

    update_manifest(args.manifest_path, args.operation, args.key, args.value, args.json_value)