[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.13, Mon Dec 5 19:06:30 2005 UTC
# Line 1  Line 1 
1    #
2    # Copyright (c) 2003-2006 University of Chicago and Fellowship
3    # for Interpretations of Genomes. All Rights Reserved.
4    #
5    # This file is part of the SEED Toolkit.
6    #
7    # The SEED Toolkit is free software. You can redistribute
8    # it and/or modify it under the terms of the SEED Toolkit
9    # Public License.
10    #
11    # You should have received a copy of the SEED Toolkit Public License
12    # along with this program; if not write to the University of Chicago
13    # at info@ci.uchicago.edu or the Fellowship for Interpretation of
14    # Genomes at veronika@thefig.info or download a copy from
15    # http://www.theseed.org/LICENSE.TXT.
16    #
17    
18  import urlparse  import urlparse
19  import socket  import socket
20  import os  import os
21    import popen2
22  import sys  import sys
23  import re  import re
24    import xmlrpclib
25    
26  import FIG_Config  import FIG_Config
27    
28    import Clearinghouse
29    
30    class NoSubsystemException(Exception):
31        pass
32    
33    def get_clearinghouse(url = None):
34        return Clearinghouse.Clearinghouse(url)
35    
36    def get_ss_hierarchy(url = None):
37        return Clearinghouse.SSHierarchy(url)
38    
39    #
40    # Subsystem helper code.
41    #
42    
43    def get_subsystem(name):
44        try:
45            sub = Subsystem(name)
46        except NoSubsystemException:
47            sub = None
48    
49        return sub
50    
51    
52  def get_local_hostname():  def get_local_hostname():
53      #      #
54        # See if there is a FIGdisk/config/hostname file. If there
55        # is, force the hostname to be that.
56        #
57    
58        try:
59            fh = open(os.path.join(FIG_Config.fig_disk, "config", "hostname"))
60            host = fh.readline()
61            return host.strip()
62        except:
63            pass
64    
65        #
66      # First check to see if we our hostname is correct.      # First check to see if we our hostname is correct.
67      #      #
68      # 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.
69      #      #
70    
71      hostname = socket.getfqdn()      hostname = socket.getfqdn()
72      hostname = "yips"  
73        #
74        # See if hostname is something.local., which is what
75        # a Mac will return if it didn't get a name via some
76        # other mechanism (DHCP or static config). We have to
77        # check here because otherwise it will pass the fqdn and
78        # local binding test.
79        #
80    
81        if not re.search(r"\.local\.?$", hostname):
82    
83      #      #
84      # 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.
85      #      #
# Line 131  Line 197 
197      for l in fh:      for l in fh:
198          cols = l.strip().split()          cols = l.strip().split()
199    
200          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"):
201              interface_name = cols[-1]              interface_name = cols[-1]
202              break              break
203    
204      fh.close()      fh.close()
205    
206      print "Default route on ", interface_name      # print "Default route on ", interface_name
207    
208      #      #
209      # Find ifconfig.      # Find ifconfig.
# Line 157  Line 223 
223          print >> sys.stderr, "Ifconfig not found"          print >> sys.stderr, "Ifconfig not found"
224          return "localhost"          return "localhost"
225    
226      print >> sys.stderr, "found ifconfig ", ifconfig      # print >> sys.stderr, "found ifconfig ", ifconfig
227    
228      try:      try:
229          fh = os.popen(ifconfig+ " " + interface_name, "r")          fh = os.popen(ifconfig+ " " + interface_name, "r")
# Line 204  Line 270 
270    
271      return ip      return ip
272    
273    def top_link():
274    
275        #
276        # Determine if this is a toplevel cgi or one in one of the subdirs (currently
277        # just /p2p).
278        #
279    
280        sname = os.getenv("SCRIPT_NAME")
281    
282        if sname is not None:
283            parts = os.getenv("SCRIPT_NAME").split('/');
284    
285            if len(parts) > 2 and parts[-2] == 'FIG':
286                top = '.'
287            elif len(parts) > 3 and parts[-3] == 'FIG':
288                top = '..'
289            else:
290                top = FIG_Config.cgi_base
291        else:
292            top = FIG_Config.cgi_base
293    
294    
295    
296        return top
297    
298  def cgi_url():  def cgi_url():
299      return plug_url(FIG_Config.cgi_url)      return top_link()
300        # return plug_url(FIG_Config.cgi_url)
301    
302  def temp_url():  def temp_url():
303      return plug_url(FIG_Config.temp_url)      return plug_url(FIG_Config.temp_url)
# Line 226  Line 318 
318      new_url = urlparse.urlunparse(p)      new_url = urlparse.urlunparse(p)
319      return new_url      return new_url
320    
321    def get_seed_id():
322        #
323        # Retrieve the seed identifer from FIGdisk/config/seed_id.
324        #
325        # If it's not there, create one, and make it readonly.
326        #
327    
328        id_file = os.path.join(FIG_Config.fig_disk, "config", "seed_id")
329        if not os.path.isfile(id_file):
330    
331            fh = os.popen("uuidgen", "r")
332    
333            newid = fh.readline()
334            newid = newid.strip()
335    
336            fh.close()
337    
338            fh = open(id_file, "w")
339            print >>fh, newid
340            fh.close()
341    
342            os.chmod(id_file, 0444)
343    
344        fh = open(id_file)
345        id = fh.readline()
346        fh.close()
347        id = id.strip()
348        return id
349    
350    #
351    # Define a FIG class; this is analagous to the FIG class used in FIG.pm
352    #
353    # It also lets us cache stuff, and use __call__ to map calls to
354    # an XMLRPC server for the perl stuff we don't implement locally.
355    #
356    
357    class FIG:
358    
359        def __init__(self):
360            self.xmlrpc_proxy = None
361            self.xmlrpc_proc = None
362    
363        def __repr__(self):
364            return "FIG instance %s" % ( self)
365    
366        def __str__(self):
367            return "FIG instance %s" % (id(self))
368    
369        def __getattr__(self, name):
370            g = globals()
371            if g.has_key(name) and callable(g[name]):
372                return g[name]
373    
374            if name.startswith("_"):
375                return None
376    
377            #
378            # Not accessible in globals. Return an XMLRPC calling proxy.
379            #
380    
381            return XMLRPCCaller(self, name)
382    
383        def foo(self):
384            print "FOO"
385    
386    
387        def call_xmlrpc(self, name, args):
388    
389            try:
390                if self.xmlrpc_proxy is None:
391                    self.start_xmlrpc_server()
392    
393            except Exception, e:
394                print "Got exception ... ", e
395                return
396    
397            proc = getattr(self.xmlrpc_proxy, name)
398            retval = apply(proc, args)
399            return retval
400    
401        def start_xmlrpc_server(self):
402            server_path = os.path.join(FIG_Config.bin, "fig_xmlrpc_server")
403    
404            if not os.access(server_path, os.X_OK):
405                raise Exception, "XMLRPC server path %s not found" % (server_path)
406    
407    
408            proc = self.xmlrpc_proc = popen2.Popen3(server_path, 0)
409    
410            print "Server started ", proc.pid
411    
412            url = proc.fromchild.readline()
413            url = url.strip()
414            print "Read url ", url
415    
416            proc.fromchild.close()
417    
418            self.xmlrpc_proxy = xmlrpclib.ServerProxy(url)
419    
420    class XMLRPCCaller:
421        def __init__(self, fig, name):
422            self.fig = fig
423            self.name = name
424    
425        def __call__(self, *args):
426            return self.fig.call_xmlrpc(self.name, args)
427    
428    class Subsystem:
429        def __init__(self, name):
430            self.dir = os.path.join(FIG_Config.data, "Subsystems", name.replace(" ","_"))
431    
432            if not os.path.isdir(self.dir):
433                raise NoSubsystemException("Subsystem %s not found" % (name))
434    
435        def get_version(self):
436            try:
437                fh = open(os.path.join(self.dir, "VERSION"))
438                version = fh.readline().strip()
439                try:
440                    local_version = int(version)
441                except TypeError:
442                    local_version = -1
443                fh.close()
444            except:
445                local_version = -1
446    
447            return local_version;
448    
449        def get_curator(self):
450            curator = None
451            try:
452                fh = open(os.path.join(self.dir, "curation.log"))
453                l = fh.readline().strip()
454                fh.close()
455                m = re.match(r"^\d+\t(\S+)\s+started", l)
456                if m:
457                    curator = m.group(1)
458            except:
459                pass
460    
461            return curator
462    
463  if __name__ == "__main__":  if __name__ == "__main__":
464    
465      print get_local_hostname()      print get_local_hostname()
466      print cgi_url()      print cgi_url()
467      print temp_url()      print temp_url()
468    
469        print get_seed_id()

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3