-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.py
More file actions
executable file
·165 lines (151 loc) · 5.67 KB
/
Copy pathdeploy.py
File metadata and controls
executable file
·165 lines (151 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
'''
The deploy script
Currently deploys only via rsync. To configure verbosity, set the environment
variable JEKYLL_BUILD_VERBOSITY to -1, 0, or 1, where -1 means silent, 0 means
normal, and 1 means verbose.
'''
import argparse
import jsonc as json
import os
import shlex
from typing import Any, Dict, Optional, List
from subprocess import check_call, CalledProcessError
if os.name == 'nt':
# Make the print command automatically flush in Windows. Otherwise, nothing
# will appear until exit.
import functools
print = functools.partial(print, flush=True)
verbosity = 0
if os.environ.get('JEKYLL_BUILD_VERBOSITY', False) != False:
verbosity = int(os.environ['JEKYLL_BUILD_VERBOSITY'])
class GenericContainer:
site_dir: Optional[str]
user: Optional[str]
remote_path: Optional[str]
delete: bool
port: Optional[int]
flags: List[str]
exclude: List[str]
include: List[str]
exclude_file: Optional[str]
include_file: Optional[str]
def __str__(self) -> str:
return '\n'.join(
f'{repr(k)}: {repr(v)}' for k, v in sorted(self.__dict__.items()))
def make_rsync_cmd(container: GenericContainer) -> List[str]:
cmd = ['rsync']
if verbosity == 1:
cmd += ['--verbose']
elif verbosity == -1:
cmd += ['--quiet']
if container.flags:
cmd += container.flags
if container.exclude_file:
cmd += ['--exclude-from', container.exclude_file]
if container.exclude:
for f in container.exclude:
cmd += ['--exclude', f]
if container.include_file:
cmd += ['--include-from', container.include_file]
if container.include:
for f in container.include:
cmd += ['--include', f]
if container.user and container.port:
cmd += [f"--rsh=ssh -p{container.port}"]
if container.delete:
cmd += ['--delete']
local = container.site_dir
if os.name == 'nt' and local[1] == ':':
message = str("Windows rsync can't handle pathnames which include a "
"drive letter. Thus, the computed path for the site "
f"directory, {local}, will make rsync error out. To fix "
"this, make sure that your current working directory is "
f"in the same drive as {local}.")
raise RuntimeError(message)
if local is not None and not local.endswith(os.path.sep) and not local.endswith('/'):
local += os.path.sep
remote = []
if container.user:
remote.append(container.user)
remote.append(container.remote_path)
remote = ':'.join(remote)
cmd += [local, remote]
return cmd
def parse_args() -> argparse.Namespace:
def config_file(s: str) -> Dict[str, Any]:
try:
with open(s) as f:
return json.loads(f.read())
except (FileNotFoundError, IsADirectoryError, json.JSONDecodeError):
raise argparse.ArgumentTypeError(
f"The file {s} isn't a valid config file!")
def valid_directory(s: str) -> str:
s = os.path.abspath(s)
if os.path.isdir(s):
if len(os.listdir(s)) > 0:
return os.path.relpath(s)
else:
raise argparse.ArgumentTypeError(
"The source directory is empty! Either it isn't a valid "
"source directory or you haven't built the site yet.")
else:
raise argparse.ArgumentTypeError(
f"The path {s} doesn't refer to a directory!")
parser = argparse.ArgumentParser(description=__doc__)
add = parser.add_argument
add(dest='source', metavar='LOCAL_SITE_SOURCE', type=valid_directory)
add('-c', '--config', default='_deploy.jsonc', type=config_file)
add('-n', '--dry-run', action='store_true')
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.config.get('method', '') != 'rsync':
exit('Currently, the only deploy method supported is rsync. Please set '
'the method appropriately in the deploy config file.')
c = GenericContainer()
c.site_dir = args.config.get('site_dir', None)
if c.site_dir:
print('WARNING: The site_dir setting in the config file is IGNORED. Set'
' it via the command line.')
c.site_dir = args.source
c.user = args.config.get('user', None)
c.remote_path = args.config.get('remote_path', None)
c.delete = args.config.get('delete', False)
c.port = args.config.get('port', None)
c.flags = args.config.get('flags', '-avz')
c.exclude = args.config.get('exclude', [])
c.include = args.config.get('include', [])
c.exclude_file = args.config.get('exclude-from', None)
c.include_file = args.config.get('include-from', None)
if isinstance(c.flags, str):
c.flags = shlex.split(c.flags)
if isinstance(c.include, str):
c.include = [c.include]
if isinstance(c.exclude, str):
c.exclude = [c.exclude]
if verbosity == 1:
print(c)
cmd = make_rsync_cmd(c)
if args.dry_run:
print('Dry run selected.')
if verbosity >= 0:
if c.user:
print(f'Deploying to {c.user}...')
else:
print('Deploying...')
if verbosity == 1 or args.dry_run:
print('Executing the rsync command: ' + str(cmd))
try:
if args.dry_run:
cmd.insert(1, '--dry-run')
check_call(cmd)
except CalledProcessError as e:
print(f'Error running rsync: rsync exited with status {e.returncode}')
exit(32)
else:
if verbosity == 1:
print('rsync completed successfully')
exit(0)
if __name__ == '__main__':
main()