Solved

Python Script that Recives a few arguments and uses a few text files for manipulation

Posted on 2011-03-02
7
322 Views
Last Modified: 2012-06-27
Hi ,
I had a batch file i tried to wrote but cant seem to get it right , i have a system that eventually can run Python script files and im trying to create a script / snippet which does the following

short explanation
it runs a report , and checks for a text file with status (for loop)  , if its completed - it goes to the 2nd loop

recieves 2 arguments (sys.argv[1], and [2])
Runs an executable file (subprocess.popen)
outputs the stdout to a log file with the name sys.argv[1].log
does a loop (for) on a the output txt file - and looks for the 2nd token

i.e - for /f "tokens=2" %n IN ('type %1__runjob.log') DO (thats in Batch)
 
the output of sys.argv[1].log is as such (its already outputted like that , just piped into a file)
Job 1234 Submitted
       ____
     (2nd token)

:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
0
Comment
Question by:m0tek
7 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 35016132
Not sure what your actual code looks like, but within a .bat batch file, you need to double your percent signs (%%) for local variables (%a, %n and %x, etc), like this, for example

for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -s -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)

Open in new window

0
 
LVL 41

Expert Comment

by:HonorGod
ID: 35016766
Are you looking to correct this .bat file, or convert it to Python?
0
 

Author Comment

by:m0tek
ID: 35017851
either is good
i am pasting the current batch (doesnt work)


::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %n IN ('type %1__runjob.log') DO
:checkjob
for /f "tokens=4" %x IN ('arik.exe show job %n ^| findstr "status"') DO
if %x==COMPLETE (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%a.csv "%1" "%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35017917
Can be confusing!  Need to double the percent signs for the "for" loop variables, like this:

::expecting %1 to be the Policy name and %2 the domain
arik.exe run job "Everyone_FC_Shares" "testbatch" > %1__runjob.log
for /f "tokens=2" %%n IN ('type %1__runjob.log') DO 
:checkjob
for /f "tokens=4" %%x IN ('arik.exe show job %%n ^| findstr "status"') DO 
if "%%x"=="COMPLETE" (
set datetime=%date:~-4,4%%date:~-10,2%%date:~-7,2%%time:~-11,2%%time:~-8,2%%time:~-5,2%
 
arik.exe show domain -l %2 > %2_Agentlist.log
 
for /f "skip=6" %%a IN ('type %2_Agentlist.log') do arik.exe view report -sThftic -q c:\%2\%datetime%_%%a.csv "%1" "%%x" network 0
goto done)
 
ELSE  goto checkjob
 
:done
echo done

Open in new window

0
 

Author Comment

by:m0tek
ID: 35025063
still having trouble , can anyone help me convert this to python?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35028389
If no Python takers, I'm quite sure the batch file could be made to work.

Please post an actual input file we can use to test it, along with an example or two of where it fails.

Thanks.
0
 
LVL 15

Accepted Solution

by:
mish33 earned 500 total points
ID: 35117544

import time, os, sys

def main(args):
   if len(args) != 2:
      print "expecting %1 to be the Policy name and %2 the domain"
      return
   policy, domain = args
   log = policy + '__runjob.log'
   os.system('arik.exe run job "Everyone_FC_Shares" "testbatch" > ' + log)
   job = token(2, log)
   while True:
      os.system('arik.exe show job %s ^| findstr "status" > status' % job)
      status = token(4, 'status')
      if status == 'COMPLETE':
         break
   log = domain + '_Agentlist.log'
   os.system('arik.exe show domain -l %s > %s' % (domain, log))
   a = open(log).readlines()[6]
   os.system(r'arik.exe view report -sThftic -q c:\%s\%s_%s.csv "%s" "%s" network 0'
             % (domain, time.strftime('%Y%m%m%H%M%S'), a, policy, status))

def token(n, file):
   return open(file).read().split()[n-1]

if __name__ == '__main__':
   main(sys.argv[1:])

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question