#!/usr/bin/python
#===============================================================================
# CERN (BE-CO-HT)
# Extract PTS data for storage to MTF
#===============================================================================
# author: Theodor Stana (t.stana@cern.ch)
#
# date of creation: 2014-06-30
#
# version: 1.0
#
# description:
#
#   This script can be used to generate a CSV file containing data about the
#   board to be stored to MTF.
#
#   It should be run after a batch of boards are tested using PTS.
#
#   The steps below show the modifications to be made to the code in case of
#   new boards.
#
#===============================================================================
# GNU LESSER GENERAL PUBLIC LICENSE
#===============================================================================
# This source file is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation; either version 2.1 of the License, or (at your
# option) any later version. This source is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU Lesser General Public License for more details. You should have
# received a copy of the GNU Lesser General Public License along with this
# source; if not, download it from http://www.gnu.org/licenses/lgpl-2.1.html
#===============================================================================
# last changes:
#    2014-06-30    Theodor Stana     File created
#===============================================================================
#  TODO: -
#===============================================================================

import os
import glob
import subprocess
import time

#===============================================================================
# Step 1: Change log file directory and glob pattern
#===============================================================================
# Log file DFS directory and pattern for file name in which to look for the data.
DFS_DIR = "./"
FINNAME = "*.inf"


def extract_and_store():
    # Open the output file in the logfile directory
    fdata = open("equip-data.csv", 'w')
    flogs = open("logfiles.csv", 'w')

    fnull = open(os.devnull, 'w')

    # grep each file in the logfile dir for FAIL message and write entry for
    # those that didn't fail
    for fname in sorted(glob.glob(FINNAME)):

#===============================================================================
# Step 2: Add each log file of a board to a new item in the logfiles.csv file.
#         The items are separated by commas, so a comma is placed after each
#         entry.
#         Base your entry on the template received from EDMS support.
#===============================================================================
        # Get a brand new entry string
        entry = ""

        # Get board serial number and append to the entry table
        sn     = fname.find("HC")
        entry += fname[sn:sn+19]
        entry += ','
        entry += "Theodor-Adrian Stana"
        entry += ','
        entry += "t.stana@cern.ch"
        entry += ','
        entry += ','
        entry += ','
        entry += fname[sn:sn+19]
        for i in range(8):
            entry += ','
        entry += "AB-CO-MTF"
        entry += ','
        entry += "Specification"
        for i in range(5):
            entry += ','
        entry += "DOC-OWNER-MTF"
        entry += ','
        entry += "Board PTS test log"
        for i in range(9):
            entry += ','
        entry += time.strftime("%Y-%m-%d", time.localtime())
        entry += ','
        entry += DFS_DIR + '/' + fname

        flogs.write(entry + '\n')

        # Only add to equip-data.csv for files which haven't failed
        if (subprocess.call("grep FAIL " + fname, shell=True, stdout=fnull, stderr=fnull) == 1):
            # Get a brand new entry string
            entry = ""

            # Add board code to the first column
            entry += fname[sn:sn+19]
            entry += ','

            # Add number of columns to PCB ID
            for i in range(6):
                entry += ','

#===============================================================================
# Step 3: Add any parameters to look for and the number of chars to store as
#         equipment data. The parameters should be based on the template received
#         from EDMS support.
#         Make sure to add a comma after each parameter but last.
#===============================================================================
            # Add parameters from PTS log to equip-data.csv
            relgw = None
            goldgw = None
            with open(fname, 'r') as fin:
                for l in fin:
                    # Unique board ID from DS18B20 thermometer chip
                    if "Unique ID" in l:
                        entry += l[-17:-1]
                        # Add number of columns to cost
                        for i in range(10):
                            entry += ','

                    # Release & golden gateware from flash test
                    if ("Release gateware" in l):
                        relgw = "release v" + l[-4:-1]
                    if ("Golden gateware" in l):
                        goldgw = "golden v" + l[-4:-1]
                    if (relgw and goldgw):
                        entry += goldgw + " + " + relgw
                        # Golden + release gateware is last parameter to read in
                        # file, so end reading the file
                        break

            # Last, add the value of goods parameter
            for i in range(5):
                entry += ','
            entry += "392.4"

            # Write to the equip-data.csv file
            fdata.write(entry + '\n')

    fdata.close()
    flogs.close()

extract_and_store()

