4c8444438e2d355dbc3990de104bff33c4a366d9
[ppastats.git] / src / main.c
1 /*
2  * Copyright (C) 2011-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19
20 #include <libintl.h>
21 #define _(String) gettext(String)
22
23 #include <getopt.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include <cache.h>
29 #include <config.h>
30 #include <fcache.h>
31 #include <html.h>
32 #include <http.h>
33 #include <lp_ws.h>
34 #include <pio.h>
35 #include <plog.h>
36 #include <ppastats.h>
37
38 static const char *program_name;
39
40 static void display_published_binaries(const char *owner,
41                                        const char *ppa,
42                                        const char *package_status,
43                                        int ws_size)
44 {
45         struct ppa_stats *ppastats;
46         struct package_stats **packages;
47         struct version_stats **versions;
48         struct distro_stats **distros;
49         struct arch_stats **archs;
50
51         ppastats = create_ppa_stats(owner, ppa, package_status, ws_size);
52         packages = ppastats->packages;
53         while (packages && *packages) {
54                 struct package_stats *p = *packages;
55
56                 printf("%s (%d)\n", p->name, p->download_count);
57
58                 versions = p->versions;
59
60                 while (*versions) {
61                         printf("\t%s (%d)\n", (*versions)->version,
62                                (*versions)->download_count);
63
64                         distros = (*versions)->distros;
65
66                         while (*distros) {
67                                 printf("\t\t%s (%d)\n",
68                                        (*distros)->name,
69                                        (*distros)->download_count);
70
71                                 archs = (*distros)->archs;
72
73                                 while (*archs) {
74                                         printf("\t\t\t%s (%d)\n",
75                                                (*archs)->name,
76                                                (*archs)->download_count);
77
78                                         archs++;
79                                 }
80
81                                 distros++;
82                         }
83
84                         versions++;
85                 }
86
87                 packages++;
88         }
89
90         ppa_stats_free(ppastats);
91 }
92
93 static struct option long_options[] = {
94         {"version", no_argument, 0, 'v'},
95         {"help", no_argument, 0, 'h'},
96         {"output-dir", required_argument, 0, 'o'},
97         {"debug", no_argument, 0, 'd'},
98         {"status", required_argument, 0, 's'},
99         {"skip-js-css", no_argument, 0, 'S'},
100         {"get-bpph-size", required_argument, 0, 0},
101         {"theme-dir", required_argument, 0, 't'},
102         {0, 0, 0, 0}
103 };
104
105 static void print_version()
106 {
107         printf("ppastats %s\n", VERSION);
108         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
109 "License GPLv2: GNU GPL version 2 or later\n"
110 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
111 "This is free software: you are free to change and redistribute it.\n"
112 "There is NO WARRANTY, to the extent permitted by law.\n"),
113                "2011-2014");
114 }
115
116 static void print_help()
117 {
118         printf(_("Usage: %s [OPTION]... PPA_OWNER PPA_NAME\n"), program_name);
119
120         puts(_(
121 "ppastats is a command application for generating PPA statistics.\n"));
122         puts(_(
123 "Prints number of downloads for each published packages of a PPA or generates\n"
124 "an HTML page containing a graph representation."));
125
126         puts("");
127         puts(_("Options:"));
128         puts(_("  -h, --help          display this help and exit"));
129         puts(_("  -v, --version       display version information and exit"));
130
131         puts("");
132
133         puts(_("  -o, --output-dir=[PATH]  generates HTML pages into 'PATH'"));
134         puts(_("  -t, --theme-dir=[PATH]   set theme dir to 'PATH'"));
135
136         puts(_(
137 "  -s, --status=[STATUS]    retrieves only package of the given status\n"
138 "                           (possible values are: Pending, Published,\n"
139 "                           Superseded, Deleted or Obsolete)"));
140
141         puts(_(
142 " -S, --skip-js-css         skip installation of js and css files"));
143         puts(_(
144 " --get-bpph-size=[s]       size of the replies of webservice requests to get\n"
145 "                           the list of binary packages. Between 1 and 300."));
146         puts("");
147
148         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
149         puts("");
150         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
151 }
152
153 int main(int argc, char **argv)
154 {
155         char *owner, *ppa, *package_status, *output_dir, *theme_dir, *log, *tmp;
156         int optc, output_html, cmdok, install_static_files, ws_size, opti;
157
158         program_name = argv[0];
159
160         setlocale(LC_ALL, "");
161         bindtextdomain(PACKAGE, LOCALEDIR);
162         textdomain(PACKAGE);
163
164         cmdok = 1;
165         install_static_files = 1;
166         output_dir = NULL;
167         package_status = NULL;
168         output_html = 0;
169         ws_size = -1;
170         theme_dir = NULL;
171
172         while ((optc = getopt_long(argc, argv, "vho:t:ds:S", long_options,
173                                    &opti)) != -1) {
174                 switch (optc) {
175                 case 0:
176                         if (!strcmp(long_options[opti].name, "get-bpph-size"))
177                                 ws_size = atoi(optarg);
178                         break;
179                 case 'o':
180                         output_html = 1;
181                         output_dir = strdup(optarg);
182                         break;
183                 case 'd':
184                         log_level = LOG_DEBUG;
185                         break;
186                 case 'h':
187                         print_help();
188                         exit(EXIT_SUCCESS);
189                 case 'v':
190                         print_version();
191                         exit(EXIT_SUCCESS);
192                 case 's':
193                         if (optarg)
194                                 package_status = strdup(optarg);
195                         break;
196                 case 't':
197                         if (optarg)
198                                 theme_dir = strdup(optarg);
199                         break;
200                 case 'S':
201                         install_static_files = 0;
202                         break;
203                 default:
204                         cmdok = 0;
205                         break;
206                 }
207         }
208
209         if (!cmdok || optind + 2 != argc) {
210                 fprintf(stderr,
211                         _("Try `%s --help' for more information.\n"),
212                         program_name);
213                 exit(EXIT_FAILURE);
214         }
215
216         tmp = path_append(getenv("HOME"), ".ppastats");
217         log = path_append(tmp, "ppastats.log");
218         log_open(log);
219         free(tmp);
220         free(log);
221
222         owner = argv[optind];
223         ppa = argv[optind+1];
224
225         if (output_html) {
226                 if (theme_dir)
227                         html_set_theme_dir(theme_dir);
228                 else
229                         html_set_theme_dir(DEFAULT_THEME_DIR);
230                 ppa_to_html(owner,
231                             ppa,
232                             package_status,
233                             output_dir,
234                             install_static_files,
235                             ws_size);
236         } else {
237                 display_published_binaries(owner, ppa, package_status, ws_size);
238         }
239
240         /* for valgrind.... */
241         free(package_status);
242         free(output_dir);
243         http_cleanup();
244         cache_cleanup();
245         fcache_cleanup();
246         html_cleanup();
247
248         exit(EXIT_SUCCESS);
249 }