1 | # s3-bsync Copyright (c) 2021 Joshua Stockin |
2 | # <https://joshstock.in> |
3 | # <https://git.joshstock.in/s3-bsync> |
4 | # |
5 | # This software is licensed and distributed under the terms of the MIT License. |
6 | # See the MIT License in the LICENSE file of this project's root folder. |
7 | # |
8 | # This comment block and its contents, including this disclaimer, MUST be |
9 | # preserved in all copies or distributions of this software's source. |
10 |
|
11 | import os |
12 | import re |
13 | import argparse |
14 |
|
15 | import logging |
16 |
|
17 | from .meta import package_info |
18 |
|
19 | logger = logging.getLogger(__name__) |
20 |
|
21 | __all__ = ["command_parse", "sanitize_arguments"] |
22 |
|
23 |
|
24 | def command_parse(args: list[str]): |
25 | parser = argparse.ArgumentParser( |
26 | prog=package_info["name"], |
27 | description=package_info["description"], |
28 | formatter_class=lambda prog: argparse.HelpFormatter(prog, width=88), |
29 | allow_abbrev=False, |
30 | add_help=False, |
31 | ) |
32 |
|
33 | parser.add_argument( |
34 | "-h", "-?", "--help", action="help", help="Display this help message and exit." |
35 | ) |
36 | parser.add_argument( |
37 | "-v", |
38 | "--version", |
39 | action="version", |
40 | help="Display program and version information and exit.", |
41 | version=f"s3-bsync version {package_info['version_string']}\n" |
42 | "<https://joshstock.in> <josh@joshstock.in>\n" |
43 | "<https://git.joshstock.in/s3-bsync>", |
44 | ) |
45 |
|
46 | group1 = parser.add_argument_group( |
47 | "program behavior", "The program runs in sync mode by default." |
48 | ) |
49 |
|
50 | group1.add_argument( |
51 | "-i", |
52 | "--init", |
53 | action="store_true", |
54 | default=False, |
55 | help="Run in initialize mode. This allows tracking file management and directory options to be used. (default: False)", |
56 | ) |
57 | group1.add_argument( |
58 | "--debug", |
59 | action="store_true", |
60 | default=False, |
61 | help="Enables debug mode, which prints program information to stdout. (default: False)", |
62 | ) |
63 | group1.add_argument( |
64 | "--file", |
65 | nargs=1, |
66 | metavar=("SYNCFILE"), |
67 | default=None, |
68 | help='The s3sync format file used to store tracking and state information. (default: "~/.state.s3sync")', |
69 | ) |
70 | group1.add_argument( |
71 | "--dryrun", |
72 | action="store_true", |
73 | default=False, |
74 | help="Run program logic without actually making changes. Useful when paired with debug mode to see what changes would be made. (default: False)", |
75 | ) |
76 |
|
77 | group2 = parser.add_argument_group( |
78 | "tracking file management", "Requires initialize mode to be enabled." |
79 | ) |
80 |
|
81 | group2.add_argument( |
82 | "--purge", |
83 | action="store_true", |
84 | default=False, |
85 | help="Deletes the default (if not otherwise specified) tracking configuration file if it exists. (default: False)", |
86 | ) |
87 | group2.add_argument( |
88 | "--overwrite", |
89 | action="store_true", |
90 | default=False, |
91 | help="Overwrite tracking file with new directory maps instead of appending. (default: False)", |
92 | ) |
93 |
|
94 | group3 = parser.add_argument_group( |
95 | "directory mapping", "Requires initialize mode to be enabled." |
96 | ) |
97 |
|
98 | group3.add_argument( |
99 | "--dir", |
100 | action="append", |
101 | nargs=2, |
102 | metavar=("PATH", "S3_DEST"), |
103 | help="Directory map to detail which local directory corresponds to S3 bucket " |
104 | "and key prefix. Can be used multiple times to set multiple directories. " |
105 | "Local directories must be fully expanded. S3 destination in `s3://bucket-name/prefix` " |
106 | "format. Example: `--dir /home/josh/Documents s3://joshstockin/Documents`", |
107 | ) |
108 |
|
109 | return parser.parse_args(args) |
110 |
|
111 |
|
112 | def sanitize_arguments(args: argparse.Namespace): |
113 | if args.debug: |
114 | logger.debug("DEBUG mode set") |
115 |
|
116 | if args.init: |
117 | logger.debug("INIT mode set") |
118 | else: |
119 | logger.debug("SYNC mode set implicitly (INIT not set)") |
120 |
|
121 | if args.dryrun: |
122 | logger.debug("DRYRUN mode set") |
123 |
|
124 | if not args.file: |
125 | logger.debug("No tracking file set. Determining default...") |
126 | args.file = os.path.expanduser(os.path.join("~", ".state.s3sync")) |
127 | else: |
128 | logger.debug(f'User supplied tracking file "{args.file[0]}". Sanitizing...') |
129 | whitespace_pattern = re.compile(r"\s+") |
130 | args.file = re.sub(whitespace_pattern, "", args.file[0]) |
131 | if not args.file: |
132 | logger.error("Inputted tracking file path string is empty") |
133 | exit(1) |
134 | if not os.path.isabs(args.file): |
135 | logger.error("Inputted tracking file path is not an absolute path") |
136 | exit(1) |
137 | logger.debug(f'Tracking file set to "{args.file}"') |
138 |
|
139 | if args.purge: |
140 | if args.init: |
141 | logger.debug("PURGE mode set") |
142 | else: |
143 | logger.debug("PURGE mode set, but INIT mode isn't. Ignoring") |
144 |
|
145 | if args.overwrite: |
146 | if args.init: |
147 | logger.debug("OVERWRITE mode set") |
148 | else: |
149 | logger.debug("OVERWRITE mode set, but INIT mode isn't. Ignoring") |
150 |
|
151 | return args |
152 |
|