| 1 |
import argparse |
| 2 |
import os |
| 3 |
import sys |
| 4 |
from pprint import pprint |
| 5 |
|
| 6 |
from . import __version__, api |
| 7 |
|
| 8 |
|
| 9 |
def parse_args(parse_this=None): |
| 10 |
parser = build_parser() |
| 11 |
return parser.parse_args(parse_this) |
| 12 |
|
| 13 |
|
| 14 |
def build_parser(): |
| 15 |
parser = argparse.ArgumentParser() |
| 16 |
parser.add_argument( |
| 17 |
"-V", |
| 18 |
"--version", |
| 19 |
action="version", |
| 20 |
help="Show the conda-package-handling version number and exit.", |
| 21 |
version=f"conda-package-handling {__version__}", |
| 22 |
) |
| 23 |
sp = parser.add_subparsers(title="subcommands", dest="subcommand", required=True) |
| 24 |
|
| 25 |
extract_parser = sp.add_parser("extract", help="extract package contents", aliases=["x"]) |
| 26 |
extract_parser.add_argument("archive_path", help="path to archive to extract") |
| 27 |
extract_parser.add_argument( |
| 28 |
"--dest", |
| 29 |
help="destination folder to extract to. If not set, defaults to" |
| 30 |
" package filename minus extension in the same folder as the input archive." |
| 31 |
" May be relative path used in tandem with the --prefix flag.", |
| 32 |
) |
| 33 |
extract_parser.add_argument( |
| 34 |
"--prefix", |
| 35 |
help="base directory to extract to. Use this to set the base" |
| 36 |
" directory, while allowing the folder name to be automatically determined " |
| 37 |
"by the input filename. An abspath --prefix with an unset --dest will " |
| 38 |
"achieve this.", |
| 39 |
) |
| 40 |
extract_parser.add_argument( |
| 41 |
"--info", |
| 42 |
help="If the archive supports separate metadata, this" |
| 43 |
" flag extracts only the metadata in the info folder from the " |
| 44 |
"package. If the archive does not support separate metadata, this " |
| 45 |
"flag has no effect and all files are extracted.", |
| 46 |
action="store_true", |
| 47 |
) |
| 48 |
|
| 49 |
create_parser = sp.add_parser("create", help="bundle files into a package", aliases=["c"]) |
| 50 |
create_parser.add_argument( |
| 51 |
"prefix", |
| 52 |
help="folder of files to bundle. Not strictly required to" |
| 53 |
" have conda package metadata, but if conda package metadata isn't " |
| 54 |
"present, you'll see a warning and your file will not work as a " |
| 55 |
"conda package", |
| 56 |
) |
| 57 |
create_parser.add_argument( |
| 58 |
"out_fn", |
| 59 |
help="Filename of archive to be created. Extension determines package type.", |
| 60 |
) |
| 61 |
create_parser.add_argument( |
| 62 |
"--file-list", |
| 63 |
help="Path to file containing one relative path per" |
| 64 |
" line that should be included in the archive. If not provided, " |
| 65 |
"lists all files in the prefix.", |
| 66 |
) |
| 67 |
create_parser.add_argument("--out-folder", help="Folder to dump final archive to") |
| 68 |
|
| 69 |
convert_parser = sp.add_parser( |
| 70 |
"transmute", help="convert from one package type to another", aliases=["t"] |
| 71 |
) |
| 72 |
convert_parser.add_argument( |
| 73 |
"in_file", help="existing file to convert from. Glob patterns accepted." |
| 74 |
) |
| 75 |
convert_parser.add_argument( |
| 76 |
"out_ext", |
| 77 |
help="extension of file to convert to. Examples: .tar.bz2, .conda", |
| 78 |
) |
| 79 |
convert_parser.add_argument("--out-folder", help="Folder to dump final archive to") |
| 80 |
convert_parser.add_argument( |
| 81 |
"--force", action="store_true", help="Force overwrite existing package" |
| 82 |
) |
| 83 |
convert_parser.add_argument( |
| 84 |
"--processes", |
| 85 |
type=int, |
| 86 |
help="Max number of processes to use. If not set, defaults to 1.", |
| 87 |
) |
| 88 |
convert_parser.add_argument( |
| 89 |
"--zstd-compression-level", |
| 90 |
help=( |
| 91 |
"When building v2 packages, set the compression level used by " |
| 92 |
"conda-package-handling. Defaults to 19." |
| 93 |
), |
| 94 |
type=int, |
| 95 |
choices=range(1, 23), |
| 96 |
default=19, |
| 97 |
) |
| 98 |
convert_parser.add_argument( |
| 99 |
"--zstd-compression-threads", |
| 100 |
help=( |
| 101 |
"When building v2 packages, set the compression threads used by " |
| 102 |
"conda-package-handling. Defaults to 1. -1=automatic." |
| 103 |
), |
| 104 |
type=int, |
| 105 |
default=1, |
| 106 |
) |
| 107 |
list_parser = sp.add_parser( |
| 108 |
"list", |
| 109 |
aliases=["l"], |
| 110 |
help="List package contents like `python -m tarfile --list ...` would do.", |
| 111 |
) |
| 112 |
list_parser.add_argument("archive_path", help="path or URL to archive to inspect") |
| 113 |
list_parser.add_argument( |
| 114 |
"-v", |
| 115 |
"--verbose", |
| 116 |
action="store_true", |
| 117 |
help="Report more details, similar to 'ls -l'. Otherwise, only the filenames are printed.", |
| 118 |
) |
| 119 |
list_parser.add_argument( |
| 120 |
"--components", |
| 121 |
dest="components", |
| 122 |
default="info,pkg", |
| 123 |
help="Comma-separated list of components to read (.conda artifacts only; " |
| 124 |
"ignored for .tar.bz2). Allowed values: info, pkg.", |
| 125 |
) |
| 126 |
|
| 127 |
return parser |
| 128 |
|
| 129 |
|
| 130 |
def main(args=None): |
| 131 |
args = parse_args(args) |
| 132 |
if hasattr(args, "out_folder") and args.out_folder: |
| 133 |
args.out_folder = ( |
| 134 |
os.path.abspath(os.path.normpath(os.path.expanduser(args.out_folder))) + os.sep |
| 135 |
) |
| 136 |
match args.subcommand: |
| 137 |
case "extract" | "x": |
| 138 |
if args.info: |
| 139 |
api.extract(args.archive_path, args.dest, components="info", prefix=args.prefix) |
| 140 |
else: |
| 141 |
api.extract(args.archive_path, args.dest, prefix=args.prefix) |
| 142 |
case "create" | "c": |
| 143 |
api.create(args.prefix, args.file_list, args.out_fn, args.out_folder) |
| 144 |
case "transmute" | "t": |
| 145 |
failed_files = api.transmute( |
| 146 |
args.in_file, |
| 147 |
args.out_ext, |
| 148 |
args.out_folder, |
| 149 |
args.processes or 1, |
| 150 |
force=args.force, |
| 151 |
zstd_compress_level=args.zstd_compression_level, |
| 152 |
zstd_compress_threads=args.zstd_compression_threads, |
| 153 |
) |
| 154 |
if failed_files: |
| 155 |
print("failed files:") |
| 156 |
pprint(failed_files) |
| 157 |
sys.exit(1) |
| 158 |
case "list" | "l": |
| 159 |
components = [ |
| 160 |
component.strip() for component in (args.components or "info,pkg").split(",") |
| 161 |
] |
| 162 |
api.list_contents(args.archive_path, verbose=args.verbose, components=components) |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": # pragma: no cover |
| 166 |
main(args=None) |