[150] | 1 | #!/usr/bin/env perl |
---|
| 2 | # |
---|
| 3 | # 2018/01/17 Gabriel Moreau |
---|
[155] | 4 | # |
---|
| 5 | # apt-get install libyaml-syck-perl libtemplate-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, |
---|
[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 | |
---|
| 38 | my $cmd = shift @ARGV || 'help'; |
---|
| 39 | if (defined $CMD_DB{$cmd}) { |
---|
| 40 | $CMD_DB{$cmd}->(@ARGV); |
---|
| 41 | } |
---|
| 42 | else { |
---|
| 43 | print {*STDERR} "project-meta: command $cmd not found\n\n"; |
---|
| 44 | $CMD_DB{'help'}->(); |
---|
| 45 | exit 1; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | exit; |
---|
| 49 | |
---|
| 50 | ################################################################ |
---|
| 51 | # subroutine |
---|
| 52 | ################################################################ |
---|
| 53 | |
---|
| 54 | ################################################################ |
---|
| 55 | # command |
---|
| 56 | ################################################################ |
---|
| 57 | |
---|
| 58 | sub cmd_help { |
---|
| 59 | print <<END; |
---|
| 60 | NAME |
---|
| 61 | $0 - action on project metadata |
---|
| 62 | |
---|
| 63 | SYNOPSIS |
---|
| 64 | $0 [-verbose] check |
---|
| 65 | END |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | ################################################################ |
---|
| 69 | |
---|
| 70 | sub cmd_version { |
---|
| 71 | print "0.01\n"; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | ################################################################ |
---|
| 75 | |
---|
| 76 | sub print_ok { |
---|
| 77 | my ($key, $test) = @_; |
---|
| 78 | |
---|
| 79 | printf "%-35s : %s\n", $key, $test ? 'yes' : 'no'; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | ################################################################ |
---|
| 83 | |
---|
| 84 | sub cmd_check { |
---|
| 85 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 86 | |
---|
| 87 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
[154] | 88 | my $current_dir = Cwd::getcwd(); |
---|
[150] | 89 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
| 90 | |
---|
| 91 | print_ok 'project/acronym', $acronym =~ m{\d\d\w[\w\d_]+}; |
---|
| 92 | print_ok 'public-dap/dap-folder', $dap_folder ne '' and $dap_folder =~ m{^/}; |
---|
| 93 | print_ok 'dap-folder not match current_dir', $dap_folder !~ m{$current_dir}; |
---|
| 94 | |
---|
| 95 | #print YAML::Syck::Dump($meta); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | ################################################################ |
---|
| 99 | |
---|
| 100 | sub addfolder2list { |
---|
| 101 | my ($folderdb, $folder) = @_; |
---|
| 102 | |
---|
| 103 | $folder =~ s{/[^/]+$}{}; |
---|
| 104 | $folderdb->{$folder}++; |
---|
| 105 | |
---|
| 106 | addfolder2list($folderdb, $folder) if $folder =~ m{/}; |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | ################################################################ |
---|
| 110 | |
---|
| 111 | sub cmd_make_link { |
---|
| 112 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 113 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
[154] | 114 | my $current_dir = Cwd::getcwd(); |
---|
[150] | 115 | my $dap_folder = $meta->{'public-dap'}{'dap-folder'}; |
---|
| 116 | |
---|
| 117 | # Create a list of the folder |
---|
| 118 | my %folders; |
---|
| 119 | for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) { |
---|
| 120 | addfolder2list(\%folders, $dataset); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | print "chmod o+rX,o-w $current_dir\n"; |
---|
| 124 | for my $folder (sort keys %folders) { |
---|
| 125 | print "chmod o+rX,o-w $current_dir/$folder\n"; |
---|
| 126 | print "mkdir -p $dap_folder/$acronym/$folder\n"; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | for my $dataset (@{$meta->{'public-dap'}{'data-set'}}) { |
---|
| 130 | my $folder = $dataset =~ s{/[^/]+$}{}r; |
---|
| 131 | print "ln --symbolic --target-directory $dap_folder/$acronym/$folder/ $current_dir/$dataset\n"; |
---|
| 132 | |
---|
| 133 | print "chmod -R o+rX,o-w $dap_folder/$acronym/\n"; |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | ################################################################ |
---|
| 138 | |
---|
| 139 | sub cmd_make_author { |
---|
| 140 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
[154] | 141 | |
---|
| 142 | my $current_dir = Cwd::getcwd(); |
---|
[155] | 143 | |
---|
[154] | 144 | my $acronym = $meta->{'project'}{'acronym'}; |
---|
| 145 | my $authors_list = $meta->{'project'}{'authors'}; |
---|
[155] | 146 | |
---|
| 147 | if (-f "$current_dir/AUTHORS.txt") { |
---|
[156] | 148 | # Test for manual or automatically generated file |
---|
| 149 | # Automatically generated file by project-meta |
---|
| 150 | my $automatic; |
---|
[157] | 151 | open my $fh, '<', "$current_dir/AUTHORS.txt" or die $!; |
---|
| 152 | for my $line (<$fh>) { |
---|
[156] | 153 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
| 154 | } |
---|
| 155 | close $fh; |
---|
| 156 | |
---|
| 157 | if (not $automatic) { |
---|
| 158 | print "Warning: AUTHORS.txt already exists\n"; |
---|
| 159 | return; |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | print "Warning: update AUTHORS.txt\n"; |
---|
[155] | 163 | } |
---|
| 164 | |
---|
[154] | 165 | my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d'); |
---|
| 166 | my $msg_format = ''; |
---|
| 167 | $tt->process('AUTHORS.tt', |
---|
| 168 | { |
---|
| 169 | acronym => $acronym, |
---|
| 170 | authorlist => $authors_list, |
---|
| 171 | }, \$msg_format) || die $tt->error; |
---|
| 172 | |
---|
[155] | 173 | open my $fh, '>', "$current_dir/AUTHORS.txt" or die $!; |
---|
| 174 | print $fh "$msg_format\n\n"; |
---|
| 175 | close $fh; |
---|
[150] | 176 | } |
---|
| 177 | |
---|
| 178 | ################################################################ |
---|
| 179 | |
---|
| 180 | sub cmd_make_licence { |
---|
| 181 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 182 | |
---|
| 183 | my $current_dir = Cwd::getcwd(); |
---|
| 184 | |
---|
| 185 | if (-f "$current_dir/LICENCE.txt") { |
---|
| 186 | print "Warning: LICENCE.txt already exists\n"; |
---|
| 187 | return; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | my $licence = $meta->{'public-dap'}{'data-licence'}; |
---|
| 191 | |
---|
| 192 | if (not -f "/usr/share/project-meta/licence.d/$licence.txt") { |
---|
| 193 | print "Error: licence $licence doesn't exists in project-meta database\n"; |
---|
| 194 | exit 1; |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | copy("/usr/share/project-meta/licence.d/$licence.txt", "$current_dir/LICENCE.txt") |
---|
| 198 | or die "Error: licence copy failed - $!"; |
---|
[154] | 199 | |
---|
| 200 | print "Info: LICENCE.txt file create\n"; |
---|
[150] | 201 | return; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | ################################################################ |
---|
[158] | 205 | |
---|
| 206 | sub cmd_make_copyright { |
---|
| 207 | my $meta = YAML::Syck::LoadFile("PROJECT-META.yml"); |
---|
| 208 | |
---|
| 209 | my $current_dir = Cwd::getcwd(); |
---|
| 210 | |
---|
| 211 | if (-f "$current_dir/COPYRIGHT.txt") { |
---|
| 212 | # Test for manual or automatically generated file |
---|
| 213 | # Automatically generated file by project-meta |
---|
| 214 | my $automatic; |
---|
| 215 | open my $fh, '<', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
| 216 | for my $line (<$fh>) { |
---|
| 217 | $line =~ m/Automatically generated .* project-meta/i and $automatic++; |
---|
| 218 | } |
---|
| 219 | close $fh; |
---|
| 220 | |
---|
| 221 | if (not $automatic) { |
---|
| 222 | print "Warning: COPYRIGHT.txt already exists\n"; |
---|
| 223 | return; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | print "Warning: update COPYRIGHT.txt\n"; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | my $tt = Template->new(INCLUDE_PATH => '/usr/share/project-meta/template.d'); |
---|
| 230 | my $msg_format = ''; |
---|
| 231 | $tt->process('COPYRIGHT.tt', |
---|
| 232 | { |
---|
| 233 | title => $meta->{'project'}{'title'}, |
---|
| 234 | acronym => $meta->{'project'}{'acronym'}, |
---|
| 235 | authorlist => $meta->{'project'}{'authors'}, |
---|
| 236 | description => $meta->{'project'}{'short-description'}, |
---|
| 237 | licence => $meta->{'public-dap'}{'data-licence'}, |
---|
| 238 | doi => $meta->{'publication'}{'doi'}, |
---|
| 239 | urldoi => $meta->{'publication'}{'url'}, |
---|
| 240 | }, \$msg_format) || die $tt->error; |
---|
| 241 | |
---|
| 242 | open my $fh, '>', "$current_dir/COPYRIGHT.txt" or die $!; |
---|
| 243 | print $fh "$msg_format\n\n"; |
---|
| 244 | close $fh; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | ################################################################ |
---|
[161] | 248 | |
---|
| 249 | sub cmd_list_licence { |
---|
| 250 | opendir my $dh, '/usr/share/project-meta/licence.d/' or die $!; |
---|
| 251 | for my $licence (readdir $dh) { |
---|
[163] | 252 | # Keep only file |
---|
| 253 | next if not -f "/usr/share/project-meta/licence.d/$licence"; |
---|
[162] | 254 | |
---|
| 255 | # Keep only .txt file |
---|
| 256 | next if not $licence =~ m/\.txt$/; |
---|
| 257 | |
---|
[163] | 258 | $licence =~ s/\.txt$//; |
---|
[161] | 259 | print "$licence\n"; |
---|
| 260 | } |
---|
| 261 | closedir $dh; |
---|
| 262 | } |
---|