1
2
3
4 import os
5 import sys
6 import stat
7 import tempfile
8
9 from moap.extern.log import log
10 from moap.extern.command import command
11
13 """
14 Get all the modules in the given package.
15
16 @return: list of module names directly under the given packageName
17 """
18 ret = []
19
20 m = namedModule(packageName)
21 for p in m.__path__:
22 candidates = os.listdir(p)
23 for c in candidates:
24 name, ext = os.path.splitext(c)
25 if ext not in ['.py', '.pyo', '.pyc']:
26 continue
27 if name == "__init__":
28 continue
29 if ignore:
30 if name in ignore:
31 continue
32 if name not in ret:
33 ret.append(name)
34
35 return ret
36
38 """Return a module given its name."""
39 topLevel = __import__(name)
40 packages = name.split(".")[1:]
41 m = topLevel
42 for p in packages:
43 m = getattr(m, p)
44 return m
45
47 """
48 Return an editor that can be used, by checking environment variables.
49 """
50 if environ is None:
51 environ = os.environ
52 for var in ["VISUAL", "EDITOR"]:
53 if environ.has_key(var):
54 log.debug('util', 'returning editor %s' % environ[var])
55 return environ[var]
56
57 log.debug('util', 'returning None editor')
58 return None
59
61 """
62 Create a new temporary file that contains the given contents.
63
64 @return: the path to the temporary file. Unlink after use.
65 """
66 (fd, path) = tempfile.mkstemp(prefix='moap')
67 if contents:
68 for l in contents:
69 os.write(fd, "%s\n" % l)
70 os.close(fd)
71
72 return path
73
74 -def editTemp(contents=None, instructions=None, stdout=sys.stdout,
75 stderr=sys.stderr):
76 """
77 Create and edit a temporary file that contains the given
78 contents and instructions.
79
80 @type contents: list
81 @type instructions: list
82
83 @return: the list of non-empty lines in the body, or None if the file
84 was unchanged (which means, abort)
85 """
86 def w(fd, text):
87 os.write(fd, 'moap: %s\n' % text)
88
89 (fd, path) = tempfile.mkstemp(prefix='moap')
90
91 if instructions:
92 w(fd, '-' * 70)
93 for line in instructions:
94 w(fd, line)
95 w(fd, '-' * 70)
96 os.write(fd, '\n')
97
98 if contents:
99 for line in contents:
100 os.write(fd, "%s\n" % line)
101
102 os.close(fd)
103
104
105 before = os.stat(path)[stat.ST_MTIME]
106
107 editor = getEditor()
108 if not editor:
109 stderr.write("No editor found, please set the EDITOR or VISUAL "
110 "environment variable.\n")
111 os.unlink(path)
112 return None
113
114 cmd = "%s %s" % (editor, path)
115 os.system(cmd)
116
117 after = os.stat(path)[stat.ST_MTIME]
118 if before == after:
119 stdout.write("No changes, ignoring.\n")
120 os.unlink(path)
121 return None
122
123 lines = []
124 for line in open(path).readlines():
125 if line.startswith('moap:'):
126 continue
127 line = line.rstrip()
128 if not line:
129 continue
130 lines.append(line)
131
132 os.unlink(path)
133 return lines
134
136 - def __init__(self, parentCommand=None, **kwargs):
139
140
141 - def debug(self, format, *args):
144