root/gaphor/tags/gaphor-0.12.5/utils/command/run.py

Revision 1690, 5.0 kB (checked in by wrobe..@pld-linux.org, 1 year ago)

- use cProfile if possible

Line 
1 """
2 Command for running gaphor and tests directly from setup.py.
3 """
4
5 import sys, os.path
6 from distutils.core import Command
7 from pkg_resources import load_entry_point
8
9 class run(Command):
10
11     description = 'Launch Gaphor from the local directory'
12
13     user_options = [
14         ('build-dir=', None, ''),
15         ('command=', 'c', 'execute command'),
16         ('file=', 'f', 'execute file'),
17         ('doctest=', 'd', 'execute doctests in module (e.g. gaphor.geometry)'),
18         ('unittest=', 'u', 'execute unittest file (e.g. tests/test-ns.py)'),
19         ('model=', 'm', 'load a model file'),
20         ('coverage', None, 'Calculate coverage (utils/coverage.py)'),
21         ('profile', 'p', 'Run with profiling enabled'),
22     ]
23
24     def initialize_options(self):
25         self.build_lib = None
26         self.command = None
27         self.file = None
28         self.doctest = None
29         self.unittest = None
30         self.model = None
31         self.coverage = None
32         self.verbosity = 2
33         self.profile = None
34
35     def finalize_options(self):
36         self.set_undefined_options('build',
37                                    ('build_lib', 'build_lib'))
38
39     def run(self):
40         print 'Starting Gaphor...'
41
42         if self.model:
43             print 'Starting with model file', self.model
44
45         for cmd_name in self.get_sub_commands():
46             self.run_command(cmd_name)
47         #if self.build_lib not in sys.path:
48             #sys.path.insert(0, self.build_lib)
49        
50         import gaphor
51         #os.environ['GAPHOR_DATADIR'] = os.path.abspath('data')
52         if self.coverage:
53             from utils import coverage
54             coverage.start()
55
56         if self.command:
57             print 'Executing command: %s...' % self.command
58             exec self.command
59
60         elif self.doctest:
61             print 'Running doctest cases in module: %s...' % self.doctest
62             import imp
63             # use zope's one since it handles coverage right
64             from zope.testing import doctest
65
66             # Figure out the file:
67             f = os.path.join(*self.doctest.split('.')) + '.py'
68             fp = open(f)
69             # Prepend module's package path to sys.path
70             pkg = os.path.join(self.build_lib, *self.doctest.split('.')[:-1])
71             #if pkg:
72             #    sys.path.insert(0, pkg)
73             #    print 'Added', pkg, 'to sys.path'
74             # Load the module as local module (without package)
75             test_module = imp.load_source(self.doctest.split('.')[-1], f, fp)
76             failure, tests = doctest.testmod(test_module, name=self.doctest,
77                  optionflags=doctest.ELLIPSIS + doctest.NORMALIZE_WHITESPACE)
78             if self.coverage:
79                 print
80                 print 'Coverage report:'
81                 coverage.report(f)
82             sys.exit(failure != 0)
83
84         elif self.unittest:
85             # Running a unit test is done by opening the unit test file
86             # as a module and running the tests within that module.
87             print 'Running test cases in unittest file: %s...' % self.unittest
88             import imp, unittest
89             fp = open(self.unittest)
90             test_module = imp.load_source('gaphor_test', self.unittest, fp)
91             test_suite = unittest.TestLoader().loadTestsFromModule(test_module)
92             #test_suite = unittest.TestLoader().loadTestsFromName(self.unittest)
93             test_runner = unittest.TextTestRunner(verbosity=self.verbosity)
94             result = test_runner.run(test_suite)
95             if self.coverage:
96                 print
97                 print 'Coverage report:'
98                 coverage.report(self.unittest)
99             sys.exit(not result.wasSuccessful())
100
101         elif self.file:
102             print 'Executing file: %s...' % self.file
103             dir, f = os.path.split(self.file)
104             print 'Extending PYTHONPATH with %s' % dir
105             #sys.path.append(dir)
106             execfile(self.file, {})
107         else:
108             print 'Launching Gaphor...'
109             #gaphor.main(self.model)
110             starter = load_entry_point('gaphor==%s' % (self.distribution.get_version(),), 'console_scripts', 'gaphor')
111
112             if self.profile:
113                 print 'Enabling profiling...'
114                 try:
115                     import cProfile
116                     import pstats
117                     prof = cProfile.Profile()
118                     prof.runcall(starter)
119                     prof.dump_stats('gaphor.prof')
120                     p = pstats.Stats('gaphor.prof')
121                     p.strip_dirs().sort_stats('time').print_stats(20)
122                 except ImportError, ex:
123                     import hotshot, hotshot.stats
124                     prof = hotshot.Profile('gaphor.prof')
125                     prof.runcall(starter)
126                     prof.close()
127                     stats = hotshot.stats.load('gaphor.prof')
128                     stats.strip_dirs()
129                     stats.sort_stats('time', 'calls')
130                     stats.print_stats(20)
131             else:
132                 starter()
133
134     sub_commands = [('build', None)]
135
136 # vim:sw=4:et
Note: See TracBrowser for help on using the browser.