source: trunk/project-meta/project-meta @ 166

Last change on this file since 166 was 166, checked in by g7moreau, 6 years ago
  • Add a very small description
  • Property svn:executable set to *
File size: 9.0 KB
Line 
1#!/usr/bin/env perl
2#
3# 2018/01/17 Gabriel Moreau
4#
5# apt-get install libyaml-syck-perl libtemplate-perl
6
7use strict;
8use warnings;
9
10use File::Copy qw{copy};   
11use YAML::Syck;
12use Getopt::Long();
13use Cwd();
14use Template;
15
16
17my ($verbose);
18Getopt::Long::GetOptions(
19   'verbose' => \$verbose,
20   );
21
22
23my %CMD_DB = (
24   'help'            => \&cmd_help,
25   'version'         => \&cmd_version,
26   'check'           => \&cmd_check,
27   'make-link'       => \&cmd_make_link,
28   'make-author'     => \&cmd_make_author,
29   'make-licence'    => \&cmd_make_licence,
30   'make-copyright'  => \&cmd_make_copyright,
31   'list-licence'    => \&cmd_list_licence,
32   );
33
34################################################################
35# main program
36################################################################
37
38my $cmd = shift @ARGV || 'help';
39if (defined $CMD_DB{$cmd}) {
40   $CMD_DB{$cmd}->(@ARGV);
41   }
42else {
43   print {*STDERR} "project-meta: command $cmd not found\n\n";
44   $CMD_DB{'help'}->();
45   exit 1;
46   }
47
48exit;
49
50################################################################
51# subroutine
52################################################################
53
54################################################################
55# command
56################################################################
57
58sub cmd_help {
59   print <<'END';
60project-meta - opendata project metafile manager
61
62 project-meta [-verbose] check
63 project-meta make-link
64 project-meta make-author
65 project-meta make-licence
66 project-meta make-copyright
67 project-meta make-licence
68 project-meta list-licence
69END
70   }
71
72################################################################
73
74sub cmd_version {
75   print "0.01\n";
76   }
77
78################################################################
79
80sub print_ok {
81   my ($key, $test) = @_;
82   
83   printf "%-35s : %s\n", $key, $test ? 'yes' : 'no';
84   }
85
86################################################################
87
88sub cmd_check {
89   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
90
91   my $acronym     = $meta->{'project'}{'acronym'};
92   my $current_dir = Cwd::getcwd();
93   my $dap_folder  = $meta->{'public-dap'}{'dap-folder'};
94
95   print_ok 'project/acronym',                  $acronym =~ m{\d\d\w[\w\d_]+};
96   print_ok 'public-dap/dap-folder',            $dap_folder ne '' and $dap_folder =~ m{^/};
97   print_ok 'dap-folder not match current_dir', $dap_folder !~ m{$current_dir};
98
99   #print YAML::Syck::Dump($meta);
100   }
101
102################################################################
103
104sub addfolder2list {
105   my ($folderdb, $folder) = @_;
106   
107   $folder =~ s{/[^/]+$}{};
108   $folderdb->{$folder}++;
109
110   addfolder2list($folderdb, $folder) if $folder =~ m{/};
111   }
112
113################################################################
114
115sub cmd_make_link {
116   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
117   my $acronym = $meta->{'project'}{'acronym'};
118   my $current_dir = Cwd::getcwd();
119   my $dap_folder = $meta->{'public-dap'}{'dap-folder'};
120
121   # Create a list of the folder
122   my %folders;
123   for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) {
124      addfolder2list(\%folders, $dataset);
125      }
126
127   print "chmod o+rX,o-w $current_dir\n";
128   for my $folder (sort keys %folders) {
129      print "chmod o+rX,o-w $current_dir/$folder\n";
130      print "mkdir -p $dap_folder/$acronym/$folder\n";
131      }
132
133   for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) {
134      my $folder = $dataset =~ s{/[^/]+$}{}r;
135      print "ln --symbolic --target-directory $dap_folder/$acronym/$folder/ $current_dir/$dataset\n";
136
137   print "chmod -R o+rX,o-w $dap_folder/$acronym/\n";
138      }
139   }
140
141################################################################
142
143sub cmd_make_author {
144   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
145
146   my $current_dir = Cwd::getcwd();
147
148   my $acronym    = $meta->{'project'}{'acronym'};
149   my $authors_list = $meta->{'project'}{'authors'};
150
151   if (-f "$current_dir/AUTHORS.txt") {
152      # Test for manual or automatically generated file
153      # Automatically generated file by project-meta
154      my $automatic;
155      open my $fh, '<', "$current_dir/AUTHORS.txt" or die $!;
156      for my $line (<$fh>) {
157         $line =~ m/Automatically generated .* project-meta/i and $automatic++;
158         }
159      close $fh;
160
161      if (not $automatic) {
162         print "Warning: AUTHORS.txt already exists\n";
163         return;
164         }
165
166      print "Warning: update AUTHORS.txt\n";
167      }
168
169   my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d');
170   my $msg_format = '';
171   $tt->process('AUTHORS.tt',
172      {
173         acronym    => $acronym,
174         authorlist => $authors_list,
175      }, \$msg_format) || die $tt->error;
176
177   open my $fh,  '>', "$current_dir/AUTHORS.txt" or die $!;
178   print $fh "$msg_format\n\n";
179   close $fh;
180   }
181
182################################################################
183
184sub cmd_make_licence {
185   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
186
187   my $current_dir = Cwd::getcwd();
188
189   if (-f "$current_dir/LICENCE.txt") {
190      print "Warning: LICENCE.txt already exists\n";
191      return;
192      }
193
194   my $licence = $meta->{'public-dap'}{'data-licence'};
195
196   if (not -f "/usr/share/project-meta/licence.d/$licence.txt") {
197      print "Error: licence $licence doesn't exists in project-meta database\n";
198      exit 1;
199      }
200
201   copy("/usr/share/project-meta/licence.d/$licence.txt", "$current_dir/LICENCE.txt")
202      or die "Error: licence copy failed - $!";
203
204   print "Info: LICENCE.txt file create\n";
205   return;
206   }
207
208################################################################
209
210sub cmd_make_copyright {
211   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
212
213   my $current_dir = Cwd::getcwd();
214
215   if (-f "$current_dir/COPYRIGHT.txt") {
216      # Test for manual or automatically generated file
217      # Automatically generated file by project-meta
218      my $automatic;
219      open my $fh, '<', "$current_dir/COPYRIGHT.txt" or die $!;
220      for my $line (<$fh>) {
221         $line =~ m/Automatically generated .* project-meta/i and $automatic++;
222         }
223      close $fh;
224
225      if (not $automatic) {
226         print "Warning: COPYRIGHT.txt already exists\n";
227         return;
228         }
229
230      print "Warning: update COPYRIGHT.txt\n";
231      }
232
233   my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d');
234   my $msg_format = '';
235   $tt->process('COPYRIGHT.tt',
236      {
237         title       => $meta->{'project'}{'title'},
238         acronym     => $meta->{'project'}{'acronym'},
239         authorlist  => $meta->{'project'}{'authors'},
240         description => $meta->{'project'}{'short-description'},
241         licence     => $meta->{'public-dap'}{'data-licence'},
242         doi         => $meta->{'publication'}{'doi'},
243         urldoi      => $meta->{'publication'}{'url'},
244      }, \$msg_format) || die $tt->error;
245
246   open my $fh,  '>', "$current_dir/COPYRIGHT.txt" or die $!;
247   print $fh "$msg_format\n\n";
248   close $fh;
249   }
250
251################################################################
252
253sub cmd_list_licence {
254   opendir my $dh, '/usr/share/project-meta/licence.d/' or die $!;
255   for my $licence (readdir $dh) {
256      # Keep only file
257      next if not -f "/usr/share/project-meta/licence.d/$licence";
258     
259      # Keep only .txt file
260      next if not $licence =~ m/\.txt$/;
261
262      $licence =~ s/\.txt$//;
263      print "$licence\n";
264      }
265   closedir $dh;
266   }
267
268################################################################
269# documentation
270################################################################
271
272__END__
273
274=head1 NAME
275
276project-meta - opendata project metafile manager
277
278
279=head1 USAGE
280
281 project-meta [-verbose] check
282 project-meta make-link
283 project-meta make-author
284 project-meta make-licence
285 project-meta make-copyright
286 project-meta make-licence
287 project-meta list-licence
288
289=head1 DESCRIPTION
290
291project-meta is a small tool to maintain a set of open data files.
292
293=head1 COMMANDS
294
295Some command are defined in the source code but are not documented here.
296Theses could be not well defined, not finished, not well tested...
297You can read the source code and use them at your own risk
298(like for all the Klask code).
299
300=head2 make-licence
301
302 project-meta make-licence
303
304Copy the licence file from the project-meta licence database at the current folder with the file name: LICENCE.txt.
305
306The licence is defined in the PROJECT-META.yml specification under the key C<public-dap/data-licence>.
307The list of possible licence is given with the command L<list-licence>.
308
309=head2 list-licence
310
311 project-meta list-licence
312
313Give the list of all the open data licence supported by the project-meta licence database.
314
315=head1 AUTHOR
316
317Written by Gabriel Moreau, Grenoble - France
318
319=head1 SPECIAL THANKS
320
321The list of people below did not directly contribute to project-meta's source code
322but provided me with some data, returned bugs
323or helped me in another small task like having new ideas ...
324Maybe I forgot your contribution in recent years,
325please forgive me in advance and send me an e-mail to correct this.
326
327Joel Sommeria, Julien Chauchat, Cyrille Bonamy.
328
329
330=head1 LICENSE AND COPYRIGHT
331
332Licence GNU GPL version 2 or later and Perl equivalent
333
334Copyright (C) 2017-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>.
Note: See TracBrowser for help on using the repository browser.