IMP  2.0.1
The Integrative Modeling Platform
hosts.py
1 ##
2 ## The Inferential Structure Determination (ISD) software library
3 ##
4 ## Authors: Michael Habeck and Wolfgang Rieping
5 ##
6 ## Copyright (C) Michael Habeck and Wolfgang Rieping
7 ##
8 ## All rights reserved.
9 ##
10 ## NO WARRANTY. This library is provided 'as is' without warranty of any
11 ## kind, expressed or implied, including, but not limited to the implied
12 ## warranties of merchantability and fitness for a particular purpose or
13 ## a warranty of non-infringement.
14 ##
15 ## Distribution of substantively modified versions of this module is
16 ## prohibited without the explicit permission of the copyright holders.
17 ##
18 
19 import os
20 
21 def abspath(x):
22 
23  return os.path.abspath(os.path.expanduser(x))
24 
25 def remote_mkdirs(host, folder, debug):
26 
27  exists = False
28 
29  command = "ssh %s '%s -c \"import os, sys; print %s, os.path.exists(os.path.abspath(os.path.expanduser(sys.argv[1]))) \" %s'"
30 
31  tag = '%s' % id(command)
32  out = os.popen(command % (host.name, host.python, tag, folder))
33  lines = out.readlines()
34 
35  #print lines
36 
37  for line in lines: ## find the line with output of python command
38  if line.split()[0] == tag:
39  exists = eval(line.split()[-1])
40  out.close()
41 
42  if not exists:
43 
44  if debug:
45  print 'Creating temporary directory %s on the host %s... ' % (folder, host.name)
46 
47  command = "ssh %s '%s -c \"import os, sys; os.makedirs(sys.argv[1])\" %s\' > /dev/null"
48  os.system(command % (host.name, host.python, folder))
49 
50 class Host(object):
51 
52  def __str__(self):
53 
54  s = '%s(name=%s, nice=%d, python=%s)'
55 
56  return s % (self.__class__.__name__, self.name, self.niceness, self.python)
57 
58  __repr__ = __str__
59 
60  def __init__(self, name, temp_path, n_cpu=1, niceness=0, python='python', init_cmd=''):
61 
62  self.name = name
63  self.set_temp_path(temp_path)
64  self.set_niceness(niceness)
65  self.python = python
66  self.isd_shared_lib_path = None
67  self.n_cpu = n_cpu
68  self.use_n_cpu = self.n_cpu
69  self.init_cmd = init_cmd
70 
71  def set_temp_path(self, temp_path):
72 
73  self.temp_path = abspath(temp_path)
74 
75  def set_niceness(self, n):
76 
77  self.niceness = n
78 
79 def create_host_list(host_names, temp_path, niceness=0, python='python', init_cmd=''):
80 
81  host_list = [Host(name, temp_path, niceness=niceness, python=python, init_cmd=init_cmd) for name in host_names]
82 
83  return host_list