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