src/hg/makeDb/trackDbPatch/trackDbPatch.c 1.5

1.5 2010/05/11 01:43:30 kent
Refactoring to split the trackDb.tableName field into separate track and table fields. Similarly track.mapName field goes to the same track and table fields.
Index: src/hg/makeDb/trackDbPatch/trackDbPatch.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/hg/makeDb/trackDbPatch/trackDbPatch.c,v
retrieving revision 1.4
retrieving revision 1.5
diff -b -B -U 4 -r1.4 -r1.5
--- src/hg/makeDb/trackDbPatch/trackDbPatch.c	4 Jan 2010 19:12:37 -0000	1.4
+++ src/hg/makeDb/trackDbPatch/trackDbPatch.c	11 May 2010 01:43:30 -0000	1.5
@@ -12,8 +12,9 @@
 char *clPatchDir = NULL;
 char *clKey = "track";
 boolean clFirstFile = FALSE;
 boolean clMultiFile = FALSE;
+boolean clDelete = FALSE;
 
 void usage()
 /* Explain usage and exit. */
 {
@@ -27,8 +28,9 @@
   "   -test=patchDir - rather than doing patches in place, write patched output to this dir\n"
   "   -key=tagName - use tagName as key.  Default '%s'\n"
   "   -multiFile - allow multiple files in filePos tag\n"
   "   -firstFile - when a patch can go to multiple files apply it to first rather than last file\n"
+  "   -delete - delete tracks in patches.ra, which should contain only track and filePos tags\n"
   , clKey
   );
 }
 
@@ -36,8 +38,9 @@
    {"test", OPTION_STRING},
    {"key", OPTION_STRING},
    {"multiFile", OPTION_BOOLEAN},
    {"firstFile", OPTION_BOOLEAN},
+   {"delete", OPTION_BOOLEAN},
    {NULL, 0},
 };
 
 
@@ -115,9 +118,9 @@
     }
 return list;
 }
 
-struct raPatch *raPatchReadOne(struct lineFile *lf)
+struct raPatch *raPatchReadOne(struct lineFile *lf, boolean deleteFieldsOnly)
 /* Read next patch from lineFile. */
 {
 struct slPair *tagList = raNextRecordAsSlPairList(lf);
 if (tagList == NULL)
@@ -157,8 +160,11 @@
 	freez(&tag);
 	}
     else
         {
+	if (deleteFieldsOnly)
+	    errAbort("Tag %s not allowed with -delete mode in stanza ending line %d of %s.",
+	    	tag->name, lf->lineIx, lf->fileName);
 	slAddHead(&newTagList, tag);
 	}
     }
 slReverse(&newTagList);
@@ -247,9 +253,10 @@
 slReverse(&lineList);
 return lineList;
 }
 
-static void applyPatches(char *inName, struct slRef *patchRefList, char *keyField, char *outName)
+static void applyPatches(char *inName, struct slRef *patchRefList, char *keyField, char *outName,
+	boolean doDelete)
 /* Apply patches in list. */
 {
 int keyFieldLen = strlen(keyField);
 
@@ -296,8 +303,12 @@
     /* If have patch apply it, otherwise just copy. */
     if (patch)
         {
 	++glPatchRecordCount;
+	if (doDelete)
+	    verbose(2, "Deleting %s in %s\n", patch->track, inName);
+	else
+	    {
 	verbose(3, "Got patch %s with %d tags starting %s %s\n", patch->track, slCount(patch->tagList), patch->tagList->name, (char *)patch->tagList->val);
 	int indent = 0;
 	struct hash *appliedHash = hashNew(0);
 	for (line = stanza; line != NULL; line = line->next)
@@ -341,12 +352,15 @@
 		}
 	    }
 	hashFree(&appliedHash);
 	}
+	}
     else
         {
 	for (line = stanza; line != NULL; line = line->next)
 	    {
+	    if (startsWithWord("track", skipLeadingSpaces(line->name)))
+		verbose(3, "copying %s unchanged\n", line->name); 
 	    fprintf(f, "%s\n", line->name);
 	    }
 	}
 
@@ -362,10 +376,12 @@
 {
 /* Read in patch file. */
 struct lineFile *lf = lineFileOpen(patchesFile, TRUE);
 struct raPatch *patch, *patchList = NULL;
-while ((patch = raPatchReadOne(lf)) != NULL)
+while ((patch = raPatchReadOne(lf, clDelete)) != NULL)
+    {
     slAddHead(&patchList, patch);
+    }
 slReverse(&patchList);
 
 /* Group it by file to patch */
 struct fileToPatch *file, *fileList = groupPatchesByFiles(patchList, clFirstFile);
@@ -395,9 +411,9 @@
 
 
     /* Do patch reading original source and creating temp file. */
     makeDirForFile(backupPath);
-    applyPatches(file->fileName, file->patchList, clKey, tempPath);
+    applyPatches(file->fileName, file->patchList, clKey, tempPath, clDelete);
 
     /* If testing, move temp to patch */
     if (clPatchDir)
 	{
@@ -427,7 +443,8 @@
 clKey = optionVal("key", clKey);
 clPatchDir = optionVal("test", clPatchDir);
 clFirstFile = optionExists("firstFile");
 clMultiFile = optionExists("multiFile");
+clDelete = optionExists("delete");
 trackDbPatch(argv[1], argv[2]);
 return 0;
 }