snippets / enki / 

All enki's snippets (3)

  1. upload images to imageshack from console

    Script to upload images to imageshack from console. Require bash and curl.

     1 #!/bin/bash
    2 #
    3 # imageshack_upload.sh
    4 #
    5 # Copyright (c) 2007 by enki <enki@crocobox.org>
    6 #
    7 # This program is free software; you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation; either version 2 of the License, or
    10 # (at your option) any later version.
    11 #
    12 # This program is distributed in the hope that it will be useful,
    13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    15 # GNU General Public License for more details.
    16 #
    17 # You should have received a copy of the GNU General Public License
    18 # along with this program; if not, write to the Free Software
    19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    20 # USA.
    21 #
    22 # Changelog :
    23 # * 2006/10/22 - Updated to support files with space .
    24 # Changes proposed by slubman(http://www.slubman.net)
    25 # * 2006/10/22 - First version
    26
    27 myver='0.2'
    28 CURL=$(which curl)
    29
    30
    31
    32 TMPFILE=$(mktemp /tmp/imageshack.XXXXXXXXXX) || exit 1
    33
    34 cleanup() {
    35 rm -rf $TMPFILE
    36 [ "$1" ] && exit $1
    37 }
    38
    39 usage() {
    40 echo "imageshack_upload $myver"
    41 echo "usage: $0 <root>"
    42 echo
    43 echo "This script allow you to send image to htpp://www.imageshack.us"
    44 echo "in command line"
    45 echo
    46 echo "example: imageshack_upload.sh `pwd`/myimage.png"
    47 echo
    48 }
    49
    50 if [ $# -lt 1 ]; then
    51 usage
    52 rm -rf $TMPFILE
    53 exit 1
    54 fi
    55
    56 if [ "$1" = "-h" -o "$1" = "--help" ]; then
    57 usage
    58 rm -rf $TMPFILE
    59 exit 0
    60 fi
    61
    62
    63 img="$1"
    64
    65 if ! [ -f "$img" ]; then
    66 echo "Error: file don't exist"
    67 exit 1
    68 fi
    69
    70 $CURL -H Expect: -F fileupload="@${img}" -F xml=yes http://www.imageshack.us/index.php > $TMPFILE
    71
    72 echo "Url of image on imageshack:"
    73 echo $(cat $TMPFILE | grep -E "<image_link>(.*)</image_link>" | sed 's|<image_link>\(.*\)</image_link>|\1|')
    74
    75 cleanup
    first posted by enki to shell curl bash imageshack upload ... saved by 6 persons ... 0 comments ... 1 year
  2. parse archlinux pkgbuilds in ruby

    This script use the EnvBash object from this snipett : http://www.friendsnippets.com/snippet/9/

      1 #!/usr/bin/ruby
    2
    3 # Copyright (C) 2006 enki <enki@crocobox.org>
    4
    5 # This program is free software; you can redistribute it
    6 # and/or modify it under the terms of the GNU General
    7 # Public License as published by the Free Software
    8 # Foundation; either version 2 of the License, or (at
    9 # your option) any later version.
    10 #
    11 # This program is distributed in the hope that it will be
    12 # useful, but WITHOUT ANY WARRANTY; without even the
    13 # implied warranty of MERCHANTABILITY or FITNESS FOR A
    14 # PARTICULAR PURPOSE. See the GNU General Public License
    15 # for more details.
    16 #
    17 # You should have received a copy of the GNU General
    18 # Public License along with this program; if not, write
    19 # to the Free Software Foundation, Inc., 59 Temple Place,
    20 # Suite 330, Boston, MA 02111-1307 USA
    21
    22
    23 class MultiRegexp < Regexp
    24 def matches(str)
    25 str.scan(self) do
    26 yield Regexp.last_match
    27 end
    28 end
    29 end
    30
    31
    32 class EnvBash
    33 attr_reader :bash_env
    34
    35 def initialize(source)
    36 bash_script ="\
    37 source #{source}
    38 set
    39 "
    40 @bash_env = `#{bash_script}`
    41 end
    42
    43 def vars
    44 bvars=Hash.new
    45 re=MultiRegexp.new('(\w*)=(.*)$', true)
    46 re.matches(@bash_env) { |i|
    47 if (i[1] != "")
    48 if i[2] =~ /\(([^\)]*)\)/
    49 ta=Array.new
    50 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
    51 re_a.matches($1) { |j|
    52 ta.push(j[1])
    53 }
    54 bvars[i[1]]=ta
    55 elsif
    56 bvars[i[1]]=i[2]
    57 end
    58 end
    59 }
    60 return bvars
    61 end
    62 end
    63
    64 class PKGBUILD
    65 attr_reader :properties, :vars
    66
    67 def initialize(file)
    68 @properties=Hash.new
    69
    70 properties['name']=""
    71 properties['version']=""
    72 properties['rel']=""
    73 properties['desc']=""
    74 properties['depends']=""
    75 properties['url']=""
    76 properties['source']=""
    77
    78
    79 # get all vars
    80 vpkgs=EnvBash.new(file)
    81 vars=vpkgs.vars
    82
    83 properties['name'] = vars['pkgname']
    84 properties['version'] = vars['pkgver']
    85 properties['rel'] = vars['pkgrel']
    86 properties['desc'] = vars['pkgdesc']
    87 properties['depends'] = vars['depends']
    88
    89 url=""
    90 if vars['url'].instance_of?(Array)
    91 vars['url'].each { |u| url +="#{u} " }
    92 else
    93 url = vars['url']
    94 end
    95
    96 properties['url'] = url
    97
    98 source=""
    99 if vars['source'].instance_of?(Array)
    100 vars['source'].each { |s| source +="#{s} " }
    101 else
    102 source = vars['source']
    103 end
    104
    105
    106 properties['source'] = source
    107 end
    108
    109
    110 def name
    111 return properties['name']
    112 end
    113
    114 end
    115
    116
    117 pkg = PKGBUILD.new("/var/abs/kernels/kernel26/PKGBUILD")
    118 pkg.properties.each do |k,v|
    119 puts "#{k}=#{v}"
    120 end
    first posted by enki to ruby archlinux pkgbuild packages parser ... saved by 1 person ... 0 comments ... 1 year
  3. class to parse bash variable from a script

    This class allow you to parse variable in a bash script from ruby . It retourn an array of variables. bash array is also saved as ruby array.

     1 #!/usr/bin/ruby
    2 #
    3 # how to use it :
    4 # ovars=EnvBash.new(file)
    5 # bash_vars=ovars.vars
    6 #
    7
    8
    9 class MultiRegexp < Regexp
    10 def matches(str)
    11 str.scan(self) do
    12 yield Regexp.last_match
    13 end
    14 end
    15 end
    16
    17
    18 class EnvBash
    19 attr_reader :bash_env
    20
    21 def initialize(source)
    22 bash_script ="\
    23 source #{source}
    24 set
    25 "
    26 @bash_env = `#{bash_script}`
    27 end
    28
    29 def vars
    30 bvars=Hash.new
    31 re=MultiRegexp.new('(\w*)=(.*)$', true)
    32 re.matches(@bash_env) { |i|
    33 if (i[1] != "")
    34 if i[2] =~ /\(([^\)]*)\)/
    35 ta=Array.new
    36 re_a=MultiRegexp.new('\[\d*\]=\"([^"]*)\"', true)
    37 re_a.matches($1) { |j|
    38 ta.push(j[1])
    39 }
    40 bvars[i[1]]=ta
    41 elsif
    42 bvars[i[1]]=i[2]
    43 end
    44 end
    45 }
    46 return bvars
    47 end
    48 end
    49
    50 # test on a pkgbuild (archlinux)
    51 bashvars=EnvBash.new("/var/abs/kernels/kernel26/PKGBUILD")
    52
    53 bvars=bashvars.vars
    54 puts "#{bvars['pkgname']}-#{bvars['pkgver']}-#{bvars['pkgrel']}"
    55
    56 if bvars['source'].instance_of?(Array)
    57 puts "sources : "
    58 bvars['source'].each { |src|
    59 puts src
    60 }
    61 end
    first posted by enki to ruby bash variables parser ... saved by 1 person ... 0 comments ... 1 year
showing 10, 25, 50 items per pages

Pages : 1