[Bio] / FigKernelPackages / FIG.py Repository:
ViewVC logotype

Diff of /FigKernelPackages/FIG.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1, Thu Feb 12 16:00:31 2004 UTC revision 1.9, Tue May 4 16:27:45 2004 UTC
# Line 1  Line 1 
1  import urlparse  import urlparse
2  import socket  import socket
3  import os  import os
4    import popen2
5  import sys  import sys
6  import re  import re
7    import xmlrpclib
8    
9  import FIG_Config  import FIG_Config
10    
11    import Clearinghouse
12    
13    def get_clearinghouse(url = None):
14        return Clearinghouse.Clearinghouse(url)
15    
16  def get_local_hostname():  def get_local_hostname():
17      #      #
18        # See if there is a FIGdisk/config/hostname file. If there
19        # is, force the hostname to be that.
20        #
21    
22        try:
23            fh = open(os.path.join(FIG_Config.fig_disk, "config", "hostname"))
24            host = fh.readline()
25            return host.strip()
26        except:
27            pass
28    
29        #
30      # First check to see if we our hostname is correct.      # First check to see if we our hostname is correct.
31      #      #
32      # Map it to an IP address, and try to bind to that ip.      # Map it to an IP address, and try to bind to that ip.
33      #      #
34    
35      hostname = socket.getfqdn()      hostname = socket.getfqdn()
36      hostname = "yips"  
37        #
38        # See if hostname is something.local., which is what
39        # a Mac will return if it didn't get a name via some
40        # other mechanism (DHCP or static config). We have to
41        # check here because otherwise it will pass the fqdn and
42        # local binding test.
43        #
44    
45        if not re.search(r"\.local\.?$", hostname):
46    
47      #      #
48      # First check that hostname is a fqdn, and that we can bind to it.      # First check that hostname is a fqdn, and that we can bind to it.
49      #      #
# Line 131  Line 161 
161      for l in fh:      for l in fh:
162          cols = l.strip().split()          cols = l.strip().split()
163    
164          if cols[0] == "default" or cols[0] == "0.0.0.0":          if len(cols) > 0 and (cols[0] == "default" or cols[0] == "0.0.0.0"):
165              interface_name = cols[-1]              interface_name = cols[-1]
166              break              break
167    
168      fh.close()      fh.close()
169    
170      print "Default route on ", interface_name      # print "Default route on ", interface_name
171    
172      #      #
173      # Find ifconfig.      # Find ifconfig.
# Line 157  Line 187 
187          print >> sys.stderr, "Ifconfig not found"          print >> sys.stderr, "Ifconfig not found"
188          return "localhost"          return "localhost"
189    
190      print >> sys.stderr, "found ifconfig ", ifconfig      # print >> sys.stderr, "found ifconfig ", ifconfig
191    
192      try:      try:
193          fh = os.popen(ifconfig+ " " + interface_name, "r")          fh = os.popen(ifconfig+ " " + interface_name, "r")
# Line 226  Line 256 
256      new_url = urlparse.urlunparse(p)      new_url = urlparse.urlunparse(p)
257      return new_url      return new_url
258    
259    def get_seed_id():
260        #
261        # Retrieve the seed identifer from FIGdisk/config/seed_id.
262        #
263        # If it's not there, create one, and make it readonly.
264        #
265    
266        id_file = os.path.join(FIG_Config.fig_disk, "config", "seed_id")
267        if not os.path.isfile(id_file):
268    
269            fh = os.popen("uuidgen", "r")
270    
271            newid = fh.readline()
272            newid = newid.strip()
273    
274            fh.close()
275    
276            fh = open(id_file, "w")
277            print >>fh, newid
278            fh.close()
279    
280            os.chmod(id_file, 0444)
281    
282        fh = open(id_file)
283        id = fh.readline()
284        fh.close()
285        id = id.strip()
286        return id
287    
288    #
289    # Define a FIG class; this is analagous to the FIG class used in FIG.pm
290    #
291    # It also lets us cache stuff, and use __call__ to map calls to
292    # an XMLRPC server for the perl stuff we don't implement locally.
293    #
294    
295    class FIG:
296    
297        def __init__(self):
298            self.xmlrpc_proxy = None
299            self.xmlrpc_proc = None
300    
301        def __repr__(self):
302            return "FIG instance %s" % ( self)
303    
304        def __str__(self):
305            return "FIG instance %s" % (id(self))
306    
307        def __getattr__(self, name):
308            g = globals()
309            if g.has_key(name) and callable(g[name]):
310                return g[name]
311    
312            if name.startswith("_"):
313                return None
314    
315            #
316            # Not accessible in globals. Return an XMLRPC calling proxy.
317            #
318    
319            return XMLRPCCaller(self, name)
320    
321        def foo(self):
322            print "FOO"
323    
324    
325        def call_xmlrpc(self, name, args):
326    
327            try:
328                if self.xmlrpc_proxy is None:
329                    self.start_xmlrpc_server()
330    
331            except Exception, e:
332                print "Got exception ... ", e
333                return
334    
335            proc = getattr(self.xmlrpc_proxy, name)
336            retval = apply(proc, args)
337            return retval
338    
339        def start_xmlrpc_server(self):
340            server_path = os.path.join(FIG_Config.bin, "fig_xmlrpc2")
341    
342            if not os.access(server_path, os.X_OK):
343                raise Exception, "XMLRPC server path %s not found" % (server_path)
344    
345    
346            proc = self.xmlrpc_proc = popen2.Popen3(server_path, 0)
347    
348            print "Server started ", proc.pid
349    
350            url = proc.fromchild.readline()
351            url = url.strip()
352            print "Read url ", url
353    
354            proc.fromchild.close()
355    
356            self.xmlrpc_proxy = xmlrpclib.ServerProxy(url)
357    
358    class XMLRPCCaller:
359        def __init__(self, fig, name):
360            self.fig = fig
361            self.name = name
362    
363        def __call__(self, *args):
364            return self.fig.call_xmlrpc(self.name, args)
365    
366  if __name__ == "__main__":  if __name__ == "__main__":
367    
368      print get_local_hostname()      print get_local_hostname()
369      print cgi_url()      print cgi_url()
370      print temp_url()      print temp_url()
371    
372        print get_seed_id()

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.9

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3