tools/modfiles: list files in a module.
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 9 Jul 2015 05:04:56 +0000 (14:34 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 9 Jul 2015 05:25:02 +0000 (14:55 +0930)
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
.gitignore
tools/Makefile
tools/modfiles.c [new file with mode: 0644]

index 60886efd9b3c33e7068b82844cb764d25e81d001..680d1296eab359e0fc4ee43a3ef497244dd33894 100644 (file)
@@ -12,6 +12,7 @@ tools/namespacize
 tools/run_tests
 tools/ccanlint/ccanlint
 tools/ccanlint/generated-testlist
+tools/modfiles
 inter-depends
 test-depends
 lib-depends
index ddd6545fc694b9d91e9d5a3ec9014aa1c49acb8d..338befc46de51326b8fe0c707165dce1caa67d9b 100644 (file)
@@ -1,9 +1,15 @@
-ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract tools/namespacize tools/ccanlint/ccanlint
+ALL_TOOLS = tools/configurator/configurator tools/ccan_depends tools/doc_extract tools/namespacize tools/ccanlint/ccanlint tools/modfiles
 LDLIBS = -lrt
 DEP_OBJS = ccan/err/err.o \
        ccan/foreach/foreach.o \
+       ccan/hash/hash.o \
+       ccan/htable/htable.o \
        ccan/list/list.o \
        ccan/noerr/noerr.o \
+       ccan/opt/opt.o \
+       ccan/opt/helpers.o \
+       ccan/opt/parse.o \
+       ccan/opt/usage.o \
        ccan/rbuf/rbuf.o \
        ccan/read_write_all/read_write_all.o \
        ccan/str/debug.o \
@@ -36,6 +42,8 @@ tools/namespacize.o tools/depends.o: tools/tools.h
 
 tools/configurator/configurator: tools/configurator/configurator.c
 
+tools/modfiles: tools/modfiles.o tools/manifest.o $(DEP_OBJS)
+
 tools-clean: ccanlint-clean
        $(RM) $(ALL_TOOLS)
 
diff --git a/tools/modfiles.c b/tools/modfiles.c
new file mode 100644 (file)
index 0000000..b7092cd
--- /dev/null
@@ -0,0 +1,87 @@
+/* List files in a module, or modules. */
+#include <ccan/opt/opt.h>
+#include <ccan/foreach/foreach.h>
+#include <ccan/tal/str/str.h>
+#include "manifest.h"
+#include <stdio.h>
+
+static void add_file(const struct ccan_file *f,
+                    bool fullpath, bool nul_term, bool gitonly)
+{
+       /* Hacky way of seeing if git knows about this. */
+       if (gitonly) {
+               char *cmd = tal_fmt(f, "git status --porcelain --ignored %s | grep -q '^ *[!?]'", f->fullname);
+               if (system(cmd) == 0)
+                       return;
+       }
+       printf("%s%c", fullpath ? f->fullname : f->name,
+              nul_term ? '\0' : '\n');
+}
+
+int main(int argc, char *argv[])
+{
+       bool code = true;
+       bool license = true;
+       bool tests = true;
+       bool other = true;
+       bool gitonly = false;
+       bool nul_term = false;
+       bool fullpath = false;
+       int i;
+
+       opt_register_noarg("--no-code", opt_set_invbool, &code,
+                          "Don't list .c and .h files");
+       opt_register_noarg("--no-license", opt_set_invbool, &license,
+                          "Don't list license file");
+       opt_register_noarg("--no-tests", opt_set_invbool, &tests,
+                          "Don't list test files");
+       opt_register_noarg("--no-other", opt_set_invbool, &other,
+                          "Don't list other files");
+       opt_register_noarg("--git-only", opt_set_bool, &gitonly,
+                          "Only include files in git");
+       opt_register_noarg("--fullpath", opt_set_bool, &fullpath,
+                          "Print full path names instead of module-relative ones");
+       opt_register_noarg("--null|-0", opt_set_bool, &nul_term,
+                          "Separate files with nul character instead of \\n");
+       opt_register_noarg("-h|--help", opt_usage_and_exit,
+                          "<moduledir>...",
+                          "Show usage");
+
+       opt_parse(&argc, argv, opt_log_stderr_exit);
+       if (argc < 2)
+               opt_usage_exit_fail("Expected one or more module directories");
+
+       for (i = 1; i < argc; i++) {
+               struct manifest *m = get_manifest(NULL, argv[i]);
+               struct list_head *list;
+               struct ccan_file *f;
+               if (code) {
+                       foreach_ptr(list, &m->c_files, &m->h_files) {
+                               list_for_each(list, f, list)
+                                       add_file(f, fullpath, nul_term, gitonly);
+                       }
+               }
+               if (license) {
+                       list_for_each(&m->other_files, f, list) {
+                               if (streq(f->name, "LICENSE"))
+                                       add_file(f, fullpath, nul_term, gitonly);
+                       }
+               }
+               if (tests) {
+                       foreach_ptr(list, &m->run_tests, &m->api_tests,
+                                   &m->compile_ok_tests, &m->compile_fail_tests,
+                                   &m->other_test_c_files,
+                                   &m->other_test_files) {
+                               list_for_each(list, f, list)
+                                       add_file(f, fullpath, nul_term, gitonly);
+                       }
+               }
+               if (other) {
+                       list_for_each(&m->other_files, f, list) {
+                               if (!streq(f->name, "LICENSE"))
+                                       add_file(f, fullpath, nul_term, gitonly);
+                       }
+               }
+       }
+       return 0;
+}