From 973e934151bf8c6fc113b02e2cae7c984edad1ec Mon Sep 17 00:00:00 2001
From: James <james@sourcefabric-DX4840.(none)>
Date: Tue, 4 Oct 2011 17:20:22 -0400
Subject: [PATCH] CC-2927: Media Monitor: if new dir is generated in the
 process of importing a file, airtime doesn't import the file.

- fixed
---
 python_apps/media-monitor/MediaMonitor.py           |  2 +-
 .../airtimefilemonitor/airtimeprocessevent.py       | 13 ++-----------
 .../airtimefilemonitor/mediamonitorcommon.py        |  5 ++++-
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/python_apps/media-monitor/MediaMonitor.py b/python_apps/media-monitor/MediaMonitor.py
index 1b459c5f2..32ca17878 100644
--- a/python_apps/media-monitor/MediaMonitor.py
+++ b/python_apps/media-monitor/MediaMonitor.py
@@ -58,7 +58,7 @@ try:
 
 
     wm = WatchManager()
-    mmc = MediaMonitorCommon(config)
+    mmc = MediaMonitorCommon(config, wm=wm)
     pe = AirtimeProcessEvent(queue=multi_queue, airtime_config=config, wm=wm, mmc=mmc, api_client=api_client)
 
     bootstrap = AirtimeMediaMonitorBootstrap(logger, pe, api_client, mmc)
diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py
index 30685b54d..5c8e9f9d6 100644
--- a/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py
+++ b/python_apps/media-monitor/airtimefilemonitor/airtimeprocessevent.py
@@ -90,12 +90,8 @@ class AirtimeProcessEvent(ProcessEvent):
     
     def process_IN_CREATE(self, event):
         self.logger.info("event: %s", event)
-        if not event.dir:
-            if self.mmc.is_parent_directory(event.pathname, self.config.recorded_directory):
-                self.file_events.append({'mode': self.config.MODE_CREATE, 'filepath': event.pathname, 'is_recorded_show': True})
-            # record the timestamp of the time on IN_CREATE event
-            self.create_dict[event.pathname] = time.time()
-            
+        # record the timestamp of the time on IN_CREATE event
+        self.create_dict[event.pathname] = time.time()
         
     #event.dir: True if the event was raised against a directory.
     #event.name: filename
@@ -171,11 +167,6 @@ class AirtimeProcessEvent(ProcessEvent):
         else:
             self.cookies_IN_MOVED_FROM[event.cookie] = (event, time.time())
 
-
-    #Some weird thing to note about this event: it seems that if a file is moved to a newly
-    #created directory, then the IN_MOVED_FROM event will be called, but instead of a corresponding
-    #IN_MOVED_TO event, a IN_CREATED event will happen instead. However if the directory existed before
-    #then the IN_MOVED_TO event will be called.
     def process_IN_MOVED_TO(self, event):
         self.logger.info("process_IN_MOVED_TO: %s", event)
         #if stuff dropped in stor via a UI move must change file permissions.
diff --git a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py
index d8ae5d1e6..bf0ab3f8e 100644
--- a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py
+++ b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py
@@ -6,16 +6,18 @@ import logging
 from subprocess import Popen, PIPE
 from airtimemetadata import AirtimeMetadata
 from api_clients import api_client
+import pyinotify
 
 class MediaMonitorCommon:
 
     timestamp_file = "/var/tmp/airtime/last_index"
 
-    def __init__(self, airtime_config):
+    def __init__(self, airtime_config, wm=None):
         self.supported_file_formats = ['mp3', 'ogg']
         self.logger = logging.getLogger()
         self.config = airtime_config
         self.md_manager = AirtimeMetadata()
+        self.wm = wm
 
     def is_parent_directory(self, filepath, directory):
         filepath = os.path.normpath(filepath)
@@ -85,6 +87,7 @@ class MediaMonitorCommon:
             omask = os.umask(0)
             if not os.path.exists(directory):
                 os.makedirs(directory, 02777)
+                self.wm.add_watch(directory, pyinotify.ALL_EVENTS, rec=True, auto_add=True)
             elif not os.path.isdir(directory):
                 #path exists but it is a file not a directory!
                 self.logger.error("path %s exists, but it is not a directory!!!")