Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Tutorial] add verbosity to existing perl sys admin script
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
lalebarde
Guru
Guru


Joined: 03 Sep 2006
Posts: 435
Location: France, Haute-Garonne

PostPosted: Mon May 06, 2013 2:10 pm    Post subject: [Tutorial] add verbosity to existing perl sys admin script Reply with quote

Hi all,

EDIT : thanks to the remarks raised, I have rewritten my script in I hope a "readable" maner :wink: .

Just to share a command line to create a companion script to a perl system administration script when you need to know exactly what it does before executing it :

Code:
#!/usr/bin/awk -f

# mvfcs (make verbosity fake companion script) creates a companion script to a perl system administration script when you need to know exactly what it does before executing it. It can be adapted to any scripting language with system like commands. Contrary to mvcs, system commands are not executed. Only the last println in condition NR>2 differs in both scripts.
# More precisely, "system" commands are replaced by "println" commands. println comes from others and enables to print and add a Line-Feed - I have added a line number prefix. Example (from Peter Toth's zjail script) :

# system "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time"; # Tag the snapshot with "hold" to avoid accidental loss

# becomes :

# $lineOfCode = __LINE__ - 8; println "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time  # Tag the snapshot with \"hold\" to avoid accidental loss"

#  __LINE__ - 8 is to takeinto account the printline declaration which add 8 lines, and have the line numbers keep synchronised with the original script. That is the reason why I also choosed to put everything in one line.


# © Laurent Alebarde 2013 - published under the GPL Licence. Not fully tested nor warranted to work for any purpose. Use at your own risk.

# usage : mvfcs <a perl script> > <the companion fake & verbose script>

NR == 2 {
   print "\nmy $lineOfCode = \"\";\nsub println {\n\tprint \"$lineOfCode : \";\n\tlocal $\\ = \"\\n\";\n\tprint @_;\n}\n"}
(NR > 2) && /[^a-zA-Z0-9]system / {
   str=$0;
   sub(/system /, "$lineOfCode = __LINE__ - 8; println ", str);
   com=$0;
   if(sub(/.*system[ \t]*["].*["][ \t]*;[ \t]*#/, "#", com) == 1) {
      # if there is a comment, separate the command (str) from the comment (com)
      sub(/["][ \t]*;[ \t]*#.*/,"",str);
      # escape quotes in comments
      gsub(/["]/,"\\\"",com);
      # finish the rebuild by concatening both and add the quote we suppressed in str
      strcom = str "  " com "\";";
   }
   else strcom = str;
   print strcom "  "; next
}
{print $0}


In many cases, it is not enough, since the script internal logic may block quickly the normal execution flow since the real commands are no executed.

The next command line adds verbosity while the system commands are executed. Here, it brings a log like functionnality :

Code:
#!/usr/bin/awk -f

# mvcs (make verbosity companion script) creates a companion script to a perl system administration script when you need to know exactly what it has done. It can be adapted to any scripting language with system like commands. Contrary to mvfcs, commands are actually executed as in the original script. Only the last println in condition NR>2 differs in both scripts.
# More precisely, "system" commands are preceded by "println" commands. println comes from others and enables to print and add a Line-Feed - I have added a line number prefix. Example (from Peter Toth's zjail script) :

# system "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time"; # Tag the snapshot with "hold" to avoid accidental loss

# becomes :

# $lineOfCode = __LINE__ - 8; println "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time  # Tag the snapshot with \"hold\" to avoid accidental loss";      system "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time"; # Tag the snapshot with "hold" to avoid accidental loss

#  __LINE__ - 8 is to takeinto account the printline declaration which add 8 lines, and have the line numbers keep synchronised with the original script. That is the reason why I also choosed to put everything in one line.


# © Laurent Alebarde 2013 - published under the GPL Licence. Not fully tested nor warranted to work for any purpose. Use at your own risk.

# usage : mvcs <a perl script> > <the companion fake & verbose script>

NR == 2 {
   print "\nmy $lineOfCode = \"\";\nsub println {\n\tprint \"$lineOfCode : \";\n\tlocal $\\ = \"\\n\";\n\tprint @_;\n}\n"}
(NR > 2) && /[^a-zA-Z0-9]system / {
   str=$0;
   sub(/system /, "$lineOfCode = __LINE__ - 8; println ", str);
   com=$0;
   if(sub(/.*system[ \t]*["].*["][ \t]*;[ \t]*#/, "#", com) == 1) {
      # if there is a comment, separate the command (str) from the comment (com)
      sub(/["][ \t]*;[ \t]*#.*/,"",str);
      # escape quotes in comments
      gsub(/["]/,"\\\"",com);
      # finish the rebuild by concatening both and add the quote we suppressed in str
      strcom = str "  " com "\";";
   }
   else strcom = str;
   print strcom "  " $0; next
}
{print $0}


In addition, the script line numbers are added before the copy of the commands, and if the command is followed by a comment, it is added also. Possibly you may need to change the NR condition (where println declaration is inserted).

Of course, it is recommanded to put it in a script or as an alias.

For example :

Code:
        system "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time"; # Tag the snapshot with "hold" to avoid accidental loss


becomes :

Code:
        $lineOfCode = __LINE__ - 8; println "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time  # Tag the snapshot with \"hold\" to avoid accidental loss";      system "$zfs hold -r jail_clone $dataset\@clone_src_for_$opts{cname}_$snap_time"; # Tag the snapshot with "hold" to avoid accidental loss


Enjoy.....


Last edited by lalebarde on Tue May 07, 2013 8:18 am; edited 2 times in total
Back to top
View user's profile Send private message
truc
Advocate
Advocate


Joined: 25 Jul 2005
Posts: 3199

PostPosted: Mon May 06, 2013 7:36 pm    Post subject: Re: [Tutorial] add verbosity to existing perl sys admin scri Reply with quote

lalebarde wrote:
Hi all,

Just to share a command line to create a companion script to a perl system administration script when you need to know exactly what it does before executing it :

Code:
    awk 'NR == 2 {print "\nmy $lineOfCode = \"\";\nsub println {\n\tprint \"$lineOfCode : \";\n\tlocal $\\ = \"\\n\";\n\tprint @_;\n}\n"} (NR > 2) && /[^a-zA-Z0-9]system / {str=$0; sub(/system /, "$lineOfCode = __LINE__ - 8; println ", str); com=$0; sub(/.*system[:blank:]*\".*\"[:blank:]*;[:blank:]*#/, "#", com); sub(/$com/,"",str); sub(/\"[:blank:]*;[:blank:]*/, "", str); gsub(/\"/,"\"\"",com); str = str com "\"; "; print str $0; next} {print $0}' myscript > myscript_verbose_but_don_t_execute_system_commands

Enjoy.....


Seriously? this is unreadable :!: , do you know you can copy&paste multi-line command? And do you know you can put this in a file, say, file.awk and then run
Code:
awk -f file.awk


And you can even make it as a script (shebang -> #!/usr/bin/awk -f)

So please, make it readable if you want to share your, probably very usefull, script! Thank you:)
_________________
The End of the Internet!
Back to top
View user's profile Send private message
lalebarde
Guru
Guru


Joined: 03 Sep 2006
Posts: 435
Location: France, Haute-Garonne

PostPosted: Mon May 06, 2013 10:29 pm    Post subject: Re: [Tutorial] add verbosity to existing perl sys admin scri Reply with quote

lalebarde wrote:
Of course, it is recommanded to put it in a script or as an alias.

That's what I said (sorry for quoting myself) : Some will want it in a awk file, others in a script, others will define it as an alias. It is a question of taste and habit. I use it like it is. But if you want to propose something else, feel free to do it. You are welcome.
Back to top
View user's profile Send private message
John R. Graham
Administrator
Administrator


Joined: 08 Mar 2005
Posts: 7405
Location: Somewhere over Atlanta, Georgia

PostPosted: Mon May 06, 2013 10:43 pm    Post subject: Reply with quote

But you'll lose 95% of your potential audience by presenting something that unreadable. I certainly won't look at it. :roll:

- John
_________________
This space intentionally left blank.
Back to top
View user's profile Send private message
lalebarde
Guru
Guru


Joined: 03 Sep 2006
Posts: 435
Location: France, Haute-Garonne

PostPosted: Tue May 07, 2013 8:20 am    Post subject: Reply with quote

Thanks for your remarks. I agree that sharing with no usability is like the famous french expression : « pisser dans un violon » :wink: .

Done.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum