~ubuntu-branches/ubuntu/precise/ubuntuone-client/precise-201112142106

« back to all changes in this revision

Viewing changes to bin/ubuntuone-syncdaemon

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-08-09 12:47:56 UTC
  • mfrom: (1.1.53 upstream)
  • Revision ID: james.westby@ubuntu.com-20110809124756-7nzilp3oix0a1yl9
Tags: 1.7.1-0ubuntu1
* New upstream release.
* debian/*:
  - Removed obsolete pycompat file
  - Removed ubuntuone-client-gnome deps and binary packaging, as it was
    moved out to separate project upstream.
  - Updated copyright to remove obsolete file reference
* debian/patches:
  - Removed patches which have been included upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import sys
22
22
 
23
23
if sys.platform == 'win32':
24
 
    from txnamedpipes.reactor import install
25
 
    install()
 
24
    # use the standard reactor on windows
 
25
    pass
26
26
else:
27
27
    from twisted.internet import glib2reactor
28
28
    glib2reactor.install()
37
37
import sys
38
38
import shutil
39
39
 
40
 
from ubuntuone.platform import is_already_running, set_application_name, is_root
 
40
from ubuntuone.platform import (
 
41
    can_write,
 
42
    set_dir_readwrite,
 
43
    is_already_running,
 
44
    is_root,
 
45
    make_dir,
 
46
    path_exists,
 
47
    set_application_name,
 
48
)
41
49
from ubuntuone.syncdaemon import logger, config
42
50
from ubuntuone.syncdaemon.config import (
43
51
    get_config_files,
45
53
 
46
54
from ubuntuone.syncdaemon.main import Main
47
55
 
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 (
50
58
    xdg_cache_home,
51
59
    xdg_data_home,
52
60
)
53
61
 
54
62
 
 
63
class DeathException(Exception):
 
64
    """The process has commited seppuku."""
 
65
 
 
66
 
55
67
def die(msg):
 
68
    """Write the error message an die."""
56
69
    logger.root_logger.warning(msg)
57
70
    sys.stderr.write(msg + '\n')
58
 
    sys.exit(1)
 
71
    raise DeathException()
 
72
 
 
73
 
 
74
def check_death(failure):
 
75
    """Stop the reactor and exit the process."""
 
76
    failure.trap(DeathException)
 
77
    reactor.callWhenRunning(reactor.stop)
59
78
 
60
79
 
61
80
def main(argv):
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)
72
 
 
 
91
    d = async_main(parser, options, argv)
 
92
    d.addErrback(check_death)
 
93
 
 
94
    if options.debug_lsprof_file:
 
95
        try:
 
96
            from bzrlib.lsprof import profile
 
97
            ret, stats = profile(reactor.run)
 
98
            stats.save(options.debug_lsprof_file)
 
99
        except ImportError:
 
100
            logger.root_logger.warning('bzrlib.lsprof not available')
 
101
            reactor.run()
 
102
    else:
 
103
        reactor.run()
 
104
 
 
105
 
 
106
@defer.inlineCallbacks
 
107
def async_main(parser, options, argv):
 
108
    """The client entry point that can yield."""
73
109
    logger.init()
74
110
    if options.debug:
75
111
        logger.set_debug('stdout file')
92
128
        die('Files synchronization is disabled.')
93
129
 
94
130
    # check if there is another instance running
95
 
    if is_already_running():
 
131
    is_running = yield is_already_running()
 
132
 
 
133
    if is_running:
96
134
        die('Another instance is running')
97
135
 
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
110
 
                os.makedirs(parent)
 
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)
118
156
 
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)
123
161
 
124
162
    logger.rotate_logs()
 
163
 
 
164
    assert isinstance(options.root_dir, str)
 
165
    assert isinstance(options.shares_dir, str)
 
166
    assert isinstance(options.data_dir, str)
 
167
 
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,
173
216
            guppy.heapy.RM.on()
174
217
 
175
218
    main.start()
176
 
    if options.debug_lsprof_file:
177
 
        try:
178
 
            from bzrlib.lsprof import profile
179
 
            ret, stats = profile(reactor.run)
180
 
            stats.save(options.debug_lsprof_file)
181
 
        except ImportError:
182
 
            logger.root_logger.warning('bzrlib.lsprof not available')
183
 
            reactor.run()
184
 
    else:
185
 
        reactor.run()
186
219
 
187
220
 
188
221
if __name__ == '__main__':