* Package roots a build config declares, relative to the repo root. Only the * keys that mean "packages live here" are read, each in its own table: * setuptools `[tool.setuptools.packages.find] where` and * `[tool.setuptools.package-dir] "" =` (or the inline `package-dir` of * `[tool.setuptools]
(context: ResolutionContext, service: string)
| 755 | * case, which the `__init__.py` chain can't see either. |
| 756 | */ |
| 757 | function declaredPythonRoots(context: ResolutionContext, service: string): string[] { |
| 758 | const out: string[] = []; |
| 759 | const add = (raw: string): void => { |
| 760 | const dir = raw.trim().replace(/^["']|["']$/g, '').replace(/^\.(?:\/|$)/, '').replace(/\/+$/, ''); |
| 761 | if (!dir || dir.startsWith('..') || path.isAbsolute(dir)) return; |
| 762 | out.push(joinRel(service, dir)); |
| 763 | }; |
| 764 | const toml = context.readFile(joinRel(service, 'pyproject.toml')); |
| 765 | if (toml) { |
| 766 | for (const [table, body] of iniTables(toml)) { |
| 767 | if (table === 'tool.setuptools.packages.find') { |
| 768 | const m = body.match(/^\s*where\s*=\s*\[([^\]]*)\]/m); |
| 769 | if (m) for (const item of m[1]!.split(',')) add(item); |
| 770 | } else if (table === 'tool.setuptools.package-dir') { |
| 771 | const m = body.match(/^\s*["']{2}\s*=\s*["']([^"']+)["']/m); |
| 772 | if (m) add(m[1]!); |
| 773 | } else if (table === 'tool.setuptools') { |
| 774 | const m = body.match(/package-dir\s*=\s*\{[^}]*?["']{2}\s*=\s*["']([^"']+)["']/); |
| 775 | if (m) add(m[1]!); |
| 776 | } else if (table === 'tool.poetry') { |
| 777 | const pkgs = body.match(/^\s*packages\s*=\s*\[([\s\S]*?)\]\s*$/m); |
| 778 | if (pkgs) for (const m of pkgs[1]!.matchAll(/\bfrom\s*=\s*["']([^"']+)["']/g)) add(m[1]!); |
| 779 | } else if (table === 'tool.hatch.build.targets.wheel') { |
| 780 | const pkgs = body.match(/^\s*packages\s*=\s*\[([^\]]*)\]/m); |
| 781 | if (pkgs) { |
| 782 | for (const item of pkgs[1]!.split(',')) { |
| 783 | const p = item.trim().replace(/^["']|["']$/g, ''); |
| 784 | if (p.includes('/')) add(p.slice(0, p.lastIndexOf('/'))); |
| 785 | } |
| 786 | } |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | const cfg = context.readFile(joinRel(service, 'setup.cfg')); |
| 791 | if (cfg) { |
| 792 | for (const [table, body] of iniTables(cfg)) { |
| 793 | if (table === 'options') { |
| 794 | const m = body.match(/^\s*package_dir\s*=\s*(?:\n\s+)?=\s*(\S+)\s*$/m); |
| 795 | if (m) add(m[1]!); |
| 796 | } else if (table === 'options.packages.find') { |
| 797 | const m = body.match(/^\s*where\s*=\s*(\S+)\s*$/m); |
| 798 | if (m) add(m[1]!); |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | return out; |
| 803 | } |
| 804 | |
| 805 | /** `[table]` sections of a TOML / INI file, with the body under each. */ |
| 806 | function iniTables(content: string): Array<[string, string]> { |
no test coverage detected