view tests/ls-l.py @ 51721:ed28085827ec

typing: explicitly type some `mercurial.util` eol code to avoid @overload Unlike the previous commit, this makes a material difference in the generated stub file- the `pycompat.identity()` aliases generated an @overload like this: @overload def fromnativeeol(a: _T0) -> _T0: ... ... which might fail to detect a bad argument, like str. This drops the @overload for the 3 related methods, so there's a single definition for each. The `typelib.BinaryIO_Proxy` is used for subclassing (the same as was done in 8147abc05794), so that it is a `BinaryIO` type during type checking, but still inherits `object` at runtime. That way, we don't need to implement unused abstract methods.
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 19 Jul 2024 16:49:46 -0400
parents 6000f5b25c9b
children
line wrap: on
line source

#!/usr/bin/env python3

# like ls -l, but do not print date, user, or non-common mode bit, to avoid
# using globs in tests.

import os
import stat
import sys


def modestr(st):
    mode = st.st_mode
    result = ''
    if mode & stat.S_IFDIR:
        result += 'd'
    else:
        result += '-'
    for owner in ['USR', 'GRP', 'OTH']:
        for action in ['R', 'W', 'X']:
            if mode & getattr(stat, 'S_I%s%s' % (action, owner)):
                result += action.lower()
            else:
                result += '-'
    return result


def sizestr(st):
    if st.st_mode & stat.S_IFREG:
        return '%7d' % st.st_size
    else:
        # do not show size for non regular files
        return ' ' * 7


os.chdir((sys.argv[1:] + ['.'])[0])

for name in sorted(os.listdir('.')):
    st = os.stat(name)
    print('%s %s %s' % (modestr(st), sizestr(st), name))