2016-08-22 21:34:54 -04:00
|
|
|
from __future__ import print_function
|
2016-08-12 20:18:12 -04:00
|
|
|
from luigi import build
|
|
|
|
from datetime import datetime
|
2016-08-22 21:34:54 -04:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from dateutil.parser import parse
|
2016-08-24 15:52:07 -04:00
|
|
|
from six import StringIO
|
2016-08-24 22:47:11 -04:00
|
|
|
from subprocess import check_output
|
|
|
|
from os import path
|
2016-08-12 20:18:12 -04:00
|
|
|
|
2016-08-24 15:52:07 -04:00
|
|
|
from metrik.conf import get_config
|
2016-08-22 21:34:54 -04:00
|
|
|
from metrik.flows.rates_flow import LiborFlow
|
2016-08-23 22:39:32 -04:00
|
|
|
from metrik.flows.equities_flow import EquitiesFlow
|
2016-08-23 22:47:13 -04:00
|
|
|
from metrik import __version__
|
2016-08-12 20:18:12 -04:00
|
|
|
|
2016-08-22 21:34:54 -04:00
|
|
|
flows = {
|
2016-08-23 22:39:32 -04:00
|
|
|
'LiborFlow': LiborFlow,
|
|
|
|
'EquitiesFlow': EquitiesFlow
|
2016-08-22 21:34:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-23 19:13:06 -04:00
|
|
|
def run_flow(flow_class, present, live):
|
|
|
|
build([flow_class(present=present, live=live)])
|
2016-08-22 21:34:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def build_cron_file():
|
|
|
|
EXEC = 'metrik'
|
|
|
|
FLOW_FLAG = '-f'
|
2016-08-24 22:47:11 -04:00
|
|
|
USER = 'root'
|
2016-08-22 21:34:54 -04:00
|
|
|
|
2016-08-24 22:47:11 -04:00
|
|
|
metrik_command_location = path.dirname(check_output(['which', 'metrik']))
|
|
|
|
cron_header = '''# cron.d entries for metrik
|
|
|
|
|
|
|
|
SHELL=/bin/sh
|
|
|
|
PATH={}
|
|
|
|
|
|
|
|
'''.format(metrik_command_location)
|
2016-08-22 21:34:54 -04:00
|
|
|
cron_strings = []
|
|
|
|
for flow_name, flow_class in flows.items():
|
|
|
|
cron_string = flow_class.get_schedule().get_cron_string()
|
|
|
|
cron_strings.append(
|
2016-08-24 22:47:11 -04:00
|
|
|
cron_string + ' ' + USER + ' ' + EXEC + ' ' +
|
|
|
|
FLOW_FLAG + ' ' + flow_name
|
2016-08-22 21:34:54 -04:00
|
|
|
)
|
|
|
|
|
2016-08-24 22:47:11 -04:00
|
|
|
return cron_header + '\n'.join(cron_strings) + '\n'
|
2016-08-12 20:18:12 -04:00
|
|
|
|
2016-08-22 21:34:54 -04:00
|
|
|
|
|
|
|
def list_flows():
|
2016-08-23 19:13:06 -04:00
|
|
|
return "Available:\n\t" + "\n\t".join(flows.keys())
|
2016-08-22 21:34:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def handle_commandline():
|
2016-08-23 19:13:06 -04:00
|
|
|
present_moment = datetime.now()
|
2016-08-22 21:34:54 -04:00
|
|
|
parser = ArgumentParser(description='Capture ALL THE DATA off the Internet.')
|
|
|
|
parser.add_argument('-c', '--cron', dest='cron', action='store_true',
|
|
|
|
help='Build the cron file used to schedule'
|
|
|
|
'running all flows')
|
|
|
|
parser.add_argument('-d', '--date', dest='present',
|
|
|
|
help='Run a flow as if it was this time '
|
|
|
|
'(default: %(default)s).',
|
2016-08-23 19:13:06 -04:00
|
|
|
default=present_moment)
|
2016-08-22 21:34:54 -04:00
|
|
|
parser.add_argument('-f', '--flow', dest='flow', help='The flow to be run')
|
|
|
|
parser.add_argument('-l', '--list-flows', dest='list', action='store_true',
|
|
|
|
help='List all available flows to be run.')
|
2016-08-24 15:52:07 -04:00
|
|
|
parser.add_argument('-o', '--config', action='store_true')
|
2016-08-23 22:47:13 -04:00
|
|
|
parser.add_argument('-v', '--version', action='version',
|
|
|
|
version=__version__)
|
2016-08-22 21:34:54 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.cron:
|
|
|
|
print(build_cron_file())
|
|
|
|
elif args.list:
|
|
|
|
print(list_flows())
|
2016-08-24 15:52:07 -04:00
|
|
|
elif args.config:
|
|
|
|
config = get_config()
|
|
|
|
s = StringIO
|
|
|
|
config.write(s)
|
|
|
|
print(s.getvalue())
|
2016-08-22 21:34:54 -04:00
|
|
|
elif args.flow:
|
|
|
|
if type(args.present) is datetime:
|
2016-08-23 19:13:06 -04:00
|
|
|
run_flow(flows[args.flow], args.present, True)
|
2016-08-22 21:34:54 -04:00
|
|
|
else:
|
2016-08-23 19:13:06 -04:00
|
|
|
run_flow(flows[args.flow], parse(args.present), False)
|
2016-08-22 21:34:54 -04:00
|
|
|
else:
|
|
|
|
print("No actions specified, exiting.")
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
handle_commandline()
|