46
54
from ubuntuone.syncdaemon.main import Main
48
from twisted.internet import reactor
49
from xdg.BaseDirectory import (
56
from twisted.internet import reactor, defer
57
from ubuntuone.platform.xdg_base_directory import (
63
class DeathException(Exception):
64
"""The process has commited seppuku."""
68
"""Write the error message an die."""
56
69
logger.root_logger.warning(msg)
57
70
sys.stderr.write(msg + '\n')
71
raise DeathException()
74
def check_death(failure):
75
"""Stop the reactor and exit the process."""
76
failure.trap(DeathException)
77
reactor.callWhenRunning(reactor.stop)
69
88
configs.extend(get_config_files())
70
89
(parser, options, argv) = config.configglue(file(configs[0]), *configs[1:],
71
90
args=args, usage=usage)
91
d = async_main(parser, options, argv)
92
d.addErrback(check_death)
94
if options.debug_lsprof_file:
96
from bzrlib.lsprof import profile
97
ret, stats = profile(reactor.run)
98
stats.save(options.debug_lsprof_file)
100
logger.root_logger.warning('bzrlib.lsprof not available')
106
@defer.inlineCallbacks
107
def async_main(parser, options, argv):
108
"""The client entry point that can yield."""
75
111
logger.set_debug('stdout file')
92
128
die('Files synchronization is disabled.')
94
130
# check if there is another instance running
95
if is_already_running():
131
is_running = yield is_already_running()
96
134
die('Another instance is running')
98
136
# check if we are using xdg_data_home and it doesn't exists
99
137
if xdg_data_home in options.data_dir and \
100
not os.path.exists(options.data_dir):
138
not path_exists(options.data_dir):
101
139
# if we have metadata in the old xdg_cache, move it!
102
140
old_data_dir = options.data_dir.replace(xdg_data_home, xdg_cache_home)
103
if os.path.exists(old_data_dir):
141
if path_exists(old_data_dir):
104
142
parent = os.path.dirname(options.data_dir)
105
if os.path.exists(parent) and not os.access(parent, os.W_OK):
143
if path_exists(parent) and not can_write(parent):
106
144
# make the parent dir writable
107
os.chmod(parent, 0775)
108
elif not os.path.exists(parent):
145
set_dir_readwrite(parent)
146
elif not path_exists(parent):
109
147
# if it don't exits
148
make_dir(parent, recursive=True)
111
149
shutil.move(old_data_dir, options.data_dir)
112
if not os.path.exists(options.data_dir):
150
if not path_exists(options.data_dir):
113
151
parent = os.path.dirname(options.data_dir)
114
if os.path.exists(parent) and not os.access(parent, os.W_OK):
152
if path_exists(parent) and not can_write(parent):
115
153
# make the parent dir writable
116
os.chmod(parent, 0775)
117
os.makedirs(options.data_dir)
154
set_dir_readwrite(parent)
155
make_dir(options.data_dir, recursive=True)
119
157
# create the partials_dir
120
158
partials_dir = os.path.join(xdg_cache_home, 'ubuntuone', 'partials')
121
if not os.path.exists(partials_dir):
122
os.makedirs(partials_dir)
159
if not path_exists(partials_dir):
160
make_dir(partials_dir, recursive=True)
124
162
logger.rotate_logs()
164
assert isinstance(options.root_dir, str)
165
assert isinstance(options.shares_dir, str)
166
assert isinstance(options.data_dir, str)
125
168
main = Main(options.root_dir, options.shares_dir, options.data_dir,
126
169
partials_dir, host=options.host, port=int(options.port),
127
170
dns_srv=options.dns_srv, ssl=True,