-
Notifications
You must be signed in to change notification settings - Fork 232
/
Copy pathfreeze.py
202 lines (173 loc) · 5.89 KB
/
freeze.py
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import argparse
import os
import pathlib
import sys
import typing as t
from libtmux.server import Server
from tmuxp.config_reader import ConfigReader
from tmuxp.exc import TmuxpException
from tmuxp.workspace.finders import get_workspace_dir
from .. import exc, util
from ..workspace import freezer
from .utils import prompt, prompt_choices, prompt_yes_no
if t.TYPE_CHECKING:
from typing_extensions import Literal, TypeAlias, TypeGuard
CLIOutputFormatLiteral: TypeAlias = Literal["yaml", "json"]
class CLIFreezeNamespace(argparse.Namespace):
session_name: str
socket_name: t.Optional[str]
socket_path: t.Optional[str]
workspace_format: t.Optional["CLIOutputFormatLiteral"]
save_to: t.Optional[str]
answer_yes: t.Optional[bool]
quiet: t.Optional[bool]
force: t.Optional[bool]
def session_completion(ctx, params, incomplete):
server = Server()
choices = [session.name for session in server.sessions]
return sorted(str(c) for c in choices if str(c).startswith(incomplete))
def create_freeze_subparser(
parser: argparse.ArgumentParser,
) -> argparse.ArgumentParser:
parser.add_argument(
dest="session_name",
metavar="session-name",
nargs="?",
action="store",
)
parser.add_argument(
"-S", dest="socket_path", metavar="socket-path", help="pass-through for tmux -S"
)
parser.add_argument(
"-L", dest="socket_name", metavar="socket-name", help="pass-through for tmux -L"
)
parser.add_argument(
"-f",
"--workspace-format",
choices=["yaml", "json"],
help="format to save in",
)
parser.add_argument(
"-o",
"--save-to",
metavar="output-path",
type=pathlib.Path,
help="file to save to",
)
parser.add_argument(
"--yes",
"-y",
dest="answer_yes",
action="store_true",
help="always answer yes",
)
parser.add_argument(
"--quiet",
"-q",
dest="quiet",
action="store_true",
help="don't prompt for confirmation",
)
parser.add_argument(
"--force",
dest="force",
action="store_true",
help="overwrite the workspace file",
)
return parser
def command_freeze(
args: CLIFreezeNamespace,
parser: t.Optional[argparse.ArgumentParser] = None,
) -> None:
"""Snapshot a tmux session into a tmuxp workspace.
If SESSION_NAME is provided, snapshot that session. Otherwise, use the
current session."""
server = Server(socket_name=args.socket_name, socket_path=args.socket_path)
try:
if args.session_name:
session = server.sessions.get(session_name=args.session_name, default=None)
else:
session = util.get_session(server)
if not session:
raise exc.SessionNotFound()
except TmuxpException as e:
print(e)
return
frozen_workspace = freezer.freeze(session)
workspace = freezer.inline(frozen_workspace)
configparser = ConfigReader(workspace)
if not args.quiet:
print(
"---------------------------------------------------------------"
"\n"
"Freeze does its best to snapshot live tmux sessions.\n"
)
if not (
args.answer_yes
or prompt_yes_no(
"The new workspace will require adjusting afterwards. Save workspace file?"
)
):
if not args.quiet:
print(
"tmuxp has examples in JSON and YAML format at "
"<http://tmuxp.git-pull.com/examples.html>\n"
"View tmuxp docs at <http://tmuxp.git-pull.com/>."
)
sys.exit()
dest = args.save_to
while not dest:
save_to = os.path.abspath(
os.path.join(
get_workspace_dir(),
"{}.{}".format(
frozen_workspace.get("session_name"),
args.workspace_format or "yaml",
),
)
)
dest_prompt = prompt(
"Save to: %s" % save_to,
default=save_to,
)
if not args.force and os.path.exists(dest_prompt):
print("%s exists. Pick a new filename." % dest_prompt)
continue
dest = dest_prompt
dest = os.path.abspath(os.path.relpath(os.path.expanduser(dest)))
workspace_format = args.workspace_format
valid_workspace_formats: t.List["CLIOutputFormatLiteral"] = ["json", "yaml"]
def is_valid_ext(stem: t.Optional[str]) -> "TypeGuard[CLIOutputFormatLiteral]":
return stem in valid_workspace_formats
if not is_valid_ext(workspace_format):
def extract_workspace_format(
val: str,
) -> t.Optional["CLIOutputFormatLiteral"]:
suffix = pathlib.Path(val).suffix
if isinstance(suffix, str):
suffix = suffix.lower().lstrip(".")
if is_valid_ext(suffix):
return suffix
return None
workspace_format = extract_workspace_format(dest)
if not is_valid_ext(workspace_format):
workspace_format = prompt_choices(
"Couldn't ascertain one of [%s] from file name. Convert to"
% ", ".join(valid_workspace_formats),
choices=valid_workspace_formats,
default="yaml",
)
if workspace_format == "yaml":
workspace = configparser.dump(
format="yaml", indent=2, default_flow_style=False, safe=True
)
elif workspace_format == "json":
workspace = configparser.dump(format="json", indent=2)
if args.answer_yes or prompt_yes_no("Save to %s?" % dest):
destdir = os.path.dirname(dest)
if not os.path.isdir(destdir):
os.makedirs(destdir)
with open(dest, "w") as buf:
buf.write(workspace)
if not args.quiet:
print("Saved to %s." % dest)