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 __reduce__(self):
47 # Prevent pickling of File 48 raise pickle.PicklingError("Can't pickle a File: %s" % self)
49
50 - def _stat(self):
51 if self._os_stat is None: 52 self._os_stat = os.lstat(self.abspath) 53 return self._os_stat
54
55 - def _abspath(self):
56 if self._file: 57 return os.path.join(self._dir, self._file) 58 else: 59 return self._dir
60 61 abspath = property(lambda self: os.path.abspath(self), 62 doc = """Absolute path to this file.""") 63 stat = property(lambda self: self._stat(), 64 doc = """Information on this file, as returned by C{os.stat}.""") 65 mode = property(lambda self: self._stat()[0], 66 doc = """mode of this file.""") 67 inode = property(lambda self: self._stat()[1], 68 doc = """inode of this file.""") 69 device = property(lambda self: self._stat()[2], 70 doc = """device of this file.""") 71 links = property(lambda self: self._stat()[3], 72 doc = """ Number of links of this file.""") 73 uid = property(lambda self: self._stat()[4], 74 doc = """Owner of this file.""") 75 gid = property(lambda self: self._stat()[5], 76 doc = """Owning group of this file.""") 77 size = property(lambda self: self._stat()[6], 78 doc = """Size of this file (bytes).""") 79 atime = property(lambda self: self._stat()[7], 80 doc = """Access time of this file.""") 81 mtime = property(lambda self: self._stat()[8], 82 doc = """Modify time of this file.""") 83 ctime = property(lambda self: self._stat()[9], 84 doc = """Change time of this file.""") 85 isdir = property(lambda self: self.mode & FILE_TYPE_MASK == DIR_MASK, 86 doc = """True iff this file is a directory.""") 87 isfile = property(lambda self: self.mode & FILE_TYPE_MASK == FILE_MASK, 88 doc = """True iff this file is neither a directory nor a symlink.""") 89 islink = property(lambda self: self.mode & FILE_TYPE_MASK == LINK_MASK, 90 doc = """True iff this file is a symlink.""")
91