|
| 1 | +import os |
| 2 | +from collections import OrderedDict |
| 3 | + |
| 4 | +from cfbs.utils import ( |
| 5 | + dict_diff, |
| 6 | + read_json, |
| 7 | + CFBSExitError, |
| 8 | + write_json, |
| 9 | + version_as_comparable_list, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +def check_download_matches_git(versions): |
| 14 | + """Check that the downloadable files match the git files. |
| 15 | +
|
| 16 | + This can be used to monitor / detect if something has been changed, accidentally or maliciously. |
| 17 | +
|
| 18 | + Generates a `differences-*.txt` file for each version. |
| 19 | + """ |
| 20 | + assert os.path.isfile("versions.json") |
| 21 | + assert os.path.isfile("versions-git.json") |
| 22 | + |
| 23 | + download_versions_dict = read_json("versions.json") |
| 24 | + git_versions_dict = read_json("versions-git.json") |
| 25 | + |
| 26 | + assert download_versions_dict is not None |
| 27 | + assert git_versions_dict is not None |
| 28 | + |
| 29 | + diffs_dict = {"differences": {}} |
| 30 | + |
| 31 | + nonmatching_versions = [] |
| 32 | + extraneous_count = 0 |
| 33 | + differing_count = 0 |
| 34 | + |
| 35 | + for version in versions: |
| 36 | + dl_version_files_dict = download_versions_dict["versions"][version] |
| 37 | + git_version_files_dict = git_versions_dict["versions"][version] |
| 38 | + |
| 39 | + # normalize downloaded version dictionary filepaths |
| 40 | + # necessary because the downloaded version and git version dictionaries have filepaths of different forms |
| 41 | + new_download_dict = {} |
| 42 | + for key, value in dl_version_files_dict.items(): |
| 43 | + if key.startswith("masterfiles/"): |
| 44 | + key = key[12:] |
| 45 | + new_download_dict[key] = value |
| 46 | + dl_version_files_dict = new_download_dict |
| 47 | + |
| 48 | + version_diffs_dict = {} |
| 49 | + version_diffs_dict["files_only_in_downloads"] = [] |
| 50 | + version_diffs_dict["files_only_in_git"] = [] |
| 51 | + version_diffs_dict["files_with_different_content"] = [] |
| 52 | + |
| 53 | + only_dl, only_git, value_diff = dict_diff( |
| 54 | + dl_version_files_dict, git_version_files_dict |
| 55 | + ) |
| 56 | + |
| 57 | + for filepath in only_dl: |
| 58 | + version_diffs_dict["files_only_in_downloads"].append(filepath) |
| 59 | + for filepath in only_git: |
| 60 | + version_diffs_dict["files_only_in_git"].append(filepath) |
| 61 | + for filepath, _, _ in value_diff: |
| 62 | + version_diffs_dict["files_with_different_content"].append(filepath) |
| 63 | + |
| 64 | + diffs_dict["differences"][version] = version_diffs_dict |
| 65 | + |
| 66 | + if len(only_dl) > 0 or len(value_diff) > 0: |
| 67 | + nonmatching_versions.append(version) |
| 68 | + extraneous_count += len(only_dl) |
| 69 | + differing_count += len(value_diff) |
| 70 | + |
| 71 | + nonmatching_versions.sort(key=lambda v: version_as_comparable_list(v), reverse=True) |
| 72 | + |
| 73 | + # fully sort differences.json: |
| 74 | + working_dict = diffs_dict["differences"] |
| 75 | + # sort filepaths of each version, alphabetically |
| 76 | + for k in working_dict.keys(): |
| 77 | + working_dict[k]["files_only_in_downloads"].sort() |
| 78 | + working_dict[k]["files_only_in_git"].sort() |
| 79 | + working_dict[k]["files_with_different_content"].sort() |
| 80 | + # sort version numbers, in decreasing order |
| 81 | + diffs_dict["differences"] = OrderedDict( |
| 82 | + sorted( |
| 83 | + working_dict.items(), |
| 84 | + key=lambda p: version_as_comparable_list(p[0]), |
| 85 | + reverse=True, |
| 86 | + ) |
| 87 | + ) |
| 88 | + |
| 89 | + write_json("differences.json", diffs_dict) |
| 90 | + |
| 91 | + if len(nonmatching_versions) > 0: |
| 92 | + raise CFBSExitError( |
| 93 | + "The masterfiles downloaded from github.com and cfengine.com do not match - found " |
| 94 | + + str(extraneous_count) |
| 95 | + + " extraneous file" |
| 96 | + + ("" if extraneous_count == 1 else "s") |
| 97 | + + " and " |
| 98 | + + str(differing_count) |
| 99 | + + " differing file" |
| 100 | + + ("" if differing_count == 1 else "s") |
| 101 | + + " across " |
| 102 | + + str(len(nonmatching_versions)) |
| 103 | + + " version" |
| 104 | + + ("" if len(nonmatching_versions) == 1 else "s") |
| 105 | + + " (" |
| 106 | + + ", ".join(nonmatching_versions) |
| 107 | + + "). See ./differences.json" |
| 108 | + ) |
0 commit comments