source: trunk/bin/Parameter.cc @ 829

Last change on this file since 829 was 829, checked in by Peter Johansson, 12 years ago

adding svndigest url to help output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 6.8 KB
Line 
1// $Id: Parameter.cc 829 2009-10-27 02:36:08Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://dev.thep.lu.se/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "Parameter.h"
23
24#include "OptionVersion.h"
25#include "../lib/utility.h" // to avoid inclusion of yat file
26
27#include "yat/ColumnStream.h"
28#include "yat/Exception.h"
29#include "yat/OptionArg.h"
30#include "yat/OptionHelp.h"
31#include "yat/OptionSwitch.h"
32
33#include <config.h> // this header file is created by configure
34
35#include <cassert>
36#include <cerrno>
37#include <cstddef>
38#include <cstring>
39#include <fstream>
40#include <iostream>
41#include <sstream>
42#include <stdexcept>
43#include <string>
44#include <sys/stat.h>
45
46namespace theplu {
47namespace svndigest {
48
49  Parameter::Parameter( int argc, char *argv[])
50    : cmd_( std::string("Mandatory arguments to long options ") + 
51            std::string("are mandatory for short options too.")), 
52      config_file_(cmd_, "config-file", 
53                   "configuration file [<ROOT>/.svndigest/config]"),
54      copyright_(cmd_, "copyright", "update copyright statement"),
55      force_(cmd_, "f,force", 
56             std::string("if sub-directory named <ROOT> exists in target dire")+
57             std::string("ctory, remove sub-directory before writing results")),
58      generate_config_(cmd_, "g,generate-config", 
59                       "write configuration to standard output"),
60      help_(cmd_),
61      ignore_cache_(cmd_, "ignore-cache", 
62                    std::string("ignore cache files and analyze ") +
63                    std::string("everything from repository")),
64      report_(cmd_, "report", "create no HTML report", true),
65      revisions_(cmd_, "revisions", 
66                 "use revision number as time scale instead of dates [dates]"),
67      root_(cmd_, "r,root", 
68            "svn controlled directory to perform statistics on [.]"),
69      target_(cmd_, "t,target", "output directory [.]"),
70      verbose_(cmd_, "v,verbose", "explain what is being done"),
71      version_(cmd_, "version", "print version information and exit", &verbose_)
72  {
73    init();
74    try {
75      cmd_.parse(argc, argv);
76    }
77    catch (yat::utility::cmd_error& e) {
78      std::cerr << e.what() << std::endl;
79      exit (-1);
80    }
81
82    // set default values
83    if (!root_.present())
84      root_.value(".");
85
86    if (!target_.present())
87      target_.value(".");
88
89    if (!config_file_.present())
90      config_file_.value(concatenate_path(root_.value(),".svndigest/config"));
91
92    // analyse arguments
93    analyse();
94  }
95
96
97  Parameter::~Parameter(void)
98  {
99  }
100
101
102  void Parameter::analyse(void)
103  {
104    std::string save_wd = pwd();
105
106    // check root but not if -g option given
107    if (!generate_config()) {
108      check_existence(root_.value());
109      check_readable(root_.value());
110      if (chdir(root_.value().c_str())) {
111        std::stringstream ss;
112        ss << "svndigest: cannot read `" << root_.value() << "': " 
113           << strerror(errno);
114        throw yat::utility::cmd_error(ss.str());
115      }
116      root_.value(pwd());
117      chdir(save_wd.c_str());
118
119      // check target (only if we write report)
120      if (report()) {
121        check_existence(target_.value());
122        check_readable(target_.value());
123        std::string base_root = file_name(root_.value());
124        std::string path = concatenate_path(target_.value(),base_root); 
125        if (access_rights(target_.value().c_str(), "w")) {
126          std::stringstream ss;
127          ss << "svndigest: cannot create directory `" << path
128             << "': " << strerror(errno);
129          throw yat::utility::cmd_error(ss.str());
130        }
131        if (node_exist(path) && !force()) {
132          std::stringstream ss;
133          ss << "svndigest: cannot create directory `" << path << "' "
134             << strerror(EEXIST);
135          throw yat::utility::cmd_error(ss.str());
136        }
137        if (chdir(target_.value().c_str())) {
138          std::stringstream ss;
139          ss << "svndigest: cannot read `" << target_.value() << "': " 
140             << strerror(errno);
141          throw yat::utility::cmd_error(ss.str());
142        }
143        target_.value(pwd());
144        chdir(save_wd.c_str());
145      }
146    }
147
148    // check config file
149    struct stat nodestat;
150    // true also if there is a broken symlink named...
151    bool config_exists = !lstat(config_file_.value().c_str(), &nodestat);
152    // the latter case in order to catch broken symlink
153    if (config_file_.present() || config_exists)
154      // throws if file does not exists
155      check_existence(config_file_.value());
156    if (config_exists) {
157      // throws if file is not readable
158      check_readable(config_file_.value());
159      stat(config_file_.value().c_str(), &nodestat);
160      if (!S_ISREG(nodestat.st_mode)) {
161        std::stringstream ss;
162        ss << "svndigest: `" << config_file_.value() 
163           << "' is not a regular file";
164        throw yat::utility::cmd_error(ss.str());
165      }
166    }
167  }
168
169
170  void Parameter::check_existence(std::string path) const
171  {
172    if (node_exist(path))
173      return;
174    std::stringstream ss;
175    ss << "svndigest: cannot stat `" << path << "': " << strerror(errno);
176    throw yat::utility::cmd_error(ss.str());
177  }
178
179 
180  void Parameter::check_readable(std::string path) const
181  {
182    if (!access_rights(path, "r"))
183      return;
184    std::stringstream ss;
185    ss << "svndigest: cannot open `" << path << "': " << strerror(errno);
186    throw yat::utility::cmd_error(ss.str());
187  }
188 
189
190  std::string Parameter::config_file(void) const
191  {
192    return config_file_.value();
193  }
194
195
196  bool Parameter::copyright(void) const
197  {
198    return copyright_.present();
199  }
200
201
202  bool Parameter::force(void) const
203  {
204    return force_.present();
205  }
206
207
208  bool Parameter::generate_config(void) const
209  {
210    return generate_config_.present();
211  }
212
213
214  bool Parameter::ignore_cache(void) const
215  {
216    return ignore_cache_.present();
217  }
218
219
220  void Parameter::init(void)
221  {
222    // don't use argv[0] because user may rename the binary
223    cmd_.program_name() = PACKAGE_NAME;
224    config_file_.print_arg("=FILE");
225    root_.print_arg("=ROOT");
226    target_.print_arg("=TARGET");
227    std::stringstream ss;
228    ss << "Report bugs to " << PACKAGE_BUGREPORT << ".\n"
229       << PACKAGE << " home page: <" << PACKAGE_URL << ">.\n";
230    help_.post_arguments() = ss.str();
231    help_.synopsis() = 
232      "Generate statistical report for a subversion repository\n";
233  }
234
235
236  bool Parameter::report(void) const
237  {
238    return report_.value();
239  }
240
241
242  bool Parameter::revisions(void) const
243  {
244    return revisions_.present();
245  }
246
247
248  std::string Parameter::root(void) const
249  {
250    return root_.value();
251  }
252
253 
254  std::string Parameter::targetdir(void) const
255  {
256    return target_.value();
257  }
258
259 
260  bool Parameter::verbose(void) const
261  {
262    return verbose_.present();
263  }
264
265
266}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.