Загрузка данных


#!/usr/bin/env python3
import sys
import os

STATS_FILE = "chown_stats.txt"

def load_stats():
    if not os.path.exists(STATS_FILE):
        return 0, 0
    try:
        with open(STATS_FILE, "r") as f:
            data = f.read().split()
            if len(data) == 3:
                return int(data[0]), int(data[1])
    except Exception:
        pass
    return 0, 0

def save_stats(success, failed):
    try:
        with open(STATS_FILE, "w") as f:
            # Запись трех параметров в файл статистики
            f.write(f"{success} {failed} {success + failed}\n")
    except Exception:
        pass

def is_valid_name(s):
    if not s:
        return False
    if s.isdigit():
        return True
    for char in s:
        if not (char.isalnum() or char == '-'):
            return False
    if s[0].isdigit():
        return False
    return True

def validate_spec(spec):
    if ':' not in spec:
        return is_valid_name(spec)
    
    parts = spec.split(':')
    if len(parts) > 2:
        return False
    owner_part, group_part = parts[0], parts[1]
    if not owner_part and not group_part:
        return False
    
    valid = True
    if owner_part:
        valid = valid and is_valid_name(owner_part)
    if group_part:
        valid = valid and is_valid_name(group_part)
    return valid

def parse_and_validate(line):
    tokens = line.strip().split()
    if not tokens:
        return False
    
    if tokens[0] != 'chown':
        return False
    
    has_help_version = False
    has_reference_key = False
    key_end_index = 1
    
    valid_short_chars = set(['c', 'f', 'v', 'h', 'R', 'H', 'L', 'P'])
    valid_long_no_param = set([
        '--changes', '--silent', '--quiet', '--verbose',
        '--dereference', '--no-dereference', '--no-preserve-root', '--preserve-root'
    ])
    
    while key_end_index < len(tokens):
        tok = tokens[key_end_index]
        if tok.startswith('-'):
            if tok in ('-', '--'):
                return False
            if tok.startswith('--'):
                if tok in ('--help', '--version'):
                    has_help_version = True
                elif tok in valid_long_no_param:
                    pass
                elif tok.startswith('--from='):
                    if not validate_spec(tok[7:]):
                        return False
                elif tok.startswith('--reference='):
                    if len(tok) <= 12:
                        return False
                    has_reference_key = True
                else:
                    return False
            else:
                for char in tok[1:]:
                    if char not in valid_short_chars:
                        return False
            key_end_index += 1
        else:
            break
            
    if has_help_version:
        return True
        
    rem = len(tokens) - key_end_index
    if has_reference_key:
        return rem >= 1
    else:
        if rem < 2:
            return False
        return validate_spec(tokens[key_end_index])

def main():
    for line in sys.stdin:
        line = line.rstrip('\r\n')
        if not line:
            continue
            
        if line == 'getstats':
            success, failed = load_stats()
            print(f"{success} {failed} {success + failed}")
            sys.stdout.flush()
            continue
        if line == 'getsuccess':
            success, _ = load_stats()
            print(success)
            sys.stdout.flush()
            continue
        if line == 'getfailed':
            _, failed = load_stats()
            print(failed)
            sys.stdout.flush()
            continue
        if line == 'resetstats':
            save_stats(0, 0)
            print()
            sys.stdout.flush()
            continue
            
        valid = parse_and_validate(line)
        success, failed = load_stats()
        if valid:
            success += 1
            print("True")
        else:
            failed += 1
            print("False")
        save_stats(success, failed)
        sys.stdout.flush()

if __name__ == '__main__':
    main()