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 state file used to store tracking and state information. (default: "~/.state.s3sync")', |
69 | ) |
70 | group1.add_argument( |
71 | "--dump", |
72 | action="store_true", |
73 | default=False, |
74 | help="Dump s3sync state file configuration. --dryrun implicitly enabled. (default: False)", |
75 | ) |
76 | group1.add_argument( |
77 | "--dryrun", |
78 | action="store_true", |
79 | default=False, |
80 | help="Run program logic without actually making changes. Useful when paired with debug mode to see what changes would be made. (default: False)", |
81 | ) |
82 |
|
83 | group2 = parser.add_argument_group( |
84 | "tracking file management", "Requires initialize mode to be enabled." |
85 | ) |
86 |
|
87 | group2.add_argument( |
88 | "--purge", |
89 | action="store_true", |
90 | default=False, |
91 | help="Deletes the default (if not otherwise specified) tracking configuration file if it exists. (default: False)", |
92 | ) |
93 | group2.add_argument( |
94 | "--overwrite", |
95 | action="store_true", |
96 | default=False, |
97 | help="Overwrite tracking file with new directory maps instead of appending. (default: False)", |
98 | ) |
99 |
|
100 | group3 = parser.add_argument_group( |
101 | "directory mapping", "Requires initialize mode to be enabled." |
102 | ) |
103 |
|
104 | group3.add_argument( |
105 | "--dir", |
106 | action="append", |
107 | nargs=2, |
108 | metavar=("PATH", "S3_DEST"), |
109 | help="Directory map to detail which local directory corresponds to S3 bucket " |
110 | "and key prefix. Can be used multiple times to set multiple directories. " |
111 | "Local directories must be fully expanded. S3 destination in `s3://bucket-name/prefix` " |
112 | "format. Example: `--dir /home/josh/Documents s3://joshstockin/Documents`", |
113 | ) |
114 |
|
115 | return parser.parse_args(args) |
116 |
|
117 |
|
118 | def sanitize_arguments(args: argparse.Namespace): |
119 | if args.debug: |
120 | logger.debug("DEBUG mode set") |
121 |
|
122 | if args.init: |
123 | logger.debug("INIT mode set") |
124 | else: |
125 | logger.debug("SYNC mode set implicitly (INIT not set)") |
126 |
|
127 | if args.dryrun: |
128 | logger.debug("DRYRUN mode set") |
129 |
|
130 | if not args.file: |
131 | logger.debug("No tracking file set. Determining default...") |
132 | args.file = os.path.expanduser(os.path.join("~", ".state.s3sync")) |
133 | else: |
134 | logger.debug(f'User supplied tracking file "{args.file[0]}". Sanitizing...') |
135 | whitespace_pattern = re.compile(r"\s+") |
136 | args.file = re.sub(whitespace_pattern, "", args.file[0]) |
137 | if not args.file: |
138 | logger.error("Inputted tracking file path string is empty") |
139 | exit(1) |
140 | if not os.path.isabs(args.file): |
141 | logger.error("Inputted tracking file path is not an absolute path") |
142 | exit(1) |
143 | logger.debug(f'Tracking file set to "{args.file}"') |
144 |
|
145 | if args.purge: |
146 | if args.init: |
147 | logger.debug("PURGE mode set") |
148 | else: |
149 | logger.debug("PURGE mode set, but INIT mode isn't. Ignoring") |
150 |
|
151 | if args.overwrite: |
152 | if args.init: |
153 | logger.debug("OVERWRITE mode set") |
154 | else: |
155 | logger.debug("OVERWRITE mode set, but INIT mode isn't. Ignoring") |
156 |
|
157 | return args |
158 |
|