Include images with the mirror package (#15659)

Co-authored-by: Carlos Mocholí <carlossmocholi@gmail.com>
This commit is contained in:
Jirka Borovec 2022-11-12 20:27:13 +01:00 committed by GitHub
parent aab8f48749
commit c06ea41056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 16 deletions

View File

@ -1,8 +1,9 @@
import os import os
import re import re
import shutil
from itertools import chain from itertools import chain
from os.path import dirname, isfile
from pathlib import Path from pathlib import Path
from pprint import pprint
from typing import Dict, List, Optional, Sequence, Tuple from typing import Dict, List, Optional, Sequence, Tuple
import pkg_resources import pkg_resources
@ -65,6 +66,7 @@ def _replace_imports(lines: List[str], mapping: List[Tuple[str, str]]) -> List[s
def copy_replace_imports( def copy_replace_imports(
source_dir: str, source_imports: List[str], target_imports: List[str], target_dir: Optional[str] = None source_dir: str, source_imports: List[str], target_imports: List[str], target_dir: Optional[str] = None
) -> None: ) -> None:
"""Copy package content with import adjustments."""
print(f"Replacing imports: {locals()}") print(f"Replacing imports: {locals()}")
assert len(source_imports) == len(target_imports), ( assert len(source_imports) == len(target_imports), (
"source and target imports must have the same length, " "source and target imports must have the same length, "
@ -75,19 +77,27 @@ def copy_replace_imports(
ls = _retrieve_files(source_dir) ls = _retrieve_files(source_dir)
for fp in ls: for fp in ls:
if fp.endswith(".py") or not fp.endswith(".pyc"): fp_new = fp.replace(source_dir, target_dir)
with open(fp, encoding="utf-8") as fo: _, ext = os.path.splitext(fp)
try: if ext in (".png", ".jpg", ".ico"):
lines = fo.readlines() os.makedirs(dirname(fp_new), exist_ok=True)
except UnicodeDecodeError: if not isfile(fp_new):
# a binary file, skip shutil.copy(fp, fp_new)
print(f"Skipped replacing imports for {fp}") continue
continue elif ext in (".pyc",):
lines = _replace_imports(lines, list(zip(source_imports, target_imports))) continue
fp_new = fp.replace(source_dir, target_dir) # Try to parse everything else
os.makedirs(os.path.dirname(fp_new), exist_ok=True) with open(fp, encoding="utf-8") as fo:
with open(fp_new, "w", encoding="utf-8") as fo: try:
fo.writelines(lines) lines = fo.readlines()
except UnicodeDecodeError:
# a binary file, skip
print(f"Skipped replacing imports for {fp}")
continue
lines = _replace_imports(lines, list(zip(source_imports, target_imports)))
os.makedirs(os.path.dirname(fp_new), exist_ok=True)
with open(fp_new, "w", encoding="utf-8") as fo:
fo.writelines(lines)
def create_mirror_package(source_dir: str, package_mapping: Dict[str, str]) -> None: def create_mirror_package(source_dir: str, package_mapping: Dict[str, str]) -> None:
@ -129,7 +139,7 @@ class AssistantCLI:
req = list(pkg_resources.parse_requirements(ln_))[0] req = list(pkg_resources.parse_requirements(ln_))[0]
if req.name not in packages: if req.name not in packages:
final.append(line) final.append(line)
pprint(final) print(final)
path.write_text("\n".join(final)) path.write_text("\n".join(final))
@staticmethod @staticmethod
@ -147,7 +157,7 @@ class AssistantCLI:
def copy_replace_imports( def copy_replace_imports(
source_dir: str, source_import: str, target_import: str, target_dir: Optional[str] = None source_dir: str, source_import: str, target_import: str, target_dir: Optional[str] = None
) -> None: ) -> None:
"""Recursively replace imports in given folder.""" """Copy package content with import adjustments."""
source_imports = source_import.strip().split(",") source_imports = source_import.strip().split(",")
target_imports = target_import.strip().split(",") target_imports = target_import.strip().split(",")
copy_replace_imports(source_dir, source_imports, target_imports, target_dir=target_dir) copy_replace_imports(source_dir, source_imports, target_imports, target_dir=target_dir)