|
19 | 19 | # from charon.pkgs.pkg_utils import invalidate_cf_paths |
20 | 20 | from charon.constants import (INDEX_HTML_TEMPLATE, NPM_INDEX_HTML_TEMPLATE, |
21 | 21 | 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 |
23 | 23 | from jinja2 import Template |
24 | 24 | import os |
25 | 25 | import logging |
@@ -155,8 +155,7 @@ def __to_html(package_type: str, contents: List[str], folder: str, top_level: st |
155 | 155 | if folder == "/": |
156 | 156 | html_path = os.path.join(top_level, "index.html") |
157 | 157 | 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) |
160 | 159 | return html_path |
161 | 160 |
|
162 | 161 |
|
@@ -267,7 +266,7 @@ def re_index( |
267 | 266 | path: str, |
268 | 267 | package_type: str, |
269 | 268 | aws_profile: str = None, |
270 | | - # cf_enable: bool = False, |
| 269 | + recursive: bool = False, |
271 | 270 | dry_run: bool = False |
272 | 271 | ): |
273 | 272 | """Refresh the index.html for the specified folder in the bucket. |
@@ -307,17 +306,31 @@ def re_index( |
307 | 306 | logger.debug("The re-indexed page content: %s", index_content) |
308 | 307 | if not dry_run: |
309 | 308 | index_path = os.path.join(path, "index.html") |
| 309 | + logger.info("Start re-indexing %s in bucket %s", index_path, bucket_name) |
310 | 310 | if path == "/": |
311 | 311 | index_path = "index.html" |
312 | 312 | s3_client.simple_delete_file(index_path, (bucket_name, real_prefix)) |
313 | 313 | s3_client.simple_upload_file( |
314 | 314 | index_path, index_content, (bucket_name, real_prefix), |
315 | 315 | "text/html", digest_content(index_content) |
316 | 316 | ) |
317 | | - # We will not invalidate index.html per cost consideration |
318 | | - # if cf_enable: |
319 | | - # cf_client = CFClient(aws_profile=aws_profile) |
320 | | - # invalidate_cf_paths(cf_client, bucket, [index_path]) |
| 317 | + logger.info("%s re-indexing finished", index_path) |
| 318 | + if recursive: |
| 319 | + for c in contents: |
| 320 | + if c.endswith("/"): |
| 321 | + sub_path = c.removeprefix(real_prefix).strip() |
| 322 | + if sub_path.startswith("/"): |
| 323 | + sub_path = sub_path.removeprefix("/") |
| 324 | + logger.debug("subpath: %s", sub_path) |
| 325 | + args = { |
| 326 | + "target": target, |
| 327 | + "path": sub_path, |
| 328 | + "package_type": package_type, |
| 329 | + "aws_profile": aws_profile, |
| 330 | + "recursive": recursive, |
| 331 | + "dry_run": dry_run |
| 332 | + } |
| 333 | + re_index(**args) # type: ignore |
321 | 334 | else: |
322 | 335 | logger.warning( |
323 | 336 | "The path %s does not contain any contents in bucket %s. " |
|
0 commit comments