diff options
author | Robert C. Helling <helling@atdotde.de> | 2020-11-28 14:33:20 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2020-12-03 13:26:55 -0800 |
commit | df9a52d857e8d39e9dea88b5eb478c11d42a5881 (patch) | |
tree | d9a51d14b86a92d54f985f97e93c834eff966c9d /scripts | |
parent | 8934d9744a9b80a11061c4b7f73aaa4e85795e76 (diff) | |
download | subsurface-df9a52d857e8d39e9dea88b5eb478c11d42a5881.tar.gz |
downloader: filter possible devices and add mounts, remember last choices
Signed-off-by: Robert C. Helling <helling@atdotde.de>
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/downloader.pl | 134 |
1 files changed, 78 insertions, 56 deletions
diff --git a/scripts/downloader.pl b/scripts/downloader.pl index b15528229..59b5cfd84 100644 --- a/scripts/downloader.pl +++ b/scripts/downloader.pl @@ -26,7 +26,8 @@ print $q->h1("Subsurface"); printf "Reading config file $config_file\n"; open CONF, $config_file || die "Cannot read $config_file:$!"; while (<CONF>) { - if (/^\s*(\w+)\s*=\s*(\w.*)$/) { + s/#.*$//; + if (/^\s*(\w+)\s*=\s*(\S.*)$/) { $conf{$1} = $2; } } @@ -37,7 +38,7 @@ my %dcs; print $q->start_form(); -my $action = $q->param("action"); +my $action = $q->param("action") || "start"; if ($action eq "config") { @@ -62,7 +63,7 @@ if ($action eq "config") { print $q->hidden(-name => "Manufacturer", -default => $q->param("Manufacturer")); print "Select ",$q->param("Manufacturer")," model:"; - print $q->popup_menu("Product", $dcs{$q->param("Manufacturer")}); + print $q->popup_menu(-name => "Product", -values => $dcs{$q->param("Manufacturer")}, -default => $conf{lastProduct}); &next_action("setproduct") } elsif ($action eq "setproduct") { @@ -73,70 +74,36 @@ if ($action eq "config") { print $q->hidden(-name => "Product", -default => $q->param("Product")); opendir DIR, "/dev"; - my @devices = map {"/dev/$_"} (grep {!/^\./} (readdir DIR)); + my @devices = sort {$a cmp $b} map {"/dev/$_"} (grep {/ttyUSB|ttyS|ttyACM|rfcom/} grep {!/^\./} (readdir DIR)); closedir DIR; - print "Select mount point:"; - print $q->popup_menu(-name => "Mount point", -values => \@devices); - &next_action("startdownload"); - -} elsif ($action eq "startdownload") { - - # Do the actual download - - my $repo; - # Does the repo exist? - - if (-d $git_dir) { - - # ... yes, pull latest version - - $repo = Git::Repository->new( work_tree => $git_dir); - print "Pulling latest version from cloud."; - print $q->pre($repo->run("pull")); - } else { - - # ... no, clone it - - my $en_username = $conf{username}; - - # We need to escape the @ in the username to be able to encode it in the URL. - # Note: If we fail, the password gets written to /var/log/apache/error.log, - # Maybe there is a better way to pass the password to git... - - $en_username =~ s/\@/%40/g; - my $git_url = 'https://' . $en_username . ':' . $conf{password} . '@cloud.subsurface-divelog.org//git/' . $conf{username}; - print "Cloning repository"; - print $q->pre(Git::Repository->run( clone => $git_url, $git_dir)); - $repo = Git::Repository->new( work_tree => $git_dir ); + open MOUNTS, "/proc/mounts"; + while (<MOUNTS>) { + my ($device, $mountpoint) = split /\s/; + if ($device =~ /dev/) { + push @devices, $mountpoint; + } } - - # Assemble the command with all arguments + close MOUNTS; - my $command = "$downloader --dc-vendor=" . $q->param('Manufacturer') . - " --dc-product=" . $q->param('Product') . - " --device=" . $q->param("Mount point") . - ' ' . $git_dir . - '/[' . $conf{username} . ']'; - print $q->pre($command); + print $q->popup_menu(-name => "Mount point", -values => \@devices, -default => $conf{lastMountpoint}); + &next_action("startdownload"); - # ... and run it - - print $q->pre(`$command`); +} elsif ($action eq "startdownload") { - # Push back to the cloud - - print "Checkout user branch"; - print $q->pre($repo->run("checkout", $conf{username})); - print "Push changes to cloud"; - print $q->pre($repo->run("push", "origin", $conf{username})); + $conf{lastManufacturer} = $q->param("Manufacturer"); + $conf{lastProduct} = $q->param("Product"); + $conf{lastMountpoint} = $q->param("Mount point"); + &write_conf; + + &run_download; &next_action("start"); } else { # This is the mode we start up in print "Select dive computer manufacturer:"; - print $q->popup_menu("Manufacturer", [sort keys %dcs]); + print $q->popup_menu(-name => "Manufacturer", -values => [sort keys %dcs], -default => $conf{lastManufacturer}); &next_action("setmanufacturer") } @@ -154,11 +121,14 @@ sub load_supported_dcs { } while(<IN>) { last unless /\S/; + my ($manufacturer, $products) = /"([^:]+):\s+([^"]+)"/; - + + next unless defined $products; $products =~ s/\([^\)]*\)//g; my @products = split /,\s*/, $products; $dcs{$manufacturer} = \@products; + } close IN; } @@ -182,3 +152,55 @@ sub next_action { -value => $next); return; } + +sub run_download { + # Do the actual download + + my $repo; + + # Does the repo exist? + + if (-d $git_dir) { + + # ... yes, pull latest version + + $repo = Git::Repository->new( work_tree => $git_dir); + print "Pulling latest version from cloud."; + print $q->pre($repo->run("pull")); + } else { + + # ... no, clone it + + my $en_username = $conf{username}; + + # We need to escape the @ in the username to be able to encode it in the URL. + # Note: If we fail, the password gets written to /var/log/apache/error.log, + # Maybe there is a better way to pass the password to git... + + $en_username =~ s/\@/%40/g; + my $git_url = 'https://' . $en_username . ':' . $conf{password} . '@cloud.subsurface-divelog.org//git/' . $conf{username}; + print "Cloning repository"; + print $q->pre(Git::Repository->run( clone => $git_url, $git_dir)); + $repo = Git::Repository->new( work_tree => $git_dir ); + } + + # Assemble the command with all arguments + + my $command = "$downloader --dc-vendor=" . $q->param('Manufacturer') . + " --dc-product=" . $q->param('Product') . + " --device=" . $q->param("Mount point") . + ' ' . $git_dir . + '/[' . $conf{username} . ']'; + print $q->pre($command); + + # ... and run it + + print $q->pre(`$command`); + + # Push back to the cloud + + print "Checkout user branch"; + print $q->pre($repo->run("checkout", $conf{username})); + print "Push changes to cloud"; + print $q->pre($repo->run("push", "origin", $conf{username})); +} |