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, 2007, 2008 Guillaume Chazarain <guichaz@gmail.com>
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'
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
.version
import VERSION
35 from pysize
.core
.exception_propagation
import unexpect
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
:
43 except UINotAvailableException
:
46 raise UINotAvailableException
48 UI
= {'ascii': ui_ascii
.run
, 'curses': ui_curses
.run
,
49 'gtk': ui_gtk
.run
, 'auto': _ui_auto
}
51 def _profile(continuation
):
52 prof_file
= 'pysize.prof'
56 print 'Profiling using cProfile'
57 cProfile
.runctx('continuation()', globals(), locals(), prof_file
)
58 stats
= pstats
.Stats(prof_file
)
62 prof
= hotshot
.Profile(prof_file
)
63 print 'Profiling using hotshot'
64 prof
.runcall(continuation
)
66 stats
= hotshot
.stats
.load(prof_file
)
68 stats
.sort_stats('time', 'calls')
74 # Try to use psyco if available
80 def setprocname(name
):
81 # From comments on http://davyd.livejournal.com/166352.html
85 libc
= ctypes
.CDLL(None)
86 # Linux 2.6 PR_SET_NAME
87 if libc
.prctl(15, name
, 0, 0, 0):
89 libc
.setproctitle(name
)
96 # Linux 2.6 PR_SET_NAME
97 if libc
.call('prctl', 15, name
, 0, 0, 0):
99 libc
.call('setproctitle', name
)
106 locale
.setlocale(locale
.LC_ALL
, '')
107 setprocname('pysize')
108 usage
= '%s [OPTIONS] [DIRECTORIES...]' % (sys
.argv
[0])
109 parser
= optparse
.OptionParser(usage
=usage
, version
='pysize ' + VERSION
)
110 parser
.add_option('--ui', type='choice', choices
=UI
.keys(), default
='auto',
111 help='choose the ui between: [auto], gtk, curses, ascii')
112 parser
.add_option('--xdev', action
='store_false', dest
='cross_device',
114 help='ignore directories on other filesystems')
115 parser
.add_option('--max-depth', type='int', dest
='max_depth', default
=6,
117 help='maximum depth of the displayed tree [6]')
118 parser
.add_option('--profile', action
='store_true', dest
='profile',
119 default
=False, help=optparse
.SUPPRESS_HELP
)
120 options
, args
= parser
.parse_args()
122 if options
.max_depth
< 2:
123 parser
.error('maximum depth must be at least 2')
125 args
= map(os
.path
.realpath
, args
)
128 UI
[options
.ui
](options
, args
)
129 except UINotAvailableException
:
130 print 'The interface "%s" is not available' % (options
.ui
)
133 _profile(continuation
)