source: trunk/bin/Parameter.cc @ 631

Last change on this file since 631 was 631, checked in by Peter Johansson, 14 years ago

fixes #303

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1// $Id: Parameter.cc 631 2008-04-28 22:02:53Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of svndigest, http://trac.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 2 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 this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
25#include "Parameter.h"
26
27#include "ColumnStream.h"
28#include "subversion_info.h"
29#include "utility.h"
30#include <config.h> // this header file is created by configure
31
32#include <cstddef>
33#include <fstream>
34#include <iostream>
35#include <sstream>
36#include <stdexcept>
37#include <string>
38#include <sys/stat.h>
39
40namespace theplu {
41namespace svndigest {
42
43  Parameter::Parameter( int argc, char *argv[])
44    : config_file_(""), copyright_(false), force_(false), 
45      generate_config_(false), ignore_cache_(false), report_(true),
46      revisions_(false), root_("."), targetdir_("."), verbose_(false)
47  {
48    for (int i=1; i<argc; ++i) {
49      std::stringstream ss(argv[i]);
50      std::string myargv("");
51      std::string value("");
52      getline(ss, myargv, '=');
53      getline(ss, value);
54
55      if (myargv=="--config-file"){
56        if (value.size()) {
57          config_file_.value()= value;
58        }
59        else if (++i<argc){
60          config_file_.value()= std::string(argv[i]);
61        }
62        else {
63          missing_argument(myargv);
64        }
65      }
66      else if (myargv=="--copyright"){
67          copyright_.value()=true;
68      }
69      else if (myargv=="-f" || myargv=="--force"){
70          force_.value()=true;
71      }
72      else if (myargv=="-g" || myargv=="--generate-config"){
73          generate_config_.value()=true;
74      }
75      else if (myargv=="-h" || myargv=="--help"){
76        help();
77        exit(0);      // always exit after printing help
78      }
79      else if (myargv=="--ignore-cache"){
80          ignore_cache_.value()=true;
81      }
82      else if (myargv=="-r" || myargv=="--root"){
83        if (value.size()) {
84          root_.value()= value;
85        }
86        else if (++i<argc){
87          root_.value()= std::string(argv[i]);
88        }
89        else {
90          missing_argument(myargv);
91        }
92      }
93      else if (myargv=="--report") {
94          report_.value()=true;
95      }
96      else if (myargv=="--no-report") {
97          report_.value()=false;
98      }
99      else if (myargv=="--revisions") {
100          revisions_.value()=true;
101      }
102      else if (myargv=="-t" || myargv=="--target"){
103        if (value.size()) {
104          targetdir_.value()= value;
105        }
106        else if (++i<argc){
107          targetdir_.value()= std::string(argv[i]);
108        }
109        else {
110          missing_argument(myargv);
111        }
112      }
113      else if (myargv=="-v" || myargv=="--verbose"){
114          verbose_.value()=true;
115      }
116      else if (myargv=="--version"){
117        version();
118        exit(0);
119      }
120      else if (myargv=="-vf" || myargv=="-fv"){
121          verbose_.value()=true;
122          force_.value()=true;
123      }
124      else {
125        throw std::runtime_error("svndigest: invalid option: " + myargv +
126                                 "\nTry `svndigest --help' for usage.");
127      } 
128  }
129
130    analyse();
131  }
132
133
134  void Parameter::analyse(void)
135  {
136    using namespace std;
137
138    string workdir(pwd()); // remember current working directory (cwd).
139
140    bool root_ok = ( node_exist(root_.value()) && 
141                     !access_rights(root_.value(), "r") );
142   
143    if (root_ok) {
144      // Check that root_ is a directory
145      struct stat nodestat;
146      stat(root_.value().c_str(), &nodestat);
147      if (!S_ISDIR(nodestat.st_mode))
148        throw runtime_error(string("svndigest: accessing `") + root_.value() +
149                            "': Not a directory.");
150    }
151
152    // Checking that root_ exists and retrieve the absolute path to root_
153    if (!root_ok || chdir(root_.value().c_str()))
154      throw runtime_error(string("svndigest: Root directory (") + root_.value() +
155                          ") access failed.");
156    root_.value() = pwd();
157
158     
159
160    // need to get back to cwd if relative paths are used.
161    if (chdir(workdir.c_str()))
162      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
163
164    // Checking that targetdir_ exists and retrieve the absolute path
165    // to targetdir_
166    if (report()) {
167      if (chdir(targetdir_.value().c_str()))
168        throw runtime_error(string("svndigest: Target directory (") + 
169                            targetdir_.value() + ") access failed.");
170      targetdir_.value() = pwd();
171      // Checking write permissions for targetdir_
172      if (access_rights(targetdir_.value(),"w"))
173        throw runtime_error(string("svndigest: No write permission on target ") +
174                            "directory (" + targetdir_.value() +").");
175    }
176
177    // return back to cwd
178    if (chdir(workdir.c_str()))
179      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
180
181  }
182
183
184  std::string Parameter::config_file(void) const
185  {
186    // not default
187    if (!config_file_.value().empty())
188      return config_file_.value();
189   
190    // default behaviour
191    return root()+"/.svndigest/config";
192  }
193
194
195  void Parameter::help(void) const
196  {
197    ColumnStream cs(std::cout, 1);
198    cs.width(0)=79;
199    cs.margin(0)=0;
200    ColumnStream cs2(std::cout, 2);
201    // Widest line should fit exactly
202    cs2.width(0)=21;
203    cs2.width(1)=52;
204    cs2.margin(0)=2;
205    // Gnits standard suggest three characters gap between option and description
206    cs2.margin(1)=3;
207
208    std::cout << "Usage: svndigest [OPTION]...\n"
209              << "\n";
210
211    cs << "Generate statistical report for a subversion repository.\n";
212
213    cs << "\nMandatory arguments to long options are mandatory for "
214       << "short options too.\n";
215
216    cs2  << "    --config-file=ARG\tconfiguration file " 
217         << "[<ROOT>/.svndigest/config]\n"
218         << "    --copyright\tupdate copyright statement\n" 
219         << "-f, --force\tif sub-directory named <ROOT> exists in "
220         << "target directory, remove sub-directory before writing results\n"
221         << "-g, --generate-config\twrite configuration file " 
222         << "to standard output and exit\n"
223         << "-h, --help\tdisplay this help and exit\n"
224         << "    --ignore-cache\tIgnore cache files and analyze "
225         << "everything from repository\n"
226         << "    --no-report\tCreate no HTML report\n"
227         << "    --revisions\tUse revision numbers as time scale "
228         << "instead of dates [dates]\n"
229         << "-r, --root=ROOT\tsvn controlled directory to perform "
230         << "statistics calculation on [" << root_.default_value() << "]\n"
231         << "-t, --target=TARGET\toutput directory [" 
232         << targetdir_.default_value() << "]\n"
233         << "-v, --verbose\texplain what is being done\n"
234         << "    --version\tprint version information and exit\n";
235
236    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
237              << std::endl;
238  }
239
240
241  void Parameter::missing_argument(std::string opt) const
242  {
243    if (opt.size()>0 && opt[0]=='-')
244      opt = opt.substr(1);
245    if (opt.size()>0 && opt[0]=='-')
246      opt = opt.substr(1);
247    std::stringstream ss;
248    ss << "svndigest: option requires an argument -- " << opt << "\n"
249       << "Try `svndigest --help' for usage."; 
250    throw std::runtime_error(ss.str());
251  }
252
253  void Parameter::version(bool verbose) const
254  {
255    ColumnStream cs(std::cout, 1);
256    cs.width(0)=79;
257    cs << PACKAGE_STRING << version_string() << "\n";
258    cs << "\nCopyright (C) " << svn_year() 
259       << " Jari H\u00E4kkinen and Peter Johansson.\n"
260       << "This is free software. You may redistribute copies of it under "
261       << "the terms of the GNU General Public License "
262       << "<http://www.gnu.org/licenses/gpl.html>.\n"
263       << "There is NO WARRANTY; to the extent permitted by law.\n";
264  }
265
266}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.