I guess the title sufficiently sums up my question. I have working code to automatically check out a file:
p = Popen(['cleartool', 'co', pathname], stdin = PIPE)
p.communicate('comment for checkout')
I'm wondering how to check if the file is already checked out before executing this. Thanks in advance everyone!
You can parse the output of a cleartool ls -short pathname
If checked out, its version will end with /CHECKEDOUT.
Or you can go ahead, try to check out and test the exit status of the command. But there could be other causes for failure (other than "already checked out")
Related
Currently I'm capturing the logs that are generated while performing performance testing for an app. But the log file that is generated has all the logs of the device as well.
My ask is to filter out the logs that are getting captured with a specific keyword.
My approach is as follows -
call([adb, "logcat", "-c"])
with open(logcat_file_path, 'w') as out:
logcat_process = Popen([adb, "logcat", " | ", "grep", "'facebook'"], stdout=out)
return logcat_process
But there is no luck! Can someone please help me out here
Thanks in Advance!
i tried the following way and it worked for me.
call([adb, "logcat", "-c"])
with open(logcat_file_path, 'w') as out:
logcat_process = Popen([adb, "logcat"], stdout=subprocess.PIPE)
proc1= Popen(["grep" , "facebook"], stdin=logcat_process.stdout, stdout=out)
return proc1
A way cleaner solution would be to
catch the log using adb logcat as you do
read the log file as you do
retrieve the informations you want and write it in another file
This way
your code also works on other operating systems without grep
it's safer because Popen must be replaced by python code directly as possible, and used only when there are no other solutions
I'm working inside a system that has Jython2.5 but I need to be able to call some of Google's apis so I wrote an offline script that I wanted to call from my Jython environment and return to me small pieces of data. Like a JobID or a sheet URL or something from Google.
I've tried a number of things but I always get an error back from Windows, saying that it cannot find the file specified.
Path is done in two ways.
The first way using a string
stringPath = r"C:\GooglePipes\Scripts\filetobq.py C:\GooglePipes\Keys\DEV-BigQueryKey.json nofile C:\GooglePipes\BQ_Downtime\TESTFILE.CSV dataset1 table1"
And the second way, as a sequence (per the docs, using shell=false supply a sequence)
seqPath = [r"C:\GooglePipes\Scripts\filetobq.py",r"C:\GooglePipes\Keys\DEV-BigQueryKey.json","nofile",r"C:\GooglePipes\BQ_Downtime\TESTFILE.CSV","dataset1","table1"]
Called with
data, err = Popen(seqPath, shell=True, stderr=PIPE, stdout=PIPE).communicate()
#Read values back in
print data
print err
Replacing seqPath with stringPath to try it either way.
I've been at this all weekend, every time I run it I get from Windows
The system cannot find the path specified.
from the err print. I've been unable to debug much further than this. I'm not really sure what's happening. When I paste the stringPath variable directly into my computer's command window it executes.
I've also called subprocess.list2cmdline(seqPath) to see what it's outputting. It's giving me a ? in front of the string, but I haven't been able to figure out what that means. I can paste the rest of the string, starting after the question mark into the command window and it executes.
?C:\GooglePipes\Scripts\filetobq.py C:\GooglePipes...
I've tried a number of different combinations of true and false on shell, passing different args into Popen, double slashes, and I have no less than 30 tabs open from stack overflow and other help forums. I just have no idea what to do at this point and any help is appreciated.
Edit
The ? at the start of the sting is actually a NULL character when I did some additional logging. This seems to be the root of my problem. I can't figure out why it shows up, but it was present in my copy pastes. I started manually typing, and I got it working. When I feed the path with my Jython program it is present again.
Ultimately the error was the ?/NULL character.
I went back to the source value where the program was grabbing the path and it was present there. After I hand-re keyed it in, everything started working.
If you copy and paste what I put in the question, you can see the NULL character in the string if you run it through a string->ASCII converter.
>C:
>NULL 67 58
What a bunch of bullsh***.
I have already seen a lot of questions around this.. and a lot of proposed solutions. But none have worked for me so far. So here we go...
A simple python script called test.py:
#!/usr/bin/env python3
from datetime import datetime
print('Im alive')
fn = 'msgs.txt'
with open('/home/username/Documents/code/production/msgs.txt', 'aw') as f:
f.write('%s\n' % datetime.now())
And here the line in the sudo crontab -e file
*/1 * * * * /home/username/Documents/code/production/test.py >> /home/username/outputlog.txt
The log shows that the program runs and executes properly, I have used full paths in specifying the file that Im writing too, stated the job in sudo crontab in case anything goes wrong with the user... and at this point I am lost. I don't know what to change anymore and all others questions that Ihave seen wont help me further.
Anyone else has another idea here?
Short Answer: Change 'aw' file open mode to 'a'
With your current code, you'll get the error
ValueError: must have exactly one of create/read/write/append mode
because you are trying to combine append ('a') and write ('w') modes. Simply using append ('a') mode fixes the problem.
I can see that this is not a Python/Pexpect issue but way two Linux machines communicate with each other.
But will appreciate if someone can help me find a way to handle this.
i issue the command and then expect for the prompt , get the Before and look for what i need(response) and then i move on to next command.
what is happening is that i see that after i issue a command in the before i get the command and then command prompt.
cmd = 'rm /usr/local/file'
self.myobj.sendline(cmd)
match = self.myobj.expect(self.prompts, timeout=timeout)
print self.dut_host.before
print self.dut_host.after
if i do it manually i will be getting something like
MYPROMPT> rm /usr/local/file
rm: cannot remove '/usr/local/file': No such file or directory
But what gets printed is
MYPROMPT> rm /usr/local/file <-- self.dut_host.before
MYPROMPT> <--- self.dut_host.after
and the actual output i will see in the next command and eventually killing my program.
Has anybody faced this issue and can anybody suggest a way i can tackle this.
Thanks in Advance!!
change the code like this:
cmd = 'rm /usr/local/file'
self.myobj.sendline(cmd)
print self.dut_host.before
print self.dut_host.after
match = self.myobj.expect(self.prompts, timeout=timeout)
I have a problem that I can't find the answer.
I use the Python pexpect module to connect by ssh to a child application and I perform commands like "ls", "history" and ...etc. I need help, to do operations like retrieve the sendline output and check if it contains a specific String chaine or other operations of that kind.
Can somebody please help me?
This is a simple example of the code I use:
child = pexpect.spawn('ssh -l karaf -p 8101 localhost')
child.logfile = open("/home/user/python_HR/logs.txt", "w")
child.expect('password:')
child.sendline(mypassword)
child.sendline(command1)
Here I need to perform an "if" which will check if the result of child.sendline(command1) contains the string chaine "test"
I already know how to save all outputs in a log file. Also before and after attributes don't help me.
Thank you in advance.
Since you are hitting the EOF then you can have code like this:
index = pexpect.expect(['test', pexpect.EOF, pexpect.TIMEOUT])
if index == 0:
do_something()
elif index == 1:
print(pexpect.before)
elif index == 1:
print(pexpect.before)
I expect that if you expect the EOF that you will be able to use .before attribute.
Alternatively:
You can execute the command and pipe it's output to a file then download the file and work with it locally.
and if you want you can gain control manually by using pexpect.interact()