]>
code.delx.au - gnu-emacs/blob - modules/modhelp.py
4 import subprocess
as sp
8 EMACS
= os
.path
.join('..', 'src', 'emacs')
12 for (dirname
, dirs
, files
) in os
.walk('.'):
13 if 'Makefile' in files
:
14 modpaths
.append(dirname
)
28 print '[*] %s: ------- start -------' % m
29 print '[*] %s: running make' % m
30 r
= sp
.call(make_cmd
, cwd
=m
)
32 print '[E] %s: make failed' % m
36 print '[*] %s: running test' % m
37 testpath
= os
.path
.join(m
, 'test.el')
38 if os
.path
.isfile(testpath
):
39 emacs_cmd
= [EMACS
, '-batch', '-L', '.', '-l', 'ert', '-l', testpath
, '-f', 'ert-run-tests-batch-and-exit']
40 print ' '.join(emacs_cmd
)
41 r
= sp
.call(emacs_cmd
)
43 print '[E] %s: test failed' % m
47 print '[W] %s: no test to run' % m
49 print '\n[*] %d/%d MODULES OK' % (len(mods
)-len(failed
), len(mods
))
51 print '\tfailed: %s' % m
54 sym
= re
.sub('[_ ]', '-', sym
)
58 sym
= re
.sub('[- ]', '_', sym
)
62 if os
.path
.exists(args
.module
):
63 print "%s: file/dir '%s' already exists" % (__file__
, args
.module
)
69 'module': args
.module
,
71 'c_file': '%s.c' % args
.module
,
72 'c_func': 'F%s_%s' % (to_c_sym(args
.module
), to_c_sym(args
.fun
)),
73 'lisp_func': '%s-%s' % (args
.module
, to_lisp_sym(args
.fun
)),
76 for path
, t
in TEMPLATES
.items():
77 if isinstance(path
, string
.Template
):
78 path
= path
.substitute(template_vars
)
79 path
= os
.path
.join(args
.module
, path
)
80 print "writing %s..." % path
81 with
open(path
, "w+") as f
:
82 f
.write(t
.substitute(template_vars
))
83 print "done! you can run %s test %s" % (__file__
, args
.module
)
87 # path always written relative to this file
88 os
.chdir(os
.path
.dirname(os
.path
.realpath(__file__
)))
90 mainp
= argparse
.ArgumentParser()
91 subp
= mainp
.add_subparsers()
93 testp
= subp
.add_parser('test', help='run tests')
94 testp
.add_argument('-f', '--force', action
='store_true', help='force regeneration (make -B)')
95 testp
.add_argument('module', nargs
='*', help='path to module to test (default all)')
96 testp
.set_defaults(func
=cmd_test
)
98 initp
= subp
.add_parser('init', help='create a test module from a template')
99 initp
.add_argument('module', help='name of the new module')
100 initp
.add_argument('-f', '--fun', default
='fun', help='overide name of the default function')
101 initp
.set_defaults(func
=cmd_init
)
103 args
= mainp
.parse_args()
107 # double the $ to escape python template syntax
109 'Makefile': string
.Template('''
114 CFLAGS = -ggdb3 -Wall
117 all: ${module}.so ${module}.doc
120 $$(LD) -shared $$(LDFLAGS) -o $$@ $$<
123 $$(CC) $$(CFLAGS) -I$$(ROOT)/src -fPIC -c $$<
127 string
.Template('${c_file}'): string
.Template('''
128 #include <emacs_module.h>
130 int plugin_is_GPL_compatible;
132 static emacs_value ${c_func} (emacs_env *env, int nargs, emacs_value args[], void *data)
134 return env->intern (env, "t");
137 /* Binds NAME to FUN */
138 static void bind_function (emacs_env *env, const char *name, emacs_value Sfun)
140 emacs_value Qfset = env->intern (env, "fset");
141 emacs_value Qsym = env->intern (env, name);
142 emacs_value args[] = { Qsym, Sfun };
144 env->funcall (env, Qfset, 2, args);
147 /* Provide FEATURE to Emacs */
148 static void provide (emacs_env *env, const char *feature)
150 emacs_value Qfeat = env->intern (env, feature);
151 emacs_value Qprovide = env->intern (env, "provide");
152 emacs_value args[] = { Qfeat };
154 env->funcall (env, Qprovide, 1, args);
157 int emacs_module_init (struct emacs_runtime *ert)
159 emacs_env *env = ert->get_environment (ert);
160 bind_function (env, "${lisp_func}", env->make_function (env, 1, 1, ${c_func}, "doc", NULL));
161 provide (env, "${module}");
165 'test.el': string
.Template('''
167 (require 'module-test-common)
169 ;; #$$ works when loading, buffer-file-name when evaluating from emacs
170 (module-load (module-path (or #$$ (expand-file-name (buffer-file-name)))))
172 (ert-deftest ${lisp_func}-test ()
173 (should (eq (${lisp_func} 42) t)))
177 if __name__
== '__main__':