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