Fix input method "probhat" for Bengali
[emacs.git] / modules / modhelp.py
blob5d8f89b31bc1f0f4bd40137eca817b656d9093e2
1 #!/usr/bin/env python
3 # Module helper script.
5 # Copyright 2015-2016 Free Software Foundation, Inc.
7 # This file is part of GNU Emacs.
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.
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.
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/>.
22 import os
23 import string
24 import subprocess as sp
25 import argparse
26 import re
28 EMACS = os.path.join('..', 'src', 'emacs')
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
37 def cmd_test(args):
38 mods = args.module
39 if not mods:
40 mods = find_modules()
42 make_cmd = ['make']
43 if args.force:
44 make_cmd.append('-B')
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
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
70 print '\n[*] %d/%d MODULES OK' % (len(mods)-len(failed), len(mods))
71 for m in failed:
72 print '\tfailed: %s' % m
74 def to_lisp_sym(sym):
75 sym = re.sub('[_ ]', '-', sym)
76 return sym
78 def to_c_sym(sym):
79 sym = re.sub('[- ]', '_', sym)
80 return sym
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
87 os.mkdir(args.module)
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)),
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)
107 def main():
108 # path always written relative to this file
109 os.chdir(os.path.dirname(os.path.realpath(__file__)))
111 mainp = argparse.ArgumentParser()
112 subp = mainp.add_subparsers()
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)
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)
127 args = mainp.parse_args()
128 args.func(args)
131 # double the $ to escape python template syntax
132 TEMPLATES = {
133 'Makefile': string.Template('''
134 ROOT = ../..
136 CC = gcc
137 LD = gcc
138 CFLAGS = -ggdb3 -Wall
139 LDFLAGS =
141 all: ${module}.so ${module}.doc
143 %.so: %.o
144 $$(LD) -shared $$(LDFLAGS) -o $$@ $$<
146 %.o: %.c
147 $$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
149 '''),
151 string.Template('${c_file}'): string.Template('''
152 #include <emacs-module.h>
154 int plugin_is_GPL_compatible;
156 static emacs_value
157 ${c_func} (emacs_env *env, int nargs, emacs_value args[], void *data)
159 return env->intern (env, "t");
162 /* Bind NAME to FUN. */
163 static void
164 bind_function (emacs_env *env, const char *name, emacs_value Sfun)
166 emacs_value Qfset = env->intern (env, "fset");
167 emacs_value Qsym = env->intern (env, name);
168 emacs_value args[] = { Qsym, Sfun };
170 env->funcall (env, Qfset, 2, args);
173 /* Provide FEATURE to Emacs. */
174 static void
175 provide (emacs_env *env, const char *feature)
177 emacs_value Qfeat = env->intern (env, feature);
178 emacs_value Qprovide = env->intern (env, "provide");
179 emacs_value args[] = { Qfeat };
181 env->funcall (env, Qprovide, 1, args);
185 emacs_module_init (struct emacs_runtime *ert)
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;
193 '''),
194 'test.el': string.Template('''
195 (require 'ert)
196 (require 'module-test-common)
198 ;; #$$ works when loading, buffer-file-name when evaluating from emacs
199 (module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
201 (ert-deftest ${lisp_func}-test ()
202 (should (eq (${lisp_func} 42) t)))
203 ''')
206 if __name__ == '__main__':
207 main()