|
| 1 | +#!/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import textwrap |
| 7 | +from maven_artifact.artifact import Artifact |
| 8 | + |
| 9 | +try: |
| 10 | + from maven_artifact.utils import Utils |
| 11 | +except ImportError: |
| 12 | + sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) |
| 13 | + from maven_artifact.utils import Utils |
| 14 | + |
| 15 | +from maven_artifact.requestor import RequestException |
| 16 | +from maven_artifact.downloader import Downloader |
| 17 | + |
| 18 | + |
| 19 | +class DescriptionWrappedNewlineFormatter(argparse.HelpFormatter): |
| 20 | + """An argparse formatter that: |
| 21 | + * preserves newlines (like argparse.RawDescriptionHelpFormatter), |
| 22 | + * removes leading indent (great for multiline strings), |
| 23 | + * and applies reasonable text wrapping. |
| 24 | +
|
| 25 | + Source: https://stackoverflow.com/a/64102901/79125 |
| 26 | + """ |
| 27 | + |
| 28 | + def _fill_text(self, text, width, indent): |
| 29 | + # Strip the indent from the original python definition that plagues most of us. |
| 30 | + text = textwrap.dedent(text) |
| 31 | + text = textwrap.indent(text, indent) # Apply any requested indent. |
| 32 | + text = text.splitlines() # Make a list of lines |
| 33 | + text = [textwrap.fill(line, width) for line in text] # Wrap each line |
| 34 | + text = "\n".join(text) # Join the lines again |
| 35 | + return text |
| 36 | + |
| 37 | + |
| 38 | +class WrappedNewlineFormatter(DescriptionWrappedNewlineFormatter): |
| 39 | + """An argparse formatter that: |
| 40 | + * preserves newlines (like argparse.RawTextHelpFormatter), |
| 41 | + * removes leading indent and applies reasonable text wrapping (like DescriptionWrappedNewlineFormatter), |
| 42 | + * applies to all help text (description, arguments, epilogue). |
| 43 | + """ |
| 44 | + |
| 45 | + def _split_lines(self, text, width): |
| 46 | + # Allow multiline strings to have common leading indentation. |
| 47 | + text = textwrap.dedent(text) |
| 48 | + text = text.splitlines() |
| 49 | + lines = [] |
| 50 | + for line in text: |
| 51 | + wrapped_lines = textwrap.fill(line, width).splitlines() |
| 52 | + lines.extend(subline for subline in wrapped_lines) |
| 53 | + if line: |
| 54 | + lines.append("") # Preserve line breaks. |
| 55 | + return lines |
| 56 | + |
| 57 | + |
| 58 | +class MainCommand: |
| 59 | + def _get_arguments(self): |
| 60 | + parser = argparse.ArgumentParser(formatter_class=WrappedNewlineFormatter, epilog=__epilog__) |
| 61 | + parser.add_argument( |
| 62 | + "maven_coordinate", |
| 63 | + help=""" |
| 64 | + defined by http://maven.apache.org/pom.html#Maven_Coordinates. The possible options are: |
| 65 | + - groupId:artifactId:version |
| 66 | + - groupId:artifactId:packaging:version |
| 67 | + - groupId:artifactId:packaging:classifier:version""", |
| 68 | + ) |
| 69 | + parser.add_argument( |
| 70 | + "filename", |
| 71 | + nargs="?", |
| 72 | + help=""" |
| 73 | + If not supplied the filename will be <artifactId>.<extension>. |
| 74 | + The filename directory must exist prior to download.""", |
| 75 | + ) |
| 76 | + parser.add_argument( |
| 77 | + "-m", |
| 78 | + "--maven-repo", |
| 79 | + dest="base", |
| 80 | + default="https://repo.maven.apache.org/maven2/", |
| 81 | + help="Maven repository URL (default: https://repo.maven.apache.org/maven2/)", |
| 82 | + ) |
| 83 | + |
| 84 | + parser.add_argument("-u", "--username", help="username (must be combined with --password)") |
| 85 | + parser.add_argument( |
| 86 | + "-p", |
| 87 | + "--password", |
| 88 | + help=""" |
| 89 | + password (must be combined with --username) or |
| 90 | + base64 encoded username and password (can not not be combined with --username)""", |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "-t", "--token", help="OAuth bearer token (can not be combined with --username or --password)" |
| 94 | + ) |
| 95 | + |
| 96 | + parser.add_argument("-ht", "--hash-type", default="md5", help="hash type (default: md5)") |
| 97 | + |
| 98 | + args = parser.parse_args() |
| 99 | + |
| 100 | + username = args.username |
| 101 | + password = args.password |
| 102 | + token = args.token |
| 103 | + |
| 104 | + if username and not password: |
| 105 | + parser.error("The 'username' parameter requires the 'password' parameter.") |
| 106 | + elif (username or password) and token: |
| 107 | + parser.error("The 'token' parameter cannot be used together with 'username' or 'password'.") |
| 108 | + elif (password) and not (username or token) and not Utils.is_base64(password): |
| 109 | + parser.error("The 'password' parameter must be base64 if not used together with 'username'.") |
| 110 | + |
| 111 | + return args |
| 112 | + |
| 113 | + |
| 114 | +__epilog__ = """ |
| 115 | + Example: |
| 116 | + %(prog)s "org.apache.solr:solr:war:3.5.0"\n |
| 117 | + """ |
| 118 | + |
| 119 | + |
| 120 | +def main(): |
| 121 | + mc = MainCommand() |
| 122 | + args = mc._get_arguments() |
| 123 | + |
| 124 | + try: |
| 125 | + dl = Downloader(base=args.base, username=args.username, password=args.password, token=args.token) |
| 126 | + |
| 127 | + artifact = Artifact.parse(args.maven_coordinate) |
| 128 | + |
| 129 | + filename = args.filename |
| 130 | + |
| 131 | + if dl.download(artifact, filename, args.hash_type): |
| 132 | + sys.exit(0) |
| 133 | + else: |
| 134 | + print("Download failed.") |
| 135 | + sys.exit(1) |
| 136 | + except RequestException as e: |
| 137 | + print(e.msg) |
| 138 | + sys.exit(1) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == "__main__": |
| 142 | + main() |
0 commit comments