|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | import abc |
| 4 | +import hashlib |
4 | 5 | import json |
5 | 6 | import logging |
6 | 7 | import os |
@@ -206,26 +207,31 @@ def _parse_script(self, script): |
206 | 207 | attr=script.get("object", self.const.default_script_attr) |
207 | 208 | ) |
208 | 209 | elif "path" in script: |
209 | | - yield from self._iter_module_scripts( |
210 | | - path=script.get("path"), |
211 | | - prefix=f"{script.get("module").rstrip(".")}.", |
212 | | - object=script.get("object", self.const.default_script_object), |
213 | | - attr=script.get("object", self.const.default_script_attr), |
214 | | - ) |
| 210 | + module = script.get("module").rstrip(".") |
| 211 | + path = script.get("path") |
| 212 | + root_path = os.path.join(path, "__init__.py") |
| 213 | + if os.path.exists(root_path): |
| 214 | + m = hashlib.md5() |
| 215 | + m.update(root_path.encode()) |
| 216 | + yield ModuleEntryPoint( |
| 217 | + name=f"module-{m.hexdigest()}", |
| 218 | + module=module, |
| 219 | + ) |
| 220 | + yield from self._iter_module_scripts( |
| 221 | + path=script.get("path"), |
| 222 | + prefix=f"{module}.", |
| 223 | + object=script.get("object", self.const.default_script_object), |
| 224 | + attr=script.get("object", self.const.default_script_attr), |
| 225 | + ) |
215 | 226 |
|
216 | 227 | def _iter_module_scripts(self, path, prefix, object, attr, parents=None): |
217 | 228 | for module_info in pkgutil.iter_modules([path]): |
218 | 229 | if module_info.ispkg: |
219 | 230 | spec = module_info.module_finder.find_spec(module_info.name) |
220 | 231 | module = module_from_spec(spec) |
221 | 232 | spec.loader.exec_module(module) |
222 | | - name = getattr(module, self.const.module_command_key, module_info.name) |
223 | 233 | items = list(parents or []) |
224 | 234 | items.append(getattr(module, self.const.module_command_key, module_info.name)) |
225 | | - yield ModuleEntryPoint( |
226 | | - name=name, |
227 | | - module=f"{prefix}{module_info.name}", |
228 | | - ) |
229 | 235 | yield from self._iter_module_scripts( |
230 | 236 | path=os.path.join(path, module_info.name), |
231 | 237 | prefix=f"{prefix}{module_info.name}.", |
@@ -273,8 +279,8 @@ def finalize_distribution_options(dist: setuptools.Distribution) -> None: |
273 | 279 |
|
274 | 280 | scripts = [{ |
275 | 281 | "path": os.path.expanduser("~/Projects/linktools/src/linktools/cli/commands"), |
276 | | - "module": "linktools.cli.commands.common", |
| 282 | + "module": "linktools.cli.commands", |
277 | 283 | }] |
278 | 284 | print([ep.as_script() for ep in context._parse_scripts(scripts)]) |
279 | | - print([ep.as_script() for ep in context._parse_scripts(scripts) if isinstance(ep, ScriptEntryPoint)]) |
280 | | - print([ep.as_script() for ep in context._parse_scripts(scripts) if not isinstance(ep, SubScriptEntryPoint)]) |
| 285 | + # print([ep.as_script() for ep in context._parse_scripts(scripts) if isinstance(ep, ScriptEntryPoint)]) |
| 286 | + # print([ep.as_script() for ep in context._parse_scripts(scripts) if not isinstance(ep, SubScriptEntryPoint)]) |
0 commit comments