abc-master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
reachx_cmd.py
Go to the documentation of this file.
1 # You can use 'from pyabc import *' and then not need the pyabc. prefix everywhere
2 
3 import sys
4 import optparse
5 import subprocess
6 import tempfile
7 import threading
8 import os
9 import os.path
10 from contextlib import contextmanager, nested
11 
12 import pyabc
13 
14 
15 def popen_and_wait_with_timeout(timeout,cmd, *args, **kwargs):
16  """ Wait for a subprocess.Popen object to terminate, or until timeout (in seconds) expires. """
17 
18  p = None
19  t = None
20 
21  try:
22  p = subprocess.Popen(cmd, *args, **kwargs)
23 
24  if timeout <= 0:
25  timeout = None
26 
27  t = threading.Thread(target=lambda: p.communicate())
28  t.start()
29 
30  t.join(timeout)
31 
32  finally:
33 
34  if p is not None and p.poll() is None:
35  p.kill()
36 
37  if t is not None and t.is_alive():
38  t.join()
39 
40  if p is not None:
41  return p.returncode
42 
43  return -1
44 
45 @contextmanager
46 def temp_file_name(suffix=""):
47  file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
48  name = file.name
49  file.close()
50 
51  try:
52  yield name
53  finally:
54  os.unlink(name)
55 
56 def cygpath(path):
57  if sys.platform == "win32":
58  if os.path.isabs(path):
59  drive, tail = os.path.splitdrive(path)
60  drive = drive.lower()
61  tail = tail.split(os.path.sep)
62  return '/cygdrive/%s'%drive[0] + '/'.join(tail)
63  else:
64  path = path.split(os.path.sep)
65  return "/".join(path)
66  return path
67 
68 def run_reachx_cmd(effort, timeout):
69  with nested(temp_file_name(suffix=".aig"), temp_file_name()) as (tmpaig_name, tmplog_name):
70  pyabc.run_command("write %s"%tmpaig_name)
71 
72  cmdline = [
73  'read %s'%cygpath(tmpaig_name),
74  'qua_ffix -effort %d -L %s'%(effort, cygpath(tmplog_name)),
75  'quit'
76  ]
77 
78  cmd = ["jabc", "-c", " ; ".join(cmdline)]
79 
80  rc = popen_and_wait_with_timeout(timeout, cmd, shell=False, stdout=sys.stdout, stderr=sys.stderr)
81 
82  if rc != 0:
83  # jabc failed or stopped. Write a status file to update the status to unknown
84  with open(tmplog_name, "w") as f:
85  f.write('snl_UNK -1 unknown\n')
86  f.write('NULL\n')
87  f.write('NULL\n')
88 
89  pyabc.run_command("read_status %s"%tmplog_name)
90 
91  return rc
92 
93 def reachx_cmd(argv):
94  usage = "usage: %prog [options]"
95 
96  parser = optparse.OptionParser(usage, prog="reachx")
97 
98  parser.add_option("-e", "--effort", dest="effort", type=int, default=0, help="effort level. [default=0, means unlimited]")
99  parser.add_option("-t", "--timeout", dest="timeout", type=int, default=0, help="timeout in seconds [default=0, unlimited]")
100 
101  options, args = parser.parse_args(argv)
102 
103  rc = run_reachx_cmd(options.effort, options.timeout)
104  print "%s command: jabc returned: %d"%(argv[0], rc)
105 
106  return 0
107 
108 pyabc.add_abc_command(reachx_cmd, "Verification", "reachx", 0)
def popen_and_wait_with_timeout
Definition: reachx_cmd.py:15
def reachx_cmd
Definition: reachx_cmd.py:93
def run_reachx_cmd
Definition: reachx_cmd.py:68
def temp_file_name
Definition: reachx_cmd.py:46
def cygpath
Definition: reachx_cmd.py:56