File: __main__.py

package info (click to toggle)
python-macholib 1.16.2%2Bds0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 520 kB
  • sloc: python: 4,279; makefile: 126; sh: 10
file content (80 lines) | stat: -rw-r--r-- 1,879 bytes parent folder | download | duplicates (4)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import absolute_import, print_function

import os
import sys

from macholib import macho_dump, macho_standalone
from macholib.util import is_platform_file

gCommand = None


def check_file(fp, path, callback):
    if not os.path.exists(path):
        print("%s: %s: No such file or directory" % (gCommand, path), file=sys.stderr)
        return 1

    try:
        is_plat = is_platform_file(path)

    except IOError as msg:
        print("%s: %s: %s" % (gCommand, path, msg), file=sys.stderr)
        return 1

    else:
        if is_plat:
            callback(fp, path)
    return 0


def walk_tree(callback, paths):
    err = 0

    for base in paths:
        if os.path.isdir(base):
            for root, _dirs, files in os.walk(base):
                for fn in files:
                    err |= check_file(sys.stdout, os.path.join(root, fn), callback)
        else:
            err |= check_file(sys.stdout, base, callback)

    return err


def print_usage(fp):
    print("Usage:", file=fp)
    print("  python -mmacholib [help|--help]", file=fp)
    print("  python -mmacholib dump FILE ...", file=fp)
    print("  python -mmacholib find DIR ...", file=fp)
    print("  python -mmacholib standalone DIR ...", file=fp)


def main():
    global gCommand
    if len(sys.argv) < 3:
        print_usage(sys.stderr)
        sys.exit(1)

    gCommand = sys.argv[1]

    if gCommand == "dump":
        walk_tree(macho_dump.print_file, sys.argv[2:])

    elif gCommand == "find":
        walk_tree(lambda fp, path: print(path, file=fp), sys.argv[2:])

    elif gCommand == "standalone":
        for dn in sys.argv[2:]:
            macho_standalone.standaloneApp(dn)

    elif gCommand in ("help", "--help"):
        print_usage(sys.stdout)
        sys.exit(0)

    else:
        print_usage(sys.stderr)
        sys.exit(1)


if __name__ == "__main__":
    main()