FAQ Search Today's Posts Mark Forums Read
» Video Reviews

» Linux Archive

Linux-archive is a website aiming to archive linux email lists and to make them easily accessible for linux users/developers.


» Sponsor

» Partners

» Sponsor

Go Back   Linux Archive > Gentoo > Gentoo Development

 
 
LinkBack Thread Tools
 
Old 10-13-2010, 06:51 PM
Mike Frysinger
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

here's what i'll commit to eutils.eclass:

# @FUNCTION: path_exists
# @USAGE: [-a|-o] <paths>
# @DESCRIPTION:
# Check if the specified paths exist. Works for all types of paths
# (files/dirs/etc...). The -a and -o flags control the requirements
# of the paths. They correspond to "and" and "or" logic. So the -a
# flag means all the paths must exist while the -o flag means at least
# one of the paths must exist. The default behavior is "and". If no
# paths are specified, then the return value is "false".
path_exists() {
local opt=$1
[[ ${opt} == -[ao] ]] && shift || opt="-a"

# no paths -> return false
# same behavior as: [[ -e "" ]]
[[ $# -eq 0 ]] && return 1

local p r=0
for p in "$@" ; do
[[ -e ${p} ]]
: $(( r += $? ))
done

case ${opt} in
-a) return $(( r != 0 )) ;;
-o) return $(( r == $# )) ;;
esac
}
-mike
 
Old 10-13-2010, 07:20 PM
Amadeusz Żołnowski
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

Excerpts from Mike Frysinger's message of Wed Oct 13 20:51:35 +0200 2010:
> path_exists() {
> local opt=$1
> [[ ${opt} == -[ao] ]] && shift || opt="-a"
>
> # no paths -> return false
> # same behavior as: [[ -e "" ]]
> [[ $# -eq 0 ]] && return 1
>
> local p r=0
> for p in "$@" ; do
> [[ -e ${p} ]]
> : $(( r += $? ))
> done

1) Why check every path in both "and" and "or" cases?

2) Even simpler:
for p; do
[[ -e $p ]]
((r+=$?))
done

>
> case ${opt} in
> -a) return $(( r != 0 )) ;;
> -o) return $(( r == $# )) ;;
> esac
> }
--
Amadeusz Żołnowski

PGP key fpr: C700 CEDE 0C18 212E 49DA 4653 F013 4531 E1DB FAB5
 
Old 10-13-2010, 07:37 PM
Ulrich Mueller
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

>>>>> On Wed, 13 Oct 2010, Mike Frysinger wrote:

> # If no paths are specified, then the return value is "false".

For the "or" case that's fine. But for the "and" case, I would expect
that the function returns true if called with no arguments.

Ulrich
 
Old 10-13-2010, 07:57 PM
Amadeusz Żołnowski
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

Excerpts from Amadeusz Żołnowski's message of Wed Oct 13 21:20:01 +0200 2010:
> Excerpts from Mike Frysinger's message of Wed Oct 13 20:51:35 +0200 2010:
> > path_exists() {
> > local opt=$1
> > [[ ${opt} == -[ao] ]] && shift || opt="-a"
> >
> > # no paths -> return false
> > # same behavior as: [[ -e "" ]]
> > [[ $# -eq 0 ]] && return 1
> >
> > local p r=0
> > for p in "$@" ; do
> > [[ -e ${p} ]]
> > : $(( r += $? ))
> > done
>
> 1) Why check every path in both "and" and "or" cases?
>
> 2) Even simpler:
> for p; do
> [[ -e $p ]]
> ((r+=$?))
> done
>
> >
> > case ${opt} in
> > -a) return $(( r != 0 )) ;;
> > -o) return $(( r == $# )) ;;
> > esac
> > }


And why putting different tasks into one function? My suggestion:

any_paths() {
local f

for f; do
[[ -e $f ]] && return 0
done

return 1
}

all_paths() {
local f

for f; do
[[ -e $f ]] || return 1
done

return 0
}


Isn't it simpler approach? And have benfits over 2in1:

1) More readable in use.
2) More efficient.
3) 1 + 1 < 2 in this case ;-)
--
Amadeusz Żołnowski

PGP key fpr: C700 CEDE 0C18 212E 49DA 4653 F013 4531 E1DB FAB5
 
Old 10-13-2010, 09:45 PM
Mike Frysinger
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

On Wednesday, October 13, 2010 15:37:02 Ulrich Mueller wrote:
> >>>>> On Wed, 13 Oct 2010, Mike Frysinger wrote:
> > # If no paths are specified, then the return value is "false".
>
> For the "or" case that's fine. But for the "and" case, I would expect
> that the function returns true if called with no arguments.

i disagree as the comment says: it's sticking to the bash behavior.

f=
[[ -e ${f} ]] && [[ -e ${f} ]]
[[ -e ${f} ]] || [[ -e ${f} ]]

these both return false. as does this function:

f=
path_exists -a ${f}
path_exists -o ${f}
-mike
 
Old 10-13-2010, 09:46 PM
Mike Frysinger
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

On Wednesday, October 13, 2010 15:57:17 Amadeusz Żołnowski wrote:
> And why putting different tasks into one function?

for the same reason we dont have separate test binaries: test_exist,
test_file, test_dir, etc...

it makes more sense in my mind to combine the functionality.
-mike
 
Old 10-13-2010, 10:13 PM
Amadeusz Żołnowski
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

Excerpts from Mike Frysinger's message of Wed Oct 13 23:46:43 +0200 2010:
> On Wednesday, October 13, 2010 15:57:17 Amadeusz Żołnowski wrote:
> > And why putting different tasks into one function?
>
> for the same reason we dont have separate test binaries: test_exist,
> test_file, test_dir, etc...
>
> it makes more sense in my mind to combine the functionality.
> -mike

So the only argument for having more complicated, less intuitive and
less readable function is the old 'test' program? Please, reconsider my
solution with more reason. Moreover we're using 'test' as '[[ … ]]'
which changes much in readability. Next thing is that '-a' and '-o'
don't strictly conform to 'test' convention.
--
Amadeusz Żołnowski

PGP key fpr: C700 CEDE 0C18 212E 49DA 4653 F013 4531 E1DB FAB5
 
Old 10-13-2010, 10:32 PM
Mike Frysinger
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

On Wednesday, October 13, 2010 18:13:18 Amadeusz Żołnowski wrote:
> Excerpts from Mike Frysinger's message of Wed Oct 13 23:46:43 +0200 2010:
> > On Wednesday, October 13, 2010 15:57:17 Amadeusz Żołnowski wrote:
> > > And why putting different tasks into one function?
> >
> > for the same reason we dont have separate test binaries: test_exist,
> > test_file, test_dir, etc...
> >
> > it makes more sense in my mind to combine the functionality.
>
> So the only argument for having more complicated, less intuitive and
> less readable function is the old 'test' program? Please, reconsider my
> solution with more reason.

we prioritize differently. i prefer unified code with options. you preferred
unrolled duplicated code.

> Moreover we're using 'test' as '[[ … ]]' which changes much in readability.

what are you talking about ? no one is using `test` in their code and if they
are, their code is broken. none of the stuff ive posted is running `test`.
-mike
 
Old 10-13-2010, 11:08 PM
Amadeusz Żołnowski
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

Excerpts from Mike Frysinger's message of Thu Oct 14 00:32:40 +0200 2010:
> On Wednesday, October 13, 2010 18:13:18 Amadeusz Żołnowski wrote:
> > Excerpts from Mike Frysinger's message of Wed Oct 13 23:46:43 +0200 2010:
> > > On Wednesday, October 13, 2010 15:57:17 Amadeusz Żołnowski wrote:
> > > > And why putting different tasks into one function?
> > >
> > > for the same reason we dont have separate test binaries: test_exist,
> > > test_file, test_dir, etc...
> > >
> > > it makes more sense in my mind to combine the functionality.
> >
> > So the only argument for having more complicated, less intuitive and
> > less readable function is the old 'test' program? Please, reconsider my
> > solution with more reason.
>
> we prioritize differently. i prefer unified code with options.

In which part it's unified? As I said it doesn't conform much to 'test'
convention.


> you preferred unrolled duplicated code.

What I'm duplicating? Lines "local f", "for f; do" or "}"? And what's
bad about it while still having less LOC?


> > Moreover we're using 'test' as '[[ … ]]' which changes much in readability.
>
> what are you talking about ? no one is using `test` in their code and if they
> are, their code is broken. none of the stuff ive posted is running `test`.

I've messed up with that paragraph. The point was that "path_exists -a"
is even less readable than "test -e something". As you've said you
wouldn't use "test -e" syntax instead of "[[ -e ]]", but let's skip that
part…
--
Amadeusz Żołnowski

PGP key fpr: C700 CEDE 0C18 212E 49DA 4653 F013 4531 E1DB FAB5
 
Old 10-17-2010, 09:33 PM
Mike Frysinger
 
Default gentoo-x86 commit in net-misc/openvpn: ChangeLog openvpn-2.1.3.ebuild

On Wednesday, October 13, 2010 19:08:55 Amadeusz Żołnowski wrote:
> Excerpts from Mike Frysinger's message of Thu Oct 14 00:32:40 +0200 2010:
> > On Wednesday, October 13, 2010 18:13:18 Amadeusz Żołnowski wrote:
> > > Mike Frysinger's message of Wed Oct 13 23:46:43 +0200 2010:
> > > > On Wednesday, October 13, 2010 15:57:17 Amadeusz Żołnowski wrote:
> > > > > And why putting different tasks into one function?
> > > >
> > > > for the same reason we dont have separate test binaries: test_exist,
> > > > test_file, test_dir, etc...
> > > >
> > > > it makes more sense in my mind to combine the functionality.
> > >
> > > So the only argument for having more complicated, less intuitive and
> > > less readable function is the old 'test' program? Please, reconsider
> > > my solution with more reason.
> >
> > we prioritize differently. i prefer unified code with options.
>
> In which part it's unified?

the file checking & status accumulation. extending my code to add more
options in the future is easier as well.

> As I said it doesn't conform much to 'test' convention.

sure it does

this thread is going nowhere. i believe my proposal is the way to go, and we
arent arguing over anything of too much value (i.e. bike shedding). no one
else has an opinion, so ive gone my route.
-mike
 

Thread Tools




All times are GMT. The time now is 04:49 PM.

VBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright 2007 - 2008, www.linux-archive.org