|
| 1 | +""" |
| 2 | +Copyright (C) 2022 Red Hat, Inc. (https://github.com/Commonjava/charon) |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +""" |
| 16 | +from typing import List |
| 17 | + |
| 18 | +from charon.config import get_config |
| 19 | +from charon.pkgs.checksum_http import handle_checksum_validation_http |
| 20 | +from charon.cmd.internal import _decide_mode |
| 21 | +from click import command, option, argument |
| 22 | + |
| 23 | +import traceback |
| 24 | +import logging |
| 25 | +import os |
| 26 | +import sys |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +@argument( |
| 32 | + "path", |
| 33 | + type=str |
| 34 | +) |
| 35 | +@option( |
| 36 | + "--debug", |
| 37 | + "-D", |
| 38 | + "debug", |
| 39 | + help="Debug mode, will print all debug logs for problem tracking.", |
| 40 | + is_flag=True, |
| 41 | + default=False |
| 42 | +) |
| 43 | +@option( |
| 44 | + "--quiet", |
| 45 | + "-q", |
| 46 | + "quiet", |
| 47 | + help="Quiet mode, will shrink most of the logs except warning and errors.", |
| 48 | + is_flag=True, |
| 49 | + default=False |
| 50 | +) |
| 51 | +@option( |
| 52 | + "--skip", |
| 53 | + "-k", |
| 54 | + "skips", |
| 55 | + multiple=True, |
| 56 | + help=""" |
| 57 | + Paths to be skipped. This is used for recursive mode when $PATH has sub folders. |
| 58 | + """ |
| 59 | +) |
| 60 | +@option( |
| 61 | + "--recursive", |
| 62 | + "-r", |
| 63 | + "recursive", |
| 64 | + help=""" |
| 65 | + Decide if do validation recursively in the specified path. |
| 66 | + Warning: if the path is high level which contains lots of sub path(e.g org/ |
| 67 | + or com/), set this flag will take very long time to do the validation. |
| 68 | + """, |
| 69 | + is_flag=True, |
| 70 | + default=False |
| 71 | +) |
| 72 | +@option( |
| 73 | + "--report-file-path", |
| 74 | + "-f", |
| 75 | + "report_file_path", |
| 76 | + help=""" |
| 77 | + The path where the final report files will be generated |
| 78 | + """ |
| 79 | +) |
| 80 | +@option( |
| 81 | + "--includes", |
| 82 | + "-i", |
| 83 | + "includes", |
| 84 | + help=""" |
| 85 | + The comma splitted file suffix for all files that need to |
| 86 | + validate. e.g, ".jar,.pom,.xml". If not specified, will use |
| 87 | + default file types |
| 88 | + """ |
| 89 | +) |
| 90 | +@option( |
| 91 | + "--target", |
| 92 | + "-t", |
| 93 | + "target", |
| 94 | + help=""" |
| 95 | + The target to do the uploading, which will decide which s3 bucket |
| 96 | + and what root path where all files will be uploaded to. |
| 97 | + Can accept more than one target. |
| 98 | + """, |
| 99 | + required=True |
| 100 | +) |
| 101 | +@command() |
| 102 | +def validate( |
| 103 | + path: str, |
| 104 | + target: str, |
| 105 | + includes: List[str], |
| 106 | + report_file_path: str, |
| 107 | + skips: List[str], |
| 108 | + recursive: bool = False, |
| 109 | + quiet: bool = False, |
| 110 | + debug: bool = False |
| 111 | +): |
| 112 | + """This command will validate the checksum of the specified path for the |
| 113 | + maven repository. It will calculate the sha1 checksum of all artifact |
| 114 | + files in the specified path and compare with the companied .sha1 files |
| 115 | + of the artifacts, then record all mismatched artifacts in the report file. |
| 116 | + If some artifact files misses the companied .sha1 files, they will also |
| 117 | + be recorded. |
| 118 | + """ |
| 119 | + _decide_mode( |
| 120 | + "checksum-{}".format(target), path.replace("/", "_"), |
| 121 | + is_quiet=quiet, is_debug=debug |
| 122 | + ) |
| 123 | + try: |
| 124 | + conf = get_config() |
| 125 | + if not conf: |
| 126 | + sys.exit(1) |
| 127 | + |
| 128 | + aws_bucket = "" |
| 129 | + root_path = "" |
| 130 | + t = conf.get_target(target) |
| 131 | + if not t: |
| 132 | + sys.exit(1) |
| 133 | + for b in t: |
| 134 | + aws_bucket = b.get('bucket') |
| 135 | + prefix = b.get('prefix', '') |
| 136 | + |
| 137 | + # NOTE: This is a liitle hacky, which constrain the configuration of |
| 138 | + # of target should define the bucket to contain "prod-maven" |
| 139 | + # or "stage-maven" to decide that the bucket is for maven repo |
| 140 | + # in our defined aws env for production or stage |
| 141 | + if "prod-maven" not in aws_bucket and "stage-maven" not in aws_bucket: |
| 142 | + logger.error("The target %s is not a maven repository.", target) |
| 143 | + sys.exit(1) |
| 144 | + |
| 145 | + root_path = os.path.join(prefix, path) |
| 146 | + skip_paths = [os.path.join(prefix, p) for p in skips if p != "" and p != "/"] |
| 147 | + if path == "/": |
| 148 | + root_path = prefix |
| 149 | + handle_checksum_validation_http( |
| 150 | + aws_bucket, root_path, includes, report_file_path, recursive, skip_paths |
| 151 | + ) |
| 152 | + except Exception: |
| 153 | + print(traceback.format_exc()) |
| 154 | + sys.exit(2) |
0 commit comments