import os
import commands
from pUtil import tolog

class FileState:
    """
    This class is used to set and update the current output file state dictionary.
    When the job is running, the file fileState-<JobID>.pickle is created which contains
    the current state of the output file dictionary
    File state dictionary format:
      { file_name1 : state1, ..., file_nameN : stateN }
    where file_name does not contain file path since it will change for holding jobs (from work dir to data dir), and
    the state variables have the list form "file_state", "reg_state" (for output files) and "file_state", "transfer_mode" (input files).

    "file_state" can assume the following values for "output" files:
      "not_created"     : initial value for all files at the beginning of the job
      "created"         : file was created and is waiting to be transferred
      "not_transferred" : file has not been transferred
      "transferred"     : file has already been transferred (no further action)
      "missing"         : file was never created, the job failed (e.g. output file of a failed job; a log should never be missing)

    "file_state" can assume the following values for "input" files:
      "not_transferred" : file has not been transferred (can remain in this state for FileStager and directIO modes)
      "transferred"     : file has already been transferred (no further action)

    "reg_state" can assume the following values (relevant for output files):
      "not_registered"  : file was not registered in the LFC
      "registered"      : file was already registered in the LFC (no further action)

    "transfer_mode" can assume the following values (relevant for input files)
      "copy_to_scratch" : default file transfer mode
      "remote_io"       : direct access / remote IO tranfer mode
      "file_stager"     : file stager tranfer mode
      "no_transfer"     : input file has been skipped

    E.g. a file with state = "created", "not_registered" should first be transferred and then registered in the LFC.
    The file state dictionary should be created with "not_created" states as soon as the output files are known (pilot).
    The "created" states should be set after the payload has run and if the file in question were actually created.
    "transferred" should be set by the mover once the file in question has been transferred.
    "registered" should be added to the file state once the file has been registered.
    "copy_to_scratch" is to set for all input files by default. In case remote IO / FileStager instructions are found in copysetup[in]
    the state will be changed to "remote_io" / "file_stager". Brokerage can also decide that remote IO is to be used. In that case,
    "remote_io" will be set for the relevant input files (e.g. DBRelease and lib files are excluded, i.e. they will have "copy_to_scratch"