File: which.py

package info (click to toggle)
datalad 1.1.5-2.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,140 kB
  • sloc: python: 69,392; sh: 1,521; makefile: 220
file content (25 lines) | stat: -rw-r--r-- 813 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/env python3

import os
import os.path as op
import subprocess
import sys

if __name__ == '__main__':
    cmd = sys.argv[1]
    extra = sys.argv[2:]
    for path in os.environ['PATH'].split(os.pathsep):
        for ext in '', '.exe', '.bat', '.com':
            exe = op.join(path, cmd + ext)
            # print(exe)
            if op.lexists(exe):
                if extra:
                    r = subprocess.run([exe] + extra, capture_output=True, check=True)
                    print(exe, r.returncode == 0 and "ok" or "failed")
                    for o in "stdout", "stderr":
                        out = getattr(r, o)
                        if out:
                            print(f'{o}:')
                            print(out.decode())
                else:
                    print(exe)