get_attached_files.c
author ali@yendor.vm.bytemark.co.uk
Thu Nov 16 08:30:26 2006 +0000 (2006-11-16)
changeset 0 ae7b3fa753dc
permissions -rw-r--r--
First cut. Distintly raw around the edges:
* Assumes it will be running in /home/ali/wk/slashem/web.scripts
* Assumes cache directory will be in topdir
* No build system (simple compiling and linking against libxml2)
* No configure system (eg., tagsoup)
* Output XML untested
* Doesn't set bugzilla maintainer or exporter
* Handling of artifact priorities and resolution is suspect
     1 /*
     2  * This program parses the XML data provided by sourceforge to extract the
     3  * list of "attached files" (which are not themselves included in the XML
     4  * stream and therefore need to be seperately downloaded for archiving).
     5  */
     6 
     7 #include <stdlib.h>
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <libxml/xmlmemory.h>
    11 #include <libxml/parser.h>
    12 
    13 struct artifact_type {
    14     char *name;
    15     int id;
    16 } artifact_types[] = {
    17     "Bugs",		109746,
    18     "Support Requests",	209746,
    19     "Patches",		309746,
    20     "Feature Requests",	359746,
    21 };
    22 
    23 #ifndef NO_ELEMS
    24 #define NO_ELEMS(array)	(sizeof(array)/sizeof(*(array)))
    25 #endif
    26 
    27 void parse_history(xmlDocPtr doc,const xmlChar *artifact_id,
    28   const xmlChar *artifact_type,xmlNodePtr node)
    29 {
    30     xmlNodePtr field,cur;
    31     xmlAttrPtr attr;
    32     xmlChar *name=NULL;
    33     xmlChar *field_name=NULL;
    34     xmlChar *text;
    35     int file_id,i;
    36     for(field=node->xmlChildrenNode;field;field=field->next)
    37 	if (!xmlStrcmp(field->name,(const xmlChar *)"field"))
    38 	{
    39 	    for(attr=field->properties;attr;attr=attr->next)
    40 		if (!xmlStrcmp(attr->name,(const xmlChar *)"name"))
    41 		{
    42 		    name=xmlNodeListGetString(doc,attr->children,1);
    43 		    if (!xmlStrcmp(name,(const xmlChar *)"field_name"))
    44 			field_name=xmlNodeListGetString(doc,field->children,1);
    45 		    if (!xmlStrcmp(field_name,(const xmlChar *)"File Added") &&
    46 		      !xmlStrcmp(name,(const xmlChar *)"old_value"))
    47 		    {
    48 			text=xmlNodeListGetString(doc,field->children,1);
    49 			if (sscanf(text,"%d",&file_id)==1)
    50 			{
    51 			    for(i=NO_ELEMS(artifact_types)-1;i>=0;i--)
    52 				if (!strcmp(artifact_types[i].name,
    53 				  (const char *)artifact_type))
    54 				    break;
    55 			    if (i>=0)
    56 				printf("%d %s %d\n",
    57 				  artifact_types[i].id,artifact_id,file_id);
    58 			    else
    59 				fprintf(stderr,
    60 				  "Warning: Unknown artifact type \"%s\" - "
    61 				  "attached file %s ignored\n",
    62 				  artifact_type,text);
    63 			}
    64 			xmlFree(text);
    65 		    }
    66 		    xmlFree(name);
    67 		}
    68 	}
    69     if (field_name)
    70 	xmlFree(field_name);
    71 }
    72 
    73 void parse_artifact(xmlDocPtr doc,xmlNodePtr node)
    74 {
    75     xmlNodePtr field,cur;
    76     xmlAttrPtr attr;
    77     xmlChar *name=NULL;
    78     xmlChar *id=NULL;
    79     xmlChar *type=NULL;
    80     xmlChar *text;
    81     for(field=node->xmlChildrenNode;field;field=field->next)
    82 	if (!xmlStrcmp(field->name,(const xmlChar *)"field"))
    83 	{
    84 	    for(attr=field->properties;attr;attr=attr->next)
    85 		if (!xmlStrcmp(attr->name,(const xmlChar *)"name"))
    86 		{
    87 		    name=xmlNodeListGetString(doc,attr->children,1);
    88 		    if (!xmlStrcmp(name,(const xmlChar *)"artifact_id"))
    89 			id=xmlNodeListGetString(doc,field->children,1);
    90 		    if (!xmlStrcmp(name,(const xmlChar *)"artifact_type"))
    91 			type=xmlNodeListGetString(doc,field->children,1);
    92 		}
    93 	    if (!xmlStrcmp(name,(const xmlChar *)"artifact_history"))
    94 		for(cur=field->xmlChildrenNode;cur;cur=cur->next)
    95 		    if (!xmlStrcmp(cur->name,"history"))
    96 			parse_history(doc,id,type,cur);
    97 	    if (name)
    98 	    {
    99 		xmlFree(name);
   100 		name=NULL;
   101 	    }
   102 	}
   103     if (id)
   104 	xmlFree(id);
   105     if (type)
   106 	xmlFree(type);
   107 }
   108 
   109 int main(int argc,char **argv)
   110 {
   111     xmlDocPtr doc;
   112     xmlNodePtr cur;
   113     doc=xmlParseFile(argv[1]);
   114     if (!doc)
   115     {
   116 	fprintf(stderr,"Document not parsed successfully.\n");
   117 	exit(1);
   118     }
   119     cur=xmlDocGetRootElement(doc);
   120     if (!cur)
   121     {
   122 	fprintf(stderr,"Empty document.\n");
   123 	xmlFreeDoc(doc);
   124 	exit(1);
   125     }
   126     if (xmlStrcmp(cur->name,(const xmlChar *)"project_export"))
   127     {
   128 	fprintf(stderr,"%s does not appear to be a project export document.\n",
   129 	  argv[1]);
   130 	xmlFreeDoc(doc);
   131 	exit(1);
   132     }
   133     for(cur=cur->xmlChildrenNode;cur;cur=cur->next)
   134 	if (!xmlStrcmp(cur->name,(const xmlChar *)"artifacts"))
   135 	    break;
   136     if (!cur)
   137     {
   138 	fprintf(stderr,"%s does not appear to contain an artifacts node.\n",
   139 	  argv[1]);
   140 	xmlFreeDoc(doc);
   141 	exit(1);
   142     }
   143     for(cur=cur->xmlChildrenNode;cur;cur=cur->next)
   144 	if (!xmlStrcmp(cur->name,(const xmlChar *)"artifact"))
   145 	    parse_artifact(doc,cur);
   146     xmlFreeDoc(doc);
   147     exit(0);
   148 }