source: trunk/bin/Parameter.cc @ 226

Last change on this file since 226 was 226, checked in by Peter Johansson, 15 years ago

added reading of config file in Parameter closes ##106 and #98. Move function check_target from svndigest.cc to utility.cc and changed name to node_exist. added support for aliases in Copyright update refs #36

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.1 KB
Line 
1// $Id: Parameter.cc 226 2007-03-11 19:32:41Z peter $
2
3/*
4  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2007 Peter Johansson
6
7  This file is part of svndigest, http://lev.thep.lu.se/trac/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#include "utility.h"
27#include <config.h> // this header file is created by configure
28
29#include <fstream>
30#include <iostream>
31#include <sstream>
32#include <stdexcept>
33#include <string>
34#include <sys/stat.h>
35
36namespace theplu {
37namespace svndigest {
38
39  Parameter::Parameter(const int argc,const char *argv[])
40  {
41    defaults();
42    for (int i=1; i<argc; ++i) {
43      bool ok=false;
44      std::string myargv(argv[i]);
45      if (myargv=="-c" || myargv=="--config"){
46        if (++i<argc){
47          config_path_= std::string(argv[i]);
48          ok=true;
49        }
50      }
51      else if (myargv=="--copyright"){
52          copyright_=true;
53          ok=true;
54      }
55      else if (myargv=="-f" || myargv=="--force"){
56          force_=true;
57          ok=true;
58      }
59      else if (myargv=="-h" || myargv=="--help"){
60        help();
61        exit(0);      // always exit after printing help
62      }
63      else if (myargv=="-r" || myargv=="--root"){
64        if (++i<argc){
65          root_= std::string(argv[i]);
66          ok=true;
67        }
68      }
69      else if (myargv=="-rev" || myargv=="--revisions") {
70          revisions_=true;
71          ok=true;
72      }
73      else if (myargv=="-t" || myargv=="--target"){
74        if (++i<argc){
75          targetdir_= std::string(argv[i]);
76          ok=true;
77        }
78      }
79      else if (myargv=="-v" || myargv=="--verbose"){
80          verbose_=true;
81          ok=true;
82      }
83      else if (myargv=="--version"){
84          version();
85          exit(0);
86      }
87      else if (myargv=="-vf" || myargv=="-fv"){
88          verbose_=true;
89          force_=true;
90          ok=true;
91      }
92
93      if (!ok)
94        throw std::runtime_error("svndigest: invalid option: " + myargv +
95                                 "\nType 'svndigest --help' for usage.");
96    }
97
98    std::string home_path = getenv("HOME") + "/.svndigest";
99    if (!node_exist(home_path))
100      mkdir(home_path);
101    if (!node_exist(home_path+"/config"))
102      print_config(home_path+"/config");
103    read_config(config_path_);
104
105    analyse();
106  }
107
108
109  void Parameter::analyse(void)
110  {
111    using namespace std;
112
113    string workdir(pwd()); // remember current working directory (cwd).
114
115    // Checking that root_ exists and retrieve the absolute path to root_
116    if (chdir(root_.c_str()))
117      throw runtime_error(string("svndigest: Root directory (") + root_ +
118                          ") access failed.");
119    root_ = pwd();
120
121    // need to get back to cwd if relative paths are used.
122    if (chdir(workdir.c_str()))
123      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
124
125    // Checking that targetdir_ exists and retrieve the absolute path
126    // to targetdir_
127    if (chdir(targetdir_.c_str()))
128      throw runtime_error(string("svndigest: Target directory (") + targetdir_ +
129                          ") access failed.");
130    targetdir_ = pwd();
131    // Checking write permissions for targetdir_
132    if (access_rights(targetdir_,"w"))
133      throw runtime_error(string("svndigest: No write permission on target ") +
134                                 "directory (" + targetdir_ +").");
135
136    // return back to cwd
137    if (chdir(workdir.c_str()))
138      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
139  }
140
141
142  void Parameter::defaults(void)
143  {
144    config_path_ = getenv("HOME")+"/.svndigest/config";
145    copyright_=false;
146    force_=false;
147    revisions_=false;
148    root_=".";
149    targetdir_=".";
150    verbose_=false;
151  }
152
153
154  void Parameter::help(void)
155  {
156    defaults();
157    std::cout << "\n"
158              << "usage: svndigest [options]\n"
159              << "\n"
160              << "svndigest traverses a directory structure (controlled by\n"
161              << "subversion) and calculates developer statistics entries.\n"
162              << "The top level directory of the directory structure to\n"
163              << "traverse is set with the -r option. The result is written to\n"
164              << "the current working directory or a directory set with the\n"
165              << "-t option.\n"
166              << "\n"
167              << "Valid options:\n"
168              << "  -c [--config] arg : path to config file ["
169              << config_path_ << "]\n"
170              << "  -f [--force]   : remove target directory/file if it exists\n"
171              << "                   [no force]. NOTE recursive delete.\n"
172              << "  -h [--help]    : display this help and exit\n"
173              << "  -r [--root] arg : svn controlled directory to perform\n"
174              << "                    statistics calculation on [" 
175              << root_ << "]\n"
176              << "  -rev [--revisions]: Use revision numbers as time scale\n"
177              << "                      instead of dates [dates].\n"
178              << "  -t [--target] arg : output directory [" 
179              << targetdir_ << "]\n"
180              << "  -v [--verbose] : explain what is being done\n"
181              << "  --version      : print version information and exit\n"
182              << std::endl;
183  }
184
185
186  void Parameter::print_config(std::string path) const
187  {
188    std::ofstream os(path.c_str());
189    if (!os.good()){
190      os.close();
191      throw std::runtime_error("svndigest: error: cannot create file '" + 
192                               path + "': permission denied");
193    }
194    os << "### This file configures various behaviors for svndigest\n"
195       << "### The commented-out below are intended to demonstrate how to use\n" 
196       << "### this file.\n"
197       << "\n"
198       << "### Section for setting aliases used in copyright update\n"
199       << "#[copyright-alias]\n"
200       << "# jdoe = John Doe\n"
201       << std::endl;
202    os.close();
203  }
204
205
206  void Parameter::read_config(std::string path)
207  {
208    if (!node_exist(path))
209      throw std::runtime_error("svndigest: error: configuration file '" + 
210                               path + "' not found");
211    std::ifstream is(path.c_str());
212    if (!is.good()){
213      is.close();
214      throw std::runtime_error("svndigest: error: cannot open file '" + path +
215                               "' for reading");
216    }
217    std::string line;
218    std::string section;
219    std::string tmp;
220    while (getline(is, line)) {
221      line = ltrim(line);
222      if (line.empty() || line[0]=='#')
223        continue;
224      std::stringstream ss(line);
225      if (line[0] == '[') {
226        getline(ss, tmp, '[');
227        getline(ss, section, ']');
228      }
229      else if (section == "copyright-alias"){
230        getline(ss, tmp, '=');
231        std::string key = trim(tmp);
232        getline(ss, tmp);
233        std::string name = trim(tmp);
234        copyright_alias_[key] = name;
235      }
236    }
237    is.close();
238  }
239
240
241  void Parameter::version(void) const
242  {
243    using namespace std;
244    cout << PACKAGE_STRING
245         << "\nCopyright (C) 2006-2007 Jari Häkkinen and Peter Johansson.\n\n"
246         << "This is free software; see the source for copying conditions.\n"
247         << "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR\n"
248         << "A PARTICULAR PURPOSE." << endl;
249  }
250
251}} // of namespace wenni and namespace theplu
Note: See TracBrowser for help on using the repository browser.