Added tag pysize-0.1 for changeset 2f6eb10afebecf8fbf226fd49e8eb60357a1a993
[pysize.git] / pysize / main.py
blobaee554a2c70073e6f8b6a1220d5946fd79661e51
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.
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.
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.
15 # See the COPYING file for license information.
17 # Copyright (c) 2006 Guillaume Chazarain <guichaz@yahoo.fr>
19 import sys
20 import os
21 import optparse
22 import locale
24 from pysize.ui.ascii import ui_ascii
25 from pysize.ui.curses import ui_curses
26 from pysize.ui.gtk import ui_gtk
27 from pysize.core.sigquit_traceback import install_sigquit_traceback
29 def _ui_auto(options, args):
30 """Automatically choose the best available UI."""
31 if ui_gtk.is_available():
32 return ui_gtk.run(options, args)
34 if ui_curses.is_available():
35 return ui_curses.run(options, args)
37 if ui_ascii.is_available():
38 return ui_ascii.run(options, args)
40 print 'No UI available'
42 UI = {'ascii': ui_ascii.run, 'curses': ui_curses.run,
43 'gtk': ui_gtk.run, 'auto': _ui_auto}
45 def _profile(continuation):
46 try:
47 import cProfile
48 import pstats
49 print 'Profiling using cProfile'
50 cProfile.runctx('continuation()', globals(), locals(), 'pysize.prof')
51 stats = pstats.Stats('pysize.prof')
52 except ImportError:
53 import hotshot
54 import hotshot.stats
55 prof = hotshot.Profile('pysize.prof')
56 print 'Profiling using hotshot'
57 prof.runcall(continuation)
58 prof.close()
59 stats = hotshot.stats.load('pysize.prof')
60 stats.strip_dirs()
61 stats.sort_stats('time', 'calls')
62 stats.print_stats(40)
64 def _try_psyco():
65 try:
66 # Try to use psyco if available
67 import psyco
68 psyco.full()
69 except ImportError:
70 pass
72 def main():
73 install_sigquit_traceback()
74 _try_psyco()
75 locale.setlocale(locale.LC_ALL, '')
76 usage = '%s [OPTIONS] [DIRECTORIES...]' % (sys.argv[0])
77 parser = optparse.OptionParser(usage=usage)
78 parser.add_option('--ui', type='choice', choices=UI.keys(), default='auto',
79 help='choose the ui between: [auto], gtk, curses, ascii')
80 parser.add_option('--xdev', action='store_false', dest='cross_device',
81 default=True,
82 help="ignore directories on other filesystems")
83 parser.add_option('--max-depth', type='int', dest='max_depth', default=5,
84 metavar='DEPTH',
85 help='maximum depth of the displayed tree [5]')
86 parser.add_option('--min-size', type='str', dest='min_size', default='auto',
87 metavar='SIZE',
88 help='minimum size to consider drawing [auto]')
89 parser.add_option('--fast-drawing', action='store_true', dest='fast',
90 default=False, help='faster but simpler drawing with GTK')
91 parser.add_option('--profile', action='store_true', dest='profile',
92 default=False, help=optparse.SUPPRESS_HELP)
93 options, args = parser.parse_args()
95 if options.max_depth < 2:
96 parser.error('maximum depth must be at least 2')
98 args = map(os.path.realpath, args)
99 continuation = lambda: UI[options.ui](options, args)
101 if options.profile:
102 _profile(continuation)
103 else:
104 continuation()