[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.12, Fri Oct 21 14:50:26 2005 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    class NoSubsystemException(Exception):
14        pass
15    
16    def get_clearinghouse(url = None):
17        return Clearinghouse.Clearinghouse(url)
18    
19    def get_ss_hierarchy(url = None):
20        return Clearinghouse.SSHierarchy(url)
21    
22    #
23    # Subsystem helper code.
24    #
25    
26    def get_subsystem(name):
27        try:
28            sub = Subsystem(name)
29        except NoSubsystemException:
30            sub = None
31    
32        return sub
33    
34    
35  def get_local_hostname():  def get_local_hostname():
36      #      #
37        # See if there is a FIGdisk/config/hostname file. If there
38        # is, force the hostname to be that.
39        #
40    
41        try:
42            fh = open(os.path.join(FIG_Config.fig_disk, "config", "hostname"))
43            host = fh.readline()
44            return host.strip()
45        except:
46            pass
47    
48        #
49      # First check to see if we our hostname is correct.      # First check to see if we our hostname is correct.
50      #      #
51      # 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.
52      #      #
53    
54      hostname = socket.getfqdn()      hostname = socket.getfqdn()
55      hostname = "yips"  
56        #
57        # See if hostname is something.local., which is what
58        # a Mac will return if it didn't get a name via some
59        # other mechanism (DHCP or static config). We have to
60        # check here because otherwise it will pass the fqdn and
61        # local binding test.
62        #
63    
64        if not re.search(r"\.local\.?$", hostname):
65    
66      #      #
67      # 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.
68      #      #
# Line 131  Line 180 
180      for l in fh:      for l in fh:
181          cols = l.strip().split()          cols = l.strip().split()
182    
183          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"):
184              interface_name = cols[-1]              interface_name = cols[-1]
185              break              break
186    
187      fh.close()      fh.close()
188    
189      print "Default route on ", interface_name      # print "Default route on ", interface_name
190    
191      #      #
192      # Find ifconfig.      # Find ifconfig.
# Line 157  Line 206 
206          print >> sys.stderr, "Ifconfig not found"          print >> sys.stderr, "Ifconfig not found"
207          return "localhost"          return "localhost"
208    
209      print >> sys.stderr, "found ifconfig ", ifconfig      # print >> sys.stderr, "found ifconfig ", ifconfig
210    
211      try:      try:
212          fh = os.popen(ifconfig+ " " + interface_name, "r")          fh = os.popen(ifconfig+ " " + interface_name, "r")
# Line 204  Line 253 
253    
254      return ip      return ip
255    
256    def top_link():
257    
258        #
259        # Determine if this is a toplevel cgi or one in one of the subdirs (currently
260        # just /p2p).
261        #
262    
263        sname = os.getenv("SCRIPT_NAME")
264    
265        if sname is not None:
266            parts = os.getenv("SCRIPT_NAME").split('/');
267    
268            if len(parts) > 2 and parts[-2] == 'FIG':
269                top = '.'
270            elif len(parts) > 3 and parts[-3] == 'FIG':
271                top = '..'
272            else:
273                top = FIG_Config.cgi_base
274        else:
275            top = FIG_Config.cgi_base
276    
277    
278    
279        return top
280    
281  def cgi_url():  def cgi_url():
282      return plug_url(FIG_Config.cgi_url)      return top_link()
283        # return plug_url(FIG_Config.cgi_url)
284    
285  def temp_url():  def temp_url():
286      return plug_url(FIG_Config.temp_url)      return plug_url(FIG_Config.temp_url)
# Line 226  Line 301 
301      new_url = urlparse.urlunparse(p)      new_url = urlparse.urlunparse(p)
302      return new_url      return new_url
303    
304    def get_seed_id():
305        #
306        # Retrieve the seed identifer from FIGdisk/config/seed_id.
307        #
308        # If it's not there, create one, and make it readonly.
309        #
310    
311        id_file = os.path.join(FIG_Config.fig_disk, "config", "seed_id")
312        if not os.path.isfile(id_file):
313    
314            fh = os.popen("uuidgen", "r")
315    
316            newid = fh.readline()
317            newid = newid.strip()
318    
319            fh.close()
320    
321            fh = open(id_file, "w")
322            print >>fh, newid
323            fh.close()
324    
325            os.chmod(id_file, 0444)
326    
327        fh = open(id_file)
328        id = fh.readline()
329        fh.close()
330        id = id.strip()
331        return id
332    
333    #
334    # Define a FIG class; this is analagous to the FIG class used in FIG.pm
335    #
336    # It also lets us cache stuff, and use __call__ to map calls to
337    # an XMLRPC server for the perl stuff we don't implement locally.
338    #
339    
340    class FIG:
341    
342        def __init__(self):
343            self.xmlrpc_proxy = None
344            self.xmlrpc_proc = None
345    
346        def __repr__(self):
347            return "FIG instance %s" % ( self)
348    
349        def __str__(self):
350            return "FIG instance %s" % (id(self))
351    
352        def __getattr__(self, name):
353            g = globals()
354            if g.has_key(name) and callable(g[name]):
355                return g[name]
356    
357            if name.startswith("_"):
358                return None
359    
360            #
361            # Not accessible in globals. Return an XMLRPC calling proxy.
362            #
363    
364            return XMLRPCCaller(self, name)
365    
366        def foo(self):
367            print "FOO"
368    
369    
370        def call_xmlrpc(self, name, args):
371    
372            try:
373                if self.xmlrpc_proxy is None:
374                    self.start_xmlrpc_server()
375    
376            except Exception, e:
377                print "Got exception ... ", e
378                return
379    
380            proc = getattr(self.xmlrpc_proxy, name)
381            retval = apply(proc, args)
382            return retval
383    
384        def start_xmlrpc_server(self):
385            server_path = os.path.join(FIG_Config.bin, "fig_xmlrpc_server")
386    
387            if not os.access(server_path, os.X_OK):
388                raise Exception, "XMLRPC server path %s not found" % (server_path)
389    
390    
391            proc = self.xmlrpc_proc = popen2.Popen3(server_path, 0)
392    
393            print "Server started ", proc.pid
394    
395            url = proc.fromchild.readline()
396            url = url.strip()
397            print "Read url ", url
398    
399            proc.fromchild.close()
400    
401            self.xmlrpc_proxy = xmlrpclib.ServerProxy(url)
402    
403    class XMLRPCCaller:
404        def __init__(self, fig, name):
405            self.fig = fig
406            self.name = name
407    
408        def __call__(self, *args):
409            return self.fig.call_xmlrpc(self.name, args)
410    
411    class Subsystem:
412        def __init__(self, name):
413            self.dir = os.path.join(FIG_Config.data, "Subsystems", name.replace(" ","_"))
414    
415            if not os.path.isdir(self.dir):
416                raise NoSubsystemException("Subsystem %s not found" % (name))
417    
418        def get_version(self):
419            try:
420                fh = open(os.path.join(self.dir, "VERSION"))
421                version = fh.readline().strip()
422                try:
423                    local_version = int(version)
424                except TypeError:
425                    local_version = -1
426                fh.close()
427            except:
428                local_version = -1
429    
430            return local_version;
431    
432        def get_curator(self):
433            curator = None
434            try:
435                fh = open(os.path.join(self.dir, "curation.log"))
436                l = fh.readline().strip()
437                fh.close()
438                m = re.match(r"^\d+\t(\S+)\s+started", l)
439                if m:
440                    curator = m.group(1)
441            except:
442                pass
443    
444            return curator
445    
446  if __name__ == "__main__":  if __name__ == "__main__":
447    
448      print get_local_hostname()      print get_local_hostname()
449      print cgi_url()      print cgi_url()
450      print temp_url()      print temp_url()
451    
452        print get_seed_id()

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3