Package osh :: Module file
[frames] | no frames]

Source Code for Module osh.file

 1  # osh 
 2  # Copyright (C) Jack Orenstein <jao@geophile.com> 
 3  # 
 4  # This program is free software; you can redistribute it and/or modify 
 5  # it under the terms of the GNU General Public License as published by 
 6  # the Free Software Foundation; either version 2 of the License, or 
 7  # (at your option) any later version. 
 8  # 
 9  # This program is distributed in the hope that it will be useful, 
10  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
12  # GNU General Public License for more details. 
13  # 
14  # You should have received a copy of the GNU General Public License 
15  # along with this program; if not, write to the Free Software 
16  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
17   
18  """Represents a file or directory on the local host. 
19  """ 
20   
21  import os 
22  import pickle 
23   
24  import osh.util 
25   
26  DIR_MASK = 040000 
27  FILE_MASK = 0100000 
28  LINK_MASK = 0120000 
29  FILE_TYPE_MASK = DIR_MASK | FILE_MASK | LINK_MASK 
30   
31 -class File(str):
32 33 _os_stat = None 34
35 - def __new__(self, dir, file = None):
36 if file is None: 37 path = dir 38 else: 39 path = os.path.join(dir, file) 40 return str.__new__(self, path)
41
42 - def __init__(self, dir, file = None):
43 self._dir = dir 44 self._file = file
45
46 - def _stat(self):
47 if self._os_stat is None: 48 self._os_stat = os.lstat(self.abspath) 49 return self._os_stat
50
51 - def _abspath(self):
52 if self._file: 53 return os.path.join(self._dir, self._file) 54 else: 55 return self._dir
56 57 abspath = property(lambda self: os.path.abspath(self), 58 doc = """Absolute path to this file.""") 59 stat = property(lambda self: self._stat(), 60 doc = """Information on this file, as returned by C{os.stat}.""") 61 mode = property(lambda self: self._stat()[0], 62 doc = """mode of this file.""") 63 inode = property(lambda self: self._stat()[1], 64 doc = """inode of this file.""") 65 device = property(lambda self: self._stat()[2], 66 doc = """device of this file.""") 67 links = property(lambda self: self._stat()[3], 68 doc = """ Number of links of this file.""") 69 uid = property(lambda self: self._stat()[4], 70 doc = """Owner of this file.""") 71 gid = property(lambda self: self._stat()[5], 72 doc = """Owning group of this file.""") 73 size = property(lambda self: self._stat()[6], 74 doc = """Size of this file (bytes).""") 75 atime = property(lambda self: self._stat()[7], 76 doc = """Access time of this file.""") 77 mtime = property(lambda self: self._stat()[8], 78 doc = """Modify time of this file.""") 79 ctime = property(lambda self: self._stat()[9], 80 doc = """Change time of this file.""") 81 isdir = property(lambda self: self.mode & FILE_TYPE_MASK == DIR_MASK, 82 doc = """True iff this file is a directory.""") 83 isfile = property(lambda self: self.mode & FILE_TYPE_MASK == FILE_MASK, 84 doc = """True iff this file is neither a directory nor a symlink.""") 85 islink = property(lambda self: self.mode & FILE_TYPE_MASK == LINK_MASK, 86 doc = """True iff this file is a symlink.""")
87