1
1
import os
2
2
import re
3
+ import shutil
3
4
from itertools import chain
5
+ from os .path import dirname , isfile
4
6
from pathlib import Path
5
- from pprint import pprint
6
7
from typing import Dict , List , Optional , Sequence , Tuple
7
8
8
9
import pkg_resources
@@ -65,6 +66,7 @@ def _replace_imports(lines: List[str], mapping: List[Tuple[str, str]]) -> List[s
65
66
def copy_replace_imports (
66
67
source_dir : str , source_imports : List [str ], target_imports : List [str ], target_dir : Optional [str ] = None
67
68
) -> None :
69
+ """Copy package content with import adjustments."""
68
70
print (f"Replacing imports: { locals ()} " )
69
71
assert len (source_imports ) == len (target_imports ), (
70
72
"source and target imports must have the same length, "
@@ -75,19 +77,27 @@ def copy_replace_imports(
75
77
76
78
ls = _retrieve_files (source_dir )
77
79
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 )
91
101
92
102
93
103
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:
129
139
req = list (pkg_resources .parse_requirements (ln_ ))[0 ]
130
140
if req .name not in packages :
131
141
final .append (line )
132
- pprint (final )
142
+ print (final )
133
143
path .write_text ("\n " .join (final ))
134
144
135
145
@staticmethod
@@ -147,7 +157,7 @@ def replace_oldest_ver(requirement_fnames: Sequence[str] = REQUIREMENT_FILES_ALL
147
157
def copy_replace_imports (
148
158
source_dir : str , source_import : str , target_import : str , target_dir : Optional [str ] = None
149
159
) -> None :
150
- """Recursively replace imports in given folder ."""
160
+ """Copy package content with import adjustments ."""
151
161
source_imports = source_import .strip ().split ("," )
152
162
target_imports = target_import .strip ().split ("," )
153
163
copy_replace_imports (source_dir , source_imports , target_imports , target_dir = target_dir )
0 commit comments