Changeset 1511


Ignore:
Timestamp:
Mar 4, 2014 10:27:24 AM (8 years ago)
Author:
kpetersn
Message:

Use fcntl.flock instead of fuser to determine if a workspace is locked to allow startCSS.py to correctly detect locked workspaces when running as the same user on different computers with a shared home directory.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cssboy_deployment/src/startCSS.py

    r1162 r1511  
    1313
    1414import os
     15import fcntl
    1516
    1617def main():
     
    2021  except KeyError:
    2122    print "CSS_WORKSPACES environment variable is undefined."
    22         return
     23    return
    2324
    2425  # Verify that the CSS workspace diretory exists
     
    3738  if not os.path.isfile(css_bin):
    3839    print "CSS_BIN=%s doesn't exist." % css_bin
    39     return
    40  
    41   # Get the location of the fuser binary
    42   fuser_bin = which('fuser')
    43   if fuser_bin == None:
    44     print "'fuser' command not found"
    4540    return
    4641 
     
    6863    lock_path = workspace + os.sep + '.metadata' + os.sep + '.lock'
    6964 
    70     cmd_str = "%s -s %s" % (fuser_bin, lock_path)
    71     ret_val = os.system(cmd_str)
    72  
     65    if os.path.isfile(lock_path):
     66      # Lock file exists
     67     
     68      lfd = open(lock_path, 'r+')
     69     
     70      # Determine if lock file is locked by trying to lock it
     71      try:
     72        fcntl.flock(lfd, fcntl.LOCK_EX | fcntl.LOCK_NB)
     73      except:
     74        # The file is locked
     75        ret_val = 0
     76      else:
     77        # The file is NOT locked.
     78        fcntl.flock(lfd, fcntl.LOCK_UN)
     79        ret_val = 1
     80     
     81      lfd.close()
     82     
     83    else:
     84      # lock file doesn't exist, workspace should be available
     85      ret_val = 1
     86
    7387    #print "  ", os.path.basename(workspace), ret_val
    7488    print "  ", os.path.basename(workspace)
     
    91105
    92106
    93 # http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
    94 def which(program):
    95     import os
    96     def is_exe(fpath):
    97         return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
    98 
    99     fpath, fname = os.path.split(program)
    100     if fpath:
    101         if is_exe(program):
    102             return program
    103     else:
    104         for path in os.environ["PATH"].split(os.pathsep):
    105             exe_file = os.path.join(path, program)
    106             if is_exe(exe_file):
    107                 return exe_file
    108 
    109     return None
    110 
    111107if __name__ == '__main__':
    112108  main()
Note: See TracChangeset for help on using the changeset viewer.