support -X option in rpmmd2solv, make add_autopattern available in bindings
[platform/upstream/libsolv.git] / tools / rpmmd2solv.c
index 88cac02..d4fe2ff 100644 (file)
 #include "pool.h"
 #include "repo.h"
 #include "repo_rpmmd.h"
+#ifdef SUSE
+#include "repo_autopattern.h"
+#endif
 #include "common_write.h"
+#include "solv_xfopen.h"
 
 
-static ssize_t
-cookie_gzread(void *cookie, char *buf, size_t nbytes)
-{
-  return gzread((gzFile *)cookie, buf, nbytes);
-}
-
-static int
-cookie_gzclose(void *cookie)
-{
-  return gzclose((gzFile *)cookie);
-}
-
-FILE *
-myfopen(const char *fn)
-{
-  cookie_io_functions_t cio;
-  char *suf;
-  gzFile *gzf;
-
-  if (!fn)
-    return 0;
-  suf = strrchr(fn, '.');
-  if (!suf || strcmp(suf, ".gz") != 0)
-    return fopen(fn, "r");
-  gzf = gzopen(fn, "r");
-  if (!gzf)
-    return 0;
-  memset(&cio, 0, sizeof(cio));
-  cio.read = cookie_gzread;
-  cio.close = cookie_gzclose;
-  return  fopencookie(gzf, "r", cio);
-}
-
 static void
 usage(int status)
 {
   fprintf(stderr, "\nUsage:\n"
-          "rpmmd2solv [-a][-h][-k][-n <attrname>]\n"
-         "  reads a 'rpmmd' repository from <stdin> and writes a .solv file to <stdout>\n"
+          "rpmmd2solv [-a][-h][-n <attrname>][-l <locale>]\n"
+         "  reads 'primary' from a 'rpmmd' repository from <stdin> and writes a .solv file to <stdout>\n"
          "  -h : print help & exit\n"
-         "  -k : don't mix kinds (experimental!)\n"
          "  -n <name>: save attributes as <name>.attr\n"
+         "  -l <locale>: parse localization data for <locale>\n"
         );
    exit(status);
 }
@@ -75,21 +46,21 @@ main(int argc, char **argv)
   const char *attrname = 0;
   const char *basefile = 0;
   const char *dir = 0;
-
+  const char *locale = 0;
+#ifdef SUSE
+  int add_auto = 0;
+#endif
   
   Pool *pool = pool_create();
   Repo *repo = repo_create(pool, "<stdin>");
 
-  while ((c = getopt (argc, argv, "hkn:b:d:")) >= 0)
+  while ((c = getopt (argc, argv, "hn:b:d:l:X")) >= 0)
     {
       switch(c)
        {
         case 'h':
           usage(0);
           break;
-        case 'k':
-          flags |= RPMMD_KINDS_SEPARATELY;   /* do not use! */
-          break;
         case 'n':
           attrname = optarg;
           break;
@@ -99,6 +70,14 @@ main(int argc, char **argv)
         case 'd':
           dir = optarg;
           break;
+       case 'l':
+         locale = optarg;
+         break;
+       case 'X':
+#ifdef SUSE
+         add_auto = 1;
+#endif
+         break;
         default:
           usage(1);
           break;
@@ -109,20 +88,75 @@ main(int argc, char **argv)
       FILE *fp;
       int l;
       char *fnp;
-      l = strlen(dir);
-      fnp = sat_malloc(l + 20);
-      sprintf(fnp, "%s/primary.xml.gz", dir);
-      if (!(fp = myfopen(fnp)))
+      l = strlen(dir) + 128;
+      fnp = solv_malloc(l+1);
+      snprintf(fnp, l, "%s/primary.xml.gz", dir);
+      if (!(fp = solv_xfopen(fnp, 0)))
        {
          perror(fnp);
          exit(1);
        }
-      repo_add_rpmmd(repo, fp, flags);
+      if (repo_add_rpmmd(repo, fp, 0, flags))
+       {
+         fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
+         exit(1);
+       }
       fclose(fp);
-      sat_free(fnp);
+      snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
+      if ((fp = solv_xfopen(fnp, 0)))
+       {
+         if (repo_add_rpmmd(repo, fp, 0, flags))
+           {
+             fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
+             exit(1);
+           }
+         fclose(fp);
+       }
+      if (locale)
+       {
+         if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
+           {
+             fprintf(stderr, "-l parameter too long\n");
+             exit(1);
+           }
+         while (!(fp = solv_xfopen(fnp, 0)))
+           {
+             fprintf(stderr, "not opened %s\n", fnp);
+             if (strlen(locale) > 2)
+               {
+                 if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
+                   {
+                     fprintf(stderr, "-l parameter too long\n");
+                     exit(1);
+                   }
+                 if ((fp = solv_xfopen(fnp, 0)))
+                   break;
+               }
+             perror(fnp);
+             exit(1);
+           }
+         fprintf(stderr, "opened %s\n", fnp);
+         if (repo_add_rpmmd(repo, fp, 0, flags))
+           {
+             fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
+             exit(1);
+           }
+         fclose(fp);
+       }
+      solv_free(fnp);
     }
   else
-    repo_add_rpmmd(repo, stdin, flags);
+    {
+      if (repo_add_rpmmd(repo, stdin, 0, flags))
+       {
+         fprintf(stderr, "rpmmd2solv: %s\n", pool_errstr(pool));
+         exit(1);
+       }
+    }
+#ifdef SUSE
+  if (add_auto)
+    repo_add_autopattern(repo, 0);
+#endif
   tool_write(repo, basefile, attrname);
   pool_free(pool);
   exit(0);