Merge pull request #268619 from tweag/lib-descriptions
[NixPkgs.git] / pkgs / development / node-packages / remove-attr.py
blob2b13dcd5ee872b65e8941c04d59a747b98099665
1 #!/usr/bin/env nix-shell
2 #!nix-shell -i python3 -p python3
4 import collections.abc
5 import fileinput
6 import json
7 import os.path
8 import re
9 import sys
12 def remove(attr):
13 with open(os.path.join(os.path.dirname(__file__), 'node-packages.json'), 'r+') as node_packages_json:
14 packages = json.load(node_packages_json)
15 idx = 0
16 while idx < len(packages):
17 if packages[idx] == attr or (isinstance(packages[idx], collections.abc.Mapping) and next(iter(packages[idx].keys())) == attr):
18 del packages[idx]
19 else:
20 idx += 1
22 node_packages_json.seek(0)
23 for idx, package in enumerate(packages):
24 if idx == 0:
25 node_packages_json.write('[\n ')
26 else:
27 node_packages_json.write(', ')
28 json.dump(package, node_packages_json)
29 node_packages_json.write('\n')
30 node_packages_json.write(']\n')
31 node_packages_json.truncate()
33 with fileinput.input(os.path.join(os.path.dirname(__file__), 'node-packages.nix'), inplace=1) as node_packages:
34 safe_attr = re.escape(attr)
35 in_attr = False
36 for line in node_packages:
37 if in_attr:
38 if re.fullmatch(r' \};\n', line):
39 in_attr = False
40 else:
41 if re.fullmatch(rf' (?:{safe_attr}|"{safe_attr}") = nodeEnv\.buildNodePackage \{{\n', line):
42 in_attr = True
43 else:
44 sys.stdout.write(line)
46 with fileinput.input(os.path.join(os.path.dirname(__file__), 'main-programs.nix'), inplace=1) as main_programs:
47 safe_attr = re.escape(attr)
48 for line in main_programs:
49 if not re.fullmatch(rf' "?{safe_attr}"? = ".*";\n', line):
50 sys.stdout.write(line)
52 with fileinput.input(os.path.join(os.path.dirname(__file__), 'overrides.nix'), inplace=1) as overrides:
53 safe_attr = re.escape(attr)
54 in_attr = False
55 for line in overrides:
56 if in_attr:
57 if re.fullmatch(r' \}\)?;\n', line):
58 in_attr = False
59 else:
60 if re.fullmatch(rf' (?:{safe_attr}|"{safe_attr}") = .* \{{\n', line):
61 in_attr = True
62 else:
63 sys.stdout.write(line)
66 if __name__ == '__main__':
67 import argparse
69 parser = argparse.ArgumentParser(description='Remove a given package from the node-packages.nix file')
70 parser.add_argument('attr', help='The package attribute to remove')
71 args = parser.parse_args()
73 remove(args.attr)