A workaround to allow substituting custom tokens within ASV configuration file since there's no official way to add custom variables(e.g. env vars). Parameters ---------- in_config : str The path of ASV configuration file, e.g. '/path/to/asv.conf.json' out_confi
(in_config, out_config, **custom_vars)
| 596 | except OSError: pass |
| 597 | |
| 598 | def asv_substitute_config(in_config, out_config, **custom_vars): |
| 599 | """ |
| 600 | A workaround to allow substituting custom tokens within |
| 601 | ASV configuration file since there's no official way to add custom |
| 602 | variables(e.g. env vars). |
| 603 | |
| 604 | Parameters |
| 605 | ---------- |
| 606 | in_config : str |
| 607 | The path of ASV configuration file, e.g. '/path/to/asv.conf.json' |
| 608 | out_config : str |
| 609 | The path of generated configuration file, |
| 610 | e.g. '/path/to/asv_substituted.conf.json'. |
| 611 | |
| 612 | The other keyword arguments represent the custom variables. |
| 613 | |
| 614 | Returns |
| 615 | ------- |
| 616 | True(is cached) if 'out_config' is already generated with |
| 617 | the same '**custom_vars' and updated with latest 'in_config', |
| 618 | False otherwise. |
| 619 | |
| 620 | Examples |
| 621 | -------- |
| 622 | See asv_compare_config(). |
| 623 | """ |
| 624 | assert in_config != out_config |
| 625 | assert len(custom_vars) > 0 |
| 626 | |
| 627 | def sdbm_hash(*factors): |
| 628 | chash = 0 |
| 629 | for f in factors: |
| 630 | for char in str(f): |
| 631 | chash = ord(char) + (chash << 6) + (chash << 16) - chash |
| 632 | chash &= 0xFFFFFFFF |
| 633 | return chash |
| 634 | |
| 635 | vars_hash = sdbm_hash(custom_vars, os.path.getmtime(in_config)) |
| 636 | try: |
| 637 | with open(out_config) as wfd: |
| 638 | hash_line = wfd.readline().split('hash:') |
| 639 | if len(hash_line) > 1 and int(hash_line[1]) == vars_hash: |
| 640 | return True |
| 641 | except OSError: |
| 642 | pass |
| 643 | |
| 644 | custom_vars = {f'{{{k}}}':v for k, v in custom_vars.items()} |
| 645 | with open(in_config, "r") as rfd, open(out_config, "w") as wfd: |
| 646 | wfd.write(f"// hash:{vars_hash}\n") |
| 647 | wfd.write("// This file is automatically generated by runtests.py\n") |
| 648 | for line in rfd: |
| 649 | for key, val in custom_vars.items(): |
| 650 | line = line.replace(key, val) |
| 651 | wfd.write(line) |
| 652 | return False |
| 653 | |
| 654 | # |
| 655 | # GCOV support |