def init_chart(rt_path, branch, unstable): chart_dir = os.path.join(rt_path, 'email_src', branch) if not os.path.exists(chart_dir): os.mkdir(chart_dir) chart_path = os.path.join(chart_dir, 'data_'+branch+'_init.xls') chart_file = xlwt.Workbook() parameters = read_para.read_conf(rt_path, branch) date_list, result_dict = get_data(branch, parameters['data_link'], parameters['graph']['day_num'], parameters['graph']['target_num']) init_add_sheet(branch, chart_file, date_list, result_dict, unstable) chart_file.save(chart_path)
def init_chart_R99A(rt_path, new_day_num, unstable): chart_dir = os.path.join(rt_path, 'email_src', 'R99A') if not os.path.exists(chart_dir): os.mkdir(chart_dir) chart_path = os.path.join(chart_dir, 'data_R99A_init.xls') chart_file = xlwt.Workbook() parameters = read_para.read_conf(rt_path, 'R99A') date_list, result_dict = get_data('R99A', parameters['data_link'], new_day_num, parameters['graph']['target_num']) date_old, result_old = get_data('R99A', parameters['olddata_link'], parameters['graph']['day_num']-new_day_num, parameters['graph']['target_num']) date_list += date_old result_dict = dict(result_dict.items()+result_old.items()) init_add_sheet('R99A', chart_file, date_list, result_dict, unstable) chart_file.save(chart_path) return True
def update_chart(rt_path, branch, parameter_dict=0, unstable=-1): # chart = chart_file.get_sheet(branch) chart_path = os.path.join(rt_path, 'email_src', branch, 'data_'+branch+'.xls') old_chart = xlrd.open_workbook(chart_path) old_sheet = old_chart.sheet_by_name(branch) if parameter_dict == 0: parameter_dict = read_para.read_conf(rt_path, branch) if unstable == -1: unstable = int(old_sheet.cell(1, 5).value) new_data = get_new_data(branch, parameter_dict['data_link'], parameter_dict['graph']['target_num']) date_list = [new_data['date']] passed_list = [new_data['Passed']] failed_list = [new_data['Failed']] skipped_list = [new_data['Skipped']] total_list = [new_data['Total']] unstable_list = [unstable] result_dict = {} if int(new_data['Total']) == 0: return {'date':old_sheet.cell(1, 0).value} if new_data['date'] == old_sheet.cell(1, 0).value: date_list += old_sheet.col_values(0, 2) passed_list += old_sheet.col_values(1, 2) failed_list += old_sheet.col_values(2, 2) skipped_list += old_sheet.col_values(3, 2) total_list += old_sheet.col_values(4, 2) unstable_list += old_sheet.col_values(5, 2) else: date_list += old_sheet.col_values(0, 1) passed_list += old_sheet.col_values(1, 1) failed_list += old_sheet.col_values(2, 1) skipped_list += old_sheet.col_values(3, 1) total_list += old_sheet.col_values(4, 1) unstable_list += old_sheet.col_values(5, 1) result_dict = {} for i in range(len(date_list)): result_dict[date_list[i]] = { 'Passed': passed_list[i], 'Failed': failed_list[i], 'Skipped': skipped_list[i], 'Total': total_list[i], 'Unstable': unstable_list[i] } new_chart = xlwt.Workbook() add_sheet(branch, new_chart, date_list, result_dict) new_chart.save(chart_path) # if os.getlogin()== read_para.owner: # os.chmod(chart_path,stat.S_IRWXG+stat.S_IRWXU) return new_data
def get_labels(rt_path, branch, date, parameter_dict=0): label_pos = {'R21A': 0, 'R23B_1': 0, 'R26B': 1, 'R27A': 1, 'R99A': 1}[branch] if parameter_dict == 0: parameter_dict = read_para.read_conf(rt_path, branch) webpage = urllib2.urlopen(parameter_dict['data_link']) content = webpage.read() soup = bs4.BeautifulSoup(content) title_temp = [re.compile('\d\d\d\d\d\d '+branch+'\n'), re.compile(date)] block = soup.html.body.find_all('div')[2].find_all('div')[1].find_all('div')[3].table.find('td', text=title_temp[1]).find_next_siblings() table = bs4.BeautifulSoup(str(block[label_pos])) mplabel = table.find('a', text='MP') sgclabel = table.find('a', text='SGC') label_dict = {} label_dict['mplink'] = str(mplabel['href']) label_dict['sgclink'] = str(sgclabel['href']) label_dict['mp'] = label_dict['mplink'].split('/')[-1] label_dict['sgc'] = label_dict['sgclink'].split('/')[-1] return label_dict
def get_target_table(rt_path, branch, date, parameter_dict=0): if parameter_dict == 0: parameter_dict = read_para.read_conf(rt_path, branch) webpage = urllib2.urlopen(parameter_dict['data_link']) content = webpage.read() soup = bs4.BeautifulSoup(content) title_temp = [date+branch+'\n', re.compile(date)] data = str(soup.html.body.find_all('div')[2].find_all('div')[1].find_all('div')[3].table.find('td', text=title_temp[1]).find_next_siblings()) soup_child = bs4.BeautifulSoup(data) text = soup_child.find_all('table')[2].find_all('tr') table = '' char_special = 0 for line in text: line = str(line) if char_special == 0: line = line[:line.index('/td')-5]+line[line.index('/td')-1:] char_special = 1 table += line return table
import time if __name__ == '__main__': current_path = os.path.split(os.path.realpath(__file__))[0] home_path = os.getenv({'nt': 'USERPROFILE', 'posix': 'HOME'}[os.name]) wiki_path = os.path.join(home_path, 'wiki_result') if not os.path.exists(wiki_path): os.mkdir(wiki_path) argument = read_para.parse_args() branches = argument.branch jpt_link_form = argument.form # choose a jpt_link format for branch in branches: start = time.clock() branch = branch.upper() path_result = os.path.join(wiki_path, branch + '_' + time.strftime('%Y%m%d', time.localtime(time.time())) + '_result.txt') file_result = open(path_result, 'w') print('------------------%s ------------------' % branch) # get config information parameters = read_para.read_conf(current_path, branch, jpt_link_form) # check the targets parameters = read_para.check_target(branch, parameters) # create the text template for replacement template = create_template.create_text(current_path, parameters['target']) # get data and replace the ${} in text template result = replace_data.replace_data(template, branch, jpt_link_form, parameters) # write down the result file_result.write(result) file_result.close() stop = time.clock() print('---------%s completed!--Time: %.0f sec----------' % (branch, (stop-start)))
if __name__ == '__main__': user = getpass.getuser() path = os.path.split(os.path.realpath(__file__))[0] # home_path = os.getenv({'nt': 'USERPROFILE', 'posix': 'HOME'}[os.name]) argument = read_para.parse_args() branches = argument.branch unstable = argument.unstable r = argument.receiver for branch in branches: start = time.clock() branch = branch.upper() print('------------------%s ------------------' % branch) print 'Loading parameters ...' # get config information parameters = read_para.read_conf(path, branch) print 'Updating chart ...' # update data in chart chart.update_chart(path, branch, parameters, unstable) print 'Reading chart ...' # get data for graph datelist, datadict = chart.read_chart(path, branch) print 'Drawing graphs ...' # draw graph for ci result pic1, pic2 = graph.draw_graph(path, branch, datelist, datadict, parameters['graph']['day_num']) # get_labels(path, branch, parameters) sources = {'s1': pic1, 's2': pic2} print 'Creating email content ...' # create the html of email sources = mail.create_email(path, branch, datelist[0], sources, parameters)