"Fossies" - the Fresh Open Source Software Archive

Member "pysize-0.2/pysize/main.py" (11 Mar 2007, 4137 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. For more information about "main.py" see the Fossies "Dox" file reference documentation.

    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 sys
   20 import os
   21 import optparse
   22 import locale
   23 
   24 if sys.hexversion < 0x02040000:
   25         print >> sys.stderr, 'Your python version is too old (%s)' % \
   26                                                         (sys.version.split()[0])
   27         print >> sys.stderr, 'You need at least Python 2.4'
   28         sys.exit(1)
   29 
   30 from pysize.ui.ascii import ui_ascii
   31 from pysize.ui.curses import ui_curses
   32 from pysize.ui.gtk import ui_gtk
   33 from pysize.ui.utils import UINotAvailableException
   34 from pysize.core.sigquit_traceback import install_sigquit_traceback
   35 from pysize.version import VERSION
   36 
   37 def _ui_auto(options, args):
   38     """Automatically choose the best available UI."""
   39     for ui_run in ui_gtk.run, ui_curses.run, ui_ascii.run:
   40         try:
   41             ui_run(options, args)
   42             return
   43         except UINotAvailableException:
   44             pass
   45 
   46     raise UINotAvailableException
   47 
   48 UI = {'ascii': ui_ascii.run, 'curses': ui_curses.run,
   49       'gtk': ui_gtk.run, 'auto': _ui_auto}
   50 
   51 def _profile(continuation):
   52     prof_file = 'pysize.prof'
   53     try:
   54         import cProfile
   55         import pstats
   56         print 'Profiling using cProfile'
   57         cProfile.runctx('continuation()', globals(), locals(), prof_file)
   58         stats = pstats.Stats(prof_file)
   59     except ImportError:
   60         import hotshot
   61         import hotshot.stats
   62         prof = hotshot.Profile(prof_file)
   63         print 'Profiling using hotshot'
   64         prof.runcall(continuation)
   65         prof.close()
   66         stats = hotshot.stats.load(prof_file)
   67     stats.strip_dirs()
   68     stats.sort_stats('time', 'calls')
   69     stats.print_stats(40)
   70     os.remove(prof_file)
   71 
   72 def _try_psyco():
   73     try:
   74         # Try to use psyco if available
   75         import psyco
   76         psyco.full()
   77     except ImportError:
   78         pass
   79 
   80 def main():
   81     install_sigquit_traceback()
   82     _try_psyco()
   83     locale.setlocale(locale.LC_ALL, '')
   84     usage = '%s [OPTIONS] [DIRECTORIES...]' % (sys.argv[0])
   85     parser = optparse.OptionParser(usage=usage, version='pysize ' + VERSION)
   86     parser.add_option('--ui', type='choice', choices=UI.keys(), default='auto',
   87                       help='choose the ui between: [auto], gtk, curses, ascii')
   88     parser.add_option('--xdev', action='store_false', dest='cross_device',
   89                       default=True,
   90                       help='ignore directories on other filesystems')
   91     parser.add_option('--max-depth', type='int', dest='max_depth', default=6,
   92                       metavar='DEPTH',
   93                       help='maximum depth of the displayed tree [6]')
   94     parser.add_option('--min-size', type='str', dest='min_size', default='auto',
   95                       metavar='SIZE',
   96                       help='minimum size to consider drawing [auto]')
   97     parser.add_option('--profile', action='store_true', dest='profile',
   98                       default=False, help=optparse.SUPPRESS_HELP)
   99     options, args = parser.parse_args()
  100 
  101     if options.max_depth < 2:
  102         parser.error('maximum depth must be at least 2')
  103 
  104     args = map(os.path.realpath, args)
  105     def continuation():
  106         try:
  107             UI[options.ui](options, args)
  108         except UINotAvailableException:
  109             print 'The interface "%s" is not available' % (options.ui)
  110 
  111     if options.profile:
  112         _profile(continuation)
  113     else:
  114         continuation()