Skip to content

Commit

Permalink
Merge pull request #120 from iamdefinitelyahuman/feat-commit-hash
Browse files Browse the repository at this point in the history
Include commit hash in `get_solc_version`
  • Loading branch information
iamdefinitelyahuman authored Dec 28, 2020
2 parents bdb35d7 + 3392e9e commit 0c23da7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
8 changes: 7 additions & 1 deletion docs/version-management.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,21 @@ Getting the Active Version

Use the following methods to check the active ``solc`` version:

.. py:function:: solcx.get_solc_version()
.. py:function:: solcx.get_solc_version(with_commit_hash=False)
Return the version of the current active ``solc`` binary, as a :py:class:`Version <semantic_version.Version>` object.

* ``with_commit_hash``: If ``True``, the returned version includes the commit hash

.. code-block:: python
>>> solcx.get_solc_version()
Version('0.7.0')
>>> solcx.get_solc_version(True)
Version('0.7.0+commit.9e61f92b')
.. py:function:: solcx.install.get_executable(version=None, solcx_binary_path=None)
Return a :py:class:`Path <pathlib.PurePath>` object for a ``solc`` binary.
Expand Down
11 changes: 7 additions & 4 deletions solcx/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@
from solcx.exceptions import ContractsNotFound, SolcError
from solcx.install import get_executable

# from solcx.wrapper import _get_solc_version, solc_wrapper


def get_solc_version() -> Version:
def get_solc_version(with_commit_hash: bool = False) -> Version:
"""
Get the version of the active `solc` binary.
Arguments
---------
with_commit_hash : bool, optional
If True, the commit hash is included within the version
Returns
-------
Version
solc version
"""
solc_binary = get_executable()
return wrapper._get_solc_version(solc_binary)
return wrapper._get_solc_version(solc_binary, with_commit_hash)


def compile_source(
Expand Down
17 changes: 13 additions & 4 deletions solcx/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,24 @@
from solcx import install
from solcx.exceptions import SolcError, UnknownOption, UnknownValue

# (major.minor.patch)(nightly)(commit)
VERSION_REGEX = r"(\d+\.\d+\.\d+)(?:-nightly.\d+.\d+.\d+|)(\+commit.\w+)"

def _get_solc_version(solc_binary: Union[Path, str]) -> Version:

def _get_solc_version(solc_binary: Union[Path, str], with_commit_hash: bool = False) -> Version:
# private wrapper function to get `solc` version
stdout_data = subprocess.check_output([str(solc_binary), "--version"], encoding="utf8")
try:
version_str = re.findall(r"\d+\.\d+\.\d+", stdout_data)[0]
except IndexError:
match = next(re.finditer(VERSION_REGEX, stdout_data))
version_str = "".join(match.groups())
except StopIteration:
raise SolcError("Could not determine the solc binary version")
return Version.coerce(version_str)

version = Version.coerce(version_str)
if with_commit_hash:
return version
else:
return version.truncate()


def _to_string(key: str, value: Any) -> str:
Expand Down
8 changes: 8 additions & 0 deletions tests/install/test_solc_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,11 @@ def test_install_solc_version_pragma(pragmapatch):
assert install_pragma("pragma solidity ^0.4.2 || >=0.5.4<0.7.0;") == Version("0.6.0")
with pytest.raises(UnsupportedVersionError):
install_pragma("pragma solidity ^0.7.1;")


def test_get_solc_version():
version = solcx.get_solc_version()
version_with_hash = solcx.get_solc_version(with_commit_hash=True)

assert version != version_with_hash
assert version == version_with_hash.truncate()

0 comments on commit 0c23da7

Please sign in to comment.