diff --git a/reportservice/automatic_run.py b/reportservice/automatic_run.py index 99a3293d352fac9e9a6cb6fa9bd0478e48ce598b..7731df351ce8e2d679bf128807b734e401e903ec 100644 --- a/reportservice/automatic_run.py +++ b/reportservice/automatic_run.py @@ -2,7 +2,6 @@ import argparse import asyncio from datetime import datetime, timedelta import logging -import urllib.parse from dateutil import parser, tz import yaml @@ -22,7 +21,9 @@ async def auto_run(cfg, timeout=3000): # list of timestamps for the report service runs run_time = cfg['GLOBAL']['run-on'] - + request = {} + request['req'] = ['all'] + request['gitpush'] = True for i, ti in enumerate(run_time): run_time[i] = parser.parse(ti) @@ -42,7 +43,7 @@ async def auto_run(cfg, timeout=3000): sock.SNDTIMEO = timeout sock.RCVTIMEO = timeout sock.connect(port) - await sock.send_pyobj(['all']) + await sock.send_pyobj(request) msg = await sock.recv_pyobj() logging.info('{} Automatic Run' .format(msg)) diff --git a/reportservice/manual_run.py b/reportservice/manual_run.py index 4e67c8fd70a59a24dac1189e82b1a83fad9aae7a..9371ea629b7724714e11993b8dc3ffaf68b93db3 100644 --- a/reportservice/manual_run.py +++ b/reportservice/manual_run.py @@ -1,8 +1,5 @@ import argparse import logging -import os -import sys -import urllib.parse import yaml import zmq @@ -14,35 +11,39 @@ def manual_run(request, cfg): and provide the requested configuration for reports generation. - :param request: a list for generating reports for the - requested Instruments. This list can - contain the Instruments names e.g ['SPB'] - or ['all'] for generating reports for all - instruments in the "report_conf.yaml". + :param request: a dictionary for generating reports for the + requested Instruments. This dict consists + of two keys. A gitpush boolean key for defining + the fate of the generated plots + (pushed to the DC repo. or staying locally) + and a key request which can either be a list + of requested Instruments names e.g ['SPB'] or ['all'] + for generating reports, or dict or new requested + configuration. - This can also be a customized conf. file(dict) - for test purposes. """ + port = cfg['GLOBAL']['server-port'] con = zmq.Context() socket = con.socket(zmq.REQ) - con = socket.connect(port) + socket.connect(port) socket.send_pyobj(request) msg = socket.recv_pyobj() logging.info('{} Manual Run'.format(msg)) arg_parser = argparse.ArgumentParser(description='Manual Launch') -arg_parser.add_argument('--instrument', default=['all'], nargs='+', +arg_parser.add_argument('--instrument', default=[None], nargs='+', help='select the requested instruments. ' - 'Default=all') -arg_parser.add_argument('--testing', dest='testing', action='store_true', - help='required for testing with different ' - 'config files') + 'Default=None. "all" can be used for selecting' + ' all instruments') +arg_parser.add_argument('--push-to-git', dest='Push to git', action='store_true', + help='required for push the generating figures ' + 'to DC git repository') arg_parser.set_defaults(testing=False) arg_parser.add_argument('--config-file', type=str, default='./report_conf.yaml', - help='config file path with reportservice port. ' + help='path to report configuration file ' 'Default=./report_conf.yaml') arg_parser.add_argument('--log-file', type=str, default='./report.log', help='The report log file path. Default=./report.log') @@ -64,9 +65,14 @@ if __name__ == "__main__": level=getattr(logging, args['logging']), format='%(levelname)-6s: %(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S') - if args["testing"]: - request = cfg - else: - request = args["instrument"] + + request = {} + + request['gitpush'] = args["no-push"] + + if args["instrument"]: + request['req'] = args["instrument"] + else: + request['req'] = cfg manual_run(request, cfg) diff --git a/reportservice/report_service.py b/reportservice/report_service.py index 36056374e5c61722b573500c430f1a7ef0b4d427..81d26847482928ce69991a5bbf8e62e1dab65808 100644 --- a/reportservice/report_service.py +++ b/reportservice/report_service.py @@ -2,16 +2,11 @@ import argparse import asyncio from asyncio.subprocess import PIPE import copy -import getpass import glob import logging import os import subprocess -import shutil -import sqlite3 -import sys from time import sleep -import urllib.parse from git import Repo, InvalidGitRepositoryError import yaml @@ -131,7 +126,15 @@ async def push_figures(repo_master, addf): """ repo = Repo(repo_master) - repo.index.add(addf) + + adding = True + while adding: + try: + repo.index.add(addf) + adding = False + except: + sleep(1) + repo.index.commit("Add {} new figures".format(len(addf))) #TODO: create an async function for pushing new figures # to avoid blocking the report service. @@ -171,7 +174,8 @@ async def server_runner(conf_file): while True: response = await socket.recv_pyobj() await socket.send_pyobj('Build DC reports through -->') - logging.info("response: {}".format(response)) + logging.info("response: {} with git pushing: {}" + .format(response['req'], response['gitpush'])) # Check if response is a list or a dict. # if list, it should either have instrument names or ['all']. @@ -181,14 +185,17 @@ async def server_runner(conf_file): # reports config file req_cfg = {} - if isinstance(response, dict): - req_cfg = response - elif isinstance(response, list): - if len(response) == 1 and response[0] == 'all': + # boolean for pushing to DC git repo. + git_push = response['gitpush'] + + if isinstance(response['req'], dict): + req_cfg = response['req'] + elif isinstance(response[], list): + if len(response['req']) == 1 and response['req'][0] == 'all': req_cfg = config else: req_cfg['GLOBAL'] = config['GLOBAL'] - for instr in response: + for instr in response['req']: try: req_cfg[instr] = config[instr] except: @@ -201,7 +208,7 @@ async def server_runner(conf_file): logging.info('Requested Configuration: {}'.format(req_cfg)) - async def do_action(cfg): + async def do_action(cfg, git_push): logging.info('Run plot production') local_repo = cfg['GLOBAL']['git']['repo-local'] @@ -298,7 +305,9 @@ async def server_runner(conf_file): all_new_files.append('{}/report_conf.yaml'.format(fig_local)) - asyncio.ensure_future(push_figures(local_repo, all_new_files)) + if git_push: + asyncio.ensure_future(push_figures(local_repo, all_new_files)) + # TODO:delete out-folder #try: # asyncio.ensure_future(del_folder(out_folder)) @@ -309,7 +318,7 @@ async def server_runner(conf_file): return try: - asyncio.ensure_future(do_action(copy.copy(req_cfg))) + asyncio.ensure_future(do_action(copy.copy(req_cfg, git_push))) except Exception as e: # actions that fail are only error logged logging.error(str(e)) break