晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。   林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。   见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝)   既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。   南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。 sh-3ll

HOME


sh-3ll 1.0
DIR:/opt/cloudlinux/venv/lib64/python3.11/site-packages/prospector/
Upload File :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/prospector/autodetect.py
import os
import re
import warnings
from pathlib import Path

from requirements_detector import find_requirements
from requirements_detector.detect import RequirementsNotFound

from prospector import encoding
from prospector.exceptions import PermissionMissing
from prospector.pathutils import is_virtualenv

POSSIBLE_LIBRARIES = ("django", "celery", "flask")


# see http://docs.python.org/2/reference/lexical_analysis.html#identifiers
_FROM_IMPORT_REGEX = re.compile(r"^\s*from ([\._a-zA-Z0-9]+) import .*$")
_IMPORT_REGEX = re.compile(r"^\s*import ([\._a-zA-Z0-9]+)$")
_IMPORT_MULTIPLE_REGEX = re.compile(r"^\s*import ([\._a-zA-Z0-9]+(, ){1})+")


def find_from_imports(file_contents):
    names = set()
    for line in file_contents.split("\n"):
        match = _IMPORT_MULTIPLE_REGEX.match(line)
        if match:
            import_names = []
            first = match.group(1)
            import_names.append(first[:-2])
            for name in line.split(first)[1].split(","):
                import_names.append(name.strip())
        else:
            match = _IMPORT_REGEX.match(line) or _FROM_IMPORT_REGEX.match(line)
            if match is None:
                continue
            import_names = match.group(1).split(".")

        for import_name in import_names:
            if import_name in POSSIBLE_LIBRARIES:
                names.add(import_name)

    return names


def find_from_path(path: Path):
    names = set()

    try:
        for item in path.iterdir():
            if item.is_dir():
                if is_virtualenv(item):
                    continue
                names |= find_from_path(item)
            elif not item.is_symlink() and item.suffix == ".py":
                try:
                    contents = encoding.read_py_file(item)
                    names |= find_from_imports(contents)
                except encoding.CouldNotHandleEncoding as err:
                    # TODO: this output will break output formats such as JSON
                    warnings.warn(f"{err.path}: {err.__cause__}", ImportWarning)

            if len(names) == len(POSSIBLE_LIBRARIES):
                # don't continue on recursing, there's no point!
                break
    except PermissionError as err:
        raise PermissionMissing(path) from err

    return names


def find_from_requirements(path):
    reqs = find_requirements(path)
    names = []
    for requirement in reqs:
        if requirement.name is not None and requirement.name.lower() in POSSIBLE_LIBRARIES:
            names.append(requirement.name.lower())
    return names


def autodetect_libraries(path):
    if os.path.isfile(path):
        path = os.path.dirname(path)
        if path == "":
            path = "."

    libraries = []

    try:
        libraries = find_from_requirements(path)
    except RequirementsNotFound:
        pass

    if len(libraries) < len(POSSIBLE_LIBRARIES):
        libraries = find_from_path(path)

    return libraries