From 2dd84bab34940d6507d83faf00081ed7499e5161 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Mon, 28 May 2012 13:13:44 +0000 Subject: Add command to index local content --- diff --git a/local_document/activities.py b/local_document/activities.py index 380b1df..31a8e37 100644 --- a/local_document/activities.py +++ b/local_document/activities.py @@ -49,8 +49,27 @@ def checkins(context): def monitor(mounts, paths): + _Monitor(mounts, paths).serve_forever() - def found_cb(impl_path): + +def populate(mounts, paths, prefix): + _Monitor(mounts, paths, prefix).populate() + + +class _Monitor(object): + + def __init__(self, mounts, paths, prefix=''): + self._mounts = mounts + self._paths = paths + self._prefix = prefix + + def serve_forever(self): + crawler.dispatch(self._paths, self.__found_cb, self.__lost_cb) + + def populate(self): + crawler.populate(self._paths, self.__found_cb, self.__lost_cb) + + def __found_cb(self, impl_path): hashed_path, checkin_path = _checkin_path(impl_path) if exists(checkin_path): return @@ -64,7 +83,7 @@ def monitor(mounts, paths): return context = spec['Activity', 'bundle_id'] - directory = mounts.home_volume['context'] + directory = self._mounts.home_volume['context'] if directory.exists(context): directory.update(context, {'keep_impl': 2}) else: @@ -89,14 +108,14 @@ def monitor(mounts, paths): context_path = _ensure_context_path(context, hashed_path) if lexists(context_path): os.unlink(context_path) - os.symlink(impl_path, context_path) + os.symlink(impl_path[len(self._prefix):], context_path) if lexists(checkin_path): os.unlink(checkin_path) env.ensure_path(checkin_path) os.symlink(relpath(context_path, dirname(checkin_path)), checkin_path) - def lost_cb(impl_path): + def __lost_cb(self, impl_path): __, checkin_path = _checkin_path(impl_path) if not lexists(checkin_path): return @@ -109,7 +128,7 @@ def monitor(mounts, paths): if not impls: context = basename(context_dir) - directory = mounts.home_volume['context'] + directory = self._mounts.home_volume['context'] if directory.exists(context): directory.update(context, {'keep_impl': 0}) @@ -117,8 +136,6 @@ def monitor(mounts, paths): os.unlink(context_path) os.unlink(checkin_path) - crawler.dispatch(paths, found_cb, lost_cb) - def _checkin_path(impl_path): hashed_path = path_to_guid(impl_path) diff --git a/local_document/crawler.py b/local_document/crawler.py index 60323ff..b28c32f 100644 --- a/local_document/crawler.py +++ b/local_document/crawler.py @@ -28,6 +28,24 @@ from local_document import env _logger = logging.getLogger('local_document.crawler') +def populate(paths, found_cb, lost_cb): + + class FakeMonitor(object): + + def add_watch(self, *args): + pass + + def found_cb(self, path): + found_cb(path) + + def lost_cb(self, path): + lost_cb(path) + + for path in paths: + env.ensure_path(path, '') + _Root(FakeMonitor(), path) + + def dispatch(paths, found_cb, lost_cb): with _Inotify(found_cb, lost_cb) as monitor: roots = [] diff --git a/sugar-network-service b/sugar-network-service index c6f05a6..a236725 100755 --- a/sugar-network-service +++ b/sugar-network-service @@ -41,16 +41,18 @@ class NullHandler(logging.Handler): class Application(application.Daemon): + _prefix = '' + _mounts = None + _ipc_server = None + _api_server = None + _api_subscriber = None + _web_server = None + _monitor = None + _reindexer = None + def __init__(self, **kwargs): application.Daemon.__init__(self, **kwargs) - self._mounts = None - self._ipc_server = None - self._api_server = None - self._api_subscriber = None - self._web_server = None - self._monitor = None - # Disable some low level logging for log_name in ( 'requests.packages.urllib3.connectionpool', @@ -73,20 +75,40 @@ class Application(application.Daemon): application.logdir.value = sugar.profile_path('logs') application.rundir.value = join(env.local_root.value, 'run') + @application.command( + _('[PREFIX-PATH]\n' + 'Index local Sugar Network database')) + def index(self): + from sugar_network_server import resources + + if self.args: + self._prefix = abspath(self.args.pop(0)) + env.activities_root.value = \ + self._prefix + env.activities_root.value + env.local_root.value = self._prefix + env.local_root.value + + ad.index_lazy_open.value = False + try: + self._mounts = Mounts(self._db_path, resources.path) + self._populate_index() + activities.populate(self._mounts, self._activity_directories, + self._prefix) + finally: + self._mounts.close() + def run(self): while not sugar.has_pubkey(): logging.info(_('No Sugar SSH key, delay start for %s seconds'), SUGAR_REGISTRATION_TIMEOUT) coroutine.sleep(SUGAR_REGISTRATION_TIMEOUT) - db_path = join(env.local_root.value, 'local') if env.server_mode.value: from sugar_network_server import resources - self._mounts = Mounts(db_path, resources.path) + self._mounts = Mounts(self._db_path, resources.path) else: from sugar_network_server.resources.context import Context from sugar_network_server.resources.report import Report - self._mounts = Mounts(db_path, [Context, Report]) + self._mounts = Mounts(self._db_path, [Context, Report]) # Point client API to `self._mounts` directly passing over IPC client.Request.connection = self._mounts @@ -114,6 +136,8 @@ class Application(application.Daemon): try: self._ipc_server.serve_forever() finally: + if self._reindexer is not None: + self._reindexer.kill() if self._monitor is not None: self._monitor.kill() if self._web_server is not None: @@ -128,14 +152,23 @@ class Application(application.Daemon): if self._ipc_server is not None: self._ipc_server.stop() + @property + def _db_path(self): + return join(env.local_root.value, 'local') + + @property + def _activity_directories(self): + return [env.activities_root.value, + self._prefix + '/usr/share/sugar/activities', + self._prefix + '/opt/sweets'] + def _delayed_start(self): logging.info(_('Proceed delayed start')) - self._monitor = coroutine.spawn(activities.monitor, self._mounts, [ - env.activities_root.value, - '/usr/share/sugar/activities', - '/opt/sweets', - ]) + self._reindexer = coroutine.spawn(self._populate_index) + + self._monitor = coroutine.spawn(activities.monitor, self._mounts, + self._activity_directories) if env.webui.value: from sugar_network_webui import app @@ -147,6 +180,11 @@ class Application(application.Daemon): ('127.0.0.1', env.webui_port.value), app) coroutine.spawn(self._web_server.serve_forever) + def _populate_index(self): + for cls in self._mounts.home_volume.values(): + for __ in cls.populate(): + coroutine.dispatch() + # New defaults application.debug.value = sugar.logger_level() -- cgit v0.9.1