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

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