Commit c610c036 by Baruch Sterin

pyabc: remove python integration from abc, it is moved to a separate extension

parent aa62165a
......@@ -29,8 +29,7 @@ MODULES := \
src/proof/pdr src/proof/abs src/proof/live src/proof/ssc src/proof/int \
src/proof/cec src/proof/dch src/proof/fraig src/proof/fra src/proof/ssw \
src/aig/aig src/aig/saig src/aig/gia src/aig/ioa src/aig/ivy src/aig/hop \
src/aig/miniaig \
src/python
src/aig/miniaig
all: $(PROG)
default: $(PROG)
......
# reachx
python new_abc_commands.py
python -c "import reachx_cmd"
# global parameters
set check # checks intermediate networks
#set checkfio # prints warnings when fanins/fanouts are duplicated
#unset checkread # does not check new networks after reading from file
#set backup # saves backup networks retrived by "undo" and "recall"
#set savesteps 1 # sets the maximum number of backup networks to save
#set progressbar # display the progress bar
# program names for internal calls
set dotwin dot.exe
set dotunix dot
set gsviewwin gsview32.exe
set gsviewunix gv
set siswin sis.exe
set sisunix sis
set mvsiswin mvsis.exe
set mvsisunix mvsis
set capowin MetaPl-Capo10.1-Win32.exe
set capounix MetaPl-Capo10.1
set gnuplotwin wgnuplot.exe
set gnuplotunix gnuplot
# Niklas Een's commands
#load_plugin C:\_projects\abc\lib\bip_win.exe "BIP"
#load_plugin /hd/common/pyabc/bip "BIP"
# standard aliases
alias hi history
alias b balance
alias cg clockgate
alias cl cleanup
alias clp collapse
alias cs care_set
alias esd ext_seq_dcs
alias f fraig
alias fs fraig_sweep
alias fsto fraig_store
alias fres fraig_restore
alias fr fretime
alias ft fraig_trust
alias ic indcut
alias lp lutpack
alias pcon print_cone
alias pd print_dsd
alias pex print_exdc -d
alias pf print_factor
alias pfan print_fanio
alias pg print_gates
alias pl print_level
alias plat print_latch
alias pio print_io
alias pk print_kmap
alias pm print_miter
alias ps print_stats
alias psb print_stats -b
alias psu print_supp
alias psy print_symm
alias pun print_unate
alias q quit
alias r read
alias ra read_aiger
alias r3 retime -M 3
alias r3f retime -M 3 -f
alias r3b retime -M 3 -b
alias ren renode
alias rh read_hie
alias ri read_init
alias rl read_blif
alias rb read_bench
alias ret retime
alias dret dretime
alias rp read_pla
alias rt read_truth
alias rv read_verilog
alias rvl read_verlib
alias rsup read_super mcnc5_old.super
alias rlib read_library
alias rlibc read_library cadence.genlib
alias rty read_liberty
alias rlut read_lut
alias rw rewrite
alias rwz rewrite -z
alias rf refactor
alias rfz refactor -z
alias re restructure
alias rez restructure -z
alias rs resub
alias rsz resub -z
alias sa set autoexec ps
alias scl scleanup
alias sif if -s
alias so source -x
alias st strash
alias sw sweep
alias ssw ssweep
alias tr0 trace_start
alias tr1 trace_check
alias trt "r c.blif; st; tr0; b; tr1"
alias u undo
alias w write
alias wa write_aiger
alias wb write_bench
alias wc write_cnf
alias wh write_hie
alias wl write_blif
alias wp write_pla
alias wv write_verilog
# standard scripts
alias resyn "b; rw; rwz; b; rwz; b"
alias resyn2 "b; rw; rf; b; rw; rwz; b; rfz; rwz; b"
alias resyn2a "b; rw; b; rw; rwz; b; rwz; b"
alias resyn3 "b; rs; rs -K 6; b; rsz; rsz -K 6; b; rsz -K 5; b"
alias compress "b -l; rw -l; rwz -l; b -l; rwz -l; b -l"
alias compress2 "b -l; rw -l; rf -l; b -l; rw -l; rwz -l; b -l; rfz -l; rwz -l; b -l"
alias choice "fraig_store; resyn; fraig_store; resyn2; fraig_store; fraig_restore"
alias choice2 "fraig_store; balance; fraig_store; resyn; fraig_store; resyn2; fraig_store; resyn2; fraig_store; fraig_restore"
alias rwsat "st; rw -l; b -l; rw -l; rf -l"
alias drwsat2 "st; drw; b -l; drw; drf; ifraig -C 20; drw; b -l; drw; drf"
alias share "st; multi -m; fx; resyn2"
alias addinit "read_init; undc; strash; zero"
alias blif2aig "undc; strash; zero"
alias v2p "&vta_gla; &ps; &gla_derive; &put; w 1.aig; pdr -v"
alias g2p "&ps; &gla_derive; &put; w 2.aig; pdr -v"
# resubstitution scripts for the IWLS paper
alias src_rw "st; rw -l; rwz -l; rwz -l"
alias src_rs "st; rs -K 6 -N 2 -l; rs -K 9 -N 2 -l; rs -K 12 -N 2 -l"
alias src_rws "st; rw -l; rs -K 6 -N 2 -l; rwz -l; rs -K 9 -N 2 -l; rwz -l; rs -K 12 -N 2 -l"
alias resyn2rs "b; rs -K 6; rw; rs -K 6 -N 2; rf; rs -K 8; b; rs -K 8 -N 2; rw; rs -K 10; rwz; rs -K 10 -N 2; b; rs -K 12; rfz; rs -K 12 -N 2; rwz; b"
alias compress2rs "b -l; rs -K 6 -l; rw -l; rs -K 6 -N 2 -l; rf -l; rs -K 8 -l; b -l; rs -K 8 -N 2 -l; rw -l; rs -K 10 -l; rwz -l; rs -K 10 -N 2 -l; b -l; rs -K 12 -l; rfz -l; rs -K 12 -N 2 -l; rwz -l; b -l"
# use this script to convert 1-valued and DC-valued flops for an AIG
alias fix_aig "logic; undc; strash; zero"
# use this script to convert 1-valued and DC-valued flops for a logic network coming from BLIF
alias fix_blif "undc; strash; zero"
# lazy man's synthesis
alias recadd3 "st; rec_add3; b; rec_add3; dc2; rec_add3; if -K 8; bidec; st; rec_add3; dc2; rec_add3; if -g -K 6; st; rec_add3"
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/multi_prove_aiger $*"
import sys
import os
import pyabc
import par
import tempfile
import shutil
import redirect
import optparse
from contextlib import contextmanager
def read_cmd(args):
if len(args)==2:
par.read_file_quiet(args[1])
else:
par.read_file()
return 0
pyabc.add_abc_command(read_cmd, "ZPython", "/rf", 0)
def chdir_cmd(args):
os.chdir( args[1] )
return 0
pyabc.add_abc_command(chdir_cmd, "ZPython", "/cd", 0)
def pwd_cmd(args):
print os.getcwd()
return 0
pyabc.add_abc_command(pwd_cmd, "ZPython", "/pwd", 0)
def ls_cmd(args):
os.system("ls " + " ".join(args[1:]))
return 0
pyabc.add_abc_command(ls_cmd, "ZPython", "/ls", 0)
pushd_temp_stack = []
def pushdtemp_cmd(args):
tmpdir = tempfile.mkdtemp()
pushd_temp_stack.append( (os.getcwd(), tmpdir) )
os.chdir(tmpdir)
return 0
pyabc.add_abc_command(pushdtemp_cmd, "ZPython", "/pushdtemp", 0)
def popdtemp_cmd(args):
prev, temp = pushd_temp_stack.pop()
os.chdir(prev)
shutil.rmtree(temp, ignore_errors=True)
return 0
pyabc.add_abc_command(popdtemp_cmd, "ZPython", "/popdtemp", 0)
pushredirect_stack = []
def push_redirect_cmd(args):
fdout = redirect.start_redirect( redirect.null_file, sys.stdout)
pushredirect_stack.append( (sys.stdout, fdout) )
fderr = redirect.start_redirect( redirect.null_file, sys.stderr)
pushredirect_stack.append( (sys.stderr, fderr) )
return 0
pyabc.add_abc_command(push_redirect_cmd, "ZPython", "/pushredirect", 0)
def pop_redirect_cmd(args):
err, fderr = pushredirect_stack.pop()
redirect.end_redirect(err, fderr)
out, fdout = pushredirect_stack.pop()
redirect.end_redirect(out, fdout)
return 0
pyabc.add_abc_command(pop_redirect_cmd, "ZPython", "/popredirect", 0)
def print_aiger_result(args):
status = pyabc.prob_status()
if status==1:
print 0
elif status==0:
print 1
else:
print 2
return 0
pyabc.add_abc_command(print_aiger_result, "ZPython", "/print_aiger_result", 0)
@contextmanager
def replace_report_result(multi):
def report_result(po, result):
print "REPORT RESULT: ", po, result
print >> stdout, "%d"%result
print >> stdout, "b%d"%po
print >> stdout, "."
def report_liveness_result(po, result):
print "REPORT RESULT: ", po, result
print >> stdout, "%d"%result
print >> stdout, "j%d"%po
print >> stdout, "."
def report_bmc_depth(depth):
if not multi:
print "REPORT BMC DEPTH:", depth
print >> stdout, "u%d"%depth
with redirect.save_stdout() as stdout:
old_report_result = par.report_result
par.report_result = report_result
#old_report_liveness_result = par.report_liveness_result
par.report_liveness_result = report_liveness_result
old_report_bmc_depth = par.report_bmc_depth
par.report_bmc_depth = report_bmc_depth
try:
yield
finally:
par.report_result = old_report_result
#~ par.report_liveness_result = report_liveness_result
par.report_bmc_depth = old_report_bmc_depth
def proof_command_wrapper_internal(prooffunc, category_name, command_name, change, multi=False):
def wrapper(argv):
usage = "usage: %prog [options] <aig_file>"
parser = optparse.OptionParser(usage, prog=command_name)
parser.add_option("-n", "--no_redirect", dest="noisy", action="store_true", default=False, help="don't redirect output")
parser.add_option("-d", "--current_dir", dest="current_dir", action="store_true", default=False, help="stay in current directory")
options, args = parser.parse_args(argv)
if len(args) != 2:
parser.print_usage()
return 0
aig_filename = os.path.abspath(args[1])
with replace_report_result(multi):
if not options.noisy:
pyabc.run_command('/pushredirect')
if not options.current_dir:
pyabc.run_command('/pushdtemp')
try:
for d in os.environ['PATH'].split(':'):
bip = os.path.join(d, 'bip')
if os.path.exists(bip):
pyabc.run_command("load_plugin %s Bip"%bip)
break
basename = os.path.basename( aig_filename )
shutil.copyfile(aig_filename, basename)
aig_filename = basename
result = prooffunc(aig_filename)
par.cex_list = []
except:
result = None
if not multi:
if result=="SAT":
par.report_result(0,1)
elif result=="UNSAT":
par.report_result(0,0)
elif type(result)==list and len(result)>0 and result[0] == "SAT":
par.report_result(0,1)
elif type(result)==list and len(result)>0 and result[0] == "UNSAT":
par.report_result(0,0)
else:
par.report_result(0,2)
if not options.current_dir:
pyabc.run_command('/popdtemp')
if not options.noisy:
pyabc.run_command('/popredirect')
return 0
pyabc.add_abc_command(wrapper, category_name, command_name, change)
def proof_command_wrapper(prooffunc, category_name, command_name, change, multi=False):
def pf(aig_filename):
par.read_file_quiet(aig_filename)
return prooffunc()
return proof_command_wrapper_internal(pf, category_name, command_name, change, multi)
proof_command_wrapper(par.sp, 'HWMCC13', '/super_prove_aiger', 0)
proof_command_wrapper(par.simple, 'HWMCC13', '/simple_aiger', 0)
proof_command_wrapper(par.simple_bip, 'HWMCC13', '/simple_bip_aiger', 0)
proof_command_wrapper(par.simple_sat, 'HWMCC13', '/simple_sat_aiger', 0)
proof_command_wrapper(par.mp, 'HWMCC13', '/multi_prove_aiger', 0, multi=True)
def simple_liveness_prooffunc(aig_filename):
import niklas
from pyaig import utils
def simplify(aiger_in, aiger_out):
with niklas.temp_file_names(2, suffix='.aig') as tmp:
saved = utils.save_po_info(aiger_in, tmp[0])
par.read_file_quiet(tmp[0])
par.pre_simp()
pyabc.run_command( 'write_aiger %s'%tmp[1] )
utils.restore_po_info( saved, tmp[1], aiger_out )
return True
def report_result(id, res):
if res and 'result' in res:
result = res['result']
if result=='proved':
par.report_liveness_result(id, 0)
return True
elif result=='failed':
par.report_liveness_result(id, 1)
return True
return False
try:
niklas.run_niklas_multi(aig_filename, simplify=simplify, report_result=report_result)
except:
import traceback
traceback.print_exc()
proof_command_wrapper_internal( simple_liveness_prooffunc, "HWMCC13", "/simple_liveness_aiger", 0, multi=True)
import os
import sys
import time
import tempfile
import subprocess
from contextlib import contextmanager
import pyabc
import pyabc_split
@contextmanager
def temp_file_names(N, suffix=""):
files = []
try:
for i in xrange(N):
files.append( tempfile.NamedTemporaryFile(suffix=suffix) )
yield [ f.name for f in files ]
finally:
for f in files:
f.close()
def parse_bip_status(status):
res = {}
for line in open(status, 'r'):
line = line.strip()
colon = line.find(':')
if colon < 0:
continue
field = line[:colon]
data = line[colon+2:]
res[field] = data
return res
def run_bip(args, aiger):
import redirect
with redirect.redirect():
with temp_file_names(1) as tmpnames:
args = [
'bip',
'-abc',
'-input=%s'%aiger,
'-output=%s'%tmpnames[0],
] + args;
rc = subprocess.call(args, preexec_fn=pyabc._set_death_signal)
if rc!=0:
return None
return parse_bip_status(tmpnames[0])
from pyaig import AIG, read_aiger, write_aiger, utils
def run_niklas_single(aiger, simplify, report_result, timeout=None):
orig_args = [
[ ',live', '-k=l2s', '-eng=treb-abs' ],
[ ',live', '-k=inc' ],
[ ',live', '-k=l2s', '-eng=bmc' ],
]
simplified_args = [
[ ',live', '-k=inc' ],
[ ',live', '-k=l2s', '-eng=bmc' ],
[ ',live', '-k=l2s', '-eng=treb' ],
]
with temp_file_names(1, suffix='.aig') as simple_aiger:
orig_funcs = [ pyabc_split.defer(run_bip)(a, aiger) for a in orig_args ]
simplified_funcs = [ pyabc_split.defer(run_bip)(a, simple_aiger[0]) for a in simplified_args ]
with pyabc_split.make_splitter() as splitter:
sleep_id = splitter.fork_one( lambda : time.sleep(timeout ) ) if timeout else None
ids = splitter.fork_all( orig_funcs )
kill_if_simplified = ids[1:]
simplifier_id = splitter.fork_one( pyabc_split.defer(simplify)(aiger, simple_aiger[0]) )
for id, res in splitter:
print 'NIKLAS: process %d finished with'%id, res
if id == sleep_id:
print 'NIKLAS: timeout'
return False
elif id == simplifier_id:
print 'NIKLAS: simplify ended'
if not res:
continue
print 'NIKLAS: killing'
splitter.kill(kill_if_simplified)
splitter.fork_all( simplified_funcs )
continue
elif report_result(res):
print 'NIKLAS: RESULT'
return True
return False
def run_niklas_multi(aiger, simplify, report_result):
with open(aiger, 'r') as fin:
aig = read_aiger( fin )
n_j_pos = aig.n_justice()
assert n_j_pos > 0
if n_j_pos==1:
return run_niklas_single( aiger, simplify, report_result=lambda res: report_result(0, res) )
with temp_file_names(n_j_pos, suffix='.aig') as single_aiger:
def extract(j_po):
with open(single_aiger[j_po], 'w') as fout:
write_aiger(utils.extract_justice_po(aig, j_po), fout)
for _ in pyabc_split.split_all_full( [pyabc_split.defer(extract)(i) for i in xrange(n_j_pos) ] ):
pass
unsolved = set( xrange(n_j_pos) )
timeout = 1
while unsolved:
for j_po in sorted(unsolved):
if run_niklas_single( single_aiger[j_po], simplify, report_result=lambda res: report_result(j_po, res), timeout=timeout ):
unsolved.remove(j_po)
timeout *= 2
return not unsolved
if __name__ == "__main__":
def simplify(aiger_in, aiger_out):
with temp_file_names(2, suffix='.aig') as tmp:
saved = utils.save_po_info(aiger_in, tmp[0])
pyabc.run_command( 'read_aiger %s'%tmp[0] )
pyabc.run_command( 'dc2 ; dc2 ; dc2 ; dc2' )
pyabc.run_command( 'write_aiger %s'%tmp[1] )
utils.restore_po_info( saved, tmp[1], aiger_out )
return True
def report_result(id, res):
if res and 'result' in res:
result = res['result']
if result=='proved':
print "PROVED: ", id
return True
elif result=='failed':
print "FAILED:", id
return True
return False
aiger = "test.aig"
while True:
try:
run_niklas_multi(aiger, simplify=simplify, report_result=report_result)
except:
import traceback
traceback.print_exc()
This source diff could not be displayed because it is too large. You can view the blob instead.
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/simple_aiger $*"
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/simple_bip_aiger $*"
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/simple_liveness_aiger $*"
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/simple_sat_aiger $*"
#!/bin/sh
abc_root()
{
cwd="$(pwd)"
cd $(dirname "$1")
echo $(dirname "$(pwd)")
cd "${cwd}"
}
abc_dir=$(abc_root "$0")
bin_dir="${abc_dir}"/bin
exec ${bin_dir}/abc -q "/super_prove_aiger $*"
......@@ -45,10 +45,6 @@ SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
***********************************************************************/
#ifdef ABC_PYTHON_EMBED
#include <Python.h>
#endif /* ABC_PYTHON_EMBED */
#include "base/abc/abc.h"
#include "mainInt.h"
#include "base/wlc/wlc.h"
......@@ -108,29 +104,6 @@ int Abc_RealMain( int argc, char * argv[] )
pAbc = Abc_FrameGetGlobalFrame();
pAbc->sBinary = argv[0];
#ifdef ABC_PYTHON_EMBED
{
PyObject* pModule;
void init_pyabc(void);
Py_SetProgramName(argv[0]);
Py_NoSiteFlag = 1;
Py_Initialize();
init_pyabc();
pModule = PyImport_ImportModule("pyabc");
if (pModule)
{
Py_DECREF(pModule);
}
else
{
fprintf( pAbc->Err, "error: pyabc.py not found. PYTHONPATH may not be set properly.\n");
}
}
#endif /* ABC_PYTHON_EMBED */
// default options
fBatch = INTERACTIVE;
fInitSource = 1;
......@@ -354,12 +327,6 @@ int Abc_RealMain( int argc, char * argv[] )
}
}
#ifdef ABC_PYTHON_EMBED
{
Py_Finalize();
}
#endif /* ABC_PYTHON_EMBED */
// if the memory should be freed, quit packages
// if ( fStatus < 0 )
{
......
......@@ -190,23 +190,6 @@ void Abc_UtilsSource( Abc_Frame_t * pAbc )
}
#endif
#ifdef ABC_PYTHON_EMBED
if ( getenv("ABC_PYTHON_ABC_RC") )
{
/* read script file from $ABC_PYTHON_ABC_RC */
char * sPath = getenv("ABC_PYTHON_ABC_RC");
if (sPath){
char * sCmd = ABC_ALLOC(char, strlen(sPath) + 50);
(void) sprintf(sCmd, "source -s %s", sPath);
(void) Cmd_CommandExecute(pAbc, sCmd);
ABC_FREE(sCmd);
}
}
else
#endif /* #ifdef ABC_PYTHON_EMBED */
{
char * sPath1, * sPath2;
char * home;
......
......@@ -41,8 +41,6 @@ ABC_NAMESPACE_IMPL_START
/// FUNCTION DEFINITIONS ///
////////////////////////////////////////////////////////////////////////
#ifndef ABC_PYTHON_EMBED
int Util_SignalSystem(const char* cmd)
{
return system(cmd);
......@@ -63,8 +61,6 @@ void Util_SignalTmpFileRemove(const char* fname, int fLeave)
}
}
#endif /* ABC_PYTHON_EMBED */
ABC_NAMESPACE_IMPL_END
////////////////////////////////////////////////////////////////////////
......
#!/bin/sh
#
# Setup the ABC/Py environment and run the ABC/Py executable
# (ABC/Py stands for ABC with embedded Python)
#
# ABC/Py expects the following directory structure
#
# abc_root/
# bin/
# abc - this script
# abc_exe - the ABC executable
# ... - Other scripts
# lib/
# pyabc.py - support file for pyabc extension
# python_library.zip - The Python standard library. Only if not using the system Python interpreter.
# *.so - Python extensions, Only if not using the system Python interpreter.
# scripts/
# *.py - default directory for python scripts
#
# usage: abspath <dir>
# get the absolute path of <dir>
abspath()
{
cwd="$(pwd)"
cd "$1"
echo "$(pwd)"
cd "${cwd}"
}
self=$0
self_dir=$(dirname "${self}")
self_dir=$(abspath "${self_dir}")
abc_root=$(dirname "${self_dir}")
abc_exe="${abc_root}/bin/abc_exe"
PYTHONPATH="${abc_root}/lib":"${PYTHONPATH}"
export PYTHONPATH
if [ -d "${abc_root}/scripts" ] ; then
ABC_PYTHON_SCRIPTS="${abc_root}/scripts"
export ABC_PYTHON_SCRIPTS
PYTHONPATH="${ABC_PYTHON_SCRIPTS}":"${PYTHONPATH}"
export PYTHONPATH
fi
if [ -f "${abc_root}/scripts/abc.rc" ] ; then
ABC_PYTHON_ABC_RC="${abc_root}/scripts/abc.rc"
export ABC_PYTHON_ABC_RC
fi
if [ -f "${abc_root}/lib/python_library.zip" ] ; then
PYTHONHOME="${abc_root}"
export PYTHONHOME
PYTHONPATH="${abc_root}/lib/python_library.zip":"${PYTHONPATH}"
export PYTHONPATH
fi
PATH="${abc_root}/bin:$PATH"
export PATH
if [ "$1" = "--debug" ]; then
shift
abc_debugger="$1"
shift
echo export PYTHONHOME=$PYTHONHOME
echo export PYTHONPATH=$PYTHONPATH
echo export ABC_PYTHON_SCRIPTS=$ABC_PYTHON_SCRIPTS
echo export ABC_PYTHON_ABC_RC=$ABC_PYTHON_ABC_RC
fi
exec ${abc_debugger} "${abc_exe}" "$@"
# You can use 'from pyabc import *' and then not need the pyabc. prefix everywhere
import pyabc
# A new command is just a function that accepts a list of string arguments
# The first argument is always the name of the command
# It MUST return an integer. -1: user quits, -2: error. Return 0 for success.
# a simple command that just prints its arguments and returns success
def pytest1_cmd(args):
print args
return 0
# registers the command:
# The first argument is the function
# The second argument is the category (mainly for the ABC help command)
# The third argument is the new command name
# Keet the fourth argument 0, or consult with Alan
pyabc.add_abc_command(pytest1_cmd, "Python-Test", "pytest1", 0)
# a simple command that just prints its arguments and runs the command 'scorr -h'
def pytest2_cmd(args):
print args
pyabc.run_command('scorr -h')
return 0
pyabc.add_abc_command(pytest2_cmd, "Python-Test", "pytest2", 0)
# Now a more complicated command with argument parsing
# This command gets two command line arguments -c and -v. -c cmd runs the command 'cmd -h' and -v prints the python version
# for more details see the optparse module: http://docs.python.org/library/optparse.html
import optparse
def pytest3_cmd(args):
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage, prog="pytest3")
parser.add_option("-c", "--cmd", dest="cmd", help="command to ask help for")
parser.add_option("-v", "--version", action="store_true", dest="version", help="display Python Version")
options, args = parser.parse_args(args)
if options.version:
print sys.version
return 0
if options.cmd:
pyabc.run_command("%s -h"%options.cmd)
return 0
return 0
pyabc.add_abc_command(pytest3_cmd, "Python-Test", "pytest3", 0)
On Windows:
python setup.py build
python setup.py bdist_wininst
On Linux (from the main abc directory)
To build the extensions (make sure -fPIC is added to OPTFLAG in the main ABC Makefile first)
make ABC_PYTHON=/usr/bin/python pyabc_extension_install
To build the ABC with embedded python
make pyabc.tgz
Updating the latest version on mima:
alanmi@mima:~/abc_60$ cp ./src/python/build/lib.linux-x86_64-2.6/_pyabc.so /hd/common/pyabc/builds/101030/_pyabc.so
alanmi@mima:~/abc_60$ cp ./src/python/build/lib.linux-x86_64-2.6/pyabc.py /hd/common/pyabc/builds/101030/pyabc.py
alanmi@mima:/hd/common/pyabc$ rm current
alanmi@mima:/hd/common/pyabc$ ln -s builds/101030 current
alanmi@mima:/hd/common/pyabc$ ls -l
total 4
lrwxrwxrwx 1 alanmi common 13 2010-10-30 14:55 current -> builds/101030
Latest documentation:
http://goo.gl/jNV2
\ No newline at end of file
class _Getch:
"""Gets a single character from standard input. Does not echo to the screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()
def __call__(self): return self.impl()
class _GetchUnix:
def __init__(self):
import tty, sys
def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
class _GetchWindows:
def __init__(self):
import msvcrt
def __call__(self):
import msvcrt
return msvcrt.getch()
getch = _Getch()
# To compile with the embedded python interpreter set
# the variable ABC_PYTHON to point to the python executable
#
# Examples:
# make ABC_PYTHON=/usr/bin/python
# make ABC_PYTHON=/usr/bin/python2.5
#
# To build the Python extension build the target pyabc
# To create a package of ABC with embedded Python use the target pyabc.tgz
ifdef ABC_PYTHON
# get the directory containing this file
ABC_PYTHON_FILES_PREFIX := $(CURDIR)/src/python
ABC_SWIG := swig
ABC_PYTHON_CONFIG := $(ABC_PYTHON)-config
ABC_PYTHON_CFLAGS := $(shell $(ABC_PYTHON_CONFIG) --includes) -DABC_PYTHON_EMBED=1
ABC_PYTHON_LDFLAGS := $(shell $(ABC_PYTHON_CONFIG) --ldflags)
CFLAGS += $(ABC_PYTHON_CFLAGS)
CXXFLAGS += $(ABC_PYTHON_CFLAGS)
LIBS += $(ABC_PYTHON_LDFLAGS)
ABC_PYTHON_SRC := $(ABC_PYTHON_FILES_PREFIX)/pyabc_wrap.c
SRC += $(ABC_PYTHON_SRC)
GARBAGE += \
$(ABC_PYTHON_SRC) \
$(ABC_PYTHON_SRC:_wrap.c=.py) \
$(ABC_PYTHON_SRC:_wrap.c=.pyc) \
$(ABC_PYTHON_FILES_PREFIX)/build \
$(ABC_PYTHON_FILES_PREFIX)/dist \
pyabc.tgz
ABC_PYABC_DIR ?= pyabc
ABC_PYABC_TGZ ?= pyabc.tgz
ABC_PYABC_EXTRA_BIN ?=
ABC_PYABC_EXTRA_LIB ?=
%_wrap.c %.py : %.i
$(ABC_SWIG) -python -outdir $(<D) $<
.PHONY: pyabc_extension_build
pyabc_extension_build : lib$(PROG).a $(ABC_PYTHON_SRC) $(ABC_PYTHON_SRC:_wrap.c=.py)
( cd $(ABC_PYTHON_FILES_PREFIX) && rm -rf build/ )
( cd $(ABC_PYTHON_FILES_PREFIX) && $(ABC_PYTHON) setup.py build )
.PHONY: pyabc_extension_install
pyabc_extension_install : pyabc_extension_build
( cd $(ABC_PYTHON_FILES_PREFIX) && $(ABC_PYTHON) setup.py install --user )
.PHONY: pyabc_extension_bdist
pyabc_extension_bdist : pyabc_extension_build
( cd $(ABC_PYTHON_FILES_PREFIX) && python setup.py bdist )
.PHONY: pyabc_tgz
pyabc_tgz : $(ABC_PYABC_TGZ)
$(ABC_PYABC_TGZ) : $(PROG) $(ABC_PYTHON_SRC:_wrap.c=.py) $(ABC_PYTHON_FILES_PREFIX)/abc.sh $(ABC_PYTHON_FILES_PREFIX)/package.py
$(ABC_PYTHON) $(ABC_PYTHON_FILES_PREFIX)/package.py \
--pyabc_dir=$(ABC_PYABC_DIR) \
--abc=$(PROG) \
--abc_sh=$(ABC_PYTHON_FILES_PREFIX)/abc.sh \
--pyabc=$(ABC_PYTHON_FILES_PREFIX) \
--extra_bin="$(ABC_PYABC_EXTRA_BIN)" \
--extra_lib="$(ABC_PYABC_EXTRA_LIB)" \
--out=$@ \
$(ABC_PYTHON_OPTIONS)
PYABC_INSTALL_TARGET ?= $(shell date +%Y-%m-%d_%H-%M.%N_${USER})
PYABC_INSTALL_TARGET := $(PYABC_INSTALL_TARGET)
PYABC_INSTALL_DIR ?= /hd/common/pyabc/builds/pyabc_builds/
pyabc_install_target: pyabc_extension_bdist
mkdir -p "$(PYABC_INSTALL_DIR)/$(PYABC_INSTALL_TARGET)"
tar \
--directory="$(PYABC_INSTALL_DIR)/$(PYABC_INSTALL_TARGET)" \
--show-transformed-names \
--transform='s#^.*/##g' \
-xvzf "$(ABC_PYTHON_FILES_PREFIX)/dist/pyabc-1.0.linux-x86_64.tar.gz"
find "$(PYABC_INSTALL_DIR)/$(PYABC_INSTALL_TARGET)/"* -type d | xargs rmdir
echo "Installed at $(PYABC_INSTALL_DIR)/$(PYABC_INSTALL_TARGET)"
endif
import os
import sys
import optparse
import zipfile
import tarfile
import tempfile
import time
import py_compile
def zip_library(f, extra_files = []):
lib = "%s/lib/python%s/"%(sys.prefix,sys.version[:3])
zf = zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED)
for root, _, files in os.walk(lib):
arcroot = os.path.relpath(root, lib)
for f in files:
_, ext = os.path.splitext(f)
if ext in ['.py']:
zf.write(os.path.join(root,f), os.path.join(arcroot, f))
for s, r in extra_files:
zf.write( s, r )
zf.close()
def add_python_lib(tf, lib_dir, lib, mtime):
_, prefix = os.path.split(lib)
for root, _, files in os.walk(lib):
relpath = os.path.relpath(root, lib)
if '.hg' in relpath.split('/'):
continue
if relpath=='.':
arcroot = lib_dir
else:
arcroot = os.path.join( lib_dir, os.path.relpath(root, lib) )
arcroot = os.path.join(arcroot, prefix)
add_dir(tf, arcroot, mtime)
for f in files:
_, ext = os.path.splitext(f)
if ext in ['.py', '.so']:
add_file( tf, os.path.join(root,f), os.path.join(arcroot, f), 0666, mtime)
def add_dir(tf, dir, mtime):
ti = tarfile.TarInfo(dir)
ti.mode = 0777
ti.mtime = mtime
ti.type = tarfile.DIRTYPE
tf.addfile(ti)
def add_fileobj(tf, f, arcname, mode, mtime):
ti = tarfile.TarInfo(arcname)
ti.mode = mode
ti.mtime = mtime
f.seek(0, os.SEEK_END)
ti.size = f.tell()
f.seek(0, os.SEEK_SET)
tf.addfile(ti, f)
def add_file(tf, fname, arcname, mode, mtime):
print "\t adding %s as %s"%(fname, arcname)
with open(fname, "rb") as f:
add_fileobj(tf, f, arcname, mode, mtime)
def package(pyabc_dir, extra_bin, extra_lib, extra_files, abc_exe, abc_sh, pyabc, ofname, scripts_dir, use_sys):
mtime = time.time()
tf = tarfile.open(ofname, "w:gz")
add_dir(tf, "%s"%pyabc_dir, mtime)
add_dir(tf, "%s/bin"%pyabc_dir, mtime)
add_file(tf, abc_exe, "%s/bin/abc_exe"%pyabc_dir, 0777, mtime)
add_file(tf, abc_sh, "%s/bin/abc"%pyabc_dir, 0777, mtime)
if scripts_dir:
for fn in os.listdir(scripts_dir):
if fn.startswith('.'):
continue
fullname = os.path.join(scripts_dir, fn)
if os.path.isfile(fullname):
fnroot, fnext = os.path.splitext(fn)
if fnext==".sh":
add_file( tf, fullname, os.path.join("%s/bin"%pyabc_dir, fnroot), 0777, mtime)
elif fnext not in ( '.pyc', '.pyo'):
add_file( tf, fullname, os.path.join("%s/scripts"%pyabc_dir, fn), 0666, mtime)
for bin in extra_bin:
add_file( tf, bin, os.path.join("%s/bin"%pyabc_dir, os.path.basename(bin)), 0777, mtime)
lib_dir = "%s/lib"%pyabc_dir
add_dir(tf, lib_dir, mtime)
for lib in extra_lib:
add_python_lib( tf, lib_dir, lib, mtime)
for file, dest in extra_files:
add_file(tf, file, '%s/%s'%(pyabc_dir, dest), 0666, mtime)
for entry in os.listdir(pyabc):
if entry.endswith('.py'):
add_file( tf, os.path.join(pyabc, entry), os.path.join("%s/lib"%pyabc_dir, entry), 0666, mtime)
if not use_sys:
# ZIP standard library
zf = tempfile.NamedTemporaryFile("w+b")
#zip_library(zf, [(pyabc, "pyabc.py")])
zip_library(zf, [])
zf.flush()
add_fileobj(tf, zf, "%s/lib/python_library.zip"%pyabc_dir, 0666, mtime)
zf.close()
# add all extensions
lib_dynload = os.path.join(sys.exec_prefix,"lib", "python%s"%sys.version[:3], "lib-dynload")
for fn in os.listdir(lib_dynload):
fullname = os.path.join(lib_dynload, fn)
if os.path.isfile(fullname):
add_file( tf, fullname, os.path.join("%s/lib"%pyabc_dir, fn), 0666, mtime)
tf.close()
def main(args):
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage)
parser.add_option("-d", "--pyabc_dir", dest="pyabc_dir", help="name of generated directory" )
parser.add_option("-b", "--extra_bin", dest="extra_bin", help="extra binaries to pack" )
parser.add_option("-l", "--extra_lib", dest="extra_lib", help="extra directories in lib to pack" )
parser.add_option("-f", "--extra_files", dest="extra_files", help="additional files (comma separated pairs of file:dest" )
parser.add_option("-a", "--abc", dest="abc", help="location of the ABC exeutable")
parser.add_option("-s", "--abc_sh", dest="abc_sh", help="location of the ABC setup script")
parser.add_option("-p", "--pyabc", dest="pyabc", help="location of pyabc.py")
parser.add_option("-o", "--out", dest="out", help="location of output tar gzipped file")
parser.add_option("-x", "--scripts", dest="scripts", default="scripts", help="location of scripts")
parser.add_option("-S", "--system", action="store_false", dest="sys", default=True, help="use default python installation")
options, args = parser.parse_args(args)
if len(args) > 1:
parser.print_help()
return 1
if not options.pyabc_dir or not options.abc or not options.abc_sh or not options.pyabc or not options.out:
parser.print_help()
return 1
extra_bin = options.extra_bin.split(',') if options.extra_bin else []
extra_lib = options.extra_lib.split(',') if options.extra_lib else []
extra_files = [ s.split(':') for s in options.extra_files.split(',')] if options.extra_files else []
return package(options.pyabc_dir, extra_bin, extra_lib, extra_files, options.abc, options.abc_sh, options.pyabc, options.out, options.scripts, options.sys)
if __name__=="__main__":
main(sys.argv)
# You can use 'from pyabc import *' and then not need the pyabc. prefix everywhere
import sys
import optparse
import subprocess
import tempfile
import threading
import os
import os.path
from contextlib import contextmanager, nested
import pyabc
def popen_and_wait_with_timeout(timeout,cmd, *args, **kwargs):
""" Wait for a subprocess.Popen object to terminate, or until timeout (in seconds) expires. """
p = None
t = None
try:
p = subprocess.Popen(cmd, *args, **kwargs)
if timeout <= 0:
timeout = None
t = threading.Thread(target=lambda: p.communicate())
t.start()
t.join(timeout)
finally:
if p is not None and p.poll() is None:
p.kill()
if t is not None and t.is_alive():
t.join()
if p is not None:
return p.returncode
return -1
@contextmanager
def temp_file_name(suffix=""):
file = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
name = file.name
file.close()
try:
yield name
finally:
os.unlink(name)
def cygpath(path):
if sys.platform == "win32":
if os.path.isabs(path):
drive, tail = os.path.splitdrive(path)
drive = drive.lower()
tail = tail.split(os.path.sep)
return '/cygdrive/%s'%drive[0] + '/'.join(tail)
else:
path = path.split(os.path.sep)
return "/".join(path)
return path
def run_reachx_cmd(effort, timeout):
with nested(temp_file_name(suffix=".aig"), temp_file_name()) as (tmpaig_name, tmplog_name):
pyabc.run_command("write %s"%tmpaig_name)
cmdline = [
'read %s'%cygpath(tmpaig_name),
'qua_ffix -effort %d -L %s'%(effort, cygpath(tmplog_name)),
'quit'
]
cmd = ["jabc", "-c", " ; ".join(cmdline)]
rc = popen_and_wait_with_timeout(timeout, cmd, shell=False, stdout=sys.stdout, stderr=sys.stderr)
if rc != 0:
# jabc failed or stopped. Write a status file to update the status to unknown
with open(tmplog_name, "w") as f:
f.write('snl_UNK -1 unknown\n')
f.write('NULL\n')
f.write('NULL\n')
pyabc.run_command("read_status %s"%tmplog_name)
return rc
def reachx_cmd(argv):
usage = "usage: %prog [options]"
parser = optparse.OptionParser(usage, prog="reachx")
parser.add_option("-e", "--effort", dest="effort", type=int, default=0, help="effort level. [default=0, means unlimited]")
parser.add_option("-t", "--timeout", dest="timeout", type=int, default=0, help="timeout in seconds [default=0, unlimited]")
options, args = parser.parse_args(argv)
rc = run_reachx_cmd(options.effort, options.timeout)
print "%s command: jabc returned: %d"%(argv[0], rc)
return 0
pyabc.add_abc_command(reachx_cmd, "Verification", "reachx", 0)
"""
A simple context manager for redirecting streams in Python.
The streams are redirected at the the C runtime level so that the output of C extensions
that use stdio will also be redirected.
null_file : a stream representing the null device (e.g. /dev/null on Unix)
redirect: a context manager for redirecting streams
Author: Baruch Sterin (sterin@berkeley.edu)
"""
import os
import sys
from contextlib import contextmanager
null_file = open( os.devnull, "w" )
@contextmanager
def _dup( f ):
fd = os.dup( f.fileno() )
yield fd
os.close(fd)
@contextmanager
def save_stdout( src = sys.stdout ):
"""
Redirect
"""
fd = os.dup( src.fileno() )
own = True
try:
with os.fdopen( fd, "w", 0) as f:
own = False
yield f
except:
if own:
os.close(fd)
raise
@contextmanager
def redirect(dst = null_file, src = sys.stdout):
"""
Redirect the src stream into dst.
Example:
with redirect( open("somefile.txt", sys.stdout ) ):
do some stuff ...
"""
if src.fileno() == dst.fileno():
yield
return
with _dup( src ) as fd_dup_src:
dst.flush()
src.flush()
os.close( src.fileno() )
os.dup2( dst.fileno(), src.fileno() )
yield
src.flush()
os.close( src.fileno() )
os.dup2( fd_dup_src, src.fileno() )
def start_redirect(dst = null_file, src = sys.stdout):
"""
Start redirection of src stream into dst. Return the duplicated file handle of the source.
Example:
fd = start_redirect( open("somefile.txt"), sys.stdout )
... do some stuff ...
end_redirect(sys.stdout, fd)
"""
if src.fileno() == dst.fileno():
return None
fd_dup_src = os.dup( src.fileno() )
dst.flush()
src.flush()
os.close( src.fileno() )
os.dup2( dst.fileno(), src.fileno() )
return fd_dup_src
def end_redirect(src, fd_dup_src):
"""
End redirection of stream src.Redirect the src stream into dst. src is the source stream and fd_dup_src is the value returned by
start_redirect()
"""
if fd_dup_src is None:
return
src.flush()
os.close( src.fileno() )
os.dup2( fd_dup_src, src.fileno() )
os.close(fd_dup_src)
import sys
from distutils.core import setup, Extension
from distutils.sysconfig import get_config_vars
from distutils import util
from distutils.command.build_ext import build_ext
from distutils import sysconfig
define_macros = []
libraries = []
library_dirs = []
if sys.platform == "win32":
src_file = [ 'pyabc.i' ]
define_macros.append( ('WIN32', 1) )
define_macros.append( ('ABC_DLL', 'ABC_DLLEXPORT') )
libraries.append('abcr')
library_dirs.append('./../../lib')
else:
src_file = [ 'pyabc_wrap.c' ]
if get_config_vars()['SIZEOF_VOID_P'] > 4:
define_macros.append( ('LIN64', 1) )
else:
define_macros.append( ('LIN', 1) )
libraries.append( 'abc' )
libraries.append( 'rt' )
libraries.append( 'readline' )
library_dirs.append('./../../')
# ugly hack to silence strict-prototype warnings
class build_ext_subclass( build_ext ):
def build_extensions(self):
CC = sysconfig.get_config_var("CC")
if self.compiler.compiler_type == 'unix' and ( 'gcc' in CC or 'g++' in CC):
for e in self.extensions:
e.extra_compile_args.append( '-Wno-strict-prototypes' )
build_ext.build_extensions(self)
ext = Extension(
'_pyabc',
src_file,
define_macros=define_macros,
include_dirs = ["../../src"],
library_dirs=library_dirs,
libraries=libraries
)
setup(
name='pyabc',
version='1.0',
ext_modules=[ext],
py_modules=['pyabc','getch','pyabc_split','redirect', 'reachx_cmd'],
cmdclass = {'build_ext': build_ext_subclass }
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment