Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pipermerriam committed Jun 29, 2016
0 parents commit 2490b24
Show file tree
Hide file tree
Showing 39 changed files with 1,280 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .github/ISSUE_TEMPLATE.md
@@ -0,0 +1,17 @@
* `py-geth` Version: x.x.x
* `go-ethereum` Version: x.x.x
* Python Version: x.x.x
* OS: osx/linux/win


### What was wrong?

Please include information like:

* full output of the error you received
* what command you ran
* the code that caused the failure

#### Cute Animal Picture

> put a cute animal picture here.
11 changes: 11 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
@@ -0,0 +1,11 @@
### What was wrong?



### How was it fixed?



#### Cute Animal Picture

> put a cute animal picture here.
51 changes: 51 additions & 0 deletions .gitignore
@@ -0,0 +1,51 @@
*.py[cod]

# C extensions
*.so

# Packages
*.egg
*.egg-info
dist
build
eggs
parts
bin
var
sdist
develop-eggs
.installed.cfg
lib
lib64

# Installer logs
pip-log.txt

# Unit test / coverage reports
.coverage
.tox
nosetests.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Complexity
output/*.html
output/*/index.html

# Sphinx
docs/_build

# Blockchain
**/chains/*/nodes/*
**/chains/*/nodekey
**/chains/*/dapp/*
**/chains/*/chaindata/*

# Known Contracts
**/known_contracts.json
23 changes: 23 additions & 0 deletions .travis.yml
@@ -0,0 +1,23 @@
language: python
python:
- "3.5"
dist: trusty
sudo: required
before_install:
- sudo add-apt-repository -y ppa:ethereum/ethereum
- sudo apt-get update
- sudo apt-get install -y ethereum
env:
matrix:
- TOX_ENV=py27
- TOX_ENV=py34
- TOX_ENV=py35
- TOX_ENV=flake8
cache: pip
install:
- travis_retry pip install setuptools --upgrade
- travis_retry pip install tox
script:
- tox -e $TOX_ENV
after_script:
- cat .tox/$TOX_ENV/log/*.log
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -0,0 +1,4 @@
0.1.0
-----

- Initial Release
34 changes: 34 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,34 @@
# Development

To start development for Populus you should begin by cloning the repo.

```bash
$ git clone git@github.com/pipermerriam/py-geth.git
```


# Cute Animal Pictures

All pull requests need to have a cute animal picture. This is a very important
part of the development process.


# Pull Requests

In general, pull requests are welcome. Please try to adhere to the following.

- code should conform to PEP8 and as well as the linting done by flake8
- include tests.
- include any relevant documentation updates.

It's a good idea to make pull requests early on. A pull request represents the
start of a discussion, and doesn't necessarily need to be the final, finished
submission.

GitHub's documentation for working on pull requests is [available here][pull-requests].

Always run the tests before submitting pull requests, and ideally run `tox` in
order to check that your modifications don't break anything.

Once you've made a pull request take a look at the travis build status in the
GitHub interface and make sure the tests are runnning as you'd expect.
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Piper Merriam

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions MANIFEST.in
@@ -0,0 +1,9 @@
include LICENSE
include README.md
include requirements.txt

recursive-exclude * __pycache__
recursive-exclude * *.py[co]

include pygeth/default_blockchain_password
include pygeth/genesis.json
38 changes: 38 additions & 0 deletions Makefile
@@ -0,0 +1,38 @@
.PHONY: clean-pyc clean-build

help:
@echo "clean-build - remove build artifacts"
@echo "clean-pyc - remove Python file artifacts"
@echo "lint - check style with flake8"
@echo "test - run tests quickly with the default Python"
@echo "testall - run tests on every Python version with tox"
@echo "release - package and upload a release"
@echo "sdist - package"

clean: clean-build clean-pyc

clean-build:
rm -fr build/
rm -fr dist/
rm -fr *.egg-info

clean-pyc:
find . -name '*.pyc' -exec rm -f {} +
find . -name '*.pyo' -exec rm -f {} +
find . -name '*~' -exec rm -f {} +

lint:
flake8 pygeth

test:
py.test --tb native tests

test-all:
tox

release: clean
python setup.py sdist bdist bdist_wheel upload

sdist: clean
python setup.py sdist bdist bdist_wheel
ls -l dist
9 changes: 9 additions & 0 deletions README.md
@@ -0,0 +1,9 @@
# PyGeth

[![Build Status](https://travis-ci.org/pipermerriam/py-geth.png)](https://travis-ci.org/pipermerriam/py-geth)
[![Documentation Status](https://readthedocs.org/projects/py-geth/badge/?version=latest)](https://readthedocs.org/projects/py-geth/?badge=latest)
[![PyPi version](https://pypip.in/v/py-geth/badge.png)](https://pypi.python.org/pypi/py-geth)
[![PyPi downloads](https://pypip.in/d/py-geth/badge.png)](https://pypi.python.org/pypi/py-geth)


Python wrapper around running `geth` as a subprocess
50 changes: 50 additions & 0 deletions conftest.py
@@ -0,0 +1,50 @@
import pytest
import json
import requests


@pytest.fixture
def open_port():
from pygeth.utils import get_open_port
return get_open_port()


@pytest.fixture()
def rpc_client(open_port):
from pygeth.utils.encoding import force_obj_to_text
endpoint = "http://127.0.0.1:{port}".format(port=open_port)

def make_request(method, params=None, raise_on_error=True):
global nonce
nonce += 1 # NOQA
payload = {
"id": nonce,
"jsonrpc": "2.0",
"method": method,
"params": params or [],
}
payload_data = json.dumps(force_obj_to_text(payload))
response = requests.post(endpoint, data=payload_data)

if raise_on_error:
assert response.status_code == 200

result = response.json()

if 'error' in result:
raise AssertionError(result['error'])

assert set(result.keys()) == {"id", "jsonrpc", "result"}
return response.json()['result']

return make_request


@pytest.fixture()
def data_dir(tmpdir):
return str(tmpdir.mkdir("data-dir"))


@pytest.fixture()
def base_dir(tmpdir):
return str(tmpdir.mkdir("base-dir"))
7 changes: 7 additions & 0 deletions pygeth/__init__.py
@@ -0,0 +1,7 @@
import pkg_resources

from gevent import monkey
monkey.patch_all()


__version__ = pkg_resources.get_distribution("py-geth").version
108 changes: 108 additions & 0 deletions pygeth/accounts.py
@@ -0,0 +1,108 @@
import os
import re

from .wrapper import spawn_geth
from .utils.proc import format_error_message
from .chain import (
get_genesis_file_path,
is_live_chain,
is_testnet_chain,
write_genesis_file,
)


def get_accounts(data_dir, **geth_kwargs):
"""
Returns all geth accounts as tuple of hex encoded strings
>>> geth_accounts()
... ('0x...', '0x...')
"""
command, proc = spawn_geth(dict(
data_dir=data_dir,
suffix_args=['account', 'list'],
**geth_kwargs
))
stdoutdata, stderrdata = proc.communicate()

if proc.returncode:
if "no keys in store" in stderrdata:
return tuple()
else:
raise ValueError(format_error_message(
"Error trying to list accounts",
command,
proc.returncode,
stdoutdata,
stderrdata,
))
accounts = parse_geth_accounts(stdoutdata)
return accounts


account_regex = re.compile(b'\{([a-f0-9]{40})\}')


def create_new_account(data_dir, password, **geth_kwargs):
if os.path.exists(password):
geth_kwargs['password'] = password

command, proc = spawn_geth(dict(
data_dir=data_dir,
suffix_args=['account', 'new'],
**geth_kwargs
))

if os.path.exists(password):
stdoutdata, stderrdata = proc.communicate()
else:
stdoutdata, stderrdata = proc.communicate(b"\n".join((password, password)))

if proc.returncode:
raise ValueError(format_error_message(
"Error trying to create a new account",
command,
proc.returncode,
stdoutdata,
stderrdata,
))

match = account_regex.search(stdoutdata)
if not match:
raise ValueError(format_error_message(
"Did not find an address in process output",
command,
proc.returncode,
stdoutdata,
stderrdata,
))

return b'0x' + match.groups()[0]


def ensure_account_exists(data_dir, **geth_kwargs):
accounts = get_accounts(data_dir, **geth_kwargs)
if not accounts:
account = create_new_account(data_dir, **geth_kwargs)
genesis_file_path = get_genesis_file_path(data_dir)

should_write_genesis = not any((
os.path.exists(genesis_file_path),
is_live_chain(data_dir),
is_testnet_chain(data_dir),
))
if should_write_genesis:
write_genesis_file(
genesis_file_path,
alloc=dict([
(account, "1000000000000000000000000000"), # 1 billion ether.
]),
)
else:
account = accounts[0]
return account


def parse_geth_accounts(raw_accounts_output):
accounts = account_regex.findall(raw_accounts_output)
return tuple(b'0x' + account for account in accounts)

0 comments on commit 2490b24

Please sign in to comment.