summaryrefslogtreecommitdiffhomepage
path: root/libs/srt_tools/srt
blob: c8c21b545a564ef234b2b921bf4dbcb540c7cd98 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python

import os
import sys
import errno


SRT_BIN_PREFIX = "srt-"


def find_srt_commands_in_path():
    paths = os.environ.get("PATH", "").split(os.pathsep)

    for path in paths:
        try:
            path_files = os.listdir(path)
        except OSError as thrown_exc:
            if thrown_exc.errno in (errno.ENOENT, errno.ENOTDIR):
                continue
            else:
                raise

        for path_file in path_files:
            if path_file.startswith(SRT_BIN_PREFIX):
                yield path_file[len(SRT_BIN_PREFIX) :]


def show_help():
    print(
        "Available commands "
        "(pass --help to a specific command for usage information):\n"
    )
    commands = sorted(set(find_srt_commands_in_path()))
    for command in commands:
        print("- {}".format(command))


def main():
    if len(sys.argv) < 2 or sys.argv[1].startswith("-"):
        show_help()
        sys.exit(0)

    command = sys.argv[1]

    available_commands = find_srt_commands_in_path()

    if command not in available_commands:
        print('Unknown command: "{}"\n'.format(command))
        show_help()
        sys.exit(1)

    real_command = SRT_BIN_PREFIX + command
    os.execvp(real_command, [real_command] + sys.argv[2:])


if __name__ == "__main__":  # pragma: no cover
    main()