Skip to content

Commit 39a2257

Browse files
authored
Merge pull request #139 from ligangty/copy-to
Feat: multi-targets support
2 parents a7d66a4 + 1936bc3 commit 39a2257

25 files changed

+2334
-559
lines changed

charon/cmd/command.py

Lines changed: 43 additions & 31 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,
@@ -140,48 +142,44 @@ def upload(
140142
if not aws_profile:
141143
logger.warning("No AWS profile specified!")
142144

143-
aws_bucket = conf.get_aws_bucket(target)
144-
if not aws_bucket:
145-
sys.exit(1)
146-
147145
archive_path = __get_local_repo(repo)
148146
npm_archive_type = detect_npm_archive(archive_path)
149147
product_key = f"{product}-{version}"
150-
prefix_ = conf.get_bucket_prefix(target)
151148
manifest_bucket_name = conf.get_manifest_bucket()
149+
targets_ = __get_targets(target, conf)
152150
if npm_archive_type != NpmArchiveType.NOT_NPM:
153151
logger.info("This is a npm archive")
154-
tmp_dir = handle_npm_uploading(
152+
tmp_dir, succeeded = handle_npm_uploading(
155153
archive_path,
156154
product_key,
157-
bucket_name=aws_bucket,
158-
prefix=prefix_,
155+
targets=targets_,
159156
aws_profile=aws_profile,
160157
dir_=work_dir,
161158
dry_run=dryrun,
162-
target=target,
163159
manifest_bucket_name=manifest_bucket_name
164160
)
161+
if not succeeded:
162+
sys.exit(1)
165163
else:
166164
ignore_patterns_list = None
167165
if ignore_patterns:
168166
ignore_patterns_list = ignore_patterns
169167
else:
170168
ignore_patterns_list = __get_ignore_patterns(conf)
171169
logger.info("This is a maven archive")
172-
tmp_dir = handle_maven_uploading(
170+
tmp_dir, succeeded = handle_maven_uploading(
173171
archive_path,
174172
product_key,
175173
ignore_patterns_list,
176174
root=root_path,
177-
bucket_name=aws_bucket,
175+
targets=targets_,
178176
aws_profile=aws_profile,
179-
prefix=prefix_,
180177
dir_=work_dir,
181178
dry_run=dryrun,
182-
target=target,
183179
manifest_bucket_name=manifest_bucket_name
184180
)
181+
if not succeeded:
182+
sys.exit(1)
185183
except Exception:
186184
print(traceback.format_exc())
187185
sys.exit(2) # distinguish between exception and bad config or bad state
@@ -221,9 +219,10 @@ def upload(
221219
help="""
222220
The target to do the deletion, which will decide which s3 bucket
223221
and what root path where all files will be deleted from.
222+
Can accept more than one target.
224223
""",
225224
required=True,
226-
multiple=False,
225+
multiple=True,
227226
)
228227
@option(
229228
"--root_path",
@@ -270,7 +269,7 @@ def delete(
270269
repo: str,
271270
product: str,
272271
version: str,
273-
target: str,
272+
target: List[str],
274273
root_path="maven-repository",
275274
ignore_patterns: List[str] = None,
276275
work_dir: str = None,
@@ -298,48 +297,44 @@ def delete(
298297
if not aws_profile:
299298
logger.warning("No AWS profile specified!")
300299

301-
aws_bucket = conf.get_aws_bucket(target)
302-
if not aws_bucket:
303-
sys.exit(1)
304-
305300
archive_path = __get_local_repo(repo)
306301
npm_archive_type = detect_npm_archive(archive_path)
307302
product_key = f"{product}-{version}"
308-
prefix_ = conf.get_bucket_prefix(target)
309303
manifest_bucket_name = conf.get_manifest_bucket()
304+
targets_ = __get_targets(target, conf)
310305
if npm_archive_type != NpmArchiveType.NOT_NPM:
311306
logger.info("This is a npm archive")
312-
tmp_dir = handle_npm_del(
307+
tmp_dir, succeeded = handle_npm_del(
313308
archive_path,
314309
product_key,
315-
bucket_name=aws_bucket,
316-
prefix=prefix_,
310+
targets=targets_,
317311
aws_profile=aws_profile,
318312
dir_=work_dir,
319313
dry_run=dryrun,
320-
target=target,
321314
manifest_bucket_name=manifest_bucket_name
322315
)
316+
if not succeeded:
317+
sys.exit(1)
323318
else:
324319
ignore_patterns_list = None
325320
if ignore_patterns:
326321
ignore_patterns_list = ignore_patterns
327322
else:
328323
ignore_patterns_list = __get_ignore_patterns(conf)
329324
logger.info("This is a maven archive")
330-
tmp_dir = handle_maven_del(
325+
tmp_dir, succeeded = handle_maven_del(
331326
archive_path,
332327
product_key,
333328
ignore_patterns_list,
334329
root=root_path,
335-
bucket_name=aws_bucket,
330+
targets=targets_,
336331
aws_profile=aws_profile,
337-
prefix=prefix_,
338332
dir_=work_dir,
339333
dry_run=dryrun,
340-
target=target,
341334
manifest_bucket_name=manifest_bucket_name
342335
)
336+
if not succeeded:
337+
sys.exit(1)
343338
except Exception:
344339
print(traceback.format_exc())
345340
sys.exit(2) # distinguish between exception and bad config or bad state
@@ -348,6 +343,23 @@ def delete(
348343
__safe_delete(tmp_dir)
349344

350345

346+
def __get_targets(target: List[str], conf: CharonConfig) -> List[Tuple[str, str, str]]:
347+
targets_ = []
348+
for tgt in target:
349+
aws_bucket = conf.get_aws_bucket(tgt)
350+
if not aws_bucket:
351+
continue
352+
prefix = conf.get_bucket_prefix(tgt)
353+
targets_.append([tgt, aws_bucket, prefix])
354+
if len(targets_) == 0:
355+
logger.error(
356+
"All targets are not valid or configured, "
357+
"please check your charon configurations."
358+
)
359+
sys.exit(1)
360+
return targets_
361+
362+
351363
def __safe_delete(tmp_dir: str):
352364
if tmp_dir and os.path.exists(tmp_dir):
353365
logger.info("Cleaning up work directory: %s", tmp_dir)

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)