]> code.delx.au - gnu-emacs/blob - modules/modhelp.py
Update copyright year to 2016
[gnu-emacs] / modules / modhelp.py
1 #!/usr/bin/env python
2
3 # Module helper script.
4
5 # Copyright 2015-2016 Free Software Foundation, Inc.
6
7 # This file is part of GNU Emacs.
8
9 # GNU Emacs is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
13
14 # GNU Emacs is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 import os
23 import string
24 import subprocess as sp
25 import argparse
26 import re
27
28 EMACS = os.path.join('..', 'src', 'emacs')
29
30 def find_modules():
31 modpaths = []
32 for (dirname, dirs, files) in os.walk('.'):
33 if 'Makefile' in files:
34 modpaths.append(dirname)
35 return modpaths
36
37 def cmd_test(args):
38 mods = args.module
39 if not mods:
40 mods = find_modules()
41
42 make_cmd = ['make']
43 if args.force:
44 make_cmd.append('-B')
45
46 failed = []
47 for m in mods:
48 print '[*] %s: ------- start -------' % m
49 print '[*] %s: running make' % m
50 r = sp.call(make_cmd, cwd=m)
51 if r != 0:
52 print '[E] %s: make failed' % m
53 failed += [m]
54 continue
55
56 print '[*] %s: running test' % m
57 testpath = os.path.join(m, 'test.el')
58 if os.path.isfile(testpath):
59 emacs_cmd = [EMACS, '-batch', '-L', '.', '-l', 'ert',
60 '-l', testpath, '-f', 'ert-run-tests-batch-and-exit']
61 print ' '.join(emacs_cmd)
62 r = sp.call(emacs_cmd)
63 if r != 0:
64 print '[E] %s: test failed' % m
65 failed += [m]
66 continue
67 else:
68 print '[W] %s: no test to run' % m
69
70 print '\n[*] %d/%d MODULES OK' % (len(mods)-len(failed), len(mods))
71 for m in failed:
72 print '\tfailed: %s' % m
73
74 def to_lisp_sym(sym):
75 sym = re.sub('[_ ]', '-', sym)
76 return sym
77
78 def to_c_sym(sym):
79 sym = re.sub('[- ]', '_', sym)
80 return sym
81
82 def cmd_init(args):
83 if os.path.exists(args.module):
84 print "%s: file/dir '%s' already exists" % (__file__, args.module)
85 return
86
87 os.mkdir(args.module)
88
89 template_vars = {
90 'module': args.module,
91 'func': args.fun,
92 'c_file': '%s.c' % args.module,
93 'c_func': 'F%s_%s' % (to_c_sym(args.module), to_c_sym(args.fun)),
94 'lisp_func': '%s-%s' % (args.module, to_lisp_sym(args.fun)),
95 }
96
97 for path, t in TEMPLATES.items():
98 if isinstance(path, string.Template):
99 path = path.substitute(template_vars)
100 path = os.path.join(args.module, path)
101 print "writing %s..." % path
102 with open(path, "w+") as f:
103 f.write(t.substitute(template_vars))
104 print "done! you can run %s test %s" % (__file__, args.module)
105
106
107 def main():
108 # path always written relative to this file
109 os.chdir(os.path.dirname(os.path.realpath(__file__)))
110
111 mainp = argparse.ArgumentParser()
112 subp = mainp.add_subparsers()
113
114 testp = subp.add_parser('test', help='run tests')
115 testp.add_argument('-f', '--force', action='store_true',
116 help='force regeneration (make -B)')
117 testp.add_argument('module', nargs='*',
118 help='path to module to test (default all)')
119 testp.set_defaults(func=cmd_test)
120
121 initp = subp.add_parser('init', help='create a test module from a template')
122 initp.add_argument('module', help='name of the new module')
123 initp.add_argument('-f', '--fun', default='fun',
124 help='override name of the default function')
125 initp.set_defaults(func=cmd_init)
126
127 args = mainp.parse_args()
128 args.func(args)
129
130
131 # double the $ to escape python template syntax
132 TEMPLATES = {
133 'Makefile': string.Template('''
134 ROOT = ../..
135
136 CC = gcc
137 LD = gcc
138 CFLAGS = -ggdb3 -Wall
139 LDFLAGS =
140
141 all: ${module}.so ${module}.doc
142
143 %.so: %.o
144 $$(LD) -shared $$(LDFLAGS) -o $$@ $$<
145
146 %.o: %.c
147 $$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
148
149 '''),
150
151 string.Template('${c_file}'): string.Template('''
152 #include <emacs-module.h>
153
154 int plugin_is_GPL_compatible;
155
156 static emacs_value
157 ${c_func} (emacs_env *env, int nargs, emacs_value args[], void *data)
158 {
159 return env->intern (env, "t");
160 }
161
162 /* Bind NAME to FUN. */
163 static void
164 bind_function (emacs_env *env, const char *name, emacs_value Sfun)
165 {
166 emacs_value Qfset = env->intern (env, "fset");
167 emacs_value Qsym = env->intern (env, name);
168 emacs_value args[] = { Qsym, Sfun };
169
170 env->funcall (env, Qfset, 2, args);
171 }
172
173 /* Provide FEATURE to Emacs. */
174 static void
175 provide (emacs_env *env, const char *feature)
176 {
177 emacs_value Qfeat = env->intern (env, feature);
178 emacs_value Qprovide = env->intern (env, "provide");
179 emacs_value args[] = { Qfeat };
180
181 env->funcall (env, Qprovide, 1, args);
182 }
183
184 int
185 emacs_module_init (struct emacs_runtime *ert)
186 {
187 emacs_env *env = ert->get_environment (ert);
188 bind_function (env, "${lisp_func}",
189 env->make_function (env, 1, 1, ${c_func}, "doc", NULL));
190 provide (env, "${module}");
191 return 0;
192 }
193 '''),
194 'test.el': string.Template('''
195 (require 'ert)
196 (require 'module-test-common)
197
198 ;; #$$ works when loading, buffer-file-name when evaluating from emacs
199 (module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
200
201 (ert-deftest ${lisp_func}-test ()
202 (should (eq (${lisp_func} 42) t)))
203 ''')
204 }
205
206 if __name__ == '__main__':
207 main()