(spec=None, *, absolute=False, allowsuffix=True)
| 13 | |
| 14 | |
| 15 | def get_prog(spec=None, *, absolute=False, allowsuffix=True): |
| 16 | if spec is None: |
| 17 | _, spec = _find_script() |
| 18 | # This is more natural for prog than __file__ would be. |
| 19 | filename = sys.argv[0] |
| 20 | elif isinstance(spec, str): |
| 21 | filename = os.path.normpath(spec) |
| 22 | spec = None |
| 23 | else: |
| 24 | filename = spec.origin |
| 25 | if _is_standalone(filename): |
| 26 | # Check if "installed". |
| 27 | if allowsuffix or not filename.endswith('.py'): |
| 28 | basename = os.path.basename(filename) |
| 29 | found = shutil.which(basename) |
| 30 | if found: |
| 31 | script = os.path.abspath(filename) |
| 32 | found = os.path.abspath(found) |
| 33 | if os.path.normcase(script) == os.path.normcase(found): |
| 34 | return basename |
| 35 | # It is only "standalone". |
| 36 | if absolute: |
| 37 | filename = os.path.abspath(filename) |
| 38 | return filename |
| 39 | elif spec is not None: |
| 40 | module = spec.name |
| 41 | if module.endswith('.__main__'): |
| 42 | module = module[:-9] |
| 43 | return f'{sys.executable} -m {module}' |
| 44 | else: |
| 45 | if absolute: |
| 46 | filename = os.path.abspath(filename) |
| 47 | return f'{sys.executable} {filename}' |
| 48 | |
| 49 | |
| 50 | def _find_script(): |
no test coverage detected
searching dependent graphs…