c3514165980326df9a06bf2bb18ab6e76b648204 ceisenhart Thu Oct 15 16:14:53 2015 -0700 Adding in the new python library structure, refs #16156 diff --git src/utils/newPythonProg/newPythonProg.c src/utils/newPythonProg/newPythonProg.c index 18a026f..5d5266e 100644 --- src/utils/newPythonProg/newPythonProg.c +++ src/utils/newPythonProg/newPythonProg.c @@ -1,54 +1,107 @@ /* newPythonProg - Make a skeleton for a new python program. */ #include "common.h" -#include "linefile.h" -#include "hash.h" +#include "portable.h" +#include "dystring.h" #include "options.h" void usage() /* Explain usage and exit. */ { errAbort( "newPythonProg - Make a skeleton for a new python program\n" "usage:\n" " newPythonProg programName \"The usage statement\"\n" "options:\n" " -xxx=XXX\n" ); } + /* Command line validation table. */ static struct optionSpec options[] = { {NULL, 0}, }; -void newPythonProg(char *programName, char *usage) -/* newPythonProg - Make a skeleton for a new python program. */ +void makeMakefile(char *progName, char *makeName) +/* Make makefile. */ +{ +char *upLevel; +//char *L; +//char *myLibs; +FILE *f = mustOpen(makeName, "w"); + +if (fileExists("../inc/common.mk")) + upLevel = cloneString(".."); +else if (fileExists("../../inc/common.mk")) + upLevel = cloneString("../.."); +else if (fileExists("../../../inc/common.mk")) + upLevel = cloneString("../../.."); +else if (fileExists("../../../../inc/common.mk")) + upLevel = cloneString("../../../.."); +else if (fileExists("../../../../../inc/common.mk")) + upLevel = cloneString("../../../../.."); +else { -FILE *programFile = mustOpen(programName, "w"); + warn("WARNING: can not find inc/common.mk 1 to 4 directories up, fix the makefile"); + upLevel = cloneString("../../../../.."); + } +fprintf(f, +"normal::\n\t cp -p %s /cluster/home/${USER}/bin\n\t cp -p %s /cluster/home/${USER}/kent/src/pyLib/scripts\n" +"test::\n\t@if test -d tests -a -s tests/makefile; then (cd tests && ${MAKE} test); \\ " +"\n\telse echo \"# no tests directory (or perhaps no tests/makefile) in $(CURDIR)\"; fi" +, progName, progName); + +fclose(f); +} + +void writeProgram(char *fileName, char *programName, char *usage) +{ +FILE *programFile = mustOpen(fileName, "w"); // Write the python skeleton fprintf(programFile, "#!/usr/bin/env python2.7\n# %s\n\"\"\"%s\"\"\"\n", programName, usage); -fprintf(programFile, "from __future__ import print_function\nimport sys, operator, fileinput, collections, string, os.path" - "\nimport re, argparse, subprocess, math, time, common\n\n"); +fprintf(programFile, + "import os\nimport sys\nimport collections\nimport argparse\n" + "\n" + "# import the UCSC kent python library\n" + "sys.path.append(os.path.join(os.path.dirname(__file__), 'pyLib'))\n" + "import common\n\n"); fprintf(programFile, "def parseArgs(args):\n \"\"\"\n Parse the command line arguments.\n \"\"\"\n parser" "= argparse.ArgumentParser(description = __doc__)\n parser.add_argument (\"inpu" "tFile\",\n help = \" The input file. \",\n type = argparse.FileType(\"r\"))\n "); fprintf(programFile, "parser.add_argument (\"outputFile\",\n help = \" The output file. \",\n type =" "argparse.FileType(\"w\"))\n options = parser.parse_args()\n return options\n\n"); fprintf(programFile, "def main(args):\n \"\"\"\n Initialized options and calls other functions.\n \"\"\"\n " - "options = parseArgs(args)\n ommon.cloneProgToPath(__file__)\n\nif __name__ == \"" + "options = parseArgs(args)\n\nif __name__ == \"" "__main__\" : \n sys.exit(main(sys.argv))"); +} + + + +void newPythonProg(char *programName, char *usage) +/* newPythonProg - Make a skeleton for a new python program. */ +{ +char fileName[512]; +char dirName[512]; +//char fileOnly[128]; +safef(dirName, sizeof(dirName), "%s", programName); +makeDir(dirName); +// Stolen code from newProg +safef(fileName, sizeof(fileName), "%s/%s", programName, programName); +writeProgram(fileName, programName, usage); +setCurrentDir(dirName); +makeMakefile(programName, "makefile"); // Change file permissions char cmd[1024]; safef(cmd, 1024, "chmod 755 %s",programName); mustSystem(cmd); } int main(int argc, char *argv[]) /* Process command line. */ { optionInit(&argc, argv, options); if (argc != 3) usage(); newPythonProg(argv[1], argv[2]); return 0; }