2

I am trying to create a python script that will allow some interface with Cadence Skill (command line interface). I want any output to be directed to the shell. I feel like this should be simple, but I'm not able to get it working yet. With Popen however, I can't see any output on the command line, and I'm not sure that the communicate() is properly sending the command. Here is what i have so far:

import re, array
import sys
from subprocess import call
from subprocess import Popen, PIPE, STDOUT
from threading  import Thread
import os

#SET THESE VARIABLES
LibraryPath = 'path_to_library'
skillPath = 'path_to_cadence'

#Cadence Environment path
cadence_env= 'source /mscad/apps/bin/mscad_bash/cadtools --env cadence'

class cd:
    """Context manager for changing the current working directory"""
    def __init__(self, newPath):
        self.newPath = os.path.expanduser(newPath)

    def __enter__(self):
        self.savedPath = os.getcwd()
        os.chdir(self.newPath)

    def __exit__(self, etype, value, traceback):
        os.chdir(self.savedPath)

# Change to proper Cadence Directory

# Debugging Variables
etype = 0; value = 0; traceback = 0

NewPath = cd(LibraryPath)
NewPath.__enter__()

# Open Cadence Virtuoso in Shell Mode

try:
    from Queue import Queue, Empty
except ImportError:
    from queue import Queue, Empty  # python 3.x

ON_POSIX = 'posix' in sys.builtin_module_names

def enqueue_output(out, queue):
    for line in iter(out.readline, b''):
        queue.put(line)
    out.close()

p = Popen(['/bin/bash', '-i', '-c', 'cadence_env cmos12s0; virtuoso -nograph'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()



# read line without blocking
try:  line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
    print('no output yet')
else: # got line
    print(line)

load_command = "load(\""+skillPath+"\")"
print load_command
p.communicate(input=load_command)
print "Command Sent ..."


NewPath.__exit__(etype, value, traceback)
call(["ls -l"], shell=True)

Thanks in advance for the help.

References

Community
  • 1
  • 1
digitaLink
  • 458
  • 3
  • 17
  • What is specific issue? (describe using words _step by step_) What do you expect to happen, what happens instead? Don't post all the code that you have, [create a minimal but complete code example instead](http://stackoverflow.com/help/mcve) -- use a dummy child Python script instead of `cadence_env` to replicate the issue. I don't see why would you use the code from *"Non-blocking read on a subprocess.PIPE in python"* question? You should learn first how to use `.communicate()` e.g., to use `input` parameter, set `stdin=PIPE` and to see the output in the console, remove `stdout=PIPE`. – jfs Oct 10 '15 at 09:40
  • Thank you, I will try a simpler example of using communicate and then edit my post accordingly. – digitaLink Oct 11 '15 at 00:02

1 Answers1

0

I was making it more complicated than it needed to be for my application; however the main problem I had was I was missing \n when sending my command to the program. Also, removing stdout=PIPE allowed all the output of the program to go directly to the console.

proc = Popen(['/bin/bash', '-i', '-c', 'Command_To_Open_Program'], stdin=PIPE, stderr=STDOUT)
command = "command_to_program\n"
proc.stdin.write(command)
TallTed
  • 9,069
  • 2
  • 22
  • 37
digitaLink
  • 458
  • 3
  • 17