Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # ----------------------------------------------------------------------------
- # Copyright (c) 2010, Asim Ihsan
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- # THE SOFTWARE.
- # ----------------------------------------------------------------------------
- import os
- import sys
- import time
- import pprint
- import logging
- import datetime
- import glob
- import shutil
- # ----------------------------------------------------------------------------
- # Constants to change.
- # ----------------------------------------------------------------------------
- DIRECTORIES = [r"/tmp/dir1",
- r"/tmp/dir2",
- ]
- DESTINATION = r"/Volumes/MONGOOSE/temp"
- CHECK_INTERVAL = 15 * 60 # 15 minutes
- RETRY_TIME = 30 * 60 # 30 minutes
- START_TIME = datetime.time(hour=21, minute=0)
- END_TIME = datetime.time(hour=23, minute=0)
- # ----------------------------------------------------------------------------
- # ----------------------------------------------------------------------------
- # Other constants, leave alone.
- # ----------------------------------------------------------------------------
- # ----------------------------------------------------------------------------
- # create logger
- logger = logging.getLogger("monitor_directory")
- logger.setLevel(logging.DEBUG)
- # create console handler and set level to debug
- ch = logging.StreamHandler()
- ch.setLevel(logging.DEBUG)
- # create formatter
- formatter = logging.Formatter("%(asctime)s - %(message)s")
- # add formatter to ch
- ch.setFormatter(formatter)
- # add ch to logger
- logger.addHandler(ch)
- if __name__ == "__main__":
- logger.debug("making sure directories exist...")
- if any(not os.path.isdir(directory) for directory in DIRECTORIES):
- for directory in DIRECTORIES:
- if not os.path.isdir(directory):
- logger.error("directory '%s' does not exist" % (directory, ))
- sys.exit(1)
- if not os.path.isdir(DESTINATION):
- logger.error("destination directory '%s' does not exist" % (directory, ))
- sys.exit(1)
- logger.debug("all directories exist")
- logger.info("starting to monitor the following directories:\n'%s'" % (pprint.pformat(DIRECTORIES), ))
- while 1:
- time_now = datetime.datetime.now().time()
- if time_now < START_TIME or time_now > END_TIME:
- logger.debug("not the right time. will try again later.")
- time.sleep(RETRY_TIME)
- continue
- logger.debug("correct time period. what's new?")
- remote_files = [(path, os.stat(path).st_mtime) for subdir in DIRECTORIES for path in glob.glob(os.path.join(subdir, "*"))]
- local_files = [(path, os.stat(path).st_mtime) for path in glob.glob(os.path.join(DESTINATION, "*"))]
- local_base_lookup = dict([(os.path.basename(path), m_time) for (path, m_time) in local_files])
- today = datetime.date.today()
- for (remote_path, remote_m_time) in remote_files:
- logger.debug("considering %s..." % (remote_path, ))
- # do not consider files that have not been modified today.
- if datetime.datetime.fromtimestamp(remote_m_time).date() != today:
- logger.debug("remote file %s too old" % (remote_path, ))
- continue
- if os.path.basename(remote_path) not in local_base_lookup:
- logger.debug("remote path %s does not exist locally. will copy." % (remote_path, ))
- dest = os.path.join(DESTINATION, os.path.basename(remote_path))
- if os.path.isdir(remote_path):
- shutil.copytree(remote_path, dest)
- else:
- shutil.copy2(remote_path, dest)
- #elif remote_m_time > local_base_lookup[os.path.basename(remote_path)]:
- # logger.debug("remote path %s exists locally but is newer. will overwrite." % (remote_path, ))
- # if os.path.isdir(remote_path):
- # shutil.rmtree(os.path.join(local_base_lookup[os.path.basename(remote_path)]))
- # else:
- # os.delete(os.path.join(local_base_lookup[os.path.basename(remote_path)]))
- # if os.path.isdir(remote_path):
- # shutil.copytree(remote_path, dest)
- # else:
- # shutil.copy2(remote_path, dest)
- logger.debug("finished this interval, sleeping...")
- time.sleep(CHECK_INTERVAL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement