MCPcopy Index your code
hub / github.com/python/cpython / get_prog

Function get_prog

Tools/c-analyzer/c_common/scriptutil.py:15–47  ·  view source on GitHub ↗
(spec=None, *, absolute=False, allowsuffix=True)

Source from the content-addressed store, hash-verified

13
14
15def 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
50def _find_script():

Callers 3

parse_argsFunction · 0.90
parse_argsFunction · 0.90
parse_argsFunction · 0.90

Calls 6

_find_scriptFunction · 0.85
_is_standaloneFunction · 0.85
normcaseMethod · 0.80
endswithMethod · 0.45
basenameMethod · 0.45
abspathMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…