|
24 | 24 | from licensedcode._vendor import attr |
25 | 25 | from license_expression import ExpressionError |
26 | 26 | from license_expression import Licensing |
27 | | -from saneyaml import load as yaml_load |
28 | | -from saneyaml import dump as yaml_dump |
| 27 | +from saneyaml import load as saneyaml_load |
| 28 | +from saneyaml import dump as saneyaml_dump |
| 29 | +from yaml import load as yaml_load |
| 30 | +from yaml import dump as yaml_dump |
| 31 | +from yaml import CSafeLoader |
29 | 32 |
|
30 | 33 | from commoncode.fileutils import file_base_name |
31 | 34 | from commoncode.fileutils import file_name |
@@ -554,7 +557,7 @@ def spdx_keys(self): |
554 | 557 | yield key |
555 | 558 |
|
556 | 559 | @staticmethod |
557 | | - def validate(licenses, verbose=False, no_dupe_urls=False): |
| 560 | + def validate(licenses, verbose=False, no_dupe_urls=False, thorough=False): |
558 | 561 | """ |
559 | 562 | Check that the ``licenses`` a mapping of {key: License} are valid. |
560 | 563 | Return dictionaries of infos, errors and warnings mapping a license key |
@@ -659,14 +662,22 @@ def validate(licenses, verbose=False, no_dupe_urls=False): |
659 | 662 | if not len(all_licenses) == len(set(all_licenses)): |
660 | 663 | warn('Some duplicated URLs') |
661 | 664 |
|
662 | | - # local text consistency |
663 | 665 | text = lic.text |
664 | | - |
665 | | - data = {"text": text} |
666 | | - # We are testing whether we can dump as yaml and load from yaml |
667 | | - # without failing (i.e. whether the text is yaml safe) |
668 | | - yaml_string = yaml_dump(data, indent=4) |
669 | | - loaded_yaml = yaml_load(yaml_string) |
| 666 | + if thorough: |
| 667 | + # local text consistency |
| 668 | + data = {"text": text} |
| 669 | + # We are testing whether we can dump as yaml and load from yaml |
| 670 | + # without failing (i.e. whether the text is yaml safe) |
| 671 | + # Using saneyaml |
| 672 | + try: |
| 673 | + yaml_string = saneyaml_dump(data, indent=4) |
| 674 | + loaded_yaml = saneyaml_load(yaml_string) |
| 675 | + except Exception: |
| 676 | + errors['GLOBAL'].append( |
| 677 | + f'Error invalid YAML text at: {lic.key}, failed during saneyaml.load()' |
| 678 | + ) |
| 679 | + # This fails because of missing line break at text end, added by saneyaml_dump |
| 680 | + # assert text == loaded_yaml["text"] |
670 | 681 |
|
671 | 682 | license_itokens = tuple(index_tokenizer(text)) |
672 | 683 | if not license_itokens: |
@@ -750,9 +761,9 @@ def validate(licenses, verbose=False, no_dupe_urls=False): |
750 | 761 | def get_yaml_safe_text(text): |
751 | 762 |
|
752 | 763 | data = {"text": text} |
753 | | - yaml_string = yaml_dump(data, indent=4) |
| 764 | + yaml_string = saneyaml_dump(data, indent=4) |
754 | 765 | try: |
755 | | - loaded_yaml = yaml_load(yaml_string) |
| 766 | + loaded_yaml = saneyaml_load(yaml_string) |
756 | 767 | except Exception: |
757 | 768 | text = text.replace('\n\n', '\n \n') |
758 | 769 | return text |
@@ -1028,7 +1039,7 @@ def _validate_all_rules(rules, licenses_by_key): |
1028 | 1039 | errors = defaultdict(list) |
1029 | 1040 |
|
1030 | 1041 | for rule in rules: |
1031 | | - for err_msg in rule.validate(licensing): |
| 1042 | + for err_msg in rule.validate(licensing, thorough=True): |
1032 | 1043 | errors[err_msg].append(rule) |
1033 | 1044 | return errors |
1034 | 1045 |
|
@@ -1717,7 +1728,7 @@ def has_unknown(self): |
1717 | 1728 | # license flag instead |
1718 | 1729 | return self.license_expression and 'unknown' in self.license_expression |
1719 | 1730 |
|
1720 | | - def validate(self, licensing=None): |
| 1731 | + def validate(self, licensing=None, thorough=False): |
1721 | 1732 | """ |
1722 | 1733 | Validate this rule using the provided ``licensing`` Licensing and yield |
1723 | 1734 | one error message for each type of error detected. |
@@ -1811,6 +1822,17 @@ def validate(self, licensing=None): |
1811 | 1822 | if len(set(self.referenced_filenames)) != len(self.referenced_filenames): |
1812 | 1823 | yield 'referenced_filenames cannot contain duplicates.' |
1813 | 1824 |
|
| 1825 | + if thorough: |
| 1826 | + text = self.text |
| 1827 | + data = {"text": text} |
| 1828 | + # We are testing whether we can dump as yaml and load from yaml |
| 1829 | + # without failing (i.e. whether the text is yaml safe) |
| 1830 | + try: |
| 1831 | + yaml_string = saneyaml_dump(data, indent=4) |
| 1832 | + loaded_yaml = saneyaml_load(yaml_string) |
| 1833 | + except Exception: |
| 1834 | + yield (f'Error invalid YAML text at: {self.identifier}, failed during saneyaml.load()') |
| 1835 | + |
1814 | 1836 | def license_keys(self, unique=True): |
1815 | 1837 | """ |
1816 | 1838 | Return a list of license keys for this rule. |
|
0 commit comments