|
| 1 | +import concurrent.futures |
| 2 | +import os |
| 3 | +import sys |
| 4 | + |
| 5 | +import frontmatter |
| 6 | +import pkg_resources |
| 7 | +import yaml |
| 8 | + |
| 9 | + |
| 10 | +def process_plugin_file(path): |
| 11 | + output = [] |
| 12 | + |
| 13 | + def out(line, prefix="", *args, **kwargs): |
| 14 | + output.append("{}{}".format(prefix, line)) |
| 15 | + |
| 16 | + overlay = {} |
| 17 | + |
| 18 | + data = frontmatter.load(path) |
| 19 | + plugin_id = data["id"] |
| 20 | + |
| 21 | + out("Processing plugin {} at path {}".format(plugin_id, path)) |
| 22 | + |
| 23 | + if data.get("compatibility", {}).get("python"): |
| 24 | + pycompat = data["compatibility"]["python"] |
| 25 | + s = pkg_resources.Requirement.parse("Python" + pycompat) |
| 26 | + if "2.7" not in s: |
| 27 | + out( |
| 28 | + "Plugin {} is not Python 2 compatible, disable update checks in the field".format( |
| 29 | + plugin_id |
| 30 | + ) |
| 31 | + ) |
| 32 | + overlay = {plugin_id: {"enabled": False, "compatible": False}} |
| 33 | + |
| 34 | + return "\n".join(output), overlay |
| 35 | + |
| 36 | + |
| 37 | +if __name__ == "__main__": |
| 38 | + executor = concurrent.futures.ThreadPoolExecutor(max_workers=5) |
| 39 | + |
| 40 | + filtered = None |
| 41 | + if len(sys.argv) > 1: |
| 42 | + filtered = sys.argv[1:] |
| 43 | + |
| 44 | + futures_to_name = dict() |
| 45 | + |
| 46 | + overlays_path = os.path.join( |
| 47 | + os.path.dirname(os.path.abspath(__file__)), |
| 48 | + "..", |
| 49 | + "..", |
| 50 | + "_data", |
| 51 | + "update_check_overlays_py2.yaml", |
| 52 | + ) |
| 53 | + with open(overlays_path, "r") as f: |
| 54 | + data = yaml.safe_load(f) |
| 55 | + assert isinstance(data, dict) |
| 56 | + |
| 57 | + plugin_dir = os.path.join( |
| 58 | + os.path.dirname(os.path.abspath(__file__)), "..", "..", "_plugins" |
| 59 | + ) |
| 60 | + with os.scandir(plugin_dir) as it: |
| 61 | + for entry in it: |
| 62 | + if not entry.is_file() or not entry.name.endswith(".md"): |
| 63 | + continue |
| 64 | + if filtered and entry.name[:-3] not in filtered: |
| 65 | + continue |
| 66 | + if entry.name[:-3] in data: |
| 67 | + continue |
| 68 | + future = executor.submit(process_plugin_file, entry.path) |
| 69 | + futures_to_name[future] = entry.name |
| 70 | + |
| 71 | + overlays = {} |
| 72 | + for future in concurrent.futures.as_completed(futures_to_name): |
| 73 | + name = futures_to_name[future] |
| 74 | + try: |
| 75 | + output, result = future.result() |
| 76 | + overlays.update(result) |
| 77 | + |
| 78 | + print(output) |
| 79 | + print("") |
| 80 | + except Exception as exc: |
| 81 | + print("{} generated an exception: {}".format(name, exc)) |
| 82 | + |
| 83 | + if overlays: |
| 84 | + # there were overlays added, write them to the data file now |
| 85 | + print("Adding {} new overlays to {}...".format(len(overlays), overlays_path)) |
| 86 | + |
| 87 | + data.update(overlays) |
| 88 | + with open(overlays_path, "w") as f: |
| 89 | + yaml.safe_dump(data, f) |
| 90 | + |
| 91 | + print("Overlays added.") |
0 commit comments