From 09b477de7fb3225f60de1cafb3efbf59b814b941 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 11 Feb 2012 18:45:08 +0000
Subject: [PATCH] support excluding paths from upload

---
 client/djrandom_client/client.py | 22 +++++++++++++++++-----
 client/djrandom_client/upload.py | 11 ++++++++++-
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/client/djrandom_client/client.py b/client/djrandom_client/client.py
index 0daaa80..ce005cc 100644
--- a/client/djrandom_client/client.py
+++ b/client/djrandom_client/client.py
@@ -52,7 +52,7 @@ class FullScan(threading.Thread):
             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):
     log.debug('settings: server=%s, music_dir=%s, api_key=%s, '
               'bwlimit=%s, watcher=%s' % (
@@ -66,14 +66,24 @@ def run_client(server_url, music_dir, api_key, run_once, bwlimit,
         throttle.set_rate_limit(bwlimit)
 
     # 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):
         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:
         log.warn('inotify/fsevents support not enabled on this platform')
         enable_watcher = False
 
-    upl = upload.Uploader(server_url.rstrip('/'), api_key)
+    upl = upload.Uploader(server_url.rstrip('/'), api_key, abs_excludes)
     upl.setDaemon(True)
 
     # Start the full filesystem scan in the background.
@@ -130,6 +140,9 @@ def main():
     parser.add_option('--music_dir',
                       default='~/Music',
                       help='Path to your music directory')
+    parser.add_option('--exclude',
+                      action='append', default=[],
+                      help='Exclude certain paths from scanning')
     parser.add_option('--server_url',
                       default='https://djrandom.incal.net/receiver',
                       help='URL to the API endpoint')
@@ -148,7 +161,6 @@ def main():
     if not opts.api_key:
         parser.error('You must specify an API Key')
 
-    music_dir = os.path.expanduser(opts.music_dir)
     if args:
         parser.error('Too many arguments')
 
@@ -161,8 +173,8 @@ def main():
     do_realtime = not opts.no_realtime_watch
 
     daemonize.daemonize(opts, run_client, 
-                        (opts.server_url, music_dir, opts.api_key,
-                         opts.once, opts.bwlimit, do_realtime))
+                        (opts.server_url, opts.music_dir, opts.exclude, 
+                         opts.api_key, opts.once, opts.bwlimit, do_realtime))
 
 
 if __name__ == '__main__':
diff --git a/client/djrandom_client/upload.py b/client/djrandom_client/upload.py
index 44a6513..391b679 100644
--- a/client/djrandom_client/upload.py
+++ b/client/djrandom_client/upload.py
@@ -60,12 +60,13 @@ class FileDatabase(object):
 
 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):
         threading.Thread.__init__(self)
         self.api_key = api_key
         self.server_url = server_url
         self.queue = Queue.Queue(100)
+        self.excludes = excludes
         self.db_path = db_path
         self.stats = stats.Stats(state_path)
         self.opener = urllib2.build_opener(throttle.ThrottledHTTPHandler,
@@ -114,6 +115,12 @@ class Uploader(threading.Thread):
         self.stats.incr('uploaded_files')
         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):
         log.debug('starting uploader thread')
         db = FileDatabase(self.db_path)
@@ -123,6 +130,8 @@ class Uploader(threading.Thread):
                 path = self.queue.get()
                 if path is None:
                     break
+                if self._should_exclude(path):
+                    continue
                 if db.has(path):
                     continue
                 self.stats.set('uploading', path)
-- 
GitLab