Commit | Line | Data |
---|---|---|
0b302171 | 1 | # Copyright (C) 2010-2012 Free Software Foundation, Inc. |
aa2e2d8d DE |
2 | |
3 | # This program is free software; you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation; either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # This program is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
7b51bc51 | 15 | |
5e239b84 | 16 | import traceback |
b9516fa1 YPK |
17 | import os |
18 | import sys | |
19 | import _gdb | |
20 | ||
21 | from _gdb import * | |
22 | ||
23 | class GdbOutputFile: | |
24 | def close(self): | |
25 | # Do nothing. | |
26 | return None | |
27 | ||
28 | def isatty(self): | |
29 | return False | |
30 | ||
31 | def write(self, s): | |
32 | write(s, stream=STDOUT) | |
33 | ||
34 | def writelines(self, iterable): | |
35 | for line in iterable: | |
36 | self.write(line) | |
37 | ||
38 | def flush(self): | |
39 | flush() | |
40 | ||
41 | sys.stdout = GdbOutputFile() | |
42 | ||
43 | class GdbOutputErrorFile: | |
44 | def close(self): | |
45 | # Do nothing. | |
46 | return None | |
47 | ||
48 | def isatty(self): | |
49 | return False | |
50 | ||
51 | def write(self, s): | |
52 | write(s, stream=STDERR) | |
53 | ||
54 | def writelines(self, iterable): | |
55 | for line in iterable: | |
56 | self.write(line) | |
57 | ||
58 | def flush(self): | |
59 | flush() | |
60 | ||
61 | sys.stderr = GdbOutputErrorFile() | |
62 | ||
63 | # Default prompt hook does nothing. | |
64 | prompt_hook = None | |
65 | ||
66 | # Ensure that sys.argv is set to something. | |
67 | # We do not use PySys_SetArgvEx because it did not appear until 2.6.6. | |
68 | sys.argv = [''] | |
69 | ||
70 | # Initial pretty printers. | |
71 | pretty_printers = [] | |
72 | ||
18a9fc12 TT |
73 | # Initial type printers. |
74 | type_printers = [] | |
75 | ||
b9516fa1 YPK |
76 | # Convenience variable to GDB's python directory |
77 | PYTHONDIR = os.path.dirname(os.path.dirname(__file__)) | |
7b51bc51 | 78 | |
5e239b84 PM |
79 | # Auto-load all functions/commands. |
80 | ||
b9516fa1 | 81 | # Packages to auto-load. |
5e239b84 | 82 | |
b9516fa1 YPK |
83 | packages = [ |
84 | 'function', | |
85 | 'command' | |
86 | ] | |
5e239b84 | 87 | |
b9516fa1 YPK |
88 | # pkgutil.iter_modules is not available prior to Python 2.6. Instead, |
89 | # manually iterate the list, collating the Python files in each module | |
5e239b84 PM |
90 | # path. Construct the module name, and import. |
91 | ||
b9516fa1 YPK |
92 | def auto_load_packages(): |
93 | for package in packages: | |
94 | location = os.path.join(os.path.dirname(__file__), package) | |
95 | if os.path.exists(location): | |
96 | py_files = filter(lambda x: x.endswith('.py') | |
97 | and x != '__init__.py', | |
98 | os.listdir(location)) | |
99 | ||
100 | for py_file in py_files: | |
101 | # Construct from foo.py, gdb.module.foo | |
102 | modname = "%s.%s.%s" % ( __name__, package, py_file[:-3] ) | |
103 | try: | |
104 | if modname in sys.modules: | |
105 | # reload modules with duplicate names | |
106 | reload(__import__(modname)) | |
107 | else: | |
108 | __import__(modname) | |
109 | except: | |
110 | print >> sys.stderr, traceback.format_exc() | |
111 | ||
112 | auto_load_packages() | |
113 | ||
114 | def GdbSetPythonDirectory(dir): | |
115 | """Update sys.path, reload gdb and auto-load packages.""" | |
116 | global PYTHONDIR | |
117 | ||
118 | try: | |
119 | sys.path.remove(PYTHONDIR) | |
120 | except ValueError: | |
121 | pass | |
122 | sys.path.insert(0, dir) | |
123 | ||
124 | PYTHONDIR = dir | |
125 | ||
126 | # note that reload overwrites the gdb module without deleting existing | |
127 | # attributes | |
128 | reload(__import__(__name__)) | |
129 | auto_load_packages() |