Skip to content

Commit 5b0a456

Browse files
authored
Merge pull request #150 from ligangty/release
Release
2 parents cc1a242 + 2d7a3fa commit 5b0a456

31 files changed

+3009
-736
lines changed

charon/__init__.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,9 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
import logging
1716

1817
from charon.cmd.command import cli, upload, delete
19-
from charon.utils.logs import set_logging
2018

2119
# init group command
2220
cli.add_command(upload)
2321
cli.add_command(delete)
24-
25-
set_logging(level=logging.INFO) # override this however you want

charon/cmd/command.py

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
See the License for the specific language governing permissions and
1414
limitations under the License.
1515
"""
16-
from typing import List
16+
from typing import List, Tuple
17+
1718
from charon.config import CharonConfig, get_config
1819
from charon.utils.logs import set_logging
1920
from charon.utils.archive import detect_npm_archive, download_archive, NpmArchiveType
@@ -62,9 +63,10 @@
6263
help="""
6364
The target to do the uploading, which will decide which s3 bucket
6465
and what root path where all files will be uploaded to.
66+
Can accept more than one target.
6567
""",
6668
required=True,
67-
multiple=False,
69+
multiple=True,
6870
)
6971
@option(
7072
"--root_path",
@@ -112,7 +114,7 @@ def upload(
112114
repo: str,
113115
product: str,
114116
version: str,
115-
target: str,
117+
target: List[str],
116118
root_path="maven-repository",
117119
ignore_patterns: List[str] = None,
118120
work_dir: str = None,
@@ -126,10 +128,10 @@ def upload(
126128
"""
127129
tmp_dir = work_dir
128130
try:
131+
__decide_mode(product, version, is_quiet=quiet, is_debug=debug)
129132
if dryrun:
130133
logger.info("Running in dry-run mode,"
131134
"no files will be uploaded.")
132-
__decide_mode(is_quiet=quiet, is_debug=debug)
133135
if not __validate_prod_key(product, version):
134136
return
135137
conf = get_config()
@@ -138,45 +140,47 @@ def upload(
138140

139141
aws_profile = os.getenv("AWS_PROFILE") or conf.get_aws_profile()
140142
if not aws_profile:
141-
logger.warning("No AWS profile specified!")
142-
143-
aws_bucket = conf.get_aws_bucket(target)
144-
if not aws_bucket:
143+
logger.error("No AWS profile specified!")
145144
sys.exit(1)
146145

147146
archive_path = __get_local_repo(repo)
148147
npm_archive_type = detect_npm_archive(archive_path)
149148
product_key = f"{product}-{version}"
150-
prefix_ = conf.get_bucket_prefix(target)
149+
manifest_bucket_name = conf.get_manifest_bucket()
150+
targets_ = __get_targets(target, conf)
151151
if npm_archive_type != NpmArchiveType.NOT_NPM:
152152
logger.info("This is a npm archive")
153-
tmp_dir = handle_npm_uploading(
153+
tmp_dir, succeeded = handle_npm_uploading(
154154
archive_path,
155155
product_key,
156-
bucket_name=aws_bucket,
157-
prefix=prefix_,
156+
targets=targets_,
158157
aws_profile=aws_profile,
159158
dir_=work_dir,
160-
dry_run=dryrun
159+
dry_run=dryrun,
160+
manifest_bucket_name=manifest_bucket_name
161161
)
162+
if not succeeded:
163+
sys.exit(1)
162164
else:
163165
ignore_patterns_list = None
164166
if ignore_patterns:
165167
ignore_patterns_list = ignore_patterns
166168
else:
167169
ignore_patterns_list = __get_ignore_patterns(conf)
168170
logger.info("This is a maven archive")
169-
tmp_dir = handle_maven_uploading(
171+
tmp_dir, succeeded = handle_maven_uploading(
170172
archive_path,
171173
product_key,
172174
ignore_patterns_list,
173175
root=root_path,
174-
bucket_name=aws_bucket,
176+
targets=targets_,
175177
aws_profile=aws_profile,
176-
prefix=prefix_,
177178
dir_=work_dir,
178-
dry_run=dryrun
179+
dry_run=dryrun,
180+
manifest_bucket_name=manifest_bucket_name
179181
)
182+
if not succeeded:
183+
sys.exit(1)
180184
except Exception:
181185
print(traceback.format_exc())
182186
sys.exit(2) # distinguish between exception and bad config or bad state
@@ -216,9 +220,10 @@ def upload(
216220
help="""
217221
The target to do the deletion, which will decide which s3 bucket
218222
and what root path where all files will be deleted from.
223+
Can accept more than one target.
219224
""",
220225
required=True,
221-
multiple=False,
226+
multiple=True,
222227
)
223228
@option(
224229
"--root_path",
@@ -265,7 +270,7 @@ def delete(
265270
repo: str,
266271
product: str,
267272
version: str,
268-
target: str,
273+
target: List[str],
269274
root_path="maven-repository",
270275
ignore_patterns: List[str] = None,
271276
work_dir: str = None,
@@ -279,10 +284,10 @@ def delete(
279284
"""
280285
tmp_dir = work_dir
281286
try:
287+
__decide_mode(product, version, is_quiet=quiet, is_debug=debug)
282288
if dryrun:
283289
logger.info("Running in dry-run mode,"
284290
"no files will be deleted.")
285-
__decide_mode(is_quiet=quiet, is_debug=debug)
286291
if not __validate_prod_key(product, version):
287292
return
288293
conf = get_config()
@@ -291,45 +296,47 @@ def delete(
291296

292297
aws_profile = os.getenv("AWS_PROFILE") or conf.get_aws_profile()
293298
if not aws_profile:
294-
logger.warning("No AWS profile specified!")
295-
296-
aws_bucket = conf.get_aws_bucket(target)
297-
if not aws_bucket:
299+
logger.error("No AWS profile specified!")
298300
sys.exit(1)
299301

300302
archive_path = __get_local_repo(repo)
301303
npm_archive_type = detect_npm_archive(archive_path)
302304
product_key = f"{product}-{version}"
303-
prefix_ = conf.get_bucket_prefix(target)
305+
manifest_bucket_name = conf.get_manifest_bucket()
306+
targets_ = __get_targets(target, conf)
304307
if npm_archive_type != NpmArchiveType.NOT_NPM:
305308
logger.info("This is a npm archive")
306-
tmp_dir = handle_npm_del(
309+
tmp_dir, succeeded = handle_npm_del(
307310
archive_path,
308311
product_key,
309-
bucket_name=aws_bucket,
310-
prefix=prefix_,
312+
targets=targets_,
311313
aws_profile=aws_profile,
312314
dir_=work_dir,
313-
dry_run=dryrun
315+
dry_run=dryrun,
316+
manifest_bucket_name=manifest_bucket_name
314317
)
318+
if not succeeded:
319+
sys.exit(1)
315320
else:
316321
ignore_patterns_list = None
317322
if ignore_patterns:
318323
ignore_patterns_list = ignore_patterns
319324
else:
320325
ignore_patterns_list = __get_ignore_patterns(conf)
321326
logger.info("This is a maven archive")
322-
tmp_dir = handle_maven_del(
327+
tmp_dir, succeeded = handle_maven_del(
323328
archive_path,
324329
product_key,
325330
ignore_patterns_list,
326331
root=root_path,
327-
bucket_name=aws_bucket,
332+
targets=targets_,
328333
aws_profile=aws_profile,
329-
prefix=prefix_,
330334
dir_=work_dir,
331-
dry_run=dryrun
335+
dry_run=dryrun,
336+
manifest_bucket_name=manifest_bucket_name
332337
)
338+
if not succeeded:
339+
sys.exit(1)
333340
except Exception:
334341
print(traceback.format_exc())
335342
sys.exit(2) # distinguish between exception and bad config or bad state
@@ -338,6 +345,23 @@ def delete(
338345
__safe_delete(tmp_dir)
339346

340347

348+
def __get_targets(target: List[str], conf: CharonConfig) -> List[Tuple[str, str, str]]:
349+
targets_ = []
350+
for tgt in target:
351+
aws_bucket = conf.get_aws_bucket(tgt)
352+
if not aws_bucket:
353+
continue
354+
prefix = conf.get_bucket_prefix(tgt)
355+
targets_.append([tgt, aws_bucket, prefix])
356+
if len(targets_) == 0:
357+
logger.error(
358+
"All targets are not valid or configured, "
359+
"please check your charon configurations."
360+
)
361+
sys.exit(1)
362+
return targets_
363+
364+
341365
def __safe_delete(tmp_dir: str):
342366
if tmp_dir and os.path.exists(tmp_dir):
343367
logger.info("Cleaning up work directory: %s", tmp_dir)
@@ -386,15 +410,17 @@ def __validate_prod_key(product: str, version: str) -> bool:
386410
return True
387411

388412

389-
def __decide_mode(is_quiet: bool, is_debug: bool):
413+
def __decide_mode(product: str, version: str, is_quiet: bool, is_debug: bool):
390414
if is_quiet:
391415
logger.info("Quiet mode enabled, "
392416
"will only give warning and error logs.")
393-
set_logging(level=logging.WARNING)
417+
set_logging(product, version, level=logging.WARNING)
394418
elif is_debug:
395419
logger.info("Debug mode enabled, "
396420
"will give all debug logs for tracing.")
397-
set_logging(level=logging.DEBUG)
421+
set_logging(product, version, level=logging.DEBUG)
422+
else:
423+
set_logging(product, version, level=logging.INFO)
398424

399425

400426
@group()

charon/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ def __init__(self, data: Dict):
3838
self.__targets: Dict = data.get("targets", None)
3939
if not self.__targets or not isinstance(self.__targets, Dict):
4040
raise TypeError("Charon configuration is not correct: targets is invalid.")
41+
self.__manifest_bucket: str = data.get("manifest_bucket", None)
4142

4243
def get_ignore_patterns(self) -> List[str]:
4344
return self.__ignore_patterns
@@ -48,7 +49,7 @@ def get_aws_profile(self) -> str:
4849
def get_aws_bucket(self, target: str) -> str:
4950
target_: Dict = self.__targets.get(target, None)
5051
if not target_ or not isinstance(target_, Dict):
51-
logger.error("The target %s is not found in charon configuration.")
52+
logger.error("The target %s is not found in charon configuration.", target)
5253
return None
5354
bucket = target_.get("bucket", None)
5455
if not bucket:
@@ -72,6 +73,9 @@ def get_bucket_prefix(self, target: str) -> str:
7273
prefix = remove_prefix(prefix, "/")
7374
return prefix
7475

76+
def get_manifest_bucket(self) -> str:
77+
return self.__manifest_bucket
78+
7579

7680
def get_config() -> CharonConfig:
7781
config_file = os.path.join(os.getenv("HOME"), ".charon", CONFIG_FILE)

charon/constants.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,8 @@
133133
</header>
134134
<hr/>
135135
<main>
136-
<ul style="list-style: none outside;" id="contents">
137-
{% for item in index.items %}
138-
<li><a href="{{ item }}" title="{{ item }}">{{ item }}</a></li>
139-
{% endfor%}
136+
<ul style="list-style: none outside;" id="contents">{% for item in index.items %}
137+
<li><a href="{{ item }}" title="{{ item }}">{{ item }}</a></li>{% endfor%}
140138
</ul>
141139
</main>
142140
<hr/>
@@ -162,11 +160,9 @@
162160
<hr/>
163161
<main>
164162
<ul style="list-style: none outside;" id="contents">
165-
{% for item in index.items %}{% if item.startswith("@") or item.startswith("..") %}
166-
<li><a href="{{ item }}index.html" title="{{ item }}">{{ item }}</a></li>
167-
{% else %}
168-
<li><a href="{{ item }}" title="{{ item }}">{{ item }}</a></li>
169-
{% endif %}{% endfor%}
163+
{% for item in index.items %}{% if item.startswith("@") or item.startswith("..") %}
164+
<li><a href="{{ item }}index.html" title="{{ item }}">{{ item }}</a></li>{% else %}
165+
<li><a href="{{ item }}" title="{{ item }}">{{ item }}</a></li>{% endif %}{% endfor%}
170166
</ul>
171167
</main>
172168
<hr/>
@@ -175,3 +171,5 @@
175171
'''
176172

177173
PROD_INFO_SUFFIX = ".prodinfo"
174+
MANIFEST_SUFFIX = ".txt"
175+
DEFAULT_ERRORS_LOG = "errors.log"

charon/pkgs/indexing.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ def __generate_index_html(
127127
removed_index = os.path.join(top_level, folder_, "index.html")
128128
s3_client.delete_files(
129129
file_paths=[removed_index],
130-
bucket_name=bucket,
130+
target=(bucket, prefix),
131131
product=None,
132-
root=top_level,
133-
key_prefix=prefix
132+
root=top_level
134133
)
135134
elif len(contents) >= 1:
136135
real_contents = []

0 commit comments

Comments
 (0)