This repository has been archived by the owner on May 14, 2021. It is now read-only.
/
actions.py
98 lines (69 loc) · 2.59 KB
/
actions.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
import configtools
import argparse
import util
import remote
import logging
import os
import sys
def _configure_logging(args):
if 'log_level' in args:
logging.getLogger().setLevel(args['log_level'])
class BasicAction:
def __init__(self, args):
self.config = args
_configure_logging(args)
def launch(self):
raise RuntimeError('Can not launch basic action')
class ConfigurableAction(BasicAction):
def __init__(self, args):
config_from_file = configtools.get_settings()
config_from_file.update(args)
args = config_from_file
super().__init__(args)
class SendAction(ConfigurableAction):
def __init__(self, args):
super().__init__(args)
def launch(self):
return remote.send(self.config['local_root'], self.config['remote_host'], self.config['remote_root'])
class ReceiveAction(ConfigurableAction):
def __init__(self, args):
super().__init__(args)
def launch(self):
return remote.receive(self.config['local_root'], self.config['remote_host'], self.config['remote_root'])
def _receive_if_failed(config):
if 'receive_if_failed' not in config or config['receive_if_failed'] == 'ask':
try:
return util.ask_remote_failed()
except RuntimeError as e:
logging.warning(str(e))
logging.warning("Assume ansver is 'no'")
return False
else:
return config['receive_if_failed'] == 'yes'
class RemoteRunAction(ConfigurableAction):
def __init__(self, args):
super().__init__(args)
def launch(self):
sent = remote.send(self.config['local_root'], self.config['remote_host'], self.config['remote_root'])
if sent:
result = remote.remote_exec(self.config['remote_host'], self.config['remote_dir'], self.config['command'])
if result or _receive_if_failed(self.config):
remote.receive(self.config['local_root'], self.config['remote_host'], self.config['remote_root'])
else:
logging.warning('Result not synced from server.')
logging.info('To sync it manually use' + ' `' + sys.argv[0] + ' -r`')
class InitAction(BasicAction):
def __init__(self, args):
super().__init__(args)
def launch(self):
configtools.create_initial_config()
class IsConfiguredAction(BasicAction):
def __init__(self, args):
super().__init__(args)
def launch(self):
# TODO: relaise it
try:
configtools.get_settings()
return True
except:
return False