diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py index 998446596..9d1526d8b 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimemediamonitorbootstrap.py @@ -77,10 +77,8 @@ class AirtimeMediaMonitorBootstrap(): dir -- pathname of the directory """ def sync_database_to_filesystem(self, dir_id, dir): - + # TODO: is this line even necessary? dir = os.path.normpath(dir)+"/" - - """ set to hold new and/or modified files. We use a set to make it ok if files are added twice. This is because some of the tests for new files return result sets that are not @@ -94,24 +92,23 @@ class AirtimeMediaMonitorBootstrap(): for file in files['files']: db_known_files_set.add(file) - all_files = self.mmc.scan_dir_for_new_files(dir) + all_files = self.mmc.clean_dirty_file_paths( self.mmc.scan_dir_for_new_files(dir) ) all_files_set = set() for file_path in all_files: - file_path = file_path.strip(" \n") - if len(file_path) > 0 and self.config.problem_directory not in file_path: + if self.config.problem_directory not in file_path: all_files_set.add(file_path[len(dir):]) # if dir doesn't exists, update db if not os.path.exists(dir): - self.pe.handle_watched_dir_missing(dir) + self.pe.handle_stdout_files(dir) if os.path.exists(self.mmc.timestamp_file): """find files that have been modified since the last time media-monitor process started.""" time_diff_sec = time.time() - os.path.getmtime(self.mmc.timestamp_file) - command = "find '%s' -iname '*.ogg' -o -iname '*.mp3' -type f -readable -mmin -%d" % (dir, time_diff_sec/60+1) + command = self.mmc.find_command(directory=dir, extra_arguments=("-type f -readable -mmin -%d" % (time_diff_sec/60+1))) else: - command = "find '%s' -iname '*.ogg' -o -iname '*.mp3' -type f -readable" % dir + command = self.mmc.find_command(directory=dir, extra_arguments="-type f -readable") self.logger.debug(command) stdout = self.mmc.exec_command(command) @@ -120,12 +117,11 @@ class AirtimeMediaMonitorBootstrap(): self.logger.error("Unrecoverable error when syncing db to filesystem.") return - new_files = stdout.splitlines() + new_files = self.mmc.clean_dirty_file_paths(stdout.splitlines()) new_and_modified_files = set() for file_path in new_files: - file_path = file_path.strip(" \n") - if len(file_path) > 0 and self.config.problem_directory not in file_path: + if self.config.problem_directory not in file_path: new_and_modified_files.add(file_path[len(dir):]) """ @@ -156,16 +152,12 @@ class AirtimeMediaMonitorBootstrap(): self.logger.debug(full_file_path) self.pe.handle_removed_file(False, full_file_path) - for file_path in new_files_set: - self.logger.debug("new file") - full_file_path = os.path.join(dir, file_path) - self.logger.debug(full_file_path) - if os.path.exists(full_file_path): - self.pe.handle_created_file(False, full_file_path, os.path.basename(full_file_path)) - for file_path in modified_files_set: - self.logger.debug("modified file") - full_file_path = "%s%s" % (dir, file_path) - self.logger.debug(full_file_path) - if os.path.exists(full_file_path): - self.pe.handle_modified_file(False, full_file_path, os.path.basename(full_file_path)) + for file_set, debug_message, handle_attribute in [(new_files_set, "new file", "handle_created_file"), + (modified_files_set, "modified file", "handle_modified_file")]: + for file_path in file_set: + self.logger.debug(debug_message) + full_file_path = os.path.join(dir, file_path) + self.logger.debug(full_file_path) + if os.path.exists(full_file_path): + getattr(self.pe,handle_attribute)(False,full_file_path, os.path.basename(full_file_path)) diff --git a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py index 274dc26d0..6e0f6f69d 100644 --- a/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py +++ b/python_apps/media-monitor/airtimefilemonitor/airtimenotifier.py @@ -101,16 +101,12 @@ class AirtimeNotifier(Notifier): self.bootstrap.sync_database_to_filesystem(new_storage_directory_id, new_storage_directory) self.config.storage_directory = os.path.normpath(new_storage_directory) - self.config.imported_directory = os.path.normpath(new_storage_directory + '/imported') - self.config.organize_directory = os.path.normpath(new_storage_directory + '/organize') + self.config.imported_directory = os.path.normpath(os.path.join(new_storage_directory, '/imported')) + self.config.organize_directory = os.path.normpath(os.path.join(new_storage_directory, '/organize')) - self.mmc.ensure_is_dir(self.config.storage_directory) - self.mmc.ensure_is_dir(self.config.imported_directory) - self.mmc.ensure_is_dir(self.config.organize_directory) - - self.mmc.is_readable(self.config.storage_directory, True) - self.mmc.is_readable(self.config.imported_directory, True) - self.mmc.is_readable(self.config.organize_directory, True) + for directory in [self.config.storage_directory, self.config.imported_directory, self.config.organize_directory]: + self.mmc.ensure_is_dir(directory) + self.mmc.is_readable(directory, True) self.watch_directory(new_storage_directory) elif m['event_type'] == "file_delete": @@ -146,7 +142,7 @@ class AirtimeNotifier(Notifier): mode = event['mode'] md = {} - md['MDATA_KEY_FILEPATH'] = filepath + md['MDATA_KEY_FILEPATH'] = os.path.normpath(filepath) if 'data' in event: file_md = event['data'] diff --git a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py index 0c2f5b68a..c81bbaf2a 100644 --- a/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py +++ b/python_apps/media-monitor/airtimefilemonitor/mediamonitorcommon.py @@ -16,14 +16,26 @@ import pyinotify class MediaMonitorCommon: timestamp_file = "/var/tmp/airtime/media-monitor/last_index" + supported_file_formats = ['mp3', 'ogg'] 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 clean_dirty_file_paths(self, dirty_files): + """ clean dirty file paths by removing blanks and removing trailing/leading whitespace""" + return filter(lambda e: len(e) > 0, [ f.strip(" \n") for f in dirty_files ]) + + def find_command(self, directory, extra_arguments=""): + """ Builds a find command that respects supported_file_formats list + Note: Use single quotes to quote arguments """ + ext_globs = [ "-iname '*.%s'" % ext for ext in self.supported_file_formats ] + find_glob = ' -o '.join(ext_globs) + return "find '%s' %s %s" % (directory, find_glob, extra_arguments) + def is_parent_directory(self, filepath, directory): filepath = os.path.normpath(filepath) directory = os.path.normpath(directory) @@ -31,7 +43,6 @@ class MediaMonitorCommon: def is_temp_file(self, filename): info = filename.split(".") - # if file doesn't have any extension, info[-2] throws exception # Hence, checking length of info before we do anything if(len(info) >= 2): @@ -41,20 +52,19 @@ class MediaMonitorCommon: def is_audio_file(self, filename): info = filename.split(".") + if len(info) < 2: return false # handle cases like filename="mp3" return info[-1].lower() in self.supported_file_formats #check if file is readable by "nobody" def is_user_readable(self, filepath, euid='nobody', egid='nogroup'): - + f = None try: uid = pwd.getpwnam(euid)[2] gid = grp.getgrnam(egid)[2] - #drop root permissions and become "nobody" os.setegid(gid) os.seteuid(uid) - - open(filepath) + f = open(filepath) readable = True except IOError: self.logger.warn("File does not have correct permissions: '%s'", filepath) @@ -65,16 +75,15 @@ class MediaMonitorCommon: self.logger.error("traceback: %s", traceback.format_exc()) finally: #reset effective user to root + if f: f.close() os.seteuid(0) os.setegid(0) - return readable # the function only changes the permission if its not readable by www-data def is_readable(self, item, is_dir): try: - return self.is_user_readable(item, 'www-data', 'www-data') \ - and self.is_user_readable(item, 'pypo', 'pypo') + return self.is_user_readable(item, 'www-data', 'www-data') except Exception, e: self.logger.warn(u"Failed to check owner/group/permissions for %s", item) return False @@ -242,7 +251,7 @@ class MediaMonitorCommon: show_name = '-'.join(title[3:]) new_md = {} - new_md["MDATA_KEY_FILEPATH"] = original_path + new_md['MDATA_KEY_FILEPATH'] = os.path.normpath(original_path) new_md['MDATA_KEY_TITLE'] = '%s-%s-%s:%s:%s' % (show_name, orig_md['MDATA_KEY_YEAR'], show_hour, show_min, show_sec) self.md_manager.save_md_to_file(new_md) @@ -280,7 +289,7 @@ class MediaMonitorCommon: return stdout def scan_dir_for_new_files(self, dir): - command = 'find "%s" -iname "*.ogg" -o -iname "*.mp3" -type f -readable' % dir.replace('"', '\\"') + command = self.find_command(directory=dir, extra_arguments="-type f -readable") self.logger.debug(command) stdout = self.exec_command(command) @@ -316,13 +325,10 @@ class MediaMonitorCommon: if return_code != 0: #print pathname for py-interpreter.log print pathname - return (return_code == 0) def move_to_problem_dir(self, source): - dest = os.path.join(self.config.problem_directory, os.path.basename(source)) - try: omask = os.umask(0) os.rename(source, dest) diff --git a/python_apps/media-monitor/install/media-monitor-copy-files.py b/python_apps/media-monitor/install/media-monitor-copy-files.py index 736456db9..d47cd1811 100644 --- a/python_apps/media-monitor/install/media-monitor-copy-files.py +++ b/python_apps/media-monitor/install/media-monitor-copy-files.py @@ -54,14 +54,10 @@ try: #create log dir create_dir(config['log_dir']) - os.system("chown -R pypo:pypo "+config["log_dir"]) #copy python files copy_dir("%s/.."%current_script_dir, config["bin_dir"]) - #set executable permissions on python files - os.system("chown -R pypo:pypo "+config["bin_dir"]) - #copy init.d script shutil.copy(config["bin_dir"]+"/airtime-media-monitor-init-d", "/etc/init.d/airtime-media-monitor")