File: update_hash_codes.py

package info (click to toggle)
rdkit 202503.1-4
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 220,160 kB
  • sloc: cpp: 399,240; python: 77,453; ansic: 25,517; java: 8,173; javascript: 4,005; sql: 2,389; yacc: 1,565; lex: 1,263; cs: 1,081; makefile: 578; xml: 229; fortran: 183; sh: 105
file content (51 lines) | stat: -rw-r--r-- 1,387 bytes parent folder | download | duplicates (4)
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 python

# Script to update the hash codes in the cpp codes.
# Takes 2 arguments, the output from running one of the test programs
# giving the values to update, and the corresponding cpp
# file.  Output is to {file.cpp}.update.
# Note that the PNG hash codes are only for Freetype builds, so it's
# better to do the non-Freetype builds first, otherwise the PNG
# hashes will definitely be wrong.

import json
import re
import sys

updates = {}
with open(sys.argv[1], 'r') as f:
  for line in f.readlines():
    if line.startswith('file'):
      line_bits = line.strip().split()
      updates[(line_bits[1], line_bits[8])] = (line_bits[1], line_bits[4])

re_str = r'{"([\w.-]*.\w*)", (\d*U)},'
patt = re.compile(re_str)

with open(sys.argv[2], 'r') as f:
  cpp_file = f.read()

hashes = patt.findall(cpp_file)


def hash_replace(match):
  match_bits = match.group().split(',')
  match_tuple = (match_bits[0][2:-1], match_bits[1][1:-1])
  if match_tuple in updates:
    rep_tuple = updates[match_tuple]
    rep_str = f'{{"{rep_tuple[0]}", {rep_tuple[1]}}},'
    print(f'replacing {match.group()} with {rep_str}')
    return rep_str
  else:
    return match.group()


for hash in hashes:
  print(hash)
  if hash in updates:
    print(hash, updates[hash])

new_cpp_file = patt.sub(hash_replace, cpp_file)

with open(f'{sys.argv[2]}.update', 'w') as f:
  f.write(new_cpp_file)