| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package OSGi::Osgish::Command::Upload; | ||
| 4 | use strict; | ||
| 5 | use vars qw(@ISA); | ||
| 6 | use Term::ANSIColor qw(:constants); | ||
| 7 | use File::Glob ':glob'; | ||
| 8 | use Data::Dumper; | ||
| 9 | |||
| 10 | @ISA = qw(OSGi::Osgish::Command); | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | OSGi::Osgish::Command::Upload - Upload related commands | ||
| 15 | |||
| 16 | =head1 DESCRIPTION | ||
| 17 | |||
| 18 | =head1 COMMANDS | ||
| 19 | |||
| 20 | =over | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | |||
| 25 | |||
| 26 | sub name { "upload" } | ||
| 27 | |||
| 28 | sub top_commands { | ||
| 29 | my $self = shift; | ||
| 30 | return $self->agent ? $self->commands : {}; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub commands { | ||
| 34 | my $self = shift; | ||
| 35 | my $cmds = $self->sub_commands; | ||
| 36 | return | ||
| 37 | { | ||
| 38 | "upload" => { | ||
| 39 | desc => "Upload operations", | ||
| 40 | proc => $self->push_on_stack("upload",$cmds), | ||
| 41 | cmds => $cmds | ||
| 42 | }, | ||
| 43 | "u" => { alias => "upload", exclude_from_completion => 1}, | ||
| 44 | }; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub sub_commands { | ||
| 48 | my $self = shift; | ||
| 49 | return { | ||
| 50 | "ls" => { | ||
| 51 | desc => "List upload directory", | ||
| 52 | proc => $self->cmd_list, | ||
| 53 | doc => <<EOT | ||
| 54 | List the content of the upload. Installed bundles are marked | ||
| 55 | by color. | ||
| 56 | EOT | ||
| 57 | }, | ||
| 58 | "put" => { | ||
| 59 | desc => "Upload a bundle", | ||
| 60 | proc => $self->cmd_put, | ||
| 61 | args => $self->complete->files_extended, | ||
| 62 | doc => <<EOT | ||
| 63 | Upload a bundle from the local filesystem. | ||
| 64 | EOT | ||
| 65 | }, | ||
| 66 | "rm" => { | ||
| 67 | desc => "Remove a bundle", | ||
| 68 | proc => $self->cmd_delete, | ||
| 69 | args => sub { $self->agent->upload->complete_files_in_upload_dir(@_) }, | ||
| 70 | doc => <<EOT | ||
| 71 | Remove a bundle from the upload directory. Installed | ||
| 72 | bundles need to be uninstalled first. | ||
| 73 | EOT | ||
| 74 | }, | ||
| 75 | "install" => { | ||
| 76 | desc => "Install a bundle", | ||
| 77 | proc => $self->cmd_install, | ||
| 78 | args => sub { $self->agent->upload->complete_files_in_upload_dir(@_) }, | ||
| 79 | doc => <<EOT | ||
| 80 | Install a bundle out of the upload directory. | ||
| 81 | EOT | ||
| 82 | }, | ||
| 83 | "update" => { | ||
| 84 | desc => "Update a bundle", | ||
| 85 | proc => $self->cmd_update, | ||
| 86 | args => sub { $self->agent->upload->complete_files_in_upload_dir(@_) }, | ||
| 87 | doc => <<EOT | ||
| 88 | Update a bundle from its location in the upload | ||
| 89 | directory. | ||
| 90 | EOT | ||
| 91 | }, | ||
| 92 | }; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub cmd_install { | ||
| 96 | my $self = shift; | ||
| 97 | |||
| 98 | return sub { | ||
| 99 | my $file = shift || die "No file given"; | ||
| 100 | my $osgish = $self->osgish; | ||
| 101 | my $agent = $osgish->agent; | ||
| 102 | print "Not connected to a server\n" and return unless $agent; | ||
| 103 | my $list = $agent->upload->list; | ||
| 104 | my $uf = $list->{$file} || die "No file $file uploaded\n"; | ||
| 105 | my $ret = $agent->install_bundle("file://" . $uf->{canonicalPath}); | ||
| 106 | my ($color,$reset) = $osgish->color("bundle_id",RESET); | ||
| 107 | if (!ref($ret)) { | ||
| 108 | print "Installed bundle " . $color . $ret . $reset . ".\n"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub cmd_update { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | return sub { | ||
| 117 | my $file = shift || die "No file given"; | ||
| 118 | my $osgish = $self->osgish; | ||
| 119 | my $agent = $osgish->agent; | ||
| 120 | print "Not connected to a server\n" and return unless $agent; | ||
| 121 | my ($color,$reset) = $osgish->color("bundle_id",RESET); | ||
| 122 | if ($file =~ /^\d+$/) { | ||
| 123 | $agent->update_bundle($file); | ||
| 124 | print "Updated bundle " . ($color . $file . $reset) . ".\n"; | ||
| 125 | } else { | ||
| 126 | my $list = $agent->upload->list; | ||
| 127 | my $installed = $self->uploaded_installed_bundles($list); | ||
| 128 | my $b_id = $installed->{$file} || die "No bundle $file installed.\n"; | ||
| 129 | $agent->update_bundle($b_id); | ||
| 130 | print "Updated bundle $file (" . ($color . $b_id . $reset) . ").\n"; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | # ========================================================================================= | ||
| 136 | sub cmd_list { | ||
| 137 | my $self = shift; | ||
| 138 | return sub { | ||
| 139 | my $osgish = $self->osgish; | ||
| 140 | my $osgi = $osgish->agent; | ||
| 141 | print "Not connected to a server\n" and return unless $osgi; | ||
| 142 | my $list = $osgi->upload->list; | ||
| 143 | #print Dumper($list); | ||
| 144 | my $installed = $self->uploaded_installed_bundles($list); | ||
| 145 | for my $file (sort keys %$list) { | ||
| 146 | my $time = $list->{$file}->{modified} / 1000; | ||
| 147 | my $date = $self->format_date($time); | ||
| 148 | if ($installed->{$file}) { | ||
| 149 | my ($bundle_id,$color_start,$color_end) = | ||
| 150 | ($installed->{$file},$osgish->color("upload_installed",RESET)); | ||
| 151 | printf "%s%3.3s %10.10s %12.12s %s%s\n",$color_start,$bundle_id,$list->{$file}->{length},$date,$file,$color_end; | ||
| 152 | } else { | ||
| 153 | my ($color_start,$color_end) = | ||
| 154 | ($osgish->color("upload_uninstalled",RESET)); | ||
| 155 | printf " %10.10s %12.12s %s%s%s\n",$list->{$file}->{length},$date,$color_start,$file,$color_end; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | } | ||
| 159 | } | ||
| 160 | |||
| 161 | sub cmd_put { | ||
| 162 | my $self = shift; | ||
| 163 | return sub { | ||
| 164 | my $file = shift || die "No file given"; | ||
| 165 | my $osgi = $self->agent; | ||
| 166 | my @files = bsd_glob($file, GLOB_TILDE | GLOB_ERR); | ||
| 167 | for my $f (@files) { | ||
| 168 | if (-f $f && -s $f) { | ||
| 169 | $osgi->upload->upload($f,progress_bar => 1); | ||
| 170 | print "Uploaded $f\n"; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | $osgi->upload->cache_update; | ||
| 174 | } | ||
| 175 | } | ||
| 176 | |||
| 177 | sub cmd_delete { | ||
| 178 | my $self = shift; | ||
| 179 | return sub { | ||
| 180 | my $osgish = $self->osgish; | ||
| 181 | my $osgi = $osgish->agent; | ||
| 182 | my $file = shift || die "No file given"; | ||
| 183 | die "Filepath must not be absolute" if $file =~ /^\//; | ||
| 184 | my @files; | ||
| 185 | my $list = $osgi->upload->list; | ||
| 186 | my $installed = $self->uploaded_installed_bundles($list); | ||
| 187 | if ($file =~ /\*/) { | ||
| 188 | # It's a glob, we need to expand it and do multiple removes | ||
| 189 | my $pattern = $file; | ||
| 190 | $pattern =~ s/\./\\./g; | ||
| 191 | $pattern =~ s/\*/.*/g; | ||
| 192 | $pattern =~ s/\?/./g; | ||
| 193 | @files = grep { /^$pattern$/ } sort keys %$list; | ||
| 194 | } else { | ||
| 195 | @files = ( $file ); | ||
| 196 | } | ||
| 197 | for my $f (@files) { | ||
| 198 | if ($installed->{$f}) { | ||
| 199 | print "$f is still installed as bundle. Uninstall it first\n"; | ||
| 200 | next; | ||
| 201 | } | ||
| 202 | my $error = $osgi->upload->remove($f); | ||
| 203 | print $error ? "rm: $error\n" : "Removed $f.\n"; | ||
| 204 | } | ||
| 205 | $osgi->upload->cache_update; | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 209 | sub uploaded_installed_bundles { | ||
| 210 | my $self = shift; | ||
| 211 | my $list = shift; | ||
| 212 | my $osgi = $self->agent; | ||
| 213 | my $bundles = $osgi->bundles(); | ||
| 214 | my %locations = map { "file://" . $list->{$_}->{canonicalPath} => $_} keys %$list; | ||
| 215 | #print Dumper(\%locations); | ||
| 216 | my %installed; | ||
| 217 | for my $b (values %$bundles) { | ||
| 218 | my $file = $locations{$b->{Location}}; | ||
| 219 | $installed{$file} = $b->{Identifier} if $file; | ||
| 220 | } | ||
| 221 | return \%installed; | ||
| 222 | } | ||
| 223 | |||
| 224 | =head1 LICENSE | ||
| 225 | |||
| 226 | This file is part of osgish. | ||
| 227 | |||
| 228 | Osgish is free software: you can redistribute it and/or modify | ||
| 229 | it under the terms of the GNU General Public License as published by | ||
| 230 | the Free Software Foundation, either version 2 of the License, or | ||
| 231 | (at your option) any later version. | ||
| 232 | |||
| 233 | osgish is distributed in the hope that it will be useful, | ||
| 234 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 235 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 236 | GNU General Public License for more details. | ||
| 237 | |||
| 238 | You should have received a copy of the GNU General Public License | ||
| 239 | along with osgish. If not, see <http://www.gnu.org/licenses/>. | ||
| 240 | |||
| 241 | A commercial license is available as well. Please contact roland@cpan.org for | ||
| 242 | further details. | ||
| 243 | |||
| 244 | =head1 PROFESSIONAL SERVICES | ||
| 245 | |||
| 246 | Just in case you need professional support for this module (or JMX or OSGi in | ||
| 247 | general), you might want to have a look at www.consol.com Contact | ||
| 248 | roland.huss@consol.de for further information (or use the contact form at | ||
| 249 | http://www.consol.com/contact/) | ||
| 250 | |||
| 251 | =head1 AUTHOR | ||
| 252 | |||
| 253 | roland@cpan.org | ||
| 254 | |||
| 255 | =cut | ||
| 256 | |||
| 257 | 1; | ||
| 258 |