6.19.5. The platform frame

The _platform frame_ contains platform specific data and functions, such as the name of the operating system. These include build options of Python itself, and the platform dependent modules such as 'posix' representing platform specific operating system services and access paths.

Note the tmp.tmp file which is used as a bit bucket for stderr. This works on NT and Unix. It's ugly. There's no hope of this working on the Mac.

Start python section to interscript/frames/platform/__init__.py[1 /1 ]
     1: #line 14 "platform_frame.ipk"
     2: import os
     3: os_name = os.name
     4: exec 'from interscript.frames.platform.'+os_name+' import platform_frame'
     5: platform = platform_frame()
     6: 
End python section to interscript/frames/platform/__init__.py[1]
Start python section to interscript/frames/platform/posix.py[1 /1 ]
     1: #line 21 "platform_frame.ipk"
     2: import sys
     3: import os
     4: import string
     5: import errno
     6: import tempfile
     7: 
     8: class platform_frame:
     9:   def __init__(self):
    10:     self.python_plat = sys.platform
    11:     self.os = os
    12:     self.tempfile = tempfile
    13:     self.errno = errno
    14:     self.open = open
    15: 
    16:     self.uname = ['unknown','unknown','unknown']
    17:     try:
    18:       # operating system name
    19:       f = self.os.popen('uname -s 2>tmp.tmp','r')
    20:       self.uname[0] = f.read()
    21:       f.close()
    22:       del f
    23:       # operating system version
    24:       f = self.os.popen('uname -v 2>tmp.tmp','r')
    25:       self.uname[1] = f.read()
    26:       f.close()
    27:       del f
    28:       # operating system release
    29:       f = self.os.popen('uname -r 2>tmp.tmp','r')
    30:       self.uname[2] = f.read()
    31:       f.close()
    32:       del f
    33:     except:
    34:       pass# OS dependent routines.
    35: 
    36:   # Note: we use some posixpath functions, but don't trust them
    37: 
    38:   # make the given directory, no error if it exists
    39:   # this is posix specific, and should be moved to a platform
    40:   # dependent place in the code
    41: 
    42:   def create_directory(self,dir):
    43:     if not self.os.path.isdir(dir):
    44:       try: self.os.mkdir(dir)
    45:       except os.error, data:
    46:         if data[0]!=self.errno.EEXIST: # File Exists is OK, everything else is fatal
    47:           raise os.error, (data[0],data+': directory "'+dir+'"')
    48:     if not self.os.path.isdir(dir):
    49:       raise self.os.error, (self.errno.ENOENT, 'Created a directory '+dir+', but it is not there!')
    50: 
    51:   # given an os specific prefix and a list of component names,
    52:   # make the directory structure in which the last component is contained
    53:   # and synthesise and return its full os specific pathname
    54: 
    55:   def mk_dir(self,prefix, pathlist):
    56:     if len(pathlist)>1:
    57:       # there's more than one component in the list
    58:       # so create directories for all but the last component
    59: 
    60:       pathname = prefix+pathlist[0]
    61:       self.create_directory(pathname)
    62:       for component in pathlist[1:-1]:
    63:         pathname = pathname + self.os.sep + component
    64:         self.create_directory(pathname)
    65:       pathname = pathname + self.os.sep + pathlist[-1]
    66: 
    67:     else:
    68:       # only one component on the list
    69:       pathname = prefix+pathlist[0]
    70: 
    71:     if pathname[0]!=self.os.sep:
    72:       # the pathname isn't absolute, so make it so
    73:       # get current directory
    74:       curdir = self.os.getcwd()
    75: 
    76:       # strip trailing separator
    77:       # note this should fix cases like '/' (unix) or 'd:\' (nt)
    78:       # as well as cope with systems that return a trailing separator
    79:       if curdir[-1] == self.os.sep: curdir = curdir[:-1]
    80: 
    81:       # append a separator and the pathname: there will only be one
    82:       # separator at the join point unless the current directory
    83:       # ends with two separators (like the URL: http://)
    84:       pathname = curdir + self.os.sep + pathname
    85:     return pathname
    86: 
    87:   # this routine checks is a file exists and is readable
    88:   # (in the sense that it can be opened for read)
    89:   # it returns 1 if the file exist and can be read, 0 if the file
    90:   # doesn't exist, and throws an exception if anything else goes wrong
    91: 
    92:   def file_exists(self,pathname):
    93:     try:
    94:       # note this leaks a file if it is opened but not closed :-))
    95:       open(pathname,'r').close()
    96:       return 1
    97:     except IOError, data:
    98:       if data[0] == self.errno.ENOENT:
    99:         return 0
   100:       raise IOError, data
   101: 
   102:   # Note: the intention is to apply mk_dir to ensure the file has a
   103:   # parent directory, creating it if necessary, then test if the file
   104:   # already exists or has to be created. Even if it exists, it may have
   105:   # to be replaced.
   106: 
   107:   def map_filename(self, prefix, path):
   108:     return prefix+string.join(string.split(path,'/'),self.os.sep)
   109: 
   110:   def get_working_directory(self): # native filename
   111:     return os.getcwd()
   112: 
   113:   def getmtime(self, filename):
   114:     if self.file_exists(filename):
   115:       return self.os.path.getmtime(filename)
   116:     else:
   117:       return 0
End python section to interscript/frames/platform/posix.py[1]