Skip to content

Commit 6d601e0

Browse files
authored
Merge pull request #336 from ligangty/1.3.x
Feat: support recursive indexing for index function
2 parents 4bfca37 + 7fbd3d9 commit 6d601e0

File tree

4 files changed

+47
-19
lines changed

4 files changed

+47
-19
lines changed

charon/cmd/cmd_index.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
""",
4343
required=True
4444
)
45+
@option(
46+
"--recursive",
47+
"-r",
48+
help="If do indexing recursively under $path",
49+
is_flag=True,
50+
default=False
51+
)
4552
@option(
4653
"--config",
4754
"-c",
@@ -69,6 +76,7 @@
6976
def index(
7077
path: str,
7178
target: str,
79+
recursive: bool = False,
7280
config: str = None,
7381
debug: bool = False,
7482
quiet: bool = False,
@@ -120,7 +128,15 @@ def index(
120128
if not aws_bucket:
121129
logger.error("No bucket specified for target %s!", target)
122130
else:
123-
re_index(b, path, package_type, aws_profile, dryrun)
131+
args = {
132+
"target": b,
133+
"path": path,
134+
"package_type": package_type,
135+
"aws_profile": aws_profile,
136+
"recursive": recursive,
137+
"dry_run": dryrun
138+
}
139+
re_index(**args) # type: ignore
124140

125141
except Exception:
126142
print(traceback.format_exc())

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: 21 additions & 8 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

@@ -267,7 +266,7 @@ def re_index(
267266
path: str,
268267
package_type: str,
269268
aws_profile: str = None,
270-
# cf_enable: bool = False,
269+
recursive: bool = False,
271270
dry_run: bool = False
272271
):
273272
"""Refresh the index.html for the specified folder in the bucket.
@@ -307,17 +306,31 @@ def re_index(
307306
logger.debug("The re-indexed page content: %s", index_content)
308307
if not dry_run:
309308
index_path = os.path.join(path, "index.html")
309+
logger.info("Start re-indexing %s in bucket %s", index_path, bucket_name)
310310
if path == "/":
311311
index_path = "index.html"
312312
s3_client.simple_delete_file(index_path, (bucket_name, real_prefix))
313313
s3_client.simple_upload_file(
314314
index_path, index_content, (bucket_name, real_prefix),
315315
"text/html", digest_content(index_content)
316316
)
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
321334
else:
322335
logger.warning(
323336
"The path %s does not contain any contents in bucket %s. "

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)