"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/core/chdir_browsing.py" (11 Mar 2007, 2464 Bytes) of package /linux/privat/old/pysize-0.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This program is free software; you can redistribute it and/or modify
    2 # it under the terms of the GNU General Public License as published by
    3 # the Free Software Foundation; either version 2 of the License, or
    4 # (at your option) any later version.
    5 #
    6 # This program is distributed in the hope that it will be useful,
    7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    9 # GNU Library General Public License for more details.
   10 #
   11 # You should have received a copy of the GNU General Public License
   12 # along with this program; if not, write to the Free Software
   13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   14 #
   15 # See the COPYING file for license information.
   16 #
   17 # Copyright (c) 2006, 2007 Guillaume Chazarain <guichaz@yahoo.fr>
   18 
   19 import os
   20 import stat
   21 
   22 from pysize.core.deletion import filter_deleted
   23 
   24 # We cannot use a generator because we rely on the finally block to
   25 # be sure finalize() will be called. Python 2.5 allows finally block
   26 # in generator, but it relies on the GC so it's not sure it will be
   27 # called at the right time.
   28 #
   29 # The expected use of these browsing functions is:
   30 #
   31 # cookie = chdir_browsing.init(path)
   32 # try:
   33 #     for child in chdir_browsing.browsedir(cookie, (True|False)):
   34 #         .. Do something with child ..
   35 # finally:
   36 #     chdir_browsing.finalize(cookie)
   37 
   38 def init(path):
   39     previous_cwd = os.open('.', os.O_RDONLY)
   40     try:
   41         os.chdir(path)
   42         return previous_cwd
   43     except OSError, e:
   44         os.close(previous_cwd)
   45         raise e
   46 
   47 def browsedir(previous_cwd, cross_device, account_deletion=True):
   48     res = os.listdir('.')
   49     if not cross_device:
   50         def same_dev(dev, path):
   51             try:
   52                 return os.lstat(path)[stat.ST_DEV] == dev
   53             except OSError:
   54                 return True
   55         dev = os.lstat('.')[stat.ST_DEV]
   56         res = [p for p in res if same_dev(dev, p)]
   57     if account_deletion:
   58         res = filter_deleted(res)
   59     # We want the order to be always so same so that hard links
   60     # detection will be consistent across filesystems
   61     res.sort()
   62     return res
   63 
   64 def finalize(previous_cwd):
   65     if previous_cwd:
   66         try:
   67             os.fchdir(previous_cwd)
   68         finally:
   69             os.close(previous_cwd)
   70 
   71 def listdir(path, cross_device):
   72     cookie = init(path)
   73     try:
   74         return browsedir(cookie, cross_device)
   75     finally:
   76         finalize(cookie)