Skip to content
Snippets Groups Projects
Commit 09b477de authored by ale's avatar ale
Browse files

support excluding paths from upload

parent c6466493
No related branches found
No related tags found
No related merge requests found
...@@ -52,7 +52,7 @@ class FullScan(threading.Thread): ...@@ -52,7 +52,7 @@ class FullScan(threading.Thread):
time.sleep(delay) time.sleep(delay)
def run_client(server_url, music_dir, api_key, run_once, bwlimit, def run_client(server_url, music_dir, excludes, api_key, run_once, bwlimit,
enable_watcher): enable_watcher):
log.debug('settings: server=%s, music_dir=%s, api_key=%s, ' log.debug('settings: server=%s, music_dir=%s, api_key=%s, '
'bwlimit=%s, watcher=%s' % ( 'bwlimit=%s, watcher=%s' % (
...@@ -66,14 +66,24 @@ def run_client(server_url, music_dir, api_key, run_once, bwlimit, ...@@ -66,14 +66,24 @@ def run_client(server_url, music_dir, api_key, run_once, bwlimit,
throttle.set_rate_limit(bwlimit) throttle.set_rate_limit(bwlimit)
# Warn on this condition, but don't die -- the directory might exist later! # Warn on this condition, but don't die -- the directory might exist later!
music_dir = os.path.realpath(os.path.expanduser(music_dir))
if not os.path.isdir(music_dir): if not os.path.isdir(music_dir):
log.error('The music_dir you specified does not exist') log.error('The music_dir you specified does not exist')
# Compute absolute exclude paths.
abs_excludes = []
for exclude_prefix in excludes:
if not exclude_prefix.startswith('/'):
exclude_prefix = os.path.join(music_dir, exclude_prefix)
if not exclude_prefix.endswith('/'):
exclude_prefix += '/'
abs_excludes.append(os.path.realpath(exclude_prefix))
if enable_watcher and not watcher_support: if enable_watcher and not watcher_support:
log.warn('inotify/fsevents support not enabled on this platform') log.warn('inotify/fsevents support not enabled on this platform')
enable_watcher = False enable_watcher = False
upl = upload.Uploader(server_url.rstrip('/'), api_key) upl = upload.Uploader(server_url.rstrip('/'), api_key, abs_excludes)
upl.setDaemon(True) upl.setDaemon(True)
# Start the full filesystem scan in the background. # Start the full filesystem scan in the background.
...@@ -130,6 +140,9 @@ def main(): ...@@ -130,6 +140,9 @@ def main():
parser.add_option('--music_dir', parser.add_option('--music_dir',
default='~/Music', default='~/Music',
help='Path to your music directory') help='Path to your music directory')
parser.add_option('--exclude',
action='append', default=[],
help='Exclude certain paths from scanning')
parser.add_option('--server_url', parser.add_option('--server_url',
default='https://djrandom.incal.net/receiver', default='https://djrandom.incal.net/receiver',
help='URL to the API endpoint') help='URL to the API endpoint')
...@@ -148,7 +161,6 @@ def main(): ...@@ -148,7 +161,6 @@ def main():
if not opts.api_key: if not opts.api_key:
parser.error('You must specify an API Key') parser.error('You must specify an API Key')
music_dir = os.path.expanduser(opts.music_dir)
if args: if args:
parser.error('Too many arguments') parser.error('Too many arguments')
...@@ -161,8 +173,8 @@ def main(): ...@@ -161,8 +173,8 @@ def main():
do_realtime = not opts.no_realtime_watch do_realtime = not opts.no_realtime_watch
daemonize.daemonize(opts, run_client, daemonize.daemonize(opts, run_client,
(opts.server_url, music_dir, opts.api_key, (opts.server_url, opts.music_dir, opts.exclude,
opts.once, opts.bwlimit, do_realtime)) opts.api_key, opts.once, opts.bwlimit, do_realtime))
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -60,12 +60,13 @@ class FileDatabase(object): ...@@ -60,12 +60,13 @@ class FileDatabase(object):
class Uploader(threading.Thread): class Uploader(threading.Thread):
def __init__(self, server_url, api_key, db_path=None, def __init__(self, server_url, api_key, excludes=[], db_path=None,
state_path=None): state_path=None):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.api_key = api_key self.api_key = api_key
self.server_url = server_url self.server_url = server_url
self.queue = Queue.Queue(100) self.queue = Queue.Queue(100)
self.excludes = excludes
self.db_path = db_path self.db_path = db_path
self.stats = stats.Stats(state_path) self.stats = stats.Stats(state_path)
self.opener = urllib2.build_opener(throttle.ThrottledHTTPHandler, self.opener = urllib2.build_opener(throttle.ThrottledHTTPHandler,
...@@ -114,6 +115,12 @@ class Uploader(threading.Thread): ...@@ -114,6 +115,12 @@ class Uploader(threading.Thread):
self.stats.incr('uploaded_files') self.stats.incr('uploaded_files')
log.info('successfully uploaded %s (%s)' % (path, sha1)) log.info('successfully uploaded %s (%s)' % (path, sha1))
def _should_exclude(self, path):
for exclude_prefix in self.excludes:
if path.startswith(exclude_prefix):
return True
return False
def run(self): def run(self):
log.debug('starting uploader thread') log.debug('starting uploader thread')
db = FileDatabase(self.db_path) db = FileDatabase(self.db_path)
...@@ -123,6 +130,8 @@ class Uploader(threading.Thread): ...@@ -123,6 +130,8 @@ class Uploader(threading.Thread):
path = self.queue.get() path = self.queue.get()
if path is None: if path is None:
break break
if self._should_exclude(path):
continue
if db.has(path): if db.has(path):
continue continue
self.stats.set('uploading', path) self.stats.set('uploading', path)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment