Skip to content

Commit c06ea41

Browse files
Bordacarmocca
andauthored
Include images with the mirror package (#15659)
Co-authored-by: Carlos Mocholí <[email protected]>
1 parent aab8f48 commit c06ea41

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

.actions/assistant.py

+26-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import os
22
import re
3+
import shutil
34
from itertools import chain
5+
from os.path import dirname, isfile
46
from pathlib import Path
5-
from pprint import pprint
67
from typing import Dict, List, Optional, Sequence, Tuple
78

89
import pkg_resources
@@ -65,6 +66,7 @@ def _replace_imports(lines: List[str], mapping: List[Tuple[str, str]]) -> List[s
6566
def copy_replace_imports(
6667
source_dir: str, source_imports: List[str], target_imports: List[str], target_dir: Optional[str] = None
6768
) -> None:
69+
"""Copy package content with import adjustments."""
6870
print(f"Replacing imports: {locals()}")
6971
assert len(source_imports) == len(target_imports), (
7072
"source and target imports must have the same length, "
@@ -75,19 +77,27 @@ def copy_replace_imports(
7577

7678
ls = _retrieve_files(source_dir)
7779
for fp in ls:
78-
if fp.endswith(".py") or not fp.endswith(".pyc"):
79-
with open(fp, encoding="utf-8") as fo:
80-
try:
81-
lines = fo.readlines()
82-
except UnicodeDecodeError:
83-
# a binary file, skip
84-
print(f"Skipped replacing imports for {fp}")
85-
continue
86-
lines = _replace_imports(lines, list(zip(source_imports, target_imports)))
87-
fp_new = fp.replace(source_dir, target_dir)
88-
os.makedirs(os.path.dirname(fp_new), exist_ok=True)
89-
with open(fp_new, "w", encoding="utf-8") as fo:
90-
fo.writelines(lines)
80+
fp_new = fp.replace(source_dir, target_dir)
81+
_, ext = os.path.splitext(fp)
82+
if ext in (".png", ".jpg", ".ico"):
83+
os.makedirs(dirname(fp_new), exist_ok=True)
84+
if not isfile(fp_new):
85+
shutil.copy(fp, fp_new)
86+
continue
87+
elif ext in (".pyc",):
88+
continue
89+
# Try to parse everything else
90+
with open(fp, encoding="utf-8") as fo:
91+
try:
92+
lines = fo.readlines()
93+
except UnicodeDecodeError:
94+
# a binary file, skip
95+
print(f"Skipped replacing imports for {fp}")
96+
continue
97+
lines = _replace_imports(lines, list(zip(source_imports, target_imports)))
98+
os.makedirs(os.path.dirname(fp_new), exist_ok=True)
99+
with open(fp_new, "w", encoding="utf-8") as fo:
100+
fo.writelines(lines)
91101

92102

93103
def create_mirror_package(source_dir: str, package_mapping: Dict[str, str]) -> None:
@@ -129,7 +139,7 @@ def _prune_packages(req_file: str, packages: Sequence[str]) -> None:
129139
req = list(pkg_resources.parse_requirements(ln_))[0]
130140
if req.name not in packages:
131141
final.append(line)
132-
pprint(final)
142+
print(final)
133143
path.write_text("\n".join(final))
134144

135145
@staticmethod
@@ -147,7 +157,7 @@ def replace_oldest_ver(requirement_fnames: Sequence[str] = REQUIREMENT_FILES_ALL
147157
def copy_replace_imports(
148158
source_dir: str, source_import: str, target_import: str, target_dir: Optional[str] = None
149159
) -> None:
150-
"""Recursively replace imports in given folder."""
160+
"""Copy package content with import adjustments."""
151161
source_imports = source_import.strip().split(",")
152162
target_imports = target_import.strip().split(",")
153163
copy_replace_imports(source_dir, source_imports, target_imports, target_dir=target_dir)

0 commit comments

Comments
 (0)