()
| 187 | |
| 188 | |
| 189 | def main(): |
| 190 | # Step 1: Get all OS contants that we do want from upstream |
| 191 | wanted_consts = consts_from_url( |
| 192 | f"https://docs.python.org/{CPYTHON_VERSION}/library/os.html", |
| 193 | # TODO: Exclude matches if they have `(` after (those are functions) |
| 194 | pattern=re.compile(r"\bos\.(_*[A-Z]+(?:_+[A-Z]+)*_*)"), |
| 195 | ) |
| 196 | |
| 197 | # Step 2: build dict of what consts are available per cfg. `cfg -> {consts}` |
| 198 | available = collections.defaultdict(set) |
| 199 | for target in TARGETS: |
| 200 | consts = set() |
| 201 | for source in target.sources: |
| 202 | consts |= consts_from_url(source) |
| 203 | |
| 204 | for cfg in target.cfgs: |
| 205 | available[cfg] |= consts |
| 206 | |
| 207 | # Step 3: Keep only the "wanted" consts. Build a groupped mapping of `{cfgs} -> {consts}' |
| 208 | groups = collections.defaultdict(set) |
| 209 | available_items = available.items() |
| 210 | for wanted_const in wanted_consts: |
| 211 | cfgs = frozenset( |
| 212 | cfg for cfg, consts in available_items if wanted_const in consts |
| 213 | ) |
| 214 | if not cfgs: |
| 215 | # We have no cfgs for a wanted const :/ |
| 216 | continue |
| 217 | |
| 218 | groups[cfgs].add(wanted_const) |
| 219 | |
| 220 | # Step 4: Build output |
| 221 | output = "" |
| 222 | for cfgs, consts in sorted(groups.items(), key=lambda t: (len(t[0]), sorted(t[0]))): |
| 223 | target = next((target for target in TARGETS if target.cfgs == cfgs), None) |
| 224 | if target: |
| 225 | # If we found an exact target. Add its "extras" as-is |
| 226 | consts |= target.extras |
| 227 | |
| 228 | cfgs_inner = ",".join(sorted(map(str, cfgs))) |
| 229 | |
| 230 | if len(cfgs) >= 2: |
| 231 | cfgs_rust = f"#[cfg(any({cfgs_inner}))]" |
| 232 | else: |
| 233 | cfgs_rust = f"#[cfg({cfgs_inner})]" |
| 234 | |
| 235 | imports = ",".join(consts) |
| 236 | entry = f""" |
| 237 | {cfgs_rust} |
| 238 | #[pyattr] |
| 239 | use libc::{{{imports}}}; |
| 240 | """.strip() |
| 241 | |
| 242 | output += f"{entry}\n\n" |
| 243 | |
| 244 | print(rustfmt(output)) |
| 245 | |
| 246 |
no test coverage detected