Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cron optimizations #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions dbdownload.conf
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Example config
source: public/
target: ./public_local
# Check interval, in seconds (0=exit after first check)
interval: 5
log: -
# Shows more output (overrides quiet)
verbose: 1
# quiet: 1
#cache:
#exec:
109 changes: 55 additions & 54 deletions dbdownload/dbdownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import time
from base64 import b64decode
from ConfigParser import SafeConfigParser
from optparse import OptionParser
from argparse import ArgumentParser

import dropbox
import jsonpickle
Expand Down Expand Up @@ -114,7 +114,14 @@ def reset(self):

def start(self):
try:
self._monitor()
while True:
self._monitor()
if self.sleep:
self._logger.debug('sleeping for %d second(s)' % self.sleep)
time.sleep(self.sleep)
else:
self._logger.debug('sleep is 0, exiting')
break
except KeyboardInterrupt:
pass

Expand Down Expand Up @@ -153,7 +160,7 @@ def _monitor(self):
except Exception as e:
self._logger.error('error getting file list')
self._logger.exception(e)
continue
return

for entry in result.entries:
if os.path.commonprefix([entry.path_lower, self.remote_dir]) == self.remote_dir:
Expand All @@ -176,10 +183,8 @@ def _monitor(self):
if changed and self.executable:
self._launch(self.executable)

# Done processing delta, sleep and check again.
tree = {}
self._logger.debug('sleeping for %d seconds' % self.sleep)
time.sleep(self.sleep)
# Done processing delta
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the "while True" is in start() now, there's no reason to have a "while True" loop in _monitor. Remove the _monitor while loop, and I think these changes should be good to go.


# Launch a program if anything has changed.
def _launch(self, prg):
Expand Down Expand Up @@ -398,31 +403,33 @@ def readline(self):
return self.fp.readline()


def parse_config(cfg, opts):
def parse_config(cfg):
parser = SafeConfigParser()
try:
fp = open(os.path.expanduser(cfg), 'r')
except Exception:
print 'Warning: can\'t open %s, using default values' % cfg
return
return {}
parser.readfp(FakeSecHead(fp))
fp.close()

opts = {}
for section_name in parser.sections():
for name, value in parser.items(section_name):
if name not in opts:
raise Exception(u'Invalid config file option \'%s\'' % name)
opts[name] = value
return opts


def create_logger(log, verbose):
def create_logger(log, verbose, quiet):
FORMAT = '%(asctime)-15s %(message)s'
console = log.strip() == '-'
if console:
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(LOGGER)
if verbose:
logger.setLevel(logging.DEBUG)
elif quiet:
logger.setLevel(logging.WARNING)
else:
logger.setLevel(logging.INFO)
if not console:
Expand All @@ -435,59 +442,53 @@ def create_logger(log, verbose):


def main():
options = {'log': '-', 'config': '~/dbdownload.conf',
'cache': '~/.dbdownload.cache', 'interval': 300,
'source': None, 'target': None, 'verbose': False, 'reset': False,
'exec': None, 'authorizeonly': False}

# First parse any command line arguments.
parser = OptionParser(description='Do one-way Dropbox synchronization')
parser.add_option('--interval', '-i', type=int, help='check interval')
parser.add_option('--config', '-c', help='configuration file')
parser.add_option('--cache', '-a', help='cache file')
parser.add_option('--log', '-l', help='logfile (pass - for console)')
parser.add_option('--source', '-s',
# First get the config file path
parser = ArgumentParser(add_help=False)
parser.add_argument('--config', '-c', help='configuration file', default='~/dbdownload.conf')
args, remaining_argv = parser.parse_known_args()

# Then parse configuration file, use config parameters as defaults
defaults = {}
if args.config:
defaults.update(parse_config(args.config))

# Finally parse the rest of command line
parser = ArgumentParser(description='Do one-way Dropbox synchronization')
parser.add_argument('--interval', '-i', type=int, help='check interval',
default=300)
parser.add_argument('--cache', '-a', help='cache file',
default='~/.dbdownload.cache')
parser.add_argument('--log', '-l', help='logfile (pass - for console)',
default='-')
parser.add_argument('--source', '-s', required='source' not in defaults,
help='source Dropbox directory to synchronize')
parser.add_option('--target', '-t', help='local directory to download to')
parser.add_option('--verbose', '-v', action='store_true',
parser.add_argument('--target', '-t', help='local directory to download to',
required='target' not in defaults)
vgroup = parser.add_mutually_exclusive_group()
vgroup.add_argument('--verbose', '-v', action='store_true',
help='enable verbose logging')
parser.add_option('--reset', '-r', action='store_true',
vgroup.add_argument('--quiet', '-q', action='store_true',
help='suppress normal output')
parser.add_argument('--reset', '-r', action='store_true',
help='reset synchronization')
parser.add_option('--authorizeonly', '-u', action='store_true',
parser.add_argument('--authorizeonly', '-u', action='store_true',
help='only authorize application and exit')
parser.add_option('--exec', '-x',
parser.add_argument('--exec', '-x',
help='execute program when directory has changed')
(opts, args) = parser.parse_args()
if args:
print 'Leftover command line arguments', args
sys.exit(1)

# Parse configuration file.
parse_config((opts.config and [opts.config] or
[options['config']])[0], options)

# Override parameters from config file with cmdline options.
for a in options:
v = getattr(opts, a)
if v:
options[a] = v

if not options['source'] or not options['target']:
error_msg = 'Please provide source and target directories'
sys.stderr.write('Error: %s\n' % error_msg)
sys.exit(-1)
parser.set_defaults(**defaults)
options = parser.parse_args(remaining_argv)


locale.setlocale(locale.LC_ALL, 'C') # To parse time correctly.

logger = create_logger(options['log'], options['verbose'])
logger = create_logger(options.log, options.verbose, options.quiet)
logger.info(u'*** DBdownload v%s starting up ***' % VERSION)

dl = DBDownload(options['source'], options['target'], options['cache'],
options['interval'], options['exec'])
if options['reset']:
dl = DBDownload(options.source, options.target, options.cache,
options.interval, getattr(options,'exec'))
if options.reset:
dl.reset()

if not opts.authorizeonly:
if not options.authorizeonly:
dl.start()
else:
dl.reset()
Expand Down