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

Last change on this file since 170 was 169, checked in by g7moreau, 6 years ago
  • Small bug for file in root folder
  • Property svn:executable set to *
File size: 9.9 KB
RevLine 
[150]1#!/usr/bin/env perl
2#
3# 2018/01/17 Gabriel Moreau
[155]4#
[168]5# apt-get install yamllint libyaml-syck-perl libtemplate-perl
[150]6
7use strict;
8use warnings;
9
10use File::Copy qw{copy};   
11use YAML::Syck;
12use Getopt::Long();
13use Cwd();
[154]14use Template;
[150]15
[154]16
[150]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,
[153]28   'make-author'     => \&cmd_make_author,
29   'make-licence'    => \&cmd_make_licence,
[158]30   'make-copyright'  => \&cmd_make_copyright,
[161]31   'list-licence'    => \&cmd_list_licence,
[150]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 {
[164]59   print <<'END';
60project-meta - opendata project metafile manager
[150]61
[168]62 project-meta check
[164]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
[150]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'};
[154]92   my $current_dir = Cwd::getcwd();
[150]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'};
[154]118   my $current_dir = Cwd::getcwd();
[150]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";
[169]130      print "mkdir -p $dap_folder/$acronym/$folder\n" if -d "$current_dir/$folder";
[150]131      }
132
133   for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) {
[169]134      if ($dataset =~ m{/}) {
135         # Folder case
136         my $folder = $dataset =~ s{/[^/]+$}{}r;
137         print "ln --symbolic --target-directory $dap_folder/$acronym/$folder/ $current_dir/$dataset\n";
138         }
139      else {
140         # File case
141         print "ln --symbolic --target-directory $dap_folder/$acronym/ $current_dir/$dataset\n";
142         }
[150]143
144   print "chmod -R o+rX,o-w $dap_folder/$acronym/\n";
145      }
146   }
147
148################################################################
149
150sub cmd_make_author {
151   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
[154]152
153   my $current_dir = Cwd::getcwd();
[155]154
[154]155   my $acronym    = $meta->{'project'}{'acronym'};
156   my $authors_list = $meta->{'project'}{'authors'};
[155]157
158   if (-f "$current_dir/AUTHORS.txt") {
[156]159      # Test for manual or automatically generated file
160      # Automatically generated file by project-meta
161      my $automatic;
[157]162      open my $fh, '<', "$current_dir/AUTHORS.txt" or die $!;
163      for my $line (<$fh>) {
[156]164         $line =~ m/Automatically generated .* project-meta/i and $automatic++;
165         }
166      close $fh;
167
168      if (not $automatic) {
169         print "Warning: AUTHORS.txt already exists\n";
170         return;
171         }
172
173      print "Warning: update AUTHORS.txt\n";
[155]174      }
175
[154]176   my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d');
177   my $msg_format = '';
178   $tt->process('AUTHORS.tt',
179      {
180         acronym    => $acronym,
181         authorlist => $authors_list,
182      }, \$msg_format) || die $tt->error;
183
[155]184   open my $fh,  '>', "$current_dir/AUTHORS.txt" or die $!;
185   print $fh "$msg_format\n\n";
186   close $fh;
[150]187   }
188
189################################################################
190
191sub cmd_make_licence {
192   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
193
194   my $current_dir = Cwd::getcwd();
195
196   if (-f "$current_dir/LICENCE.txt") {
197      print "Warning: LICENCE.txt already exists\n";
198      return;
199      }
200
201   my $licence = $meta->{'public-dap'}{'data-licence'};
202
203   if (not -f "/usr/share/project-meta/licence.d/$licence.txt") {
204      print "Error: licence $licence doesn't exists in project-meta database\n";
205      exit 1;
206      }
207
208   copy("/usr/share/project-meta/licence.d/$licence.txt", "$current_dir/LICENCE.txt")
209      or die "Error: licence copy failed - $!";
[154]210
211   print "Info: LICENCE.txt file create\n";
[150]212   return;
213   }
214
215################################################################
[158]216
217sub cmd_make_copyright {
218   my $meta = YAML::Syck::LoadFile("PROJECT-META.yml");
219
220   my $current_dir = Cwd::getcwd();
221
222   if (-f "$current_dir/COPYRIGHT.txt") {
223      # Test for manual or automatically generated file
224      # Automatically generated file by project-meta
225      my $automatic;
226      open my $fh, '<', "$current_dir/COPYRIGHT.txt" or die $!;
227      for my $line (<$fh>) {
228         $line =~ m/Automatically generated .* project-meta/i and $automatic++;
229         }
230      close $fh;
231
232      if (not $automatic) {
233         print "Warning: COPYRIGHT.txt already exists\n";
234         return;
235         }
236
237      print "Warning: update COPYRIGHT.txt\n";
238      }
239
240   my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d');
241   my $msg_format = '';
242   $tt->process('COPYRIGHT.tt',
243      {
244         title       => $meta->{'project'}{'title'},
245         acronym     => $meta->{'project'}{'acronym'},
246         authorlist  => $meta->{'project'}{'authors'},
247         description => $meta->{'project'}{'short-description'},
248         licence     => $meta->{'public-dap'}{'data-licence'},
249         doi         => $meta->{'publication'}{'doi'},
250         urldoi      => $meta->{'publication'}{'url'},
251      }, \$msg_format) || die $tt->error;
252
253   open my $fh,  '>', "$current_dir/COPYRIGHT.txt" or die $!;
254   print $fh "$msg_format\n\n";
255   close $fh;
256   }
257
258################################################################
[161]259
260sub cmd_list_licence {
261   opendir my $dh, '/usr/share/project-meta/licence.d/' or die $!;
262   for my $licence (readdir $dh) {
[163]263      # Keep only file
264      next if not -f "/usr/share/project-meta/licence.d/$licence";
[162]265     
266      # Keep only .txt file
267      next if not $licence =~ m/\.txt$/;
268
[163]269      $licence =~ s/\.txt$//;
[161]270      print "$licence\n";
271      }
272   closedir $dh;
273   }
[164]274
275################################################################
276# documentation
277################################################################
278
279__END__
280
281=head1 NAME
282
283project-meta - opendata project metafile manager
284
285
286=head1 USAGE
287
[168]288 project-meta check
[164]289 project-meta make-link
290 project-meta make-author
291 project-meta make-licence
292 project-meta make-copyright
293 project-meta make-licence
294 project-meta list-licence
295
296=head1 DESCRIPTION
297
[166]298project-meta is a small tool to maintain a set of open data files.
299
[164]300=head1 COMMANDS
301
302Some command are defined in the source code but are not documented here.
303Theses could be not well defined, not finished, not well tested...
304You can read the source code and use them at your own risk
305(like for all the Klask code).
306
[168]307=head2 check
308
309 project-meta check
310
311Check your F<PROJECT-META.yml> has the good key.
312If your metafile is not a valid YAML file,
313you can use C<yamllint> command to check just it's format.
314
[164]315=head2 make-licence
316
317 project-meta make-licence
318
319Copy the licence file from the project-meta licence database at the current folder with the file name: LICENCE.txt.
320
321The licence is defined in the PROJECT-META.yml specification under the key C<public-dap/data-licence>.
322The list of possible licence is given with the command L<list-licence>.
323
324=head2 list-licence
325
326 project-meta list-licence
327
328Give the list of all the open data licence supported by the project-meta licence database.
[167]329At this time the possible licence are:
[164]330
[167]331 license-ouverte-v2.0
332 open-database-license-v1.0
333
334
335=head1 METAFILE SPECIFICATION
336
337Each project must have an open data metafile which describe the project : C<PROJECT-META.yml>.
338The file is in YAML format because this is a human readable style of text file.
339
340You can find in the project-meta software a C<PROJECT-META.sample.yml> example.
341This one is actually the master reference specification!
342
343
[168]344=head1 SEE ALSO
345
346yamllint
347
348
[164]349=head1 AUTHOR
350
[168]351Written by Gabriel Moreau, LEGI UMR5519, CNRS, Grenoble - France
[164]352
353=head1 SPECIAL THANKS
354
355The list of people below did not directly contribute to project-meta's source code
356but provided me with some data, returned bugs
357or helped me in another small task like having new ideas ...
358Maybe I forgot your contribution in recent years,
359please forgive me in advance and send me an e-mail to correct this.
360
[168]361Joel Sommeria, Julien Chauchat, Cyrille Bonamy, Antoine Mathieu.
[164]362
363
364=head1 LICENSE AND COPYRIGHT
365
366Licence GNU GPL version 2 or later and Perl equivalent
367
[165]368Copyright (C) 2017-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>.
Note: See TracBrowser for help on using the repository browser.