Skip to content

Commit

Permalink
Refactor IDE code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nurchik committed May 24, 2021
1 parent 83aaa45 commit 51e4bab
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 87 deletions.
46 changes: 1 addition & 45 deletions mythx_cli/fuzz/ide/brownie.py
@@ -1,7 +1,6 @@
import json
import logging
from pathlib import Path
from typing import Dict, List
from typing import List

from mythx_cli.fuzz.exceptions import BuildArtifactsError
from mythx_cli.fuzz.ide.generic import IDEArtifacts, JobBuilder
Expand Down Expand Up @@ -84,49 +83,6 @@ def fetch_data(self, build_files_by_source_file):
}
return result_contracts, result_sources

@staticmethod
def _get_build_artifacts(build_dir) -> Dict:
"""Build indexes of Brownie build artifacts.
This function starts by loading the contents from the Brownie build artifacts json, found in the /build
folder, which contain the following data:
* :code: `allSourcePaths`
* :code: `deployedSourceMap`
* :code: `deployedBytecode`
* :code: `sourceMap`
* :code: `bytecode`
* :code: `contractName`
* :code: `sourcePath`
It then stores that data in two separate dictionaires, build_files and build_files_by_source_file. The first
is indexed by the compilation artifact file name (the json found in /build/*) and the second is indexed by the
source file (.sol) found in the .sourcePath property of the json.
"""
build_files_by_source_file = {}

build_dir = Path(build_dir)

if not build_dir.is_dir():
raise BuildArtifactsError("Build directory doesn't exist")

for child in build_dir.glob("**/*"):
if not child.is_file():
continue
if not child.name.endswith(".json"):
continue

data = json.loads(child.read_text("utf-8"))
source_path = data["sourcePath"]

if source_path not in build_files_by_source_file:
# initialize the array of contracts with a list
build_files_by_source_file[source_path] = []

build_files_by_source_file[source_path].append(data)

return build_files_by_source_file


class BrownieJob:
def __init__(
Expand Down
31 changes: 31 additions & 0 deletions mythx_cli/fuzz/ide/generic.py
@@ -1,6 +1,10 @@
import json
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Dict

from mythx_cli.fuzz.exceptions import BuildArtifactsError


class IDEArtifacts(ABC):
@property
Expand Down Expand Up @@ -32,6 +36,33 @@ def sources(self) -> Dict:
"""
pass

@staticmethod
def _get_build_artifacts(build_dir) -> Dict:
build_files_by_source_file = {}

build_dir = Path(build_dir)

if not build_dir.is_dir():
raise BuildArtifactsError("Build directory doesn't exist")

for child in build_dir.glob("**/*"):
if not child.is_file():
continue
if not child.name.endswith(".json"):
continue

data = json.loads(child.read_text("utf-8"))

source_path = data["sourcePath"]

if source_path not in build_files_by_source_file:
# initialize the array of contracts with a list
build_files_by_source_file[source_path] = []

build_files_by_source_file[source_path].append(data)

return build_files_by_source_file


class JobBuilder:
def __init__(self, artifacts: IDEArtifacts):
Expand Down
42 changes: 0 additions & 42 deletions mythx_cli/fuzz/ide/truffle.py
Expand Up @@ -170,48 +170,6 @@ def _get_project_sources(project_dir: str) -> Dict[str, List[str]]:
)
return contracts

@staticmethod
def _get_build_artifacts(build_dir) -> Dict:
"""Build indexes of Truffle build artifacts.
This function starts by loading the contents from the Truffle build artifacts json, found in the /build
folder, which contain the following data:
* :code: `deployedSourceMap`
* :code: `deployedBytecode`
* :code: `sourceMap`
* :code: `bytecode`
* :code: `contractName`
* :code: `sourcePath`
It then stores that data in build_files_by_source_file dictionary. The one is indexed by the
source file (.sol) found in the .sourcePath property of the json.
"""
build_files_by_source_file = {}

build_dir = Path(build_dir)

if not build_dir.is_dir():
raise BuildArtifactsError("Build directory doesn't exist")

for child in build_dir.glob("**/*"):
if not child.is_file():
continue
if not child.name.endswith(".json"):
continue

data = json.loads(child.read_text("utf-8"))

source_path = data["sourcePath"]

if source_path not in build_files_by_source_file:
# initialize the array of contracts with a list
build_files_by_source_file[source_path] = []

build_files_by_source_file[source_path].append(data)

return build_files_by_source_file

@property
def contracts(self):
return self._contracts
Expand Down

0 comments on commit 51e4bab

Please sign in to comment.