"""fixperms sys.argv cli argument parsing"""
from dataclasses import dataclass
from logging import Logger
from argparse import ArgumentParser, ArgumentTypeError
from typing import Literal
"""Dataclass for more strict type hints for parse_args"""
role: Literal['CWP', 'cPanel', 'WP3']
"""Validates custom args"""
def __init__(self) -> None:
self._all_cwp_users = set()
self._all_cwp_users = cwp.all_users()
self.user = self._cwp_user
self.user = self._cpanel_user
elif os.path.exists('/etc/ansible/wordpress-ultrastack'):
self.user = self._wp3_user
sys.exit("fixperms requires a cPanel, CWP, or WP3 server")
def _cwp_user(self, val: str) -> str:
if val in self._all_cwp_users:
raise ArgumentTypeError(f'{val} is not a valid CWP user')
def _cpanel_user(val: str) -> str:
raise ArgumentTypeError(f'{val} is not a valid cPanel user')
def _wp3_user(val: str) -> str:
raise ArgumentTypeError(f'{val} is not a valid WP3 user')
def positive(val: str) -> int:
"""Test an arg is an int >= 1"""
raise ArgumentTypeError(f'{val} must be >= 1')
def parse_args() -> Args:
custom_args = ArgValidator()
description="Safely corrects permission issues on a "
f"{custom_args.role} user"
group = parser.add_mutually_exclusive_group()
'-p', '--procs', type=custom_args.positive, default=4, metavar='NUM',
help='max users to process at once, if multiple were requested '
'-v', '--verbose', action='store_true',
help='show verbose output of every permissions change',
'-q', '--quiet', action='store_true', help='hide all output but errors'
'-n', '--noop', action='store_true',
help='Test mode; Make no actual changes to the account',
'--skip', type=os.path.realpath, metavar='PATH', nargs='+', default=[],
help='path(s) to skip changing permissions on. '
'If you want to skip etc and mail, use --skip-mail instead',
'--no-preserve-exec', dest='preserve_exec', action='store_false',
help="Don't preserve the execute bit on files",
if custom_args.role != 'WP3':
'--skip-mail', action='store_true',
help='Do not run mailperm to fix permissions on etc and mail dirs',
args = parser.parse_args()
logger = rads.setup_verbosity(loglevel)
skip_mail=args.skip_mail if custom_args.role != 'WP3' else True,
preserve_exec=args.preserve_exec,