Return a mapping of {key: License} loaded from license data and text files found in ``licenses_data_dir``. Raise Exceptions if there are dangling or orphaned files. Optionally include deprecated license if ``with_deprecated`` is True. Optionally check for dangling orphaned files
(
licenses_data_dir=licenses_data_dir,
with_deprecated=False,
check_consistency=True,
is_builtin=True,
)
| 799 | |
| 800 | |
| 801 | def load_licenses( |
| 802 | licenses_data_dir=licenses_data_dir, |
| 803 | with_deprecated=False, |
| 804 | check_consistency=True, |
| 805 | is_builtin=True, |
| 806 | ): |
| 807 | """ |
| 808 | Return a mapping of {key: License} loaded from license data and text files |
| 809 | found in ``licenses_data_dir``. Raise Exceptions if there are dangling or |
| 810 | orphaned files. |
| 811 | Optionally include deprecated license if ``with_deprecated`` is True. |
| 812 | Optionally check for dangling orphaned files if ``check_dangling`` is True. |
| 813 | """ |
| 814 | |
| 815 | all_files = list(resource_iter( |
| 816 | location=licenses_data_dir, |
| 817 | ignored=ignore_editor_tmp_files, |
| 818 | with_dirs=False, |
| 819 | follow_symlinks=True, |
| 820 | )) |
| 821 | |
| 822 | licenses = {} |
| 823 | |
| 824 | for license_file in all_files: |
| 825 | if license_file.endswith('.LICENSE'): |
| 826 | if TRACE: |
| 827 | logger_debug('load_licenses: license_file:', license_file) |
| 828 | |
| 829 | key = file_base_name(license_file) |
| 830 | |
| 831 | try: |
| 832 | lic = License.from_dir( |
| 833 | key=key, |
| 834 | licenses_data_dir=licenses_data_dir, |
| 835 | check_consistency=check_consistency, |
| 836 | is_builtin=is_builtin, |
| 837 | ) |
| 838 | except Exception as e: |
| 839 | msg = ( |
| 840 | f'Failed to load license: {key} from: ' |
| 841 | f'file://{licenses_data_dir}/{key}.LICENSE with error: {e}' |
| 842 | ) |
| 843 | raise InvalidLicense(msg) from e |
| 844 | |
| 845 | if not with_deprecated and lic.is_deprecated: |
| 846 | continue |
| 847 | |
| 848 | licenses[key] = lic |
| 849 | |
| 850 | if not licenses: |
| 851 | msg = ( |
| 852 | 'No licenses were loaded. Check to see if the license data files ' |
| 853 | f'are available at "{licenses_data_dir}".' |
| 854 | ) |
| 855 | raise InvalidLicense(msg) |
| 856 | |
| 857 | return licenses |
| 858 |