diff --git a/pysol/MANIFEST.in b/pysol/MANIFEST.in new file mode 100644 index 000000000..a3edb5ce1 --- /dev/null +++ b/pysol/MANIFEST.in @@ -0,0 +1,13 @@ +include pysol/*.cpp +include *.py +include libdevcore/*cpp +include libdevcore/*h +include libdevcrypto/*cpp +include libdevcrypto/*h +include libethcore/*cpp +include libethcore/*h +include libsolidity/*cpp +include libsolidity/*h +include libevmcore/*cpp +include libevmcore/*h +include pysol/README.md diff --git a/pysol/README.md b/pysol/README.md new file mode 100644 index 000000000..e69de29bb diff --git a/pysol/pysolidity.cpp b/pysol/pysolidity.cpp new file mode 100644 index 000000000..a2214c67e --- /dev/null +++ b/pysol/pysolidity.cpp @@ -0,0 +1,115 @@ +#include +#include "structmember.h" +#include +#include +#include +#include + +#include "../libdevcore/CommonData.h" + + +#include +#include +#include +#include + + +std::string compile(std::string src) { + dev::solidity::CompilerStack compiler; + try + { + std::vector m_data = compiler.compile(src, false); + return std::string(m_data.begin(), m_data.end()); + } + catch (dev::Exception const& exception) + { + std::ostringstream error; + dev::solidity::SourceReferenceFormatter::printExceptionInformation(error, exception, "Error", compiler); + std::string e = error.str(); + throw(e); + } +} + + +std::string mk_full_signature(std::string src) { + dev::solidity::CompilerStack compiler; + try + { + compiler.compile(src); + return compiler.getInterface(""); + } + catch (dev::Exception const& exception) + { + std::ostringstream error; + dev::solidity::SourceReferenceFormatter::printExceptionInformation(error, exception, "Error", compiler); + std::string e = error.str(); + throw(e); + } +} + +std::string bob(std::string src) { return src + src; } + +#define PYMETHOD(name, FROM, method, TO) \ + static PyObject * name(PyObject *, PyObject *args) { \ + try { \ + FROM(med) \ + return TO(method(med)); \ + } \ + catch (std::string e) { \ + PyErr_SetString(PyExc_Exception, e.c_str()); \ + return NULL; \ + } \ + } + +#define FROMSTR(v) \ + const char *command; \ + int len; \ + if (!PyArg_ParseTuple(args, "s#", &command, &len)) \ + return NULL; \ + std::string v = std::string(command, len); \ + +// Convert string into python wrapper form +PyObject* pyifyString(std::string s) { + return Py_BuildValue("s#", s.c_str(), s.length()); +} + +// Convert integer into python wrapper form +PyObject* pyifyInteger(unsigned int i) { + return Py_BuildValue("i", i); +} + +// Convert pyobject int into normal form +int cppifyInt(PyObject* o) { + int out; + if (!PyArg_Parse(o, "i", &out)) + throw("Argument should be integer"); + return out; +} + +// Convert pyobject string into normal form +std::string cppifyString(PyObject* o) { + const char *command; + if (!PyArg_Parse(o, "s", &command)) + throw("Argument should be string"); + return std::string(command); +} + +int fh(std::string i) { + return dev::fromHex(i[0]); +} + +PYMETHOD(ps_compile, FROMSTR, compile, pyifyString) +PYMETHOD(ps_mk_full_signature, FROMSTR, mk_full_signature, pyifyString) + +static PyMethodDef PyextMethods[] = { + {"compile", ps_compile, METH_VARARGS, + "Compile code."}, + {"mk_full_signature", ps_mk_full_signature, METH_VARARGS, + "Get the signature of a piece of code."}, + {NULL, NULL, 0, NULL} /* Sentinel */ +}; + +PyMODINIT_FUNC initsolidity(void) +{ + Py_InitModule( "solidity", PyextMethods ); +} diff --git a/pysol/setup.py b/pysol/setup.py new file mode 100755 index 000000000..9aa13c248 --- /dev/null +++ b/pysol/setup.py @@ -0,0 +1,41 @@ +import os +os.chdir('..') + +from setuptools import setup, Extension + +from distutils.sysconfig import get_config_vars + +(opt,) = get_config_vars('OPT') +os.environ['OPT'] = " ".join( + flag for flag in opt.split() if flag != '-Wstrict-prototypes' +) + +setup( + # Name of this package + name="ethereum-solidity", + + # Package version + version='1.8.0', + + description='Solidity compiler python wrapper', + maintainer='Vitalik Buterin', + maintainer_email='v@buterin.com', + license='WTFPL', + url='http://www.ethereum.org/', + + # Describes how to build the actual extension module from C source files. + ext_modules=[ + Extension( + 'solidity', # Python name of the module + sources= ['libdevcore/Common.cpp', 'libdevcore/CommonData.cpp', 'libdevcore/CommonIO.cpp', 'libdevcore/FixedHash.cpp', 'libdevcore/Guards.cpp', 'libdevcore/Log.cpp', 'libdevcore/RangeMask.cpp', 'libdevcore/RLP.cpp', 'libdevcore/Worker.cpp', 'libdevcrypto/AES.cpp', 'libdevcrypto/Common.cpp', 'libdevcrypto/CryptoPP.cpp', 'libdevcrypto/ECDHE.cpp', 'libdevcrypto/FileSystem.cpp', 'libdevcrypto/MemoryDB.cpp', 'libdevcrypto/OverlayDB.cpp', 'libdevcrypto/SHA3.cpp', 'libdevcrypto/TrieCommon.cpp', 'libdevcrypto/TrieDB.cpp', 'libethcore/CommonEth.cpp', 'libethcore/CommonJS.cpp', 'libethcore/Exceptions.cpp', 'libsolidity/AST.cpp', 'libsolidity/ASTJsonConverter.cpp', 'libsolidity/ASTPrinter.cpp', 'libsolidity/CompilerContext.cpp', 'libsolidity/Compiler.cpp', 'libsolidity/CompilerStack.cpp', 'libsolidity/CompilerUtils.cpp', 'libsolidity/DeclarationContainer.cpp', 'libsolidity/ExpressionCompiler.cpp', 'libsolidity/GlobalContext.cpp', 'libsolidity/InterfaceHandler.cpp', 'libsolidity/NameAndTypeResolver.cpp', 'libsolidity/Parser.cpp', 'libsolidity/Scanner.cpp', 'libsolidity/SourceReferenceFormatter.cpp', 'libsolidity/Token.cpp', 'libsolidity/Types.cpp', 'libevmcore/Assembly.cpp', 'libevmcore/Instruction.cpp', 'pysol/pysolidity.cpp'], + libraries=['boost_python', 'boost_filesystem', 'boost_chrono', 'boost_thread', 'cryptopp', 'leveldb', 'jsoncpp'], + include_dirs=['/usr/include/boost', '..', '../..', '.'], + extra_compile_args=['--std=c++11', '-Wno-unknown-pragmas'] + )], + py_modules=[ + ], + scripts=[ + ], + entry_points={ + } + ),