source: trunk/bin/Parameter.cc @ 892

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

define PACKAGE_URL in configure.ac also for autoconf 2.63 and older. Use PACKAGE_URL in footer rather than hard-coded value.

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