#!/usr/bin/env python3
import re
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parents[1]))

from harness import LintWarning, file_check

SOURCES_RE = re.compile(r"\bcommon_sources\s*=\s*\[(.*?)\]", re.S)


def check(path, text):
    match = SOURCES_RE.search(text)
    if not match:
        yield LintWarning(path, "unable to find sources array")
        return

    block = match.group(1)
    lines = [l.strip().strip(",") for l in block.splitlines() if l.strip()]
    if not lines:
        yield LintWarning(path, "unable to parse sources array")
        return

    clean = [l.strip("'") for l in lines]
    init = [l for l in clean if l == "init"]
    resources = [l for l in clean if l == "resources"]
    middle = [l for l in clean if l not in ("init", "resources")]
    if clean != init + sorted(middle) + resources:
        yield LintWarning(path, "source list is not ordered")


file_check(check)
