1

I am trying to open a subprocess so that i can open cygwin.bat file from a python script. I need to extract data from ctag parser. To extract this data I am using following command :

 ctags --c-kinds=vf --fields=+SKz -f - Name_of_file

It gives me variables and functions from the file on command line. This data I have to put in a file using python script. How to do this?

Malwinder Singh
  • 6,644
  • 14
  • 65
  • 103
Aabha Geed
  • 39
  • 1
  • 11

2 Answers2

0

I suppose that you just want to execute system command and read its output. Am I right?

If so, you can do

os.popen(r'ctags --c-kinds=vf --fields=+SKz -f - Name_of_file').read()

or even directly redirect output stream to file by appending > output.txt to your command.

If your problem is to setup cygwin environment and execute script, this problem is not related to Python. In this case you can try to create you bat file or use && operator in your command to setup environment first:

os.popen(r'cygwin.bat && ctags --c-kinds=vf --fields=+SKz -f - Name_of_file').read()
George Sovetov
  • 4,942
  • 5
  • 36
  • 57
  • I tried your suggestion. I am able to open batch file but it does not process the command. so that i can get data from ctags – Aabha Geed Sep 06 '14 at 11:15
  • @AabhaGeed, what commands you run before you run command written in question? In general, you should be able to run one command (possibly complex one) from command line with no prior environment setup. In other word, if cygwin require some environment variables, they should be set in same command. Also, ensure that your command works and output to stdout. After you make your command to work in one line, you can read its stdout in Python. Anyway, please, provide all commands you run and their output. – George Sovetov Sep 06 '14 at 11:27
  • I am able to open cygwin window using open as well as with p = subprocess.Popen(filepath , shell=True, stdout = subprocess.PIPE) After doing so If i execute the command : ctags --c-kinds=vf --fields=+SKz -f - _02_Platform/Source/Target/*.h i get desired output on the screen – Aabha Geed Sep 06 '14 at 11:40
  • @AabhaGeed, find out how to run your command from cmd, not cygwin window. Hope, this may help: http://stackoverflow.com/questions/673278/cygwin-run-script-silenty-from-run-command . I guess your command should be `c:\cygwin\bin\run.exe ctags --c-kinds=vf --fields=+SKz -f - Name_of_file` . – George Sovetov Sep 06 '14 at 11:47
  • ctags is a parser for files. this parser is of gcc. I have to get data from the parser. so from the windows environment i open cygwin somewhow. trigger the parser. get required data for processing. Now.. i want to achieve this automatically from windows environment. so i need to trigger cygwin execute command, generate data in cygwin and process this data in windows environment – Aabha Geed Sep 06 '14 at 12:06
  • @AabhaGeed, let's be slightly abstract. You want to tun cygwin command and process its output in Python. Does it matter what ctags do? It is important is that ctags output data you want to capture. Cygwin commands are run from Windows console (not from cygwin window) using `c:\cygwin\bin\run.exe `. In python, `os.popen('c:\cygwin\bin\run.exe ctags --c-kinds=vf --fields=+SKz -f - Name_of_file').read()` returns string with ctags output. – George Sovetov Sep 06 '14 at 12:39
  • by Either of the ways, I am able to open cygwin. But I am not able to execute the command. Can you please help? – Aabha Geed Sep 08 '14 at 07:16
  • @AabhaGeed, please, for each try you have done, include in your question code you execute and errors you get. – George Sovetov Sep 08 '14 at 07:53
  • I could get through! Thanks for your help ! – Aabha Geed Sep 08 '14 at 09:59
  • @AabhaGeed, congratulations. – George Sovetov Sep 08 '14 at 10:22
0

If you are not able to switch in environments, write the command in os.popen or os.system or subprocess.Popen. Open cygwin -> Python Python_File.py to execute the script. This should do.

Aabha Geed
  • 39
  • 1
  • 11