1 | #!/usr/bin/perl |
---|
2 | # ------------------------------------------------------------------------------ |
---|
3 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
4 | # |
---|
5 | # This file is part of FCM, tools for managing and building source code. |
---|
6 | # |
---|
7 | # FCM is free software: you can redistribute it and/or modify |
---|
8 | # it under the terms of the GNU General Public License as published by |
---|
9 | # the Free Software Foundation, either version 3 of the License, or |
---|
10 | # (at your option) any later version. |
---|
11 | # |
---|
12 | # FCM is distributed in the hope that it will be useful, |
---|
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | # GNU General Public License for more details. |
---|
16 | # |
---|
17 | # You should have received a copy of the GNU General Public License |
---|
18 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | # ------------------------------------------------------------------------------ |
---|
20 | |
---|
21 | use strict; |
---|
22 | use warnings; |
---|
23 | |
---|
24 | use FindBin; |
---|
25 | use lib "$FindBin::Bin/../lib"; |
---|
26 | use File::Basename qw{basename}; |
---|
27 | use File::Spec; |
---|
28 | |
---|
29 | use FCM::Admin::Config; |
---|
30 | use FCM::Admin::Runner; |
---|
31 | use FCM::Admin::System qw{ |
---|
32 | distribute_wc |
---|
33 | filter_projects |
---|
34 | get_projects_from_svn_live |
---|
35 | install_svn_hook |
---|
36 | }; |
---|
37 | use FCM::Admin::Util qw{ |
---|
38 | run_mkpath |
---|
39 | run_rmtree |
---|
40 | run_svn_info |
---|
41 | run_svn_update |
---|
42 | write_file |
---|
43 | }; |
---|
44 | use Getopt::Long qw{GetOptions}; |
---|
45 | use Pod::Usage qw{pod2usage}; |
---|
46 | use Text::ParseWords qw{shellwords}; |
---|
47 | |
---|
48 | # ------------------------------------------------------------------------------ |
---|
49 | my $CONFIG = FCM::Admin::Config->instance(); |
---|
50 | my %PATTERN_OF = ( |
---|
51 | q{} => qr{.*}xms, |
---|
52 | SRC_HOOK => qr{svn-hooks/}xms, |
---|
53 | ); |
---|
54 | |
---|
55 | if (!caller()) { |
---|
56 | main(@ARGV); |
---|
57 | } |
---|
58 | |
---|
59 | # ------------------------------------------------------------------------------ |
---|
60 | # The main logic. |
---|
61 | sub main { |
---|
62 | local(@ARGV) = @_; |
---|
63 | my %option; |
---|
64 | my $result = GetOptions( |
---|
65 | \%option, |
---|
66 | q{help|usage|h}, |
---|
67 | q{force}, |
---|
68 | ); |
---|
69 | if (!$result) { |
---|
70 | pod2usage(1); |
---|
71 | } |
---|
72 | if (exists($option{help})) { |
---|
73 | pod2usage(q{-verbose} => 1); |
---|
74 | } |
---|
75 | create_lock() || return; |
---|
76 | my $RUNNER = FCM::Admin::Runner->instance(); |
---|
77 | my $is_force = $option{'force'}; |
---|
78 | UPDATE: |
---|
79 | while (1) { |
---|
80 | my @updates; |
---|
81 | for my $source_key (shellwords($CONFIG->get_mirror_keys())) { |
---|
82 | my $method = "get_$source_key"; |
---|
83 | push(@updates, run_svn_update($CONFIG->$method())); |
---|
84 | } |
---|
85 | if (!$is_force && !@updates) { |
---|
86 | last UPDATE; |
---|
87 | } |
---|
88 | if ($is_force || grep {$_ =~ $PATTERN_OF{'SRC_HOOK'}} @updates) { |
---|
89 | $RUNNER->run( |
---|
90 | '(re-)installing hook scripts', |
---|
91 | sub { |
---|
92 | for my $project (get_projects_from_svn_live()) { |
---|
93 | install_svn_hook($project); |
---|
94 | } |
---|
95 | return 1; |
---|
96 | } |
---|
97 | ); |
---|
98 | } |
---|
99 | if ($is_force || grep {$_ =~ $PATTERN_OF{q{}}} @updates) { |
---|
100 | $RUNNER->run( |
---|
101 | 'distributing FCM to standard locations', \&distribute_wc); |
---|
102 | } |
---|
103 | $is_force = 0; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | # ------------------------------------------------------------------------------ |
---|
108 | # Creates a lock. Returns true on success. Removes lock when program finishes. |
---|
109 | our $LOCK; |
---|
110 | sub create_lock { |
---|
111 | my $home = (getpwuid($<))[7]; |
---|
112 | $LOCK = File::Spec->catfile($home, sprintf(".%s.lock", basename($0))); |
---|
113 | if (-e $LOCK) { |
---|
114 | $LOCK = undef; |
---|
115 | return; |
---|
116 | } |
---|
117 | return run_mkpath($LOCK); |
---|
118 | END { |
---|
119 | if ($LOCK) { |
---|
120 | run_rmtree($LOCK); |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | __END__ |
---|
126 | |
---|
127 | =head1 NAME |
---|
128 | |
---|
129 | fcm-commit-update |
---|
130 | |
---|
131 | =head1 SYNOPSIS |
---|
132 | |
---|
133 | fcm-commit-update |
---|
134 | |
---|
135 | =head1 DESCRIPTION |
---|
136 | |
---|
137 | This program performs the post-commit update for the FCM system. It runs |
---|
138 | continuously until no more update is available. It prevent another copy from |
---|
139 | running by creating a lock. If another copy detects a lock, it exits without |
---|
140 | doing anything. |
---|
141 | |
---|
142 | =head1 OPTIONS |
---|
143 | |
---|
144 | =over 4 |
---|
145 | |
---|
146 | =item --force |
---|
147 | |
---|
148 | Force an update. |
---|
149 | |
---|
150 | =back |
---|
151 | |
---|
152 | =head1 ARGUMENTS |
---|
153 | |
---|
154 | =over 4 |
---|
155 | |
---|
156 | =item REPOS-NAME |
---|
157 | |
---|
158 | The name of the repository invoking this program. |
---|
159 | |
---|
160 | =item LOG-DIR-PATH |
---|
161 | |
---|
162 | The path to the log directory. |
---|
163 | |
---|
164 | =back |
---|
165 | |
---|
166 | =head1 COPYRIGHT |
---|
167 | |
---|
168 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
169 | |
---|
170 | =cut |
---|