in antlir/compiler/items/stat_options.py [0:0]
def mode_to_octal_str(mode: Mode) -> str:
"""Converts an instance of `Mode` to an octal string. If `mode` is a string,
it's expected to be in the chmod symbolic string format with the following
added restrictions:
- Only append ("+") actions are supported, as we always apply the changes on
top of mode 0.
- No "X" is supported, as this conversion must be compatible with `stat(1)`.
"""
# `mode` can be the empty string
mode = mode or 0
if isinstance(mode, int):
return f"{mode:04o}"
assert (
"-" not in mode and "=" not in mode
), "Only append actions ('+') are supported in mode strings"
result = 0
for directive in mode.split(","):
try:
classes, perms = directive.split("+")
except ValueError:
raise ValueError(
"Expected directive in the form [classes...]+[perms...] "
f"for {mode}"
)
# Support empty classes
classes = classes or ["a"]
for stat_cls in classes:
stat_cls_fn = _STAT_CLASSES.get(stat_cls, None)
assert stat_cls_fn, (
f'Only classes of "{",".join(_STAT_CLASSES.keys())}" '
"are supported when setting mode"
)
for perm in perms:
if perm in {"s", "t"}:
result |= _STAT_EXTRA_PERMS.get((perm, stat_cls), 0) << 9
else:
mask = _STAT_PERMS.get(perm, None)
assert mask, (
f'Only permissions of "{",".join(_STAT_PERMS.keys())}" '
"are supported when setting mode"
)
result |= stat_cls_fn(mask)
return f"{result:04o}"