def sourceProjFiles(p4sync_bool=True,
                    a='setup/setup_qca.cshrc',
                    b='setup/config_asic.sh'):
    #import pdb; pdb.set_trace()
    #    if (CLEAN and p4sync_bool):
    #        print ("Running P4 to clean workspace")
    #        utils.runShellCmd("p4 sync $DEPOT_IP_TOP/...#none", returnOutput=False)

    if p4sync_bool:
        print("Sync p4 setup/ and scripts/")
        utils.runShellCmd("p4 sync $DEPOT_IP_TOP/setup/...",
                          returnOutput=False)
        utils.runShellCmd("p4 sync $DEPOT_IP_TOP/scripts/...",
                          returnOutput=False)
    '''
    Basically, need to source a) setup_qca.cshrc and b) config_asic.sh then load env
    The approach below isn't perfect but it works for most env settings
    '''
    command = "source $PROJECTS_HOME/{0} > & /dev/null && source $PROJECTS_HOME/{1} > & /dev/null ; env"
    output = utils.runShellCmd(command.format(a, b))
    lines = output.split('\n')

    for line in lines:
        line = line.strip()
        if '=' in line:
            array = line.split('=')
            var = array[0]
            val = ''.join(array[1:])
            os.environ[var] = val
def getInputFileStruct():
    global NapierInputStruct, HawkeyeInputStruct

    ##Napier#
    output = utils.runShellCmd(
        r"cat {0}/scripts/lithium_reg_input.txt | grep -iE '^\S+?(_napier):://' \
            ".format(HELIUM_TOOLS))  #| cut -d: -f 1,8,9,10,11")
    inputFileStructHelper(output, NapierInputStruct)

    ##Hawkeye#
    output = utils.runShellCmd(
        r"cat {0}/scripts/lithium_reg_input.txt | grep -viE '^\S+?(_napier):://' \
            ".format(HELIUM_TOOLS))  #| cut -d: -f 1,8,9,10,11")
    inputFileStructHelper(output, HawkeyeInputStruct)
def sendMail(subj, failedCmd=''):

    command = \
"echo 'prefix: {0}\r \
NapierVerif={3},\r \
VerifCommon={4},\r \
HKDesignFile={5},\r \
Command={7}' \
| mail -s '[Auto-Create Label] {1}: {6}' {2}"

    ##
    command = command.format(PREFIX, re.sub('_napier', '', BLOCK),
                             config.emailOnlyMe,
                             NP_VERIF_INFO_FILE_PATH.split('/')[-1],
                             VERIF_COMMON_LABEL.split('/')[-1],
                             HK_DESIGN_LABEL.split('/')[-1], subj, failedCmd)
    #
    utils.runShellCmd(command, returnOutput=False)
def createP4Client(prefix):
    assert (prefix), "No prefix"
    os.environ['P4CLIENT'] = prefix
    #import pdb; pdb.set_trace()
    p4Input = p4_functions.P4CLIENT_TEMPLATE.format(
        prefix=prefix,
        user=os.environ.get('USER'),
        projectsHome=os.environ.get('PROJECTS_HOME'),
        depotIpTop=os.environ.get('DEPOT_IP_TOP'))
    p4Input = p4Input.replace('\n', r'\n')
    command = "echo '{}' | p4 client -i".format(p4Input)
    output = utils.runShellCmd(command)
    if not ('saved.' in output or 'not changed.' in output):
        print("Could not create the P4 client")
        raise SystemError
    HKObj = HawkeyeInputStruct[BLOCK.replace('_napier', '')]
    HK_VERIF_INFO_FILE_PATH = "{}/{}".format(HKObj.relPathToVerif,
                                             HKObj.verifInfoFile)
    ##
    VERIF_COMMON_LABEL = ''
    HK_DESIGN_LABEL = ''
    NP_DESIGN_LABEL = ''
    ###

    print(
        "\n\n\t1) get verif common label for napier; Note: use verif name and replace 'NAPIER' with 'COMMON' to find verif common label"
    )
    command = "p4 print {verifInfoFile} | grep {verifCommonFile}".format(
        verifInfoFile=NP_VERIF_INFO_FILE_PATH,
        verifCommonFile=VERIF_COMMON_FILE)
    VERIF_COMMON_LABEL = utils.runShellCmd(command)

    print("\n\n\t2) get napier design label")
    command = "p4 print {verifInfoFile} | grep {designInfoFile}".format(
        verifInfoFile=NP_VERIF_INFO_FILE_PATH,
        designInfoFile=NapObj.designInfoFile)
    NP_DESIGN_LABEL = utils.runShellCmd(command)

    print("\n\n\t3) get timestamp of napier design label")
    command = "p4 filelog -t {} | grep '^... #{}'".format(
        NP_DESIGN_LABEL.split('#')[0],
        NP_DESIGN_LABEL.split('#')[1])
    temp = utils.runShellCmd(command)
    timestamp = temp.split()[6]

    print("\n\n\t4.a) get HK design file path")