Skip to content

Commit

Permalink
remove _wget in favor of requests, closes #16
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed Sep 5, 2019
1 parent 6f75275 commit 51b52e3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
4 changes: 4 additions & 0 deletions solcx/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ class ContractsNotFound(SolcError):

class SolcNotInstalled(Exception):
pass


class DownloadError(Exception):
pass
65 changes: 33 additions & 32 deletions solcx/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
import zipfile
import logging

from .exceptions import SolcNotInstalled
from .exceptions import (
DownloadError,
SolcNotInstalled,
)

DOWNLOAD_BASE = "https://github.com/ethereum/solidity/releases/download/{}/{}"
ALL_RELEASES = "https://api.github.com/repos/ethereum/solidity/releases"
Expand Down Expand Up @@ -234,18 +237,6 @@ def _chmod_plus_x(executable_path):
executable_path.chmod(executable_path.stat().st_mode | stat.S_IEXEC)


def _wget(url, path):
try:
_check_subprocess_call(
["wget", url, "-O", str(path)],
"Downloading solc from {}".format(url)
)
except subprocess.CalledProcessError:
if path.exists():
path.unlink()
raise


def _check_for_installed_version(version):
path = get_solc_folder().joinpath("solc-" + version)
if path.exists():
Expand All @@ -262,13 +253,26 @@ def _get_temp_folder():
return path


def _download_solc(url):
response = requests.get(url)
if response.status_code != 200:
raise DownloadError(
"Received status code {} when attempting to download from {}".format(
response.status_code,
url
)
)
return response.content


def _install_solc_linux(version):
download = DOWNLOAD_BASE.format(version, "solc-static-linux")
binary_path = _check_for_installed_version(version)
if binary_path:
temp_path = _get_temp_folder().joinpath("solc-binary")
_wget(download, temp_path)
temp_path.rename(binary_path)
LOGGER.info("Downloading solc {} from {}".format(version, download))
content = _download_solc(download)
with open(binary_path, 'wb') as fp:
fp.write(content)
_chmod_plus_x(binary_path)


Expand All @@ -277,46 +281,44 @@ def _install_solc_windows(version):
install_folder = _check_for_installed_version(version)
if install_folder:
temp_path = _get_temp_folder()
LOGGER.info("Downloading solc {} from {}".format(version, download))
request = requests.get(download)
with zipfile.ZipFile(BytesIO(request.content)) as zf:
content = _download_solc(download)
with zipfile.ZipFile(BytesIO(content)) as zf:
zf.extractall(str(temp_path))
install_folder = get_solc_folder().joinpath("solc-" + version)
temp_path.rename(install_folder)


def _install_solc_osx(version, allow):
if version.startswith("v0.4") and not allow:
def _install_solc_osx(version, allow_osx):
if version.startswith("v0.4") and not allow_osx:
raise ValueError(
"Py-solc-x cannot build solc versions 0.4.x on OSX. If you install solc 0.4.x "
"using brew and reload solcx, the installed version will be available. "
"See https://github.com/ethereum/homebrew-ethereum for installation instructions.\n\n"
"To ignore this error, include 'allow_osx=True' when calling solcx.install_solc()"
)
temp_path = _get_temp_folder().joinpath("solc-source.tar.gz".format(version))
source_folder = _get_temp_folder().joinpath("solidity_" + version[1:])
temp_path = _get_temp_folder()
download = DOWNLOAD_BASE.format(version, "solidity_{}.tar.gz".format(version[1:]))
binary_path = _check_for_installed_version(version)
if not binary_path:
return

_wget(download, temp_path)
with tarfile.open(str(temp_path), "r") as tar:
tar.extractall(str(_get_temp_folder()))
content = _download_solc(download)
with tarfile.open(fileobj=BytesIO(content)) as tar:
tar.extractall(temp_path)
temp_path = temp_path.joinpath('solidity_{}'.format(version[1:]))

_check_subprocess_call(
["sh", str(source_folder.joinpath('scripts/install_deps.sh'))],
["sh", str(temp_path.joinpath('scripts/install_deps.sh'))],
message="Running dependency installation script `install_deps.sh` @ {}".format(temp_path)
)

original_path = os.getcwd()
source_folder.joinpath('build').mkdir(exist_ok=True)
os.chdir(str(source_folder.joinpath('build').resolve()))
temp_path.joinpath('build').mkdir(exist_ok=True)
os.chdir(str(temp_path.joinpath('build').resolve()))
try:
for cmd in (["cmake", ".."], ["make"]):
_check_subprocess_call(cmd, message="Running {}".format(cmd[0]))
os.chdir(original_path)
source_folder.joinpath('build/solc/solc').rename(binary_path)
temp_path.joinpath('build/solc/solc').rename(binary_path)
except subprocess.CalledProcessError as e:
raise OSError(
"{} returned non-zero exit status {}".format(cmd[0], e.returncode) +
Expand All @@ -325,7 +327,6 @@ def _install_solc_osx(version, allow):
)
finally:
os.chdir(original_path)
shutil.rmtree(str(temp_path.parent))

_chmod_plus_x(binary_path)

Expand Down

0 comments on commit 51b52e3

Please sign in to comment.