source: cssboy_deployment/src/startCSS.py @ 1131

Last change on this file since 1131 was 1131, checked in by kpetersn, 10 years ago

Added startCSS.py

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1#!/usr/bin/env python
2# Intelligently start CSS in a shared-user, multi-workspace environment
3#
4# Assumptions:
5#  1. All the workspaces are identical and the user doesn't care which workspace is used.
6#  2. The CSS_WORKSPACES and CSS_BIN environment variables will be set properly
7#
8
9import os
10
11def main():
12  # Get the location of CSS workspaces
13  try:
14    workspace_dir = os.path.expanduser(os.environ['CSS_WORKSPACES'])
15  except KeyError:
16    print "CSS_WORKSPACES environment variable is undefined."
17    return
18
19  # Verify that the CSS workspace diretory exists
20  if not os.path.isdir(workspace_dir):
21    print "CSS_WORKSPACES=%s doesn't exist." % workspace_dir
22    return
23 
24  # Get the location of the CSS binary
25  try:
26    css_bin = os.path.expanduser(os.environ['CSS_BIN'])
27  except KeyError:
28    print "CSS_BIN environment variable is undefined."
29    return
30
31  # Verify that the CSS binary exists
32  if not os.path.isfile(css_bin):
33    print "CSS_BIN=%s doesn't exist." % css_bin
34    return
35 
36  # Get the location of the fuser binary
37  fuser_bin = which('fuser')
38  if fuser_bin == None:
39    print "'fuser' command not found"
40    return
41 
42  # Get contents of the workspace directory
43  workspace_contents = os.listdir(workspace_dir)
44
45  # Create list of full paths to diretcory contents
46  full_workspace_paths = ["%s%s%s" % (workspace_dir, os.sep, x) for x in workspace_contents]
47
48  # Prune files from the list
49  workspace_dirs = filter(os.path.isdir, full_workspace_paths)
50
51  # Prune hidden folders
52  workspaces = []
53  for i in range(len(workspace_dirs)):
54    if os.path.basename(workspace_dirs[i])[0] != '.':
55      workspaces.append(workspace_dirs[i])
56
57  workspaces = sorted(workspaces)
58  #!print workspaces
59  #!print
60
61  success = False
62  for workspace in workspaces:
63    lock_path = workspace + os.sep + '.metadata' + os.sep + '.lock'
64 
65    cmd_str = "%s -s %s" % (fuser_bin, lock_path)
66    ret_val = os.system(cmd_str)
67 
68    #print "  ", os.path.basename(workspace), ret_val
69    print "  ", os.path.basename(workspace)
70 
71    if ret_val == 0:
72      print "\tWorkspace is in use"
73    else:
74      print "\tWorkspace is available"
75
76    if ret_val != 0:
77      success = True
78      # Launch CSS
79      cmd_str = "%s -data %s &" % (css_bin, workspace)
80      print cmd_str
81      os.system(cmd_str)
82      break
83
84  if success == False:
85    print "All workspaces are in use!"
86
87
88# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
89def which(program):
90    import os
91    def is_exe(fpath):
92        return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
93
94    fpath, fname = os.path.split(program)
95    if fpath:
96        if is_exe(program):
97            return program
98    else:
99        for path in os.environ["PATH"].split(os.pathsep):
100            exe_file = os.path.join(path, program)
101            if is_exe(exe_file):
102                return exe_file
103
104    return None
105
106if __name__ == '__main__':
107  main()
Note: See TracBrowser for help on using the repository browser.