|
| 1 | +import json |
| 2 | +import re |
| 3 | +import sys |
| 4 | + |
| 5 | +from collections import OrderedDict |
| 6 | +from pathlib import Path |
| 7 | +from urllib.request import Request, urlopen |
| 8 | + |
| 9 | +REPO = Path(__file__).absolute().parent.parent |
| 10 | +sys.path.append(str(REPO / "src")) |
| 11 | + |
| 12 | +from manage.urlutils import IndexDownloader |
| 13 | +from manage.tagutils import CompanyTag, tag_or_range |
| 14 | +from manage.verutils import Version |
| 15 | + |
| 16 | + |
| 17 | +def usage(): |
| 18 | + print("Usage: repartition-index.py [-i options <FILENAME> ...] [options <OUTPUT> ...]") |
| 19 | + print() |
| 20 | + print(" --windows-default Implies default output files and configurations.") |
| 21 | + print() |
| 22 | + print(" -i <FILENAME> One or more files or URLs to read existing entries from.") |
| 23 | + print(" -i -n/--no-recurse Do not follow 'next' info") |
| 24 | + print("If no files are provided, uses the current online index") |
| 25 | + print() |
| 26 | + print(" <OUTPUT> Filename to write entries into") |
| 27 | + print(" -d/--allow-dup Include entries written in previous outputs") |
| 28 | + print(" --only-dup Only include entries written in previous outputs") |
| 29 | + print(" --pre Include entries marked as prereleases") |
| 30 | + print(" -t/--tag TAG Include only the specified tags (comma-separated)") |
| 31 | + print(" -r/--range RANGE Include only the specified range (comma-separated)") |
| 32 | + print(" --latest-micro Include only the latest x.y.z version") |
| 33 | + print() |
| 34 | + print("An output of 'nul' is permitted to drop entries.") |
| 35 | + print("Providing the same inputs and outputs is permitted, as all inputs are read") |
| 36 | + print("before any outputs are written.") |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | + |
| 40 | +class ReadFile: |
| 41 | + def __init__(self): |
| 42 | + self.source = None |
| 43 | + self.recurse = True |
| 44 | + |
| 45 | + def add_arg(self, arg): |
| 46 | + if arg[:1] != "-": |
| 47 | + self.source = arg |
| 48 | + return True |
| 49 | + if arg in ("-n", "--no-recurse"): |
| 50 | + self.recurse = False |
| 51 | + return False |
| 52 | + raise ValueError("Unknown argument: " + arg) |
| 53 | + |
| 54 | + def execute(self, versions, context): |
| 55 | + for _, data in IndexDownloader(self.source, lambda *a: a): |
| 56 | + versions.extend(data["versions"]) |
| 57 | + if not self.recurse: |
| 58 | + break |
| 59 | + |
| 60 | + |
| 61 | +class SortVersions: |
| 62 | + def __init__(self): |
| 63 | + pass |
| 64 | + |
| 65 | + def add_arg(self, arg): |
| 66 | + raise ValueError("Unknown argument: " + arg) |
| 67 | + |
| 68 | + def _number_sortkey(self, k): |
| 69 | + bits = [] |
| 70 | + for n in re.split(r"(\d+)", k): |
| 71 | + try: |
| 72 | + bits.append(f"{int(n):020}") |
| 73 | + except ValueError: |
| 74 | + bits.append(n) |
| 75 | + return tuple(bits) |
| 76 | + |
| 77 | + def _sort_key(self, v): |
| 78 | + from manage.tagutils import _CompanyKey, _DescendingVersion |
| 79 | + return ( |
| 80 | + _DescendingVersion(v["sort-version"]), |
| 81 | + _CompanyKey(v["company"]), |
| 82 | + self._number_sortkey(v["id"]), |
| 83 | + ) |
| 84 | + |
| 85 | + def execute(self, versions, context): |
| 86 | + versions.sort(key=self._sort_key) |
| 87 | + print("Processing {} entries".format(len(versions))) |
| 88 | + |
| 89 | + |
| 90 | +class SplitToFile: |
| 91 | + def __init__(self): |
| 92 | + self.target = None |
| 93 | + self.allow_dup = False |
| 94 | + self.only_dup = False |
| 95 | + self.pre = False |
| 96 | + self.tag_or_range = None |
| 97 | + self._expect_tag_or_range = False |
| 98 | + self.latest_micro = False |
| 99 | + |
| 100 | + def add_arg(self, arg): |
| 101 | + if arg[:1] != "-": |
| 102 | + if self._expect_tag_or_range: |
| 103 | + self.tag_or_range = tag_or_range(arg) |
| 104 | + self._expect_tag_or_range = False |
| 105 | + return False |
| 106 | + self.target = arg |
| 107 | + return True |
| 108 | + if arg in ("-d", "--allow-dup"): |
| 109 | + self.allow_dup = True |
| 110 | + return False |
| 111 | + if arg == "--only-dup": |
| 112 | + self.allow_dup = True |
| 113 | + self.only_dup = True |
| 114 | + return False |
| 115 | + if arg == "--pre": |
| 116 | + self.pre = True |
| 117 | + return False |
| 118 | + if arg in ("-t", "--tag", "-r", "--range"): |
| 119 | + self._expect_tag_or_range = True |
| 120 | + return False |
| 121 | + if arg == "--latest-micro": |
| 122 | + self.latest_micro = True |
| 123 | + return False |
| 124 | + raise ValueError("Unknown argument: " + arg) |
| 125 | + |
| 126 | + def execute(self, versions, context): |
| 127 | + written = context.setdefault("written", set()) |
| 128 | + written_now = set() |
| 129 | + outputs = context.setdefault("outputs", {}) |
| 130 | + if self.target != "nul": |
| 131 | + try: |
| 132 | + output = outputs[self.target] |
| 133 | + except KeyError: |
| 134 | + context.setdefault("output_order", []).append(self.target) |
| 135 | + output = outputs.setdefault(self.target, []) |
| 136 | + else: |
| 137 | + # Write to a list that'll be forgotten |
| 138 | + output = [] |
| 139 | + |
| 140 | + latest_micro_skip = set() |
| 141 | + |
| 142 | + for i in versions: |
| 143 | + k = i["id"].casefold(), i["sort-version"].casefold() |
| 144 | + v = Version(i["sort-version"]) |
| 145 | + if self.only_dup and k not in written_now: |
| 146 | + written_now.add(k) |
| 147 | + continue |
| 148 | + if not self.allow_dup and k in written: |
| 149 | + continue |
| 150 | + if not self.pre and v.is_prerelease: |
| 151 | + continue |
| 152 | + if self.tag_or_range and not any( |
| 153 | + self.tag_or_range.satisfied_by(CompanyTag(i["company"], t)) |
| 154 | + for t in i["install-for"] |
| 155 | + ): |
| 156 | + continue |
| 157 | + if self.latest_micro: |
| 158 | + k2 = i["id"].casefold(), v.to_python_style(2, with_dev=False) |
| 159 | + if k2 in latest_micro_skip: |
| 160 | + continue |
| 161 | + latest_micro_skip.add(k2) |
| 162 | + written.add(k) |
| 163 | + output.append(i) |
| 164 | + |
| 165 | + |
| 166 | +class WriteFiles: |
| 167 | + def __init__(self): |
| 168 | + self.indent = None |
| 169 | + |
| 170 | + def add_arg(self, arg): |
| 171 | + if arg == "-w-indent": |
| 172 | + self.indent = 4 |
| 173 | + return False |
| 174 | + if arg == "-w-indent1": |
| 175 | + self.indent = 1 |
| 176 | + return False |
| 177 | + raise ValueError("Unknown argument: " + arg) |
| 178 | + |
| 179 | + def execute(self, versions, context): |
| 180 | + outputs = context.get("outputs") or {} |
| 181 | + output_order = context.get("output_order", []) |
| 182 | + for target, next_target in zip(output_order, [*output_order[1:], None]): |
| 183 | + data = { |
| 184 | + "versions": outputs[target] |
| 185 | + } |
| 186 | + if next_target: |
| 187 | + data["next"] = next_target |
| 188 | + with open(target, "w", encoding="utf-8") as f: |
| 189 | + json.dump(data, f, indent=self.indent) |
| 190 | + print("Wrote {} ({} entries, {} bytes)".format( |
| 191 | + target, len(data["versions"]), Path(target).stat().st_size |
| 192 | + )) |
| 193 | + |
| 194 | + |
| 195 | +def parse_cli(args): |
| 196 | + plan_read = [] |
| 197 | + plan_split = [] |
| 198 | + sort = SortVersions() |
| 199 | + action = None |
| 200 | + write = WriteFiles() |
| 201 | + for a in args: |
| 202 | + if a == "--windows-default": |
| 203 | + print("Using equivalent of: --pre --latest-micro -r >=3.11.0 index-windows.json") |
| 204 | + print(" --pre -r >=3.11.0 index-windows-recent.json") |
| 205 | + print(" index-windows-legacy.json") |
| 206 | + plan_split = [SplitToFile(), SplitToFile(), SplitToFile()] |
| 207 | + plan_split[0].target = "index-windows.json" |
| 208 | + plan_split[1].target = "index-windows-recent.json" |
| 209 | + plan_split[2].target = "index-windows-legacy.json" |
| 210 | + plan_split[0].pre = plan_split[1].pre = plan_split[2].pre = True |
| 211 | + plan_split[0].latest_micro = True |
| 212 | + plan_split[0].tag_or_range = tag_or_range(">=3.11.0") |
| 213 | + plan_split[1].tag_or_range = tag_or_range(">=3.11.0") |
| 214 | + elif a == "-i": |
| 215 | + action = ReadFile() |
| 216 | + plan_read.append(action) |
| 217 | + elif a.startswith("-s-"): |
| 218 | + sort.add_arg(a) |
| 219 | + elif a.startswith("-w-"): |
| 220 | + write.add_arg(a) |
| 221 | + else: |
| 222 | + try: |
| 223 | + if action is None: |
| 224 | + action = SplitToFile() |
| 225 | + plan_split.append(action) |
| 226 | + if action.add_arg(a): |
| 227 | + action = None |
| 228 | + continue |
| 229 | + except ValueError as ex: |
| 230 | + print(ex) |
| 231 | + usage() |
| 232 | + if not plan_read: |
| 233 | + action = ReadFile() |
| 234 | + action.source = "https://www.python.org/ftp/python/index-windows.json" |
| 235 | + plan_read.append(action) |
| 236 | + if not plan_split: |
| 237 | + print("No outputs specified") |
| 238 | + print(args) |
| 239 | + usage() |
| 240 | + return [*plan_read, sort, *plan_split, write] |
| 241 | + |
| 242 | + |
| 243 | +if __name__ == "__main__": |
| 244 | + plan = parse_cli(sys.argv[1:]) |
| 245 | + VERSIONS = [] |
| 246 | + CONTEXT = {} |
| 247 | + for p in plan: |
| 248 | + p.execute(VERSIONS, CONTEXT) |
| 249 | + |
0 commit comments