From: salamond Date: 2012-06-28T12:21:38+09:00 Subject: Re: how about adding method hook --14dae93996ed4de1fa04c37fd4f1 Content-Type: text/plain; charset=ISO-8859-1 sorry to robert. Took me a night to figure out the git code. with method_added and class_eval. nice piece of code. it works too. On Wed, Jun 27, 2012 at 10:19 PM, salamond wrote: > Hi, Henry. > > thanks for the info. > > method missing might be a good option. > > class Process > def run; end > end > > class FileUtils > def mkdir; end > end > > class Agent > def method_missing > if Process.new.respond_to(method) || FileUtiles.response_to(method) > do_as_me > Process.new.send(method) > end > end > end > > sort of like this. what do you think? > > > > > On Wed, Jun 27, 2012 at 6:26 PM, Henry Maddocks wrote: > >> >> On 27/06/2012, at 8:12 PM, salamond wrote: >> >> Hi, guys. >> >> I want An agent on machine, so that it can fake user actions. >> >> ... >> >> My problem is, I have a very long list of actions, like mv, touch, mkdir, >> rm, etc. >> I don't want to add save and restore euid for all methods. >> >> >> First thing I'd do is remove all the duplication, maybe something like >> this... >> >> class Agent >> def initialize(user, dir) >> @user = user >> @dir = dir >> end >> >> def as_me_do >> bak = Process.euid >> yield >> >> ensure # hat tip Robert >> Process.euid = bak >> end >> >> def mkdir(dir) >> as_me_do {`mkdir`} >> end >> >> def create_file(file, content) >> as_me_do {`echo #{content} > #{file}`} >> end >> end >> >> The next step might be more obvious. >> >> I kind of guess here I should use some meta programming skills. here's >> what i tried. >> >> >> Defining methods is going to be tricky, because you don't have consistent >> method signatures and system calls. Maybe a have a hash of methods => >> system calls and method missing? >> >> Also, why back ticks, why not use ruby File methods? >> >> Henry >> >> > --14dae93996ed4de1fa04c37fd4f1 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable sorry to robert.

Took me a night to figure out the git c= ode. with method_added and class_eval.

nice piece = of code.

it works too.


On Wed, Jun 27, 2012 at 10:19 PM, salamond <jarodzz@gmail.com> wrote:
Hi, Henry.

thanks for the info.
<= div>
method missing might be a good option.

class Process
=A0 def run; end
end
class FileUtils
=A0 def mkdir; end
end

class Agent<= /div>
=A0 def method_missing
=A0 =A0 if Process.new.respond_t= o(method) || FileUtiles.response_to(method)
=A0 =A0 =A0 do_as_me<= /div>
=A0 =A0 =A0 =A0 =A0Process.new.send(method)
=A0 =A0 =A0 end
=
=A0 =A0end
end

sort of like this. w= hat do you think?




On Wed, Jun 27, 2012 at 6:26 PM, Henry Maddocks <hmaddocks@me.com> wrote:

On 27/06/2012, at 8:12 PM= , salamond <jarod= zz@gmail.com> wrote:

Hi, guys.

I want An agent on machine, so that = it can fake user actions.

...

My problem is, I have a very long list of actions, like mv,= touch, mkdir, rm, etc.
I don't want to add save and restore euid for all methods.
<= /div>

First thing I'd do is remove all = the duplication, maybe something like this...

class Agent
=A0 def initialize(user, dir)
=A0 =A0 = @user =3D user
=A0 =A0 @dir =3D dir
=A0 end
<= br>
=A0 def as_me_do
=A0 =A0 bak =3D Process.euid=
=A0 =A0 =A0 yield

=A0 ensure # hat tip Robert
=A0 =A0 Process.e= uid =3D bak
=A0 end

=A0 def mkdir(dir)
=A0 =A0 as_me_do {`mkdir`}
=A0 end

=A0 def create_file(file, content)
=A0 =A0 as_me_do {`echo = #{content} > #{file}`}
=A0 end
end

The next step might be more obvious.

I kind of guess here I should use some meta programming skills. h= ere's what i tried.

Defining methods is going to be tricky, because= you don't have consistent method signatures and system calls. Maybe a = have a hash of methods =3D> system calls and method missing?

Also, why back ticks, why not use ruby File methods?

Henry

<= /font>


--14dae93996ed4de1fa04c37fd4f1--