Increment the default max depth
[pysize.git] / pysize / main.py
blob426cfbf08473f28504cd526eccb2dcd6700c06e9
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 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)
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
36 def _ui_auto(options, args):
37 """Automatically choose the best available UI."""
38 for ui_run in ui_gtk.run, ui_curses.run, ui_ascii.run:
39 try:
40 ui_run(options, args)
41 return
42 except UINotAvailableException:
43 pass
45 raise UINotAvailableException
47 UI = {'ascii': ui_ascii.run, 'curses': ui_curses.run,
48 'gtk': ui_gtk.run, 'auto': _ui_auto}
50 def _profile(continuation):
51 prof_file = 'pysize.prof'
52 try:
53 import cProfile
54 import pstats
55 print 'Profiling using cProfile'
56 cProfile.runctx('continuation()', globals(), locals(), prof_file)
57 stats = pstats.Stats(prof_file)
58 except ImportError:
59 import hotshot
60 import hotshot.stats
61 prof = hotshot.Profile(prof_file)
62 print 'Profiling using hotshot'
63 prof.runcall(continuation)
64 prof.close()
65 stats = hotshot.stats.load(prof_file)
66 stats.strip_dirs()
67 stats.sort_stats('time', 'calls')
68 stats.print_stats(40)
69 os.remove(prof_file)
71 def _try_psyco():
72 try:
73 # Try to use psyco if available
74 import psyco
75 psyco.full()
76 except ImportError:
77 pass
79 def main():
80 install_sigquit_traceback()
81 _try_psyco()
82 locale.setlocale(locale.LC_ALL, '')
83 usage = '%s [OPTIONS] [DIRECTORIES...]' % (sys.argv[0])
84 parser = optparse.OptionParser(usage=usage)
85 parser.add_option('--ui', type='choice', choices=UI.keys(), default='auto',
86 help='choose the ui between: [auto], gtk, curses, ascii')
87 parser.add_option('--xdev', action='store_false', dest='cross_device',
88 default=True,
89 help='ignore directories on other filesystems')
90 parser.add_option('--max-depth', type='int', dest='max_depth', default=6,
91 metavar='DEPTH',
92 help='maximum depth of the displayed tree [6]')
93 parser.add_option('--min-size', type='str', dest='min_size', default='auto',
94 metavar='SIZE',
95 help='minimum size to consider drawing [auto]')
96 parser.add_option('--fast-drawing', action='store_true', dest='fast',
97 default=False, help='faster but simpler drawing with GTK')
98 parser.add_option('--profile', action='store_true', dest='profile',
99 default=False, help=optparse.SUPPRESS_HELP)
100 options, args = parser.parse_args()
102 if options.max_depth < 2:
103 parser.error('maximum depth must be at least 2')
105 args = map(os.path.realpath, args)
106 def continuation():
107 try:
108 UI[options.ui](options, args)
109 except UINotAvailableException:
110 print 'The interface "%s" is not available' % (options.ui)
112 if options.profile:
113 _profile(continuation)
114 else:
115 continuation()