Skip to content

Commit

Permalink
format code
Browse files Browse the repository at this point in the history
  • Loading branch information
StardustDL committed Jan 28, 2024
1 parent 88c342f commit 5fdc037
Show file tree
Hide file tree
Showing 16 changed files with 109 additions and 215 deletions.
5 changes: 1 addition & 4 deletions core/aexpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def initializeLogging(level: int = logging.WARNING) -> None:
def getAppDirectory() -> pathlib.Path:
return pathlib.Path(__file__).parent.resolve()


def getCacheDirectory() -> pathlib.Path:
return getAppDirectory() / "cache"

Expand All @@ -40,7 +41,3 @@ def getBuildDate() -> datetime:
return datetime.fromisoformat(os.getenv("BUILD_DATE", "unknown"))
except:
return datetime.now()


def getPythonExe() -> str:
return os.getenv("AEXPY_PYTHON_EXE", "python")
60 changes: 45 additions & 15 deletions core/aexpy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,34 @@ def main(ctx=None, verbose: int = 0, interact: bool = False) -> None:
)
@click.argument("distribution", type=click.File("w"))
@click.option("-m", "--module", multiple=True, help="Top level module names.")
@click.option("-p", "--project", default="", help="Release string, e.g. project, project@version")
@click.option("-s", "--src", "mode", flag_value="src", default=True, help="Source code directory mode.")
@click.option("-d", "--dist", "mode", flag_value="dist", help="Distribution directory mode.")
@click.option(
"-p", "--project", default="", help="Release string, e.g. project, project@version"
)
@click.option(
"-s",
"--src",
"mode",
flag_value="src",
default=True,
help="Source code directory mode.",
)
@click.option(
"-d", "--dist", "mode", flag_value="dist", help="Distribution directory mode."
)
@click.option("-w", "--wheel", "mode", flag_value="wheel", help="Wheel file mode")
@click.option("-r", "--release", "mode", flag_value="release", help="Release ID mode")
def preprocess(
path: Path,
distribution: IO[str],
module: list[str] | None = None,
project: str = "",
mode: Literal["src"] | Literal["dist"] | Literal["wheel"] | Literal["release"] = "src",
mode: Literal["src"]
| Literal["dist"]
| Literal["wheel"]
| Literal["release"] = "src",
):
"""Preprocess and generate a package distribution file for subsequent steps.
"""Preprocess and generate a package distribution file.
DISTRIBUTION describes the output package distribution file (in json format, use `-` for stdout).
PATH describes the target path for each mode:
mode=src, PATH points to the directory that contains the package code directory
Expand All @@ -138,19 +152,30 @@ def preprocess(
"""
from .models import Distribution

with produce(Distribution(release=Release.fromId(project), rootPath=path, topModules=list(module or []))) as context:

with produce(
Distribution(
release=Release.fromId(project),
rootPath=path,
topModules=list(module or []),
)
) as context:
if mode == "release":
assert path.is_dir(), "The cache path should be a directory."
assert context.product.release.project and context.product.release.version, "Please give the release ID."
assert (
context.product.release.project and context.product.release.version
), "Please give the release ID."
from .preprocessing.download import PipWheelDownloadPreprocessor
preprocessor = PipWheelDownloadPreprocessor(cacheDir=path, logger=context.logger)

preprocessor = PipWheelDownloadPreprocessor(
cacheDir=path, logger=context.logger
)
context.use(preprocessor)
preprocessor.preprocess(context.product)
mode = "wheel"

if mode == "wheel":
from .preprocessing.wheel import WheelUnpackPreprocessor

if path.is_file():
# a path to wheel file
context.product.wheelFile = path
Expand All @@ -162,10 +187,11 @@ def preprocess(
context.use(preprocessor)
preprocessor.preprocess(context.product)
mode = "dist"

if mode == "dist":
assert path.is_dir(), "The target path should be a directory."
from .preprocessing.wheel import WheelMetadataPreprocessor

preprocessor = WheelMetadataPreprocessor(logger=context.logger)
context.use(preprocessor)
preprocessor.preprocess(context.product)
Expand All @@ -174,6 +200,7 @@ def preprocess(
assert mode == "src"
assert path.is_dir(), "The target path should be a directory."
from .preprocessing.counter import FileCounterPreprocessor

preprocessor = FileCounterPreprocessor(context.logger)
context.use(preprocessor)
preprocessor.preprocess(context.product)
Expand All @@ -193,7 +220,7 @@ def preprocess(
@click.argument("description", type=click.File("w"))
def extract(distribution: IO[str], description: IO[str]):
"""Extract the API in a distribution.
DISTRIBUTION describes the input package distribution file (in json format, use `-` for stdin).
DESCRIPTION describes the output API description file (in json format, use `-` for stdout).
Expand Down Expand Up @@ -227,7 +254,7 @@ def extract(distribution: IO[str], description: IO[str]):
@click.argument("difference", type=click.File("w"))
def diff(old: IO[str], new: IO[str], difference: IO[str]):
"""Diff the API description and find all changes.
OLD describes the input API description file of the old distribution (in json format, use `-` for stdin).
NEW describes the input API description file of the new distribution (in json format, use `-` for stdin).
DIFFERENCE describes the output API difference file (in json format, use `-` for stdout).
Expand Down Expand Up @@ -263,7 +290,7 @@ def diff(old: IO[str], new: IO[str], difference: IO[str]):
@click.argument("report", type=click.File("w"))
def report(difference: IO[str], report: IO[str]):
"""Generate a report for the API difference file.
DIFFERENCE describes the input API difference file (in json format, use `-` for stdin).
REPORT describes the output report file (in json format, use `-` for stdout).
Expand Down Expand Up @@ -292,7 +319,10 @@ def report(difference: IO[str], report: IO[str]):
@main.command()
@click.argument("file", type=click.File("r"))
def view(file: IO[str]):
"""View produced data (support distribution, api-description, api-difference, and report, in json format)."""
"""View produced data.
Supports distribution, api-description, api-difference, and report file (in json format).
"""

from pydantic import TypeAdapter

Expand Down
4 changes: 1 addition & 3 deletions core/aexpy/environments/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import sys
import logging

from .. import getPythonExe


class ExecutionEnvironment:
"""Environment that runs extractor code."""
Expand Down Expand Up @@ -53,4 +51,4 @@ def run(self, command: str, **kwargs):
def runPython(self, command: str, **kwargs):
"""Run a command in the environment."""

return self.run(f"{getPythonExe()} {command}", **kwargs)
return self.run(f"{sys.executable} {command}", **kwargs)
11 changes: 9 additions & 2 deletions core/aexpy/extracting/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@

from pydantic import TypeAdapter

from aexpy.models.description import TRANSFER_BEGIN, ApiEntryType, CollectionEntry, isPrivate
from aexpy.models.description import (
TRANSFER_BEGIN,
ApiEntryType,
CollectionEntry,
isPrivate,
)

from .. import getAppDirectory
from ..models import ApiDescription, Distribution
Expand Down Expand Up @@ -72,7 +77,9 @@ def extractInEnv(self, result, run, runPython):
subres.check_returncode()

data = subres.stdout.split(TRANSFER_BEGIN, 1)[1]
entries: list[ApiEntryType] = TypeAdapter(list[ApiEntryType]).validate_json(data)
entries: list[ApiEntryType] = TypeAdapter(list[ApiEntryType]).validate_json(
data
)
for entry in entries:
if entry.id not in result.entries:
result.addEntry(entry)
Expand Down
5 changes: 0 additions & 5 deletions core/aexpy/extracting/default.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import subprocess
from logging import Logger

from aexpy import getPythonExe
from aexpy.environments import ExecutionEnvironment
from aexpy.extracting.enriching.callgraph import Callgraph
from aexpy.extracting.third.mypyserver import PackageMypyServer
from aexpy.models.description import TRANSFER_BEGIN, FunctionEntry

from .. import getAppDirectory
from ..models import ApiDescription, Distribution
from . import Extractor

Expand Down
2 changes: 1 addition & 1 deletion core/aexpy/extracting/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ExtractorEnvironment(CondaEnvironment):

__baseenvprefix__ = "aexpy-extbase-"
__envprefix__ = "aexpy-ext-"
__packages__ = ["pydantic", "mypy"]
__packages__ = ["pydantic"]


class EnvirontmentExtractor[E: ExecutionEnvironment](Extractor):
Expand Down
2 changes: 1 addition & 1 deletion core/aexpy/extracting/main/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def process(self, root: ModuleType, modules: "list[ModuleType]"):
self.visitModule(module)
except Exception as ex:
self.logger.error(f"Failed to visit module {module}.", exc_info=ex)

def allEntries(self):
return list(self.mapper.values())

Expand Down
7 changes: 2 additions & 5 deletions core/aexpy/extracting/main/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

from aexpy import initializeLogging
from aexpy.models import Distribution
from aexpy.models.description import (
TRANSFER_BEGIN,
ApiEntryType
)
from aexpy.models.description import TRANSFER_BEGIN, ApiEntryType

from . import Processor

Expand Down Expand Up @@ -91,7 +88,7 @@ def main(dist: Distribution):
sys.path.insert(0, str(dist.rootPath.resolve()))

from pydantic import TypeAdapter

output = TypeAdapter(list[ApiEntryType]).dump_json(main(dist))
print(TRANSFER_BEGIN, end="")
print(output.decode())
6 changes: 2 additions & 4 deletions core/aexpy/extracting/third/mypyserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,9 @@ def members(self, entry: ClassEntry) -> dict[str, SymbolTableNode]:

self.cacheMembers[entry.id] = result
return self.cacheMembers[entry.id]

@overload
def element(
self, entry: ModuleEntry
) -> State | None:
def element(self, entry: ModuleEntry) -> State | None:
...

@overload
Expand Down
3 changes: 2 additions & 1 deletion core/aexpy/preprocessing/counter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from . import Preprocessor
from typing import override


class FileCounterPreprocessor(Preprocessor):
@override
def preprocess(self, product):
Expand All @@ -16,4 +17,4 @@ def preprocess(self, product):
product.fileSize += item.stat().st_size
product.locCount += len(item.read_text().splitlines())
except Exception as ex:
self.logger.error(f"Failed to stat file {item}.", exc_info=ex)
self.logger.error(f"Failed to stat file {item}.", exc_info=ex)
43 changes: 25 additions & 18 deletions core/aexpy/preprocessing/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
PYVERSIONS = [f"3.{i}" for i in range(8, 13)]


def wheelByPip(release: Release, path: Path, logger: Logger | None, mirror: bool = False) -> tuple[Path, str]:
def wheelByPip(
release: Release, path: Path, logger: Logger | None, mirror: bool = False
) -> tuple[Path, str]:
logger = logger or logging.getLogger("pre-download-pip")
index = INDEX_TSINGHUA if mirror else INDEX_ORIGIN

Expand All @@ -38,11 +40,7 @@ def check(s: str):
)

return list(
(
i
for i in path.glob(f"*{suffix}")
if check(i.name.removesuffix(suffix))
)
(i for i in path.glob(f"*{suffix}") if check(i.name.removesuffix(suffix)))
)

for item in glob(".whl"):
Expand Down Expand Up @@ -125,18 +123,14 @@ def check(s: str):
files = glob(".tar.gz")
assert len(files) > 0

logger.info(
f"Build wheel distribution for Python {pyversion}: {files[0]}."
)
logger.info(f"Build wheel distribution for Python {pyversion}: {files[0]}.")
subres = subprocess.run(
f"pip wheel {release.project}=={release.version} --no-deps -i {index}",
cwd=path,
capture_output=True,
text=True,
)
logger.info(
f"Inner pip wheel {files[0]} exit with {subres.returncode}."
)
logger.info(f"Inner pip wheel {files[0]} exit with {subres.returncode}.")
if subres.stdout.strip():
logger.debug(f"STDOUT:\n{subres.stdout}")
if subres.stderr.strip():
Expand Down Expand Up @@ -167,7 +161,10 @@ class DownloadInfo:
def __post_init__(self):
self.name = parse.urlparse(self.url).path.split("/")[-1]

def wheelByHttp(release: Release , path: Path, logger: Logger | None, mirror: bool = False) -> Path:

def wheelByHttp(
release: Release, path: Path, logger: Logger | None, mirror: bool = False
) -> Path:
logger = logger or logging.getLogger("pre-download-http")

rels = getReleases(release.project)
Expand All @@ -180,7 +177,10 @@ def wheelByHttp(release: Release , path: Path, logger: Logger | None, mirror: bo
raise Exception(f"Not found the valid distribution {release}")
return downloadRawWheel(download, path, logger, mirror)

def getDownloadInfo(release: list[dict], packagetype="bdist_wheel") -> DownloadInfo | None:

def getDownloadInfo(
release: list[dict], packagetype="bdist_wheel"
) -> DownloadInfo | None:
py3 = []

for item in release:
Expand Down Expand Up @@ -240,7 +240,10 @@ def getDownloadInfo(release: list[dict], packagetype="bdist_wheel") -> DownloadI

return None

def downloadRawWheel(info: DownloadInfo, path: Path, logger: Logger, mirror: bool = False) -> Path:

def downloadRawWheel(
info: DownloadInfo, path: Path, logger: Logger, mirror: bool = False
) -> Path:
cacheFile = path / info.name

if mirror:
Expand Down Expand Up @@ -270,14 +273,18 @@ def downloadRawWheel(info: DownloadInfo, path: Path, logger: Logger, mirror: boo


class PipWheelDownloadPreprocessor(Preprocessor):
def __init__(self, cacheDir: Path | None, mirror: bool = False, logger: Logger | None = None):
def __init__(
self, cacheDir: Path | None, mirror: bool = False, logger: Logger | None = None
):
super().__init__(logger)
self.mirror = mirror
self.cacheDir = cacheDir or getCacheDirectory()
utils.ensureDirectory(self.cacheDir)

@override
def preprocess(self, product):
wheelFile, pyversion = wheelByPip(product.release, self.cacheDir, logger=self.logger)
wheelFile, pyversion = wheelByPip(
product.release, self.cacheDir, logger=self.logger
)
product.pyversion = pyversion
product.wheelFile = wheelFile
product.wheelFile = wheelFile
Loading

0 comments on commit 5fdc037

Please sign in to comment.