[150] | 1 | #!/usr/bin/env perl |
---|
| 2 | # |
---|
| 3 | # 2018/01/17 Gabriel Moreau |
---|
[155] | 4 | # |
---|
[179] | 5 | # apt-get install yamllint libyaml-syck-perl libtemplate-perl libarchive-zip-perl |
---|
[150] | 6 | |
---|
| 7 | use strict; |
---|
| 8 | use warnings; |
---|
| 9 | |
---|
| 10 | use File::Copy qw{copy}; |
---|
| 11 | use YAML::Syck; |
---|
| 12 | use Getopt::Long(); |
---|
| 13 | use Cwd(); |
---|
[154] | 14 | use Template; |
---|
[150] | 15 | |
---|
[154] | 16 | |
---|
[150] | 17 | my ($verbose); |
---|
| 18 | Getopt::Long::GetOptions( |
---|
| 19 | 'verbose' => \$verbose, |
---|
| 20 | ); |
---|
| 21 | |
---|
| 22 | |
---|
| 23 | my %CMD_DB = ( |
---|
| 24 | 'help' => \&cmd_help, |
---|
| 25 | 'version' => \&cmd_version, |
---|
| 26 | 'check' => \&cmd_check, |
---|
| 27 | 'make-link' => \&cmd_make_link, |
---|
[178] | 28 | 'make-zip' => \&cmd_make_zip, |
---|
[153] | 29 | 'make-author' => \&cmd_make_author, |
---|
| 30 | 'make-licence' => \&cmd_make_licence, |
---|
[158] | 31 | 'make-copyright' => \&cmd_make_copyright, |
---|
[161] | 32 | 'list-licence' => \&cmd_list_licence, |
---|
[150] | 33 | ); |
---|
| 34 | |
---|
| 35 | ################################################################ |
---|
| 36 | # main program |
---|
| 37 | ################################################################ |
---|
| 38 | |
---|
| 39 | my $cmd = shift @ARGV || 'help'; |
---|
| 40 | if (defined $CMD_DB{$cmd}) { |
---|
| 41 | $CMD_DB{$cmd}->(@ARGV); |
---|
| 42 | } |
---|
| 43 | else { |
---|
| 44 | print {*STDERR} "project-meta: command $cmd not found\n\n"; |
---|
| 45 | $CMD_DB{'help'}->(); |
---|
| 46 | exit 1; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | exit; |
---|
| 50 | |
---|
| 51 | ################################################################ |
---|
| 52 | # subroutine |
---|
| 53 | ################################################################ |
---|
| 54 | |
---|
| 55 | ################################################################ |
---|
| 56 | # command |
---|
| 57 | ################################################################ |
---|
| 58 | |
---|
| 59 | sub cmd_help { |
---|
[164] | 60 | print <<'END'; |
---|
| 61 | project-meta - opendata project metafile manager |
---|
[150] | 62 | |
---|
[177] | 63 | project-meta help |
---|
| 64 | project-meta version |
---|
[168] | 65 | project-meta check |
---|
[164] | 66 | project-meta make-link |
---|
[186] | 67 | project-meta make-zip |
---|
[164] | 68 | project-meta make-author |
---|
| 69 | project-meta make-licence |
---|
| 70 | project-meta make-copyright |
---|
| 71 | project-meta make-licence |
---|
| 72 | project-meta list-licence |
---|
[150] | 73 | END |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | ################################################################ |
---|
| 77 | |
---|
| 78 | sub cmd_version { |
---|
[177] | 79 | print "0.0.2\n"; |
---|
[150] | 80 | } |
---|
| 81 | |
---|
| 82 | ################################################################ |
---|
| 83 | |
---|
| 84 | sub print_ok { |
---|
| 85 | my ($key, $test) = @_; |
---|
| 86 | |
---|
| 87 | printf "%-35s : %s\n", $key, $test ? 'yes' : 'no'; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | ################################################################ |
---|
| 91 | |
---|
| 92 | sub cmd_check { |
---|
| 93 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 94 | |
---|
| 95 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
[154] | 96 | my $current_dir = Cwd::getcwd(); |
---|
[150] | 97 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
| 98 | |
---|
| 99 | print_ok 'project/acronym', $acronym =~ m{\d\d\w[\w\d_]+}; |
---|
| 100 | print_ok 'public-dap/dap-folder', $dap_folder ne '' and $dap_folder =~ m{^/}; |
---|
| 101 | print_ok 'dap-folder not match current_dir', $dap_folder !~ m{$current_dir}; |
---|
| 102 | |
---|
| 103 | #print YAML::Syck::Dump($meta); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | ################################################################ |
---|
| 107 | |
---|
| 108 | sub addfolder2list { |
---|
| 109 | my ($folderdb, $folder) = @_; |
---|
| 110 | |
---|
| 111 | $folder =~ s{/[^/]+$}{}; |
---|
[171] | 112 | |
---|
| 113 | return if $folder !~ m{/}; |
---|
| 114 | |
---|
[150] | 115 | $folderdb->{$folder}++; |
---|
[171] | 116 | return addfolder2list($folderdb, $folder); |
---|
[150] | 117 | } |
---|
| 118 | |
---|
| 119 | ################################################################ |
---|
| 120 | |
---|
| 121 | sub cmd_make_link { |
---|
| 122 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
[154] | 123 | my $current_dir = Cwd::getcwd(); |
---|
[175] | 124 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
| 125 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
| 126 | my $data_set = $meta->{'public-dap'}{'data-set'}; |
---|
[150] | 127 | |
---|
[175] | 128 | push @{$data_set}, 'AUTHORS.txt', 'COPYRIGHT.txt', 'LICENCE.txt'; |
---|
[174] | 129 | { |
---|
| 130 | # Remove doublon |
---|
| 131 | my %seen = (); |
---|
[175] | 132 | @{$data_set} = grep { ! $seen{$_}++ } @{$data_set}; |
---|
[174] | 133 | } |
---|
| 134 | |
---|
[150] | 135 | # Create a list of the folder |
---|
| 136 | my %folders; |
---|
[175] | 137 | for my $dataset (@{$data_set}) { |
---|
[150] | 138 | addfolder2list(\%folders, $dataset); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | print "chmod o+rX,o-w $current_dir\n"; |
---|
[172] | 142 | print "mkdir -p $dap_folder/$acronym\n" if not -d "$dap_folder/$acronym"; |
---|
[150] | 143 | for my $folder (sort keys %folders) { |
---|
| 144 | print "chmod o+rX,o-w $current_dir/$folder\n"; |
---|
[169] | 145 | print "mkdir -p $dap_folder/$acronym/$folder\n" if -d "$current_dir/$folder"; |
---|
[150] | 146 | } |
---|
| 147 | |
---|
[175] | 148 | for my $dataset (@{$data_set}) { |
---|
[169] | 149 | if ($dataset =~ m{/}) { |
---|
| 150 | # Folder case |
---|
| 151 | my $folder = $dataset =~ s{/[^/]+$}{}r; |
---|
| 152 | print "ln --symbolic --target-directory $dap_folder/$acronym/$folder/ $current_dir/$dataset\n"; |
---|
| 153 | } |
---|
| 154 | else { |
---|
| 155 | # File case |
---|
| 156 | print "ln --symbolic --target-directory $dap_folder/$acronym/ $current_dir/$dataset\n"; |
---|
| 157 | } |
---|
[150] | 158 | |
---|
[171] | 159 | } |
---|
[150] | 160 | print "chmod -R o+rX,o-w $dap_folder/$acronym/\n"; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | ################################################################ |
---|
| 164 | |
---|
[178] | 165 | sub cmd_make_zip { |
---|
| 166 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 167 | my $current_dir = Cwd::getcwd(); |
---|
| 168 | my $data_set = $meta->{'public-dap'}{'data-set'}; |
---|
| 169 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
| 170 | |
---|
| 171 | push @{$data_set}, 'AUTHORS.txt', 'COPYRIGHT.txt', 'LICENCE.txt'; |
---|
| 172 | { |
---|
| 173 | # Remove doublon |
---|
| 174 | my %seen = (); |
---|
| 175 | @{$data_set} = grep { ! $seen{$_}++ } @{$data_set}; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | # Create a Zip file |
---|
| 179 | use Archive::Zip qw( :ERROR_CODES :CONSTANTS ); |
---|
| 180 | |
---|
| 181 | my $zip = Archive::Zip->new(); |
---|
| 182 | |
---|
| 183 | for my $dataset (@{$data_set}) { |
---|
| 184 | if (-d $dataset) { |
---|
| 185 | # Folder case |
---|
[182] | 186 | $zip->addTree($dataset, "$acronym/$dataset"); |
---|
[178] | 187 | } |
---|
| 188 | elsif (-f $dataset) { |
---|
| 189 | # File case |
---|
[183] | 190 | $zip->addFile($dataset, "$acronym/$dataset"); |
---|
[178] | 191 | } |
---|
| 192 | else { |
---|
[179] | 193 | # Strange case |
---|
[178] | 194 | print "Error: entry $dataset doesn't exists\n"; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
[181] | 198 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime time; |
---|
[180] | 199 | $year += 1900; |
---|
| 200 | $mon++; |
---|
| 201 | my $date = sprintf '%04i%02i%02i-%02i%02i', $year, $mon, $mday, $hour, $min; |
---|
| 202 | |
---|
[178] | 203 | # Save the Zip file |
---|
[180] | 204 | unless ($zip->writeToFileNamed("$current_dir/$acronym--$date.zip") == AZ_OK) { |
---|
[178] | 205 | die 'Error: zip write error'; |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | ################################################################ |
---|
| 210 | |
---|
[150] | 211 | sub cmd_make_author { |
---|
| 212 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
[154] | 213 | |
---|
| 214 | my $current_dir = Cwd::getcwd(); |
---|
[155] | 215 | |
---|
[154] | 216 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
| 217 | my $authors_list = $meta->{'project'}{'authors'}; |
---|
[155] | 218 | |
---|
| 219 | if (-f "$current_dir/AUTHORS.txt") { |
---|
[156] | 220 | # Test for manual or automatically generated file |
---|
| 221 | # Automatically generated file by project-meta |
---|
| 222 | my $automatic; |
---|
[157] | 223 | open my $fh, '<', "$current_dir/AUTHORS.txt" or die $!; |
---|
| 224 | for my $line (<$fh>) { |
---|
[156] | 225 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
| 226 | } |
---|
| 227 | close $fh; |
---|
| 228 | |
---|
| 229 | if (not $automatic) { |
---|
| 230 | print "Warning: AUTHORS.txt already exists\n"; |
---|
| 231 | return; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | print "Warning: update AUTHORS.txt\n"; |
---|
[155] | 235 | } |
---|
| 236 | |
---|
[154] | 237 | my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d'); |
---|
| 238 | my $msg_format = ''; |
---|
| 239 | $tt->process('AUTHORS.tt', |
---|
| 240 | { |
---|
| 241 | acronym => $acronym, |
---|
| 242 | authorlist => $authors_list, |
---|
| 243 | }, \$msg_format) || die $tt->error; |
---|
| 244 | |
---|
[155] | 245 | open my $fh, '>', "$current_dir/AUTHORS.txt" or die $!; |
---|
| 246 | print $fh "$msg_format\n\n"; |
---|
| 247 | close $fh; |
---|
[150] | 248 | } |
---|
| 249 | |
---|
| 250 | ################################################################ |
---|
| 251 | |
---|
| 252 | sub cmd_make_licence { |
---|
| 253 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 254 | |
---|
| 255 | my $current_dir = Cwd::getcwd(); |
---|
| 256 | |
---|
| 257 | if (-f "$current_dir/LICENCE.txt") { |
---|
| 258 | print "Warning: LICENCE.txt already exists\n"; |
---|
| 259 | return; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | my $licence = $meta->{'public-dap'}{'data-licence'}; |
---|
| 263 | |
---|
| 264 | if (not -f "/usr/share/project-meta/licence.d/$licence.txt") { |
---|
| 265 | print "Error: licence $licence doesn't exists in project-meta database\n"; |
---|
| 266 | exit 1; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | copy("/usr/share/project-meta/licence.d/$licence.txt", "$current_dir/LICENCE.txt") |
---|
| 270 | or die "Error: licence copy failed - $!"; |
---|
[154] | 271 | |
---|
| 272 | print "Info: LICENCE.txt file create\n"; |
---|
[150] | 273 | return; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | ################################################################ |
---|
[158] | 277 | |
---|
| 278 | sub cmd_make_copyright { |
---|
| 279 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 280 | |
---|
| 281 | my $current_dir = Cwd::getcwd(); |
---|
| 282 | |
---|
| 283 | if (-f "$current_dir/COPYRIGHT.txt") { |
---|
| 284 | # Test for manual or automatically generated file |
---|
| 285 | # Automatically generated file by project-meta |
---|
| 286 | my $automatic; |
---|
| 287 | open my $fh, '<', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
| 288 | for my $line (<$fh>) { |
---|
| 289 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
| 290 | } |
---|
| 291 | close $fh; |
---|
| 292 | |
---|
| 293 | if (not $automatic) { |
---|
| 294 | print "Warning: COPYRIGHT.txt already exists\n"; |
---|
| 295 | return; |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | print "Warning: update COPYRIGHT.txt\n"; |
---|
| 299 | } |
---|
[171] | 300 | |
---|
[176] | 301 | my $tt = Template->new( |
---|
| 302 | INCLUDE_PATH => '/usr/share/project-meta/template.d', |
---|
| 303 | POST_CHOMP => 1, # Remove space and carriage return after %] |
---|
| 304 | ); |
---|
[158] | 305 | my $msg_format = ''; |
---|
| 306 | $tt->process('COPYRIGHT.tt', |
---|
| 307 | { |
---|
| 308 | title => $meta->{'project'}{'title'}, |
---|
| 309 | acronym => $meta->{'project'}{'acronym'}, |
---|
| 310 | authorlist => $meta->{'project'}{'authors'}, |
---|
| 311 | description => $meta->{'project'}{'short-description'}, |
---|
| 312 | licence => $meta->{'public-dap'}{'data-licence'}, |
---|
| 313 | doi => $meta->{'publication'}{'doi'}, |
---|
| 314 | }, \$msg_format) || die $tt->error; |
---|
| 315 | |
---|
| 316 | open my $fh, '>', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
| 317 | print $fh "$msg_format\n\n"; |
---|
| 318 | close $fh; |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | ################################################################ |
---|
[161] | 322 | |
---|
| 323 | sub cmd_list_licence { |
---|
| 324 | opendir my $dh, '/usr/share/project-meta/licence.d/' or die $!; |
---|
| 325 | for my $licence (readdir $dh) { |
---|
[163] | 326 | # Keep only file |
---|
| 327 | next if not -f "/usr/share/project-meta/licence.d/$licence"; |
---|
[162] | 328 | |
---|
| 329 | # Keep only .txt file |
---|
| 330 | next if not $licence =~ m/\.txt$/; |
---|
| 331 | |
---|
[163] | 332 | $licence =~ s/\.txt$//; |
---|
[161] | 333 | print "$licence\n"; |
---|
| 334 | } |
---|
| 335 | closedir $dh; |
---|
| 336 | } |
---|
[164] | 337 | |
---|
| 338 | ################################################################ |
---|
| 339 | # documentation |
---|
| 340 | ################################################################ |
---|
| 341 | |
---|
| 342 | __END__ |
---|
| 343 | |
---|
| 344 | =head1 NAME |
---|
| 345 | |
---|
| 346 | project-meta - opendata project metafile manager |
---|
| 347 | |
---|
| 348 | |
---|
| 349 | =head1 USAGE |
---|
| 350 | |
---|
[177] | 351 | project-meta help |
---|
| 352 | project-meta version |
---|
[168] | 353 | project-meta check |
---|
[164] | 354 | project-meta make-link |
---|
[186] | 355 | project-meta make-zip |
---|
[164] | 356 | project-meta make-author |
---|
| 357 | project-meta make-licence |
---|
| 358 | project-meta make-copyright |
---|
| 359 | project-meta make-licence |
---|
| 360 | project-meta list-licence |
---|
| 361 | |
---|
| 362 | =head1 DESCRIPTION |
---|
| 363 | |
---|
[186] | 364 | C<project-meta> is a small tool to maintain a set of open data files. |
---|
| 365 | In order to help you in this task, C<project-meta> has a set of subcommand |
---|
| 366 | to generated and maintain many files. |
---|
[166] | 367 | |
---|
[186] | 368 | Everything is declare in the metafile F<PROJECT-META.yml>. |
---|
| 369 | This YAML file must exist in your root projet folder. |
---|
| 370 | See L<METAFILE SPECIFICATION>. |
---|
| 371 | |
---|
[164] | 372 | =head1 COMMANDS |
---|
| 373 | |
---|
| 374 | Some command are defined in the source code but are not documented here. |
---|
| 375 | Theses could be not well defined, not finished, not well tested... |
---|
| 376 | You can read the source code and use them at your own risk |
---|
| 377 | (like for all the Klask code). |
---|
| 378 | |
---|
[168] | 379 | =head2 check |
---|
| 380 | |
---|
| 381 | project-meta check |
---|
| 382 | |
---|
| 383 | Check your F<PROJECT-META.yml> has the good key. |
---|
| 384 | If your metafile is not a valid YAML file, |
---|
| 385 | you can use C<yamllint> command to check just it's format. |
---|
| 386 | |
---|
[164] | 387 | =head2 make-licence |
---|
| 388 | |
---|
| 389 | project-meta make-licence |
---|
| 390 | |
---|
| 391 | Copy the licence file from the project-meta licence database at the current folder with the file name: LICENCE.txt. |
---|
| 392 | |
---|
| 393 | The licence is defined in the PROJECT-META.yml specification under the key C<public-dap/data-licence>. |
---|
| 394 | The list of possible licence is given with the command L<list-licence>. |
---|
| 395 | |
---|
| 396 | =head2 list-licence |
---|
| 397 | |
---|
| 398 | project-meta list-licence |
---|
| 399 | |
---|
| 400 | Give the list of all the open data licence supported by the project-meta licence database. |
---|
[167] | 401 | At this time the possible licence are: |
---|
[164] | 402 | |
---|
[167] | 403 | license-ouverte-v2.0 |
---|
| 404 | open-database-license-v1.0 |
---|
| 405 | |
---|
[186] | 406 | Note that these licences are dedicated to open data. |
---|
| 407 | Please do not use open licence that have been written for code or documentation for data. |
---|
[167] | 408 | |
---|
[186] | 409 | |
---|
[167] | 410 | =head1 METAFILE SPECIFICATION |
---|
| 411 | |
---|
| 412 | Each project must have an open data metafile which describe the project : C<PROJECT-META.yml>. |
---|
| 413 | The file is in YAML format because this is a human readable style of text file. |
---|
| 414 | |
---|
| 415 | You can find in the project-meta software a C<PROJECT-META.sample.yml> example. |
---|
| 416 | This one is actually the master reference specification! |
---|
| 417 | |
---|
| 418 | |
---|
[171] | 419 | =head1 BUG |
---|
| 420 | |
---|
| 421 | - make-link: folder/folder |
---|
| 422 | - make-link: support multiple doi |
---|
| 423 | - metafile: remove prop publication/url |
---|
| 424 | |
---|
[168] | 425 | =head1 SEE ALSO |
---|
| 426 | |
---|
| 427 | yamllint |
---|
| 428 | |
---|
| 429 | |
---|
[164] | 430 | =head1 AUTHOR |
---|
| 431 | |
---|
[168] | 432 | Written by Gabriel Moreau, LEGI UMR5519, CNRS, Grenoble - France |
---|
[164] | 433 | |
---|
| 434 | =head1 SPECIAL THANKS |
---|
| 435 | |
---|
| 436 | The list of people below did not directly contribute to project-meta's source code |
---|
| 437 | but provided me with some data, returned bugs |
---|
| 438 | or helped me in another small task like having new ideas ... |
---|
| 439 | Maybe I forgot your contribution in recent years, |
---|
| 440 | please forgive me in advance and send me an e-mail to correct this. |
---|
| 441 | |
---|
[168] | 442 | Joel Sommeria, Julien Chauchat, Cyrille Bonamy, Antoine Mathieu. |
---|
[164] | 443 | |
---|
| 444 | |
---|
| 445 | =head1 LICENSE AND COPYRIGHT |
---|
| 446 | |
---|
| 447 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
| 448 | |
---|
[165] | 449 | Copyright (C) 2017-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|