Skip to content

Commit 9d18501

Browse files
authored
Merge pull request #354 from ligangty/main
Merge from 1.3.x
2 parents d190477 + adf8ab1 commit 9d18501

23 files changed

+480
-109
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/cmd/cmd_upload.py

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
from typing import List
1717

1818
from charon.config import get_config
19-
from charon.utils.archive import detect_npm_archive, NpmArchiveType
19+
from charon.utils.archive import detect_npm_archives, NpmArchiveType
2020
from charon.pkgs.maven import handle_maven_uploading
2121
from charon.pkgs.npm import handle_npm_uploading
2222
from charon.cmd.internal import (
2323
_decide_mode, _validate_prod_key,
24-
_get_local_repo, _get_targets,
24+
_get_local_repos, _get_targets,
2525
_get_ignore_patterns, _safe_delete
2626
)
2727
from click import command, option, argument
@@ -35,8 +35,9 @@
3535

3636

3737
@argument(
38-
"repo",
38+
"repos",
3939
type=str,
40+
nargs=-1 # This allows multiple arguments for zip urls
4041
)
4142
@option(
4243
"--product",
@@ -146,7 +147,7 @@
146147
)
147148
@command()
148149
def upload(
149-
repo: str,
150+
repos: List[str],
150151
product: str,
151152
version: str,
152153
targets: List[str],
@@ -161,9 +162,10 @@ def upload(
161162
dryrun=False,
162163
sign_result_file=None,
163164
):
164-
"""Upload all files from a released product REPO to Ronda
165-
Service. The REPO points to a product released tarball which
166-
is hosted in a remote url or a local path.
165+
"""Upload all files from released product REPOs to Ronda
166+
Service. The REPOs point to a product released tarballs which
167+
are hosted in remote urls or local paths.
168+
Notes: It does not support multiple repos for NPM archives
167169
"""
168170
tmp_dir = work_dir
169171
try:
@@ -182,8 +184,8 @@ def upload(
182184
logger.error("No AWS profile specified!")
183185
sys.exit(1)
184186

185-
archive_path = _get_local_repo(repo)
186-
npm_archive_type = detect_npm_archive(archive_path)
187+
archive_paths = _get_local_repos(repos)
188+
archive_types = detect_npm_archives(archive_paths)
187189
product_key = f"{product}-{version}"
188190
manifest_bucket_name = conf.get_manifest_bucket()
189191
targets_ = _get_targets(targets, conf)
@@ -194,31 +196,18 @@ def upload(
194196
" are set correctly.", targets_
195197
)
196198
sys.exit(1)
197-
if npm_archive_type != NpmArchiveType.NOT_NPM:
198-
logger.info("This is a npm archive")
199-
tmp_dir, succeeded = handle_npm_uploading(
200-
archive_path,
201-
product_key,
202-
targets=targets_,
203-
aws_profile=aws_profile,
204-
dir_=work_dir,
205-
gen_sign=contain_signature,
206-
cf_enable=conf.is_aws_cf_enable(),
207-
key=sign_key,
208-
dry_run=dryrun,
209-
manifest_bucket_name=manifest_bucket_name
210-
)
211-
if not succeeded:
212-
sys.exit(1)
213-
else:
199+
200+
maven_count = archive_types.count(NpmArchiveType.NOT_NPM)
201+
npm_count = len(archive_types) - maven_count
202+
if maven_count == len(archive_types):
214203
ignore_patterns_list = None
215204
if ignore_patterns:
216205
ignore_patterns_list = ignore_patterns
217206
else:
218207
ignore_patterns_list = _get_ignore_patterns(conf)
219208
logger.info("This is a maven archive")
220209
tmp_dir, succeeded = handle_maven_uploading(
221-
archive_path,
210+
archive_paths,
222211
product_key,
223212
ignore_patterns_list,
224213
root=root_path,
@@ -235,6 +224,28 @@ def upload(
235224
)
236225
if not succeeded:
237226
sys.exit(1)
227+
elif npm_count == len(archive_types) and len(archive_types) == 1:
228+
logger.info("This is a npm archive")
229+
tmp_dir, succeeded = handle_npm_uploading(
230+
archive_paths[0],
231+
product_key,
232+
targets=targets_,
233+
aws_profile=aws_profile,
234+
dir_=work_dir,
235+
gen_sign=contain_signature,
236+
cf_enable=conf.is_aws_cf_enable(),
237+
key=sign_key,
238+
dry_run=dryrun,
239+
manifest_bucket_name=manifest_bucket_name
240+
)
241+
if not succeeded:
242+
sys.exit(1)
243+
elif npm_count == len(archive_types) and len(archive_types) > 1:
244+
logger.error("Doesn't support multiple upload for npm")
245+
sys.exit(1)
246+
else:
247+
logger.error("Upload types are not consistent")
248+
sys.exit(1)
238249
except Exception:
239250
print(traceback.format_exc())
240251
sys.exit(2) # distinguish between exception and bad config or bad state

charon/cmd/internal.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ def _get_local_repo(url: str) -> str:
7575
return archive_path
7676

7777

78+
def _get_local_repos(urls: list) -> list:
79+
archive_paths = []
80+
for url in urls:
81+
archive_path = _get_local_repo(url)
82+
archive_paths.append(archive_path)
83+
return archive_paths
84+
85+
7886
def _validate_prod_key(product: str, version: str) -> bool:
7987
if not product or product.strip() == "":
8088
logger.error("Error: product can not be empty!")

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. "

0 commit comments

Comments
 (0)