From eab64e6ecb435face78646153fe86c22895d8f41 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Wed, 7 Sep 2011 20:14:33 +0000 Subject: [PATCH] code refactoring/cleanup --- src/html.c | 127 +++++++++++++++++++++++++++++++++++------------------ src/io.c | 30 +++++++++++++ src/io.h | 2 + www/js/ppastats.js | 22 ++++++---- 4 files changed, 131 insertions(+), 50 deletions(-) diff --git a/src/html.c b/src/html.c index c0dd767..4eddeb3 100644 --- a/src/html.c +++ b/src/html.c @@ -112,38 +112,41 @@ \n\ %s" -#define HTML_INDEX_TEMPLATE \ +#define HTML_HEADER \ "\n\ - \n\ - \n\ + %s\n\ + \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ - \n\ -

N/A

\n\ -
\n\ -
\n\ - Packages:\n\ -
    \n\ -
    \n\ -
    \n\ -
    \n\ -
    \n\ -
    \n\ -%s" + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n\ + \n" + +#define HTML_INDEX_TEMPLATE \ +"

    N/A

    \n\ +
    \n\ +
    \n\ + Packages:\n\ +
      \n\ +
      \n\ +
      \n\ +
      \n\ +
      \n\ +
      \n" static char *path_new(const char *dir, const char *file, const char *suffixe) { @@ -156,7 +159,7 @@ static char *path_new(const char *dir, const char *file, const char *suffixe) strcpy(path, dir); strcat(path, "/"); strcat(path, file); - strcat(path, ".html"); + strcat(path, suffixe); return path; } @@ -332,9 +335,8 @@ static char *version_to_json(struct ppa_stats *ppa, return ret; } -static char *ppa_to_json(struct ppa_stats *ppa) +static json_object *ppa_to_json(struct ppa_stats *ppa) { - char *ret; json_object *json, *json_pkgs, *json_pkg; struct package_stats **pkgs; @@ -365,11 +367,7 @@ static char *ppa_to_json(struct ppa_stats *ppa) pkgs++; } - ret = strdup(json_object_to_json_string(json)); - - json_object_put(json); - - return ret; + return json; } @@ -452,12 +450,13 @@ packages_to_html(struct ppa_stats *ppa, } static void -index_to_html(struct ppa_stats *ppa, const char *dir) +create_html(const char *path, + const char *title, + const char *body_template, + const char *script) { - char *path; FILE *f; - path = path_new(dir, "index", ".html"); f = fopen(path, "w"); if (!f) { @@ -465,11 +464,49 @@ index_to_html(struct ppa_stats *ppa, const char *dir) return ; } - fprintf(f, HTML_INDEX_TEMPLATE, ppa_to_json(ppa), HTML_FOOTER); + fprintf(f, HTML_HEADER, title, script); + fputs(HTML_INDEX_TEMPLATE, f); + fputs(HTML_FOOTER, f); fclose(f); +} + +static char *ppa_display_name(const struct ppa_stats *ppa) +{ + char *ret; + + ret = malloc(4+strlen(ppa->name)+1+strlen(ppa->owner)+1); + + sprintf(ret, "ppa:%s/%s", ppa->owner, ppa->name); + + return ret; +} + +static void +index_to_html(struct ppa_stats *ppa, const char *dir) +{ + char *path, *json_path, *dname; + json_object *json; + + json = ppa_to_json(ppa); + + json_path = path_new(dir, "index", ".json"); + + if (debug) + printf("DEBUG: generating %s\n", json_path); + + json_object_to_file(json_path, json); + + json_object_put(json); + + path = path_new(dir, "index", ".html"); + dname = ppa_display_name(ppa); + + create_html(path, dname, HTML_INDEX_TEMPLATE, "ppastats_ppa();"); free(path); + free(dname); + free(json_path); } void @@ -495,6 +532,8 @@ ppa_to_html(const char *owner, DEFAULT_WWW_DIR"/jquery.jqplot.min.css", "css/jquery.jqplot.min.css" }; + mkdirs(output_dir, 0777); + if (install_static_files) { css_dir = path_append(output_dir, "css"); js_dir = path_append(output_dir, "js"); @@ -504,6 +543,10 @@ ppa_to_html(const char *owner, for (i = 0; i < 6; i++) { f_dst = path_append(output_dir, www_files[2*i+1]); + + if (debug) + printf("DEBUG: copying %s %s\n", + www_files[2*i], f_dst); fcopy(www_files[2*i], f_dst); free(f_dst); diff --git a/src/io.c b/src/io.c index aeeed8c..a7c46af 100644 --- a/src/io.c +++ b/src/io.c @@ -20,9 +20,18 @@ #include #include #include +#include #include "io.h" +/* Directory separator is \ when cross-compiling for MS Windows + systems */ +#if defined(__MINGW32__) +#define DIRSEP '\\' +#else +#define DIRSEP '/' +#endif + #define FCOPY_BUF_SZ 4096 static int file_copy(FILE *src, FILE *dst) @@ -88,3 +97,24 @@ char *path_append(const char *odir, const char *name) return dir; } +void mkdirs(const char *dirs, mode_t mode) +{ + char *c = (char *)dirs; + char *dir = malloc(strlen(dirs) + 1); + + int i = 0; + while (*c) { + if ((*c == DIRSEP || *c == '\0') && c != dirs) { + strncpy(dir, dirs, i); + dir[i] = '\0'; + mkdir(dir, mode); + } + + c++; + i++; + } + + mkdir(dirs, mode); + + free(dir); +} diff --git a/src/io.h b/src/io.h index 60f82b5..259e218 100644 --- a/src/io.h +++ b/src/io.h @@ -43,4 +43,6 @@ int fcopy(const char *src, const char *dst); char *path_append(const char *odir, const char *name); +void mkdirs(const char *dirs, mode_t mode); + #endif diff --git a/www/js/ppastats.js b/www/js/ppastats.js index 9c7e268..5615480 100644 --- a/www/js/ppastats.js +++ b/www/js/ppastats.js @@ -133,16 +133,22 @@ function ppastats_ppa() { $(document).ready(function() { var max_date = null; var min_date = null; - var pkg_url = data["pkg_name"]+".html"; - - $("#ppa_name").html(data["ppa_owner"]+"/"+data["ppa_name"]); + var pkg_url; - $.each(data["packages"], function(i, item) { - var url = item["name"]+".html"; - $("#pkgs").append("
    • "+item["name"]+": "+item["count"]+"
    • "); - }); + $.get("index.json", function(str_json) { + data = JSON.parse(str_json); - ppastats_chart(data["ddts"]); + pkg_url = data["pkg_name"]+".html"; + + $("#ppa_name").html(data["ppa_owner"]+"/"+data["ppa_name"]); + + $.each(data["packages"], function(i, item) { + var url = item["name"]+".html"; + $("#pkgs").append("
    • "+item["name"]+": "+item["count"]+"
    • "); + }); + + ppastats_chart(data["ddts"]); + }); }); } -- 2.7.4