|
18 | 18 | import argparse |
19 | 19 | import argcomplete |
20 | 20 | import logging |
| 21 | +import pkg_resources |
21 | 22 | import kaptan |
22 | | -from . import config |
23 | | -from distutils.util import strtobool |
24 | | -from . import log, util, exc, WorkspaceBuilder, Server |
| 23 | +from . import log, util, exc, WorkspaceBuilder, Server, config |
25 | 24 | from .util import ascii_lowercase, input |
26 | | -import pkg_resources |
| 25 | +from .workspacebuilder import freeze |
| 26 | +from distutils.util import strtobool |
| 27 | + |
27 | 28 |
|
28 | 29 | __version__ = pkg_resources.require("tmuxp")[0].version |
29 | 30 |
|
@@ -281,6 +282,70 @@ def load_workspace(config_file, args): |
281 | 282 | sys.exit() |
282 | 283 |
|
283 | 284 |
|
| 285 | +def command_freeze(args): |
| 286 | + """ Import teamocil config to tmuxp format. """ |
| 287 | + |
| 288 | + t = Server( |
| 289 | + socket_name=args.socket_name, |
| 290 | + socket_path=args.socket_path |
| 291 | + ) |
| 292 | + |
| 293 | + logger.error(args) |
| 294 | + session = t.findWhere({ |
| 295 | + 'session_name': args.session_name |
| 296 | + }) |
| 297 | + |
| 298 | + sconf = freeze(session) |
| 299 | + configparser = kaptan.Kaptan() |
| 300 | + newconfig = config.inline(sconf) |
| 301 | + configparser.import_config(newconfig) |
| 302 | + config_format = prompt_choices('Convert to', choices=[ |
| 303 | + 'yaml', 'json'], default='yaml') |
| 304 | + |
| 305 | + if config_format == 'yaml': |
| 306 | + newconfig = configparser.export( |
| 307 | + 'yaml', indent=2, default_flow_style=False, safe=True |
| 308 | + ) |
| 309 | + elif config_format == 'json': |
| 310 | + newconfig = configparser.export('json', indent=2) |
| 311 | + else: |
| 312 | + sys.exit('Unknown config format.') |
| 313 | + |
| 314 | + print(newconfig) |
| 315 | + print( |
| 316 | + '---------------------------------------------------------------') |
| 317 | + print( |
| 318 | + 'Configuration import does its best to convert teamocil files.\n') |
| 319 | + if prompt_yes_no( |
| 320 | + 'The new config *WILL* require adjusting afterwards. Save config?' |
| 321 | + ): |
| 322 | + dest = None |
| 323 | + while not dest: |
| 324 | + dest_prompt = prompt('Save to: ', os.path.abspath( |
| 325 | + os.path.join(config_dir, 'myimport.%s' % config_format))) |
| 326 | + if os.path.exists(dest_prompt): |
| 327 | + print('%s exists. Pick a new filename.' % dest_prompt) |
| 328 | + continue |
| 329 | + |
| 330 | + dest = dest_prompt |
| 331 | + |
| 332 | + dest = os.path.abspath(os.path.relpath(dest)) |
| 333 | + if prompt_yes_no('Write to %s?' % dest): |
| 334 | + buf = open(dest, 'w') |
| 335 | + buf.write(newconfig) |
| 336 | + buf.close() |
| 337 | + |
| 338 | + print('Saved to %s.' % dest) |
| 339 | + else: |
| 340 | + print( |
| 341 | + 'tmuxp has examples in JSON and YAML format at <http://tmuxp.readthedocs.org/en/latest/examples.html>\n' |
| 342 | + 'View tmuxp docs at <http://tmuxp.readthedocs.org/>' |
| 343 | + ) |
| 344 | + sys.exit() |
| 345 | + |
| 346 | + |
| 347 | + |
| 348 | + |
284 | 349 | def command_load(args): |
285 | 350 | """ Load a session from a tmuxp session file. """ |
286 | 351 | if args.list: |
@@ -542,6 +607,9 @@ def command_convert(args): |
542 | 607 | print('written new config to <%s>.' % (newfile)) |
543 | 608 |
|
544 | 609 |
|
| 610 | + |
| 611 | + |
| 612 | + |
545 | 613 | def command_attach_session(args): |
546 | 614 | """ Command to attach / switch client to a tmux session.""" |
547 | 615 | commands = [] |
@@ -624,6 +692,14 @@ def get_parser(): |
624 | 692 | type=str, |
625 | 693 | ).completer = SessionCompleter |
626 | 694 |
|
| 695 | + freeze = subparsers.add_parser('freeze') |
| 696 | + freeze.set_defaults(callback=command_freeze) |
| 697 | + |
| 698 | + freeze.add_argument( |
| 699 | + dest='session_name', |
| 700 | + type=str, |
| 701 | + ).completer = SessionCompleter |
| 702 | + |
627 | 703 | load = subparsers.add_parser('load') |
628 | 704 |
|
629 | 705 | loadgroup = load.add_mutually_exclusive_group(required=True) |
@@ -758,6 +834,8 @@ def main(): |
758 | 834 | command_import_teamocil(args) |
759 | 835 | elif args.callback is command_import_tmuxinator: |
760 | 836 | command_import_tmuxinator(args) |
| 837 | + elif args.callback is command_freeze: |
| 838 | + command_freeze(args) |
761 | 839 | elif args.callback is command_attach_session: |
762 | 840 | command_attach_session(args) |
763 | 841 | elif args.callback is command_kill_session: |
|
0 commit comments