~verterok/ubuntuone-client/tritcask-fix-825366-stable-3-0

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
#!/usr/bin/python

# ubuntuone-launch - Ubuntu One storage synchronization daemon startup helper
#
# Author: John Lenton <john.lenton@canonical.com>
#
# Copyright 2010 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 3, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Ubuntu One storage synchronization daemon (syncdaemon) startup helper.

This script decides whether to start and connect syncdaemon.

 * If you've never used Ubuntu One file sync, or if you've disabled
   file sync via Ubuntu One control panel (or equivalently via setting
   files_sync_enabled to False in the syncdaemon configuration file),
   syncdaemon is not started, and nothing happens as a result of this
   script being run.

 * otherwise if syncdaemon is not already running it is started, and
   local rescan is allowed to finish.

 * if syncdaemon has never synced to the server, or if there are no
   credentials for Ubuntu One in the keyring, nothing further happens.

 * otherwise, syncdaemon is asked to connect.

"""
import sys
import os

U1ROOT = os.path.expanduser('~/Ubuntu One/')

if __name__ == '__main__':
    # this check is done early to speed up startup on people who are not
    # (yet) using the service (this way avoids all the imports).
    if not os.path.isdir(U1ROOT):
        # no directory, just quit
        sys.exit(1)

import dbus
import glib
import gobject

from dbus.mainloop.glib import DBusGMainLoop
from twisted.internet import defer

from ubuntuone.syncdaemon.config import get_user_config
from ubuntuone.platform.tools import SyncDaemonTool, is_already_running


@defer.inlineCallbacks
def main():
    """Start syncdaemon and ask it to connect, if possible."""
    gobject.set_application_name("ubuntuone-launch")

    files_sync_enabled = get_user_config().get_files_sync_enabled()
    if not files_sync_enabled:
        # SD will not start (has been disabled by user)
        raise RuntimeError("File sync is disabled")

    loop = DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus(mainloop=loop)
    sync_daemon_tool = SyncDaemonTool(bus)
    running = yield is_already_running(bus=bus)
    if not running:
        # have SD start
        yield sync_daemon_tool.start()
        yield sync_daemon_tool.wait_for_signal('StatusChanged',
                lambda a: a.get('name', '') == 'READY')


if __name__ == '__main__':
    d = main()
    # os._exit feels like it's cheating, but it's simple and fast
    d.addCallbacks(lambda _: os._exit(0), lambda _: os._exit(1))

    mainloop = glib.MainLoop()
    mainloop.run()