| 1 | from __future__ import print_function
 | 
| 2 | """
 | 
| 3 | readlink.py - Minimal implementation of readlink -f, e.g. for OS X.
 | 
| 4 | """
 | 
| 5 | 
 | 
| 6 | import libc
 | 
| 7 | from frontend import args
 | 
| 8 | from frontend import flag_spec
 | 
| 9 | from mycpp.mylib import print_stderr
 | 
| 10 | 
 | 
| 11 | from typing import List
 | 
| 12 | 
 | 
| 13 | SPEC = flag_spec.FlagSpec('readlink')
 | 
| 14 | SPEC.ShortFlag('-f')
 | 
| 15 | 
 | 
| 16 | 
 | 
| 17 | def main(argv):
 | 
| 18 |     # type: (List[str]) -> int
 | 
| 19 |     arg_r = args.Reader(argv)
 | 
| 20 |     arg_r.Next()  # skip argv[0]
 | 
| 21 |     arg = args.Parse(SPEC, arg_r)
 | 
| 22 | 
 | 
| 23 |     if not arg.f:
 | 
| 24 |         print_stderr("readlink: -f must be passed")
 | 
| 25 |         return 1
 | 
| 26 |     for path in arg_r.Rest():
 | 
| 27 |         res = libc.realpath(path)
 | 
| 28 |         if res is None:
 | 
| 29 |             return 1
 | 
| 30 |         print(res)
 | 
| 31 |     return 0
 |