Skip to content

Instantly share code, notes, and snippets.

@somian
Last active August 29, 2015 14:06
Show Gist options
  • Save somian/880137e593554f76b0df to your computer and use it in GitHub Desktop.
Save somian/880137e593554f76b0df to your computer and use it in GitHub Desktop.
LilMonstaNumber12 for GNU/Linux X11 uses X clpbd to find URLs of CPAN pkgdists and saves them in a hierarchical fs loc (creating dirs where needed).
#!/bin/dash
#--+++-- cpan-grabdist.sh
# NODBG=ON # comment out after debugging
#--+++--
export HO_ME=$(echo ~)
TO_SUDO_OR_NOT=""
BEST=$(echo ~Public/Downloads)
TARGET=${BEST:+"$BEST/CPAN-SRC"}
writeOK ()
{
export CHK=$1
python - <<"EOPY"
import os # os.W_OK | os.X_OK
import grp
import subprocess
fqp = os.getenv("CHK")
whobool = os.access(fqp, os.W_OK | os.X_OK)
hasitt = grp.getgrnam("adm").gr_gid
if whobool:
print "0",
exit
else:
if subprocess.Popen(["/usr/bin/sudo", "/bin/chown", "root:adm", fqp]):
print "0",
exit
else: print 1,
EOPY
}
xcb_ ()
{
for NU in 0 1 2 3 4 5 6; do
$1 2>/dev/null \
-p $NU |perl -an \
-e '$l=qr/(?:<[[:alpha:]])?([hf]t{1,2}p:)/;' \
-e 'print for map{/$l/ ? "\n\t$_" : $_} $_=~m{$l(.+)}'
done
}
smartfetch ()
{
REQU=$1
SUDI=$2 ; export SUDI=${SUDI:=$TARGET}
printf \
'Fetching unless exists (no-clobber): %s\n' \
"${REQU:=http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz}"
# "Permission denied": the trouble is that wget is not using the adm group to
# save the file?
wget --no-clobber --no-verbose \
-O $(echo -n "$REQU" |$TO_SUDO_OR_NOT perl -w \
-MList::MoreUtils=after,lastidx,all,notall \
-MPath::Class=dir -MFile::Path=make_path -MData::Dumper=Dumper -MFile::Spec \
-F/ -an \
-e 'die "No Dest" unless $ENV{"SUDI"}; my($c,$z,@o); my $g=[q(NO DATA),q()];' \
-e 'my $ht=($ENV{"SUDI"}); my $fpopt={verbose=>0,mode=>0771,errors=>\$g};' \
-e 'my $pard = "$ht"; my @h=q[]; push @h,split "/"=> $pard;' \
-e ' printf STDERR qq[%s: putting under parent %s and %s exists already\n\n]' \
-e ' , $^X, join("/",@h[1..$#h]), (-d $pard ? "all" : "not all"); ' \
-e 'my @slf=(6,7,$#F -1); my @e=@F[@slf]; my $fbn= $F[$#F];'\
-e 'my @wuri=(@h,@F[@slf]); my $pco=dir(@wuri); my $ffile = $pco->file($F[$#F]); my @dl=$pco->dir_list;'\
-e 'my @fret = after {$_ eq $h[$#h]} (@h,@e); # flawed' \
-e ' printf STDERR "%s: will make sure that path exists: %s + %s\n", $^X,dir(@h),dir(@fret);' \
-e ' print STDERR "Created something?: \n", do{@o=make_path($pco,$fpopt); join qq[\n],@o};' \
-e 'if ( (!ref($g) and @$g) ' \
-e ' or scalar @o' \
-e ' or all{my $so; $c||=q[/]; do {$c=dir($c=>$_);'\
-e ' printf STDERR qq[%s\t%s\n]=>(($so=$c->stat) ? "*":"0")=>"$c"}; $_ eq ""||$c->stat}'\
-e ' @dl' \
-e ' )'\
-e '{' \
-e ' chmod(0775 , $ffile->dir()->stringify);' \
-e ' printf STDERR qq[\n%s\nSaving to %50s\n%s\n%s\n%s\n\n] =>' \
-e ' q[-] x 60, q[-] x 50' \
-e ', " ought to stat here ",' \
-e ' q[-] x 60,$ffile ;' \
-e ' print $ffile->stringify;' \
-e '} else {' \
-e ' printf STDERR "FAILED: did not make dir\n\t%s\n --reason? %s\n",$pco->stringify,Dumper($g);' \
-e ' print q[/tmp/WASHOUT];' \
-e '}') $REQU
}
pythwunk=$(writeOK $TARGET)
if [ "IGNORE" = "IG${NODBG:+NORE}" ] ; then printf '[%s]\n\n' "$pythwunk" ; fi
# Set a default for debugging:
TITI='http://search.cpan.org/CPAN/authors/id/G/GB/GBARR/IO-1.25.tar.gz'
suis=""
if [ x$DISPLAY != x ]
then
case "$(which -a ssh-askpass razor-openssh-askpass)" in
*/ssh-askpass\ * )
export SUDO_ASKPASS=/usr/bin/ssh-askpass
suis="-A "
;;
*/razor-openssh-askpass\ *)
export SUDO_ASKPASS=/usr/bin/razor-openssh-askpass
suis="-A "
;;
esac
# SETUP : must have `xcb' and `pgrep' installed; xcb must be started.
if [ -n ${xcb:=`which xcb`} ] &&
[ $(pgrep -u ${UID:-1000} -c xcb) -gt 0 ]
then
TITI=$(xcb_ $xcb)
IFS=$'\t\n' printf >&2 '%-14s caught %s\n' "${0%%*/}" "$TITI"
elif [ -n ${xsel:=`which xsel`} ]
then
TITI=$(xsel -b -o 2>/dev/null)
if test 0 -eq \
$(env T=$TITI OUTERS=$0 perl \
-e '$_=$ENV{T}; unless (m{^[hf]t?tps?:\S+$}) { printf STDERR ' \
-e 'qq[%-14s: bad non-URI string on X Clipboard:\n%s\n\n]=>$ENV{OUTERS}, $_; print 1 }; print 0')
then
for CRITTER in $TITI; do printf >&2 '%-14s: caught URI %s\n' "${0%%*/}" $CRITTER; done
else printf >&2 ' ...Aborting with nothing to fetch.\n'; exit
fi
fi
fi
export TARGET
if [ -d "$TARGET" -a "$pythwunk" -eq 0 ]
then
printf >&2 '%-14s: GOOD. We can probably write to %s\n' "${0%%*/}" "$TARGET"
export TO_SUDO_OR_NOT="sudo -E ${suis}-g adm -u root"
elif [ -d $(dirname $TARGET) -a $(writeOK $(dirname $TARGET)) -eq 0 ];
then
mkdir -m 2775 $TARGET ||
(printf >&2 'Could not create top directory "%s"!\n' "$TARGET"; exit 1)
export TO_SUDO_OR_NOT="sudo -E ${suis}-g adm -u root"
elif [ -d "$TARGET" -a "$pythwunk" -ne 0 ]
then
TARGET=${XDG_DOWNLOAD_DIR:?"Not defined."} # our own download area.
export TO_SUDO_OR_NOT=""
printf >&2 'OOPS We cannot write to %s, using instead %s\n "%s" looks like: %s.\n\n' \
"$BEST" "$TARGET" "$BEST" "$(stat -c \
"owner %U group %G perms %a" "$BEST")"
fi
#***# "main()"
if [ -n "$TITI" ] && [ -n "$TARGET" ]
then
case x$xcb in
x???* ) for CRITTER in $TITI; do
${NODBG:+echo} smartfetch "$CRITTER" "$TARGET"
done
;;
* ) ${NODBG:+echo} smartfetch "$TITI" "$TARGET"
;;
esac
fi
# Author: somian
# Last modified 28 SEP 2014
# vim7: ft=sh et sw=4:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment