def check_outFile(out_file,*submit):
  TBremoved=0
  out_file=os.path.abspath(out_file)
  sind=out_file.rfind(os.sep)
  if not os.path.isdir(out_file[:sind]): #the path doesn't exist
          wmsui_utils.errMsg("Error","UI_DIR_NOT_FOUND",out_file[:sind])
          wmsui_utils.exit(1)
  info.outFile = out_file
  if os.path.isfile(out_file): #the file already exists
    TBremoved=1
    if submit:
       f=open(out_file)
       line=f.readline()
       line = line.strip()
       f.close()
       if line == "###Submitted Job Ids###":
           return [0,out_file,0]
    wmsui_utils.errMsg("Warning","UI_FILE_EXISTS",out_file)
    if info.noint: #No-int active
       wmsui_utils.errMsg("Warning","UI_SKIP_QUESTION","overwriting the File "+out_file)
       return [0,out_file,1] #the file will be removed
    else:
       question="Do you want to overwrite?"
       answ=wmsui_utils.questionYN(question)
       print ""
       if not answ: #NO answered
           print "bye"
           return [0,"",0]
  #if submit: #The file won't be removed
  #     TBremoved=1
  return [0,out_file,TBremoved]
def checkError (apiName , inst, exit = 0):
	err , apiMsg = inst.get_error ()
	if err==0:
		return err
	em = "Error"
	if exit==0:
		em="Warning"
	if err==1:
		#ERROR
		errMsg ( em , "API_NATIVE_ERROR" , apiName  , apiMsg )
		if exit:
			wmsui_utils.exit (1)
	else:
		#WARNING
		errMsg ( em , "API_NATIVE_ERROR" , apiName  , apiMsg )
	return err
def check_proxy(*valid) :
	if valid:
		valid=valid[0]
		hours, minutes  = valid.split(":")
		secondsLeft = int(hours)*60*60 + int(minutes)*60
	uid = os.getuid()
	pid = os.getpid()
	proxy_file_name = ''
	INST = info.prefix
	if not INST:
		exit(1)
	#Initialize proxy certificate file name
	try:
		proxy_file_name = os.environ['X509_USER_PROXY']
	except:
		proxy_file_name = os.sep + "tmp" + os.sep+ "x509up_u" + repr(uid)
	#Check if Proxy certificate already exists
	#voms =  info.confAdVo.getStringValue("VirtualOrganisation")[0]
	if os.path.isfile(proxy_file_name):
		#Retrieve Proxy Information ( new Approach )
		uc = UCredential (proxy_file_name)
		timeleft = uc.getExpiration()  - int( time.time() ) -time.timezone
		err = uc.get_error()
		if err != "":
			#ERROR
			errMsg ( "Error" , "API_NATIVE_ERROR" , "UcWrapper::getExpiration"  , err )
			wmsui_utils.exit (1)
		info.issuer =uc.getIssuer()
		if not valid:
			# timeleft should be at least 20 minutes
			secondsLeft = 20*60
			valid = "00:20"
		if timeleft>secondsLeft:
			printERRORS=0
			return checkVomsExtension(proxy_file_name,printERRORS)[0]
		elif timeleft>0:
			if info.prgname=="glite-job-submit":
				errMsg("Error",'UI_PROXY_DURATION' , valid )
				return 1
		else:
			errMsg("Error",'UI_PROXY_EXPIRED' )
			return 1

	else:
		# The Proxy Does not exist
		errMsg("Error",'UI_PROXY_NOT_FOUND')
		return 1
def checkConf(conf, virtualOrg, logPath):

  override = 1

  voName = UnspecifiedVO

  vomsrc = ""
  
  src = VO_SOURCE_NONE;

  configFile = ""
  cfDefault = ""
  
  sys_exit = 0

  # Trace 
  if(conf and virtualOrg):
  
	wmsui_utils.errMsg("The following options cannot be specified together:\n" + \
		 conf + "\n" + \
		 virtualOrg + "\n\n", wmsui_utils.info.logFile)
        wmsui_utils.exit(1)
  
  # Read the GLITE_WMS_COMMANDS_CONFIG environment variable
  try:
    # Set the WMS Commands Config file
    envCommandsConfig = os.environ['GLITE_WMS_COMMANDS_CONFIG']
  except:
    # Set a empty value due to missing environment variable    
    envCommandsConfig = ""

  # Read the X509_USER_PROXY environment variable
  try:
    # Set the User Proxy filename from the environment variable
    proxy_file_name = os.environ['X509_USER_PROXY']
  except:
    # Set the default User Proxy filename 
    proxy_file_name = os.sep + "tmp" + os.sep + "x509up_u"+ repr(os.getuid())

  # Read the Proxy file if it exists		  
  if os.path.isfile(proxy_file_name):

    #Try to look for the default Vo inside the user proxy
    printERRORS=1
    err, voName = checkVomsExtension(proxy_file_name, printERRORS)
    
    # Check for errors
    if err:
	wmsui_utils.errMsg(err, wmsui_utils.info.logFile)
        wmsui_utils.exit(1)

    if voName:
      # Set the source of the voName
      src = VO_SOURCE_CERT_EXTENSION
      vomsrc = "proxy certificate extension"
      
  """
  Options Parsing
  """
  if(virtualOrg and src != VO_SOURCE_NONE):
  
    # VO name forcing ignored
    wmsui_utils.print_message (False, wmsui_utils.info.logFile, "Warning - --vo option ignored" )
    
  elif(virtualOrg):
  
    # SCR is definitely NONE
    voName = virtualOrg
    
    src = VO_SOURCE_VO_OPT
    vomsrc = "--vo option"

    # Print a debug message    
    if wmsui_utils.info.debug:
      debugMsg = "#### "+ time.ctime() + " Debug Message ####\n" + \
	         "VO Read from --vo option\n\n#### End Debug ####\n"
   
      #Print message
      wmsui_utils.print_message(False, wmsui_utils.info.logFile, debugMsg)
    
      # Set the config file
      configFile = wmsui_utils.info.prefix + os.sep + "etc" + os.sep + virtualOrg.lower() + os.sep + DEFAULT_UI_CLIENTCONFILE
    
  elif(conf):
  
    if (src == VO_SOURCE_NONE and wmsui_utils.info.debug):
      debugMsg = "#### "+ time.ctime() + " Debug Message ####\n" + \
	         "VO Read from --config option\n\n#### End Debug ####\n"
   
      #Print message
      wmsui_utils.print_message(False, wmsui_utils.info.logFile, debugMsg)
    
    # Set the config file
    configFile = conf
	
    src = VO_SOURCE_CONFIG_OPT
    vomsrc = "--config option"
    
  elif envCommandsConfig:

    if (src == VO_SOURCE_NONE and wmsui_utils.info.debug):
      debugMsg = "#### "+ time.ctime() + " Debug Message ####\n" + \
	         "VO Read from ENV option\n\n#### End Debug ####\n"
   
      #Print message
      wmsui_utils.print_message(False, wmsui_utils.info.logFile, msg)

    src = VO_SOURCE_CONFIG_VAR;
    vomsrc = "GLITE_WMSUI_CONFIG_VO env variable"
   
    # Set the GLITE_WMS_COMMANDS_CONFIG environment variable content as configuration file
    configFile = envCommandsConfig

  elif(src == VO_SOURCE_NONE):	
  
    print "checkConf\nEmpty value: Unable to find any both VirtualOrganisation and any configuration file"
    sys.exit(1)
  
  err, voName, info.confAdVo, configFile = parseVo(src, configFile, voName, "Error", override)
  
  if err == 0 and not info.confAdVo:
  	configFile = ""
  
  if wmsui_utils.info.debug and voName:
    # Print Info
    msg =  "VirtualOrganisation value :" + voName
    wmsui_utils.print_message(False, wmsui_utils.info.logFile, msg)

  # Build the default config file it a VO name has been found
  if(voName):	  
	  
    # Build the default config file 
    cfDefault = wmsui_utils.info.prefix + os.sep + "etc" + os.sep + voName.lower() + os.sep + DEFAULT_UI_CLIENTCONFILE

    # Check if the file exists
    if not os.path.isfile (cfDefault):
      # Remove the path
      cfDefault = ""

  # Load the configuration
  loadConfiguration(configFile, cfDefault, voName)
  
  if wmsui_utils.info.debug:
	  # Successfully Update needed Info
	  msg = "Selected Virtual Organisation name (from "+ vomsrc + "): " + voName

	  msg = "#### "+ time.ctime() + " Debug Message ####\n" + msg +"\nVOMS configuration file successfully loaded:\n" \
	  + configFile + info.confAdVo.toLines() +  "\n#### End Debug ####\n"

	  #Print VOMS message
	  wmsui_utils.print_message(False, wmsui_utils.info.logFile, msg)

  # Check if the log path has been set
  if logPath:
  
    # Create the log filename
    logFile = os.path.abspath (logPath)

    # Remove the log file if it exists	
    if os.path.isfile ( logFile):
      try:
        # Remove the log file
        os.remove(logFile)
      except:
        errMsg ( "Warning" , "UI_RM_FILE", logFile )
  else :
    # Create a new log file
    logFile = wmsui_utils.create_err_log ( info.prgname )

  # Rename the old logFile with the new value (if needed)
  if info.logFile:
   if info.logFile != logFile:
     try:
       shutil.copyfile( info.logFile, logFile  )
     except:
       errMsg('Error','UI_WRITE_FILE' , logFile   )
       wmsui_utils.exit(1)
     try:
       os.remove(info.logFile)
     except:
       errMsg("Warning",'UI_RM_FILE' , info.logFile   )
     info.logFile = logFile

  # Return the Virtual Organisation name
  return voName