|
19 | 19 | import yaml |
20 | 20 | import os |
21 | 21 | import glob |
| 22 | +import fnmatch |
22 | 23 | import subprocess |
23 | 24 | import shutil |
24 | 25 | from shutil import copy, copymode |
@@ -877,7 +878,7 @@ def save_project_yaml(project : OntologyProject, path : str): |
877 | 878 | with open(path, "w") as f: |
878 | 879 | f.write(yaml.dump(json_obj, default_flow_style=False)) |
879 | 880 |
|
880 | | -def unpack_files(basedir, txt, policies={}): |
| 881 | +def unpack_files(basedir, txt, policies=[]): |
881 | 882 | """ |
882 | 883 | This unpacks a custom tar-like format in which multiple file paths |
883 | 884 | can be specified, separated by ^^^s |
@@ -937,21 +938,33 @@ def must_install_file(templatefile, targetfile, policies): |
937 | 938 | Given a template filename, indicate whether the file should be |
938 | 939 | installed according to any per-file policy. |
939 | 940 |
|
940 | | - policies is a dictionary associating a template filename to one |
941 | | - of the following three values: |
942 | | - * IF_MISSING (default): install the file if it does not already exist |
| 941 | + policies is a list of (PATTERN,POLICY) tuples where PATTERN is |
| 942 | + a shell-like globbing pattern and POLICY is the update policy |
| 943 | + that should be applied to any template whose pathname matches |
| 944 | + the pattern. |
| 945 | +
|
| 946 | + Patterns are tested in the order they are found in the list, |
| 947 | + and the first match takes precedence over any subsequent match. |
| 948 | + If there is no match, the default policy is IF_MISSING. |
| 949 | +
|
| 950 | + Valid policies are: |
| 951 | + * IF_MISSING: install the file if it does not already exist |
943 | 952 | * ALWAYS: always install the file, overwrite any existing file |
944 | 953 | * NEVER: never install the file |
945 | 954 | """ |
946 | | - policy = policies.get(templatefile, IF_MISSING) |
| 955 | + policy = IF_MISSING |
| 956 | + for pattern, pattern_policy in policies: |
| 957 | + if fnmatch.fnmatch(templatefile, pattern): |
| 958 | + policy = pattern_policy |
| 959 | + break |
947 | 960 | if policy == ALWAYS: |
948 | 961 | return True |
949 | 962 | elif policy == NEVER: |
950 | 963 | return False |
951 | 964 | else: |
952 | 965 | return not os.path.exists(targetfile) |
953 | 966 |
|
954 | | -def install_template_files(generator, templatedir, targetdir, policies={}): |
| 967 | +def install_template_files(generator, templatedir, targetdir, policies=[]): |
955 | 968 | """ |
956 | 969 | Installs all template-derived files into a target directory. |
957 | 970 | """ |
@@ -1086,30 +1099,24 @@ def update(templatedir): |
1086 | 1099 | # missing (e.g. DOSDP example files) or on the contrary |
1087 | 1100 | # always reinstalled to overwrite any local changes (e.g. |
1088 | 1101 | # the main Makefile). We declare the corresponding policies. |
1089 | | - policies = {} |
1090 | | - policies['CODE_OF_CONDUCT.md'] = NEVER |
1091 | | - policies['CONTRIBUTING.md'] = NEVER |
1092 | | - policies['issue_template.md'] = NEVER |
1093 | | - policies['README.md'] = NEVER |
1094 | | - policies['src/patterns/data/default/example.tsv'] = NEVER |
1095 | | - policies['src/patterns/dosdp-patterns/example.yaml'] = NEVER |
1096 | | - policies['src/ontology/Makefile'] = ALWAYS |
1097 | | - policies['src/ontology/run.sh'] = ALWAYS |
1098 | | - policies['docs/odk-workflows'] = ALWAYS |
| 1102 | + policies = [ |
| 1103 | + ('CODE_OF_CONDUCT.md', NEVER), |
| 1104 | + ('CONTRIBUTING.md', NEVER), |
| 1105 | + ('issue_template.md', NEVER), |
| 1106 | + ('README.md', NEVER), |
| 1107 | + ('src/patterns/data/default/example.tsv', NEVER), |
| 1108 | + ('src/patterns/dosdp-patterns/example.yaml', NEVER), |
| 1109 | + ('src/ontology/Makefile', ALWAYS), |
| 1110 | + ('src/ontology/run.sh', ALWAYS), |
| 1111 | + ('src/sparql/*', ALWAYS), |
| 1112 | + ('docs/odk-workflows/*', ALWAYS) |
| 1113 | + ] |
1099 | 1114 | if 'github_actions' in project.ci: |
1100 | 1115 | for workflow in ['qc', 'diff', 'release-diff']: |
1101 | 1116 | if workflow in project.workflows: |
1102 | | - policies['.github/workflows/' + workflow + '.yml'] = ALWAYS |
| 1117 | + policies.append(('.github/workflows/' + workflow + '.yml', ALWAYS)) |
1103 | 1118 | if project.documentation is not None and 'docs' in project.workflows: |
1104 | | - policies['github/workflows/docs.yml'] = ALWAYS |
1105 | | - # HACK: For the odk-workflows documentation directory, |
1106 | | - # we want to clean up that directory completely. The |
1107 | | - # policy dictionary only works on files, so to force |
1108 | | - # reinstalling the entire directory (also removing any |
1109 | | - # non-standard file along the way), we forcefully |
1110 | | - # remove the directory. |
1111 | | - if project.documentation is not None: |
1112 | | - shutil.rmtree('../../docs/odk-workflows') |
| 1119 | + policies.append(('.github/workflows/docs.yml', ALWAYS)) |
1113 | 1120 |
|
1114 | 1121 | # Proceed with template instantiation, using the policies |
1115 | 1122 | # declared above. We instantiate directly at the root of |
|
0 commit comments