source: codes/icosagcm/trunk/tools/FCM/templates/utils/fcm_add_trac.pl @ 10

Last change on this file since 10 was 10, checked in by ymipsl, 12 years ago

dynamico tree creation

YM

  • Property svn:executable set to *
File size: 10.7 KB
Line 
1#!/usr/bin/perl
2# ------------------------------------------------------------------------------
3# NAME
4#   fcm_add_trac.pl
5#
6# SYNOPSIS
7#   fcm_add_trac.pl [OPTIONS] <system>
8#   fcm_add_trac.pl -h
9#
10# DESCRIPTION
11#   See $usage for further information.
12#
13# COPYRIGHT
14#   (C) Crown copyright Met Office. All rights reserved.
15#   For further details please refer to the file COPYRIGHT.txt
16#   which you should have received as part of this distribution.
17# ------------------------------------------------------------------------------
18
19# Standard pragmas
20use strict;
21use warnings;
22
23# Standard modules
24use Getopt::Long;
25use File::Basename;
26use File::Spec;
27use Config::IniFiles;
28
29# In-house modules
30use lib File::Spec->catfile (
31  dirname (
32    (grep {-x File::Spec->catfile ($_, 'fcm')} split (/:/, $ENV{PATH})) [0]
33  ),
34  'lib',
35);
36use Fcm::Util;
37
38# ------------------------------------------------------------------------------
39
40# Program information
41my $this      = basename ($0);
42my $year      = (localtime)[5] + 1900;
43my $copyright = <<EOF;
44
45(C) Crown copyright $year Met Office. All rights reserved.
46EOF
47my $usage     = <<EOF;
48NAME
49  $this: add a new Trac browser for a FCM project
50
51SYNOPSIS
52  $this [OPTIONS] <project> # normal usage
53  $this -h                  # print help and exit
54
55DESCRIPTION
56  This script adds a new Trac browser for a project managed by FCM. The name of
57  the project is specified in the first argument.
58 
59  With the except of --authorised and --help, all options to the script take at
60  least one argument. If an option can take more than one argument, the
61  arguments should be specified in a comma-separated list.
62
63OPTIONS
64  -a, --authorised      - specify an "authorised" permission. Only those users
65                          granted this permission will have "_CREATE" and
66                          "_MODIFY" access. The default is to grant all
67                          authenticated users "_CREATE" and "_MODIFY" access.
68
69  -c, --component       - specify one or more components.
70
71  -d, --description     - specify a description of the project.
72
73  -e, --smtp-always-cc  - specify one or more e-mail addresses, where e-mails
74                          will be sent to on ticket changes.
75
76  -h, --help            - prints the usage string and exits.
77
78  -m, --milestone       - specify one or more milestones.
79
80  -s, --svn-repos       - specify the Subversion repository location.
81
82  -u, --admin-user      - specify one or more admin users.
83
84  -v, --version         - specify one or more versions.
85$copyright
86EOF
87
88# ------------------------------------------------------------------------------
89
90# Options
91my ($authorised, $help, $description, $svn_repos);
92my (@admin_users, @components, @emails, @milestones, @versions);
93
94GetOptions (
95  'a|authorised'       => \$authorised,
96  'c|component=s'      => \@components,
97  'd|description=s'    => \$description,
98  'e|smtp-always-cc=s' => \@emails,
99  'h|help'             => \$help,
100  'm|milestone=s'      => \@milestones,
101  's|svn-repos=s'      => \$svn_repos,
102  'u|admin-user=s'     => \@admin_users,
103  'v|version=s'        => \@versions,
104);
105
106@admin_users = split (/,/, join (',', @admin_users));
107@components  = split (/,/, join (',', @components));
108@emails      = split (/,/, join (',', @emails));
109@milestones  = split (/,/, join (',', @milestones));
110@versions    = split (/,/, join (',', @versions));
111
112if ($help) {
113  print $usage;
114  exit;
115}
116
117# Arguments
118my $project = $ARGV[0];
119die $this, ': the first argument must be the name of a project' if not $project;
120
121# ------------------------------------------------------------------------------
122
123# Useful variables
124my $svn_root  = '/data/local/fcm/svn/live';
125my $trac_root = File::Spec->catfile ((getpwnam ('fcm'))[7], 'trac', 'live');
126
127# ------------------------------------------------------------------------------
128
129MAIN: {
130  # Check for the existence of Trac for the current project
131  # ----------------------------------------------------------------------------
132  my $trac_project = File::Spec->catfile ($trac_root, $project);
133  die $this, ': Trac for "', $project, '" already exists in ', $trac_project
134    if -d $trac_project;
135
136  # Check for the existence of SVN for the current project
137  # ----------------------------------------------------------------------------
138  my $svn_project = $svn_repos
139                    ? $svn_repos
140                    : File::Spec->catfile ($svn_root, $project . '_svn');
141  die $this, ': SVN for "', $project, '" does not exist in ', $svn_project
142    if not -d $svn_project;
143
144  # Set up "trac-admin" command for this project
145  # ----------------------------------------------------------------------------
146  my @admin_cmd = ('trac-admin', $trac_project);
147
148  # Create project's Trac
149  my $trac_templates = '/usr/share/trac/templates';
150
151  my @command = (
152    @admin_cmd,
153    'initenv',
154    $project,
155    'sqlite:db/trac.db',
156    $svn_project,
157    $trac_templates,
158  );
159
160  &run_command (\@command, PRINT => 1);
161
162  # Ensure the new Trac has the correct group and permissions
163  &run_command ([qw/chgrp -R apache/, $trac_project], PRINT => 1);
164  &run_command ([qw/chmod -R g+w/, $trac_project], PRINT => 1);
165
166  # Components
167  # ----------------------------------------------------------------------------
168  my @cur_cmd = (@admin_cmd, 'component');
169
170  # Remove example components
171  for my $component (qw/component1 component2/) {
172    &run_command ([@cur_cmd, 'remove', $component], PRINT => 1);
173  }
174
175  # Add specified components
176  for my $component (@components) {
177    &run_command ([@cur_cmd, 'add', $component, ''], PRINT => 1);
178  }
179
180  # List components
181  &run_command ([@cur_cmd, 'list'], PRINT => 1);
182
183  # Versions
184  # ----------------------------------------------------------------------------
185  @cur_cmd = (@admin_cmd, 'version');
186
187  # Remove example versions
188  for my $version (qw/1.0 2.0/) {
189    &run_command ([@cur_cmd, 'remove', $version], PRINT => 1);
190  }
191
192  # Add specified versions
193  for my $version (@versions) {
194    &run_command ([@cur_cmd, 'add', $version], PRINT => 1);
195  }
196
197  # List versions
198  &run_command ([@cur_cmd, 'list'], PRINT => 1);
199
200  # Milestones
201  # ----------------------------------------------------------------------------
202  @cur_cmd = (@admin_cmd, 'milestone');
203
204  # Remove example milestones
205  for my $milestone (qw/milestone1 milestone2 milestone3 milestone4/) {
206    &run_command ([@cur_cmd, 'remove', $milestone], PRINT => 1);
207  }
208
209  # Add specified milestones
210  for my $milestone (@milestones) {
211    &run_command ([@cur_cmd, 'add', $milestone], PRINT => 1);
212  }
213
214  # List milestones
215  &run_command ([@cur_cmd, 'list'], PRINT => 1);
216
217  # Priority
218  # ----------------------------------------------------------------------------
219  @cur_cmd = (@admin_cmd, 'priority');
220
221  # Change default priorities
222  my %priorities = (
223    blocker  => 'highest',
224    critical => 'high',
225    major    => 'normal',
226    minor    => 'low',
227    trivial  => 'lowest',
228  );
229  while (my ($old, $new) = each %priorities) {
230    &run_command ([@cur_cmd, 'change', $old, $new], PRINT => 1);
231  }
232
233  # List priorities
234  &run_command ([@cur_cmd, 'list'], PRINT => 1);
235
236  # Severity
237  # ----------------------------------------------------------------------------
238  @cur_cmd = (@admin_cmd, 'severity');
239
240  # Add serverities
241  for my $severity (qw/blocker critical major normal minor trivial/) {
242    &run_command ([@cur_cmd, 'add', $severity], PRINT => 1);
243  }
244
245  # List serverities
246  &run_command ([@cur_cmd, 'list'], PRINT => 1);
247
248  # Permission
249  # ----------------------------------------------------------------------------
250  @cur_cmd = (@admin_cmd, 'permission');
251
252  # Add permissions to admin users
253  for my $user (@admin_users) {
254    &run_command ([@cur_cmd, 'add', $user, 'admin'], PRINT => 1);
255  }
256  &run_command ([@cur_cmd, qw/add admin TRAC_ADMIN/], PRINT => 1);
257
258  # Remove wiki/ticket create/modify permissions from anonymous users
259  for my $per (qw/TICKET_CREATE TICKET_MODIFY WIKI_CREATE WIKI_MODIFY/) {
260    &run_command ([@cur_cmd, qw/remove anonymous/, $per], PRINT => 1);
261  }
262
263  # Add wiki/ticket create/modify permissions to authenticated/authorised users
264  for my $per (qw/TICKET_CREATE TICKET_MODIFY WIKI_CREATE WIKI_MODIFY/) {
265    &run_command (
266      [@cur_cmd, 'add', ($authorised ? 'authorised' : 'authenticated'), $per],
267      PRINT => 1,
268    );
269  }
270
271  # List permissions
272  &run_command ([@cur_cmd, 'list'], PRINT => 1);
273
274  # Misc modifications to "trac.ini"
275  # ----------------------------------------------------------------------------
276  my $trac_ini = File::Spec->catfile ($trac_project, 'conf', 'trac.ini');
277
278  # Read and parse "trac.ini"
279  my $parser = Config::IniFiles->new ('-file' => $trac_ini);
280
281  # Footer to be displayed in the each page
282  my $footer         = 'This system is managed by the<br />' .
283                       '<a href="http://fcm1/projects/FCM">' .
284                       'Flexible Configuration Management System</a>';
285
286  # Descriptive name for the current project
287  my $descr          = $description ? $description : $project;
288
289  # Default component
290  my $def_component  = @components ? $components [0] : '';
291
292  # List of e-mail addresses to always cc
293  my $smtp_always_cc = @emails ? join (',', @emails) : '';
294
295  # List of "default" config, in [section, option, value]
296  my @config = (
297    ['notification', 'always_notify_owner'   , 'true'],
298    ['notification', 'always_notify_reporter', 'true'],
299    ['notification', 'smtp_enabled'          , 'true'],
300    ['notification', 'smtp_server'           , 'localhost.metoffice.com'],
301    ['notification', 'smtp_always_cc'        , $smtp_always_cc],
302    ['project'     , 'url'                   , ''],
303    ['project'     , 'footer'                , $footer],
304    ['project'     , 'descr'                 , $descr],
305    ['timeline'    , 'ticket_show_details'   , 'true'],
306    ['ticket'      , 'default_component'     , $def_component],
307    ['ticket'      , 'default_priority'      , 'normal'],
308    ['ticket'      , 'default_severity'      , 'normal'],
309    ['timeline'    , 'changeset_show_files'  , '3'],
310    ['components'  , 'trac.ticket.report.*'  , 'disabled'],
311  );
312
313  for (@config) {
314    my ($section, $option, $value) = @{ $_ };
315
316    # Add new section, if necessary
317    $parser->AddSection ($section) if not $parser->SectionExists ($section);
318
319    if (defined ($parser->val ($section, $option))) {
320      # Modify existing option
321      $parser->setval ($section, $option, $value);
322
323    } else {
324      # Add new option
325      $parser->newval ($section, $option, $value);
326    }
327  }
328
329  # Rename original "trac.ini" to "trac.ini.orig"
330  rename $trac_ini, $trac_ini . '.orig';
331
332  # Output modifications to "trac.ini"
333  $parser->WriteConfig ($trac_ini);
334}
335
336# Finally...
337# ------------------------------------------------------------------------------
338print $this, ': finished', "\n";
339exit;
340
341# ------------------------------------------------------------------------------
342
343__END__
Note: See TracBrowser for help on using the repository browser.