]> code.delx.au - gnu-emacs/blob - modules/modhelp.py
Migrate modules/.gitignore into .gitignore
[gnu-emacs] / modules / modhelp.py
1 #!/usr/bin/env python
2
3 # Module helper script.
4
5 # Copyright 2015 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', '-l', testpath, '-f', 'ert-run-tests-batch-and-exit']
60 print ' '.join(emacs_cmd)
61 r = sp.call(emacs_cmd)
62 if r != 0:
63 print '[E] %s: test failed' % m
64 failed += [m]
65 continue
66 else:
67 print '[W] %s: no test to run' % m
68
69 print '\n[*] %d/%d MODULES OK' % (len(mods)-len(failed), len(mods))
70 for m in failed:
71 print '\tfailed: %s' % m
72
73 def to_lisp_sym(sym):
74 sym = re.sub('[_ ]', '-', sym)
75 return sym
76
77 def to_c_sym(sym):
78 sym = re.sub('[- ]', '_', sym)
79 return sym
80
81 def cmd_init(args):
82 if os.path.exists(args.module):
83 print "%s: file/dir '%s' already exists" % (__file__, args.module)
84 return
85
86 os.mkdir(args.module)
87
88 template_vars = {
89 'module': args.module,
90 'func': args.fun,
91 'c_file': '%s.c' % args.module,
92 'c_func': 'F%s_%s' % (to_c_sym(args.module), to_c_sym(args.fun)),
93 'lisp_func': '%s-%s' % (args.module, to_lisp_sym(args.fun)),
94 }
95
96 for path, t in TEMPLATES.items():
97 if isinstance(path, string.Template):
98 path = path.substitute(template_vars)
99 path = os.path.join(args.module, path)
100 print "writing %s..." % path
101 with open(path, "w+") as f:
102 f.write(t.substitute(template_vars))
103 print "done! you can run %s test %s" % (__file__, args.module)
104
105
106 def main():
107 # path always written relative to this file
108 os.chdir(os.path.dirname(os.path.realpath(__file__)))
109
110 mainp = argparse.ArgumentParser()
111 subp = mainp.add_subparsers()
112
113 testp = subp.add_parser('test', help='run tests')
114 testp.add_argument('-f', '--force', action='store_true', help='force regeneration (make -B)')
115 testp.add_argument('module', nargs='*', help='path to module to test (default all)')
116 testp.set_defaults(func=cmd_test)
117
118 initp = subp.add_parser('init', help='create a test module from a template')
119 initp.add_argument('module', help='name of the new module')
120 initp.add_argument('-f', '--fun', default='fun', help='overide name of the default function')
121 initp.set_defaults(func=cmd_init)
122
123 args = mainp.parse_args()
124 args.func(args)
125
126
127 # double the $ to escape python template syntax
128 TEMPLATES = {
129 'Makefile': string.Template('''
130 ROOT = ../..
131
132 CC = gcc
133 LD = gcc
134 CFLAGS = -ggdb3 -Wall
135 LDFLAGS =
136
137 all: ${module}.so ${module}.doc
138
139 %.so: %.o
140 $$(LD) -shared $$(LDFLAGS) -o $$@ $$<
141
142 %.o: %.c
143 $$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
144
145 '''),
146
147 string.Template('${c_file}'): string.Template('''
148 #include <module.h>
149
150 int plugin_is_GPL_compatible;
151
152 static emacs_value ${c_func} (emacs_env *env, int nargs, emacs_value args[], void *data)
153 {
154 return env->intern (env, "t");
155 }
156
157 /* Binds NAME to FUN */
158 static void bind_function (emacs_env *env, const char *name, emacs_value Sfun)
159 {
160 emacs_value Qfset = env->intern (env, "fset");
161 emacs_value Qsym = env->intern (env, name);
162 emacs_value args[] = { Qsym, Sfun };
163
164 env->funcall (env, Qfset, 2, args);
165 }
166
167 /* Provide FEATURE to Emacs */
168 static void provide (emacs_env *env, const char *feature)
169 {
170 emacs_value Qfeat = env->intern (env, feature);
171 emacs_value Qprovide = env->intern (env, "provide");
172 emacs_value args[] = { Qfeat };
173
174 env->funcall (env, Qprovide, 1, args);
175 }
176
177 int emacs_module_init (struct emacs_runtime *ert)
178 {
179 emacs_env *env = ert->get_environment (ert);
180 bind_function (env, "${lisp_func}", env->make_function (env, 1, 1, ${c_func}, "doc", NULL));
181 provide (env, "${module}");
182 return 0;
183 }
184 '''),
185 'test.el': string.Template('''
186 (require 'ert)
187 (require 'module-test-common)
188
189 ;; #$$ works when loading, buffer-file-name when evaluating from emacs
190 (module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
191
192 (ert-deftest ${lisp_func}-test ()
193 (should (eq (${lisp_func} 42) t)))
194 ''')
195 }
196
197 if __name__ == '__main__':
198 main()