1. Jérémie Ducastel
  2. django-filewatcher

Commits

jduc...@DSITOC713.ccip.fr  committed 51e9cc4

+ configurable regex filters and alternative database through settings

  • Participants
  • Parent commits fff3f7c
  • Branches default

Comments (0)

Files changed (3)

File management/commands/filewatch.py

View file
  • Ignore whitespace
 class Command(BaseCommand):
     args = 'None'
     help = "Filewatch admin command. "
-    re_filter = re.compile('(\.py$)|(\.php\d?$)|(\.html$)|(\.js$)')
+    # importing regex filters list from settings, or default
+    regex_filters = getattr(settings, 'FILEWATCHER_REGEX_FILTERS', (
+        '\.py$', '\.php\d?$', '\.html?$', '\.js$',
+        ))
+    # building the final regex filter
+    re_filter = re.compile(u'(' + u')|('.join(regex_filters) + u')')
+    # importing database conf name from settings, or to default
+    use_db = getattr(settings, 'FILEWATCHER_USE_DB', 'default')
+    # settings counters
     added, skipped, updated, deleted = 0, 0, 0, 0
     
     option_list = BaseCommand.option_list + (
         )
 
     def handle(self, *args, **options):
-        # self.stdout.write("%s" % options)
+        # self.stdout.write("%s using %s" % (options, self.use_db))
         add_path = options.get('add', None)
         update_path = options.get('update', None)
         remove_path = options.get('remove', None)
         """ saves and print report, may email it to admins """
         report = Report()
         report.build()
-        report.save()
+        report.save(using=self.use_db)
         self.stdout.write("Report saved with %s changes\n" % report.changes)
         if do_print:
             self.stdout.write(report.content)
         
     def remove(self, path):
         for file in self.files_in_path(path):
-            if FileWatch.objects.filter(path=file).exists():
-                FileWatch.objects.filter(path=file).delete()
+            if FileWatch.objects.using(self.use_db).filter(path=file).exists():
+                FileWatch.objects.using(self.use_db).filter(path=file).delete()
                 self.deleted += 1
         self._print("Deleted %s files from watchlist" % self.deleted)
         
     def list(self):
-        for watch in FileWatch.objects.all().order_by('path'):
+        counter = 0
+        for watch in FileWatch.objects.using(self.use_db).all().order_by('path'):
             self._print("%s\n" % watch.path)
-        self._print("%s files in watchlist." % FileWatch.objects.count())
+            counter += 1
+        self._print("%s files in watchlist." % counter)
         
     def clear(self):
-        self.deleted = FileWatch.objects.count()
-        FileWatch.objects.all().delete()
+        self.deleted = FileWatch.objects.using(self.use_db).count()
+        FileWatch.objects.using(self.use_db).all().delete()
         self._print("Cleared %s files from watchlist" % self.deleted)
         
     def email_report(self, report):
     def add_file(self, path, update=False):
         """ add or update a single file's path to watch list
         if not listed yet"""
-        if FileWatch.objects.filter(path=path).exists():
+        if FileWatch.objects.using(self.use_db).filter(path=path).exists():
             if not update:
                 self.skipped += 1
                 return
             else:
-                watch = FileWatch.objects.get(path=path)
+                watch = FileWatch.objects.using(self.use_db).get(path=path)
                 self.updated += 1
         else:
             watch = FileWatch(path=path)
             self.added += 1
         watch.set_stats()
         watch.set_checksum()
-        watch.save()
+        watch.save(using=self.use_db)
         return watch
     
     def filter(self, filename):

File models.py

View file
  • Ignore whitespace
     created = models.DateTimeField()
     # file modification date
     modified = models.DateTimeField()
+    # watch start date
     watch_started = models.DateTimeField(auto_now_add=True)
+    # last file footprint updated
     watch_updated = models.DateTimeField(auto_now=True)
     
     @classmethod
     #    watch = 
     
     def get_checksum(self):
+        """ get checksum (with prefix) """
         return 'md5$' + self.file_md5()
     
     def set_checksum(self):
         return os.stat(self.path)
     
     def get_modified(self, file_stat):
+        """ get file mtime as timestamp """
         return datetime.fromtimestamp(file_stat.st_mtime)
     
     def set_stats(self):
         self.time_taken = delta.microseconds
     
     def file_changes_summary(self, filewatch, changes):
+        """ build a file changes summary line """
         txt = u"%s :" % filewatch.path
         for label in ('checksum', 'permissions', 'modified'):
             change = changes.get(label, None)

File readme.txt

View file
  • Ignore whitespace
 Reports are saved in database too. See management/commands/filewatch.py
 docstring for more options (update, list, remove, clear etc)
 
-options
-=======
+command options
+===============
 
 without option
     to build and save report
     prints watch list to standard output
     
 --clear
-    removes all files from watchlist
+    removes all files from watchlist
+    
+configuration
+=============
+
+Use the settings file.
+
+You may define an alternatve database with
+FILEWATCHER_USE_DB = 'alternate_db_configuration_name'
+
+You can also define regular expression filters to define files being added when
+the add path is a folder with
+
+FILEWATCHER_REGEX_FILTERS = ('\.py$', '\.php\d?$', '\.html?$', '\.js$',)
+
+default is above.