Skip to content

Commit bc996ad

Browse files
committed
Chore: replace all file write with files.overwrite_file
1 parent 4bfca37 commit bc996ad

File tree

3 files changed

+11
-13
lines changed

3 files changed

+11
-13
lines changed

charon/pkgs/checksum_http.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
from charon.utils.files import digest, HashType
16+
from charon.utils.files import digest, HashType, overwrite_file
1717
from charon.storage import S3Client
1818
from typing import Tuple, List, Dict, Optional
1919
from html.parser import HTMLParser
@@ -169,9 +169,10 @@ def _check_and_remove_file(file_name: str):
169169
def _write_one_col_file(items: List[str], file_name: str):
170170
if items and len(items) > 0:
171171
_check_and_remove_file(file_name)
172-
with open(file_name, "w") as f:
173-
for i in items:
174-
f.write(i + "\n")
172+
content = ""
173+
for i in items:
174+
content = content + i + "\n"
175+
overwrite_file(file_name, content)
175176
logger.info("The report file %s is generated.", file_name)
176177

177178
_write_one_col_file(content[0], os.path.join(work_dir, "mismatched_files.csv"))
@@ -180,10 +181,9 @@ def _write_one_col_file(items: List[str], file_name: str):
180181
if content[2] and len(content[2]) > 0:
181182
error_file = os.path.join(work_dir, "error_files.csv")
182183
_check_and_remove_file(error_file)
183-
with open(error_file, "w") as f:
184-
f.write("path,error\n")
185-
for d in content[2]:
186-
f.write("{path},{error}\n".format(path=d["path"], error=d["error"]))
184+
f_content_lines: List[str] = []
185+
f_content = "path,error\n" + "\n".join(f_content_lines)
186+
overwrite_file(error_file, f_content)
187187
logger.info("The report file %s is generated.", error_file)
188188

189189

charon/pkgs/indexing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# from charon.pkgs.pkg_utils import invalidate_cf_paths
2020
from charon.constants import (INDEX_HTML_TEMPLATE, NPM_INDEX_HTML_TEMPLATE,
2121
PACKAGE_TYPE_MAVEN, PACKAGE_TYPE_NPM, PROD_INFO_SUFFIX)
22-
from charon.utils.files import digest_content
22+
from charon.utils.files import digest_content, overwrite_file
2323
from jinja2 import Template
2424
import os
2525
import logging
@@ -155,8 +155,7 @@ def __to_html(package_type: str, contents: List[str], folder: str, top_level: st
155155
if folder == "/":
156156
html_path = os.path.join(top_level, "index.html")
157157
os.makedirs(os.path.dirname(html_path), exist_ok=True)
158-
with open(html_path, 'w', encoding='utf-8') as html:
159-
html.write(html_content)
158+
overwrite_file(html_path, html_content)
160159
return html_path
161160

162161

charon/utils/files.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,5 @@ def write_manifest(paths: List[str], root: str, product_key: str) -> Tuple[str,
125125
if not os.path.isfile(manifest_path):
126126
with open(manifest_path, mode="a", encoding="utf-8"):
127127
pass
128-
with open(manifest_path, mode="w", encoding="utf-8") as f:
129-
f.write('\n'.join(artifacts))
128+
overwrite_file(manifest_path, '\n'.join(artifacts))
130129
return manifest_name, manifest_path

0 commit comments

Comments
 (0)