From 6106019782fd3b6d323f1ed41e1ebc43bc623ac7 Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Tue, 26 Feb 2019 12:21:59 +0000 Subject: [PATCH 1/3] refactor(BaseFinance): Remove unused import --- contracts/Base/BaseFinance.sol | 1 - 1 file changed, 1 deletion(-) diff --git a/contracts/Base/BaseFinance.sol b/contracts/Base/BaseFinance.sol index 272723e..bff927d 100644 --- a/contracts/Base/BaseFinance.sol +++ b/contracts/Base/BaseFinance.sol @@ -2,7 +2,6 @@ pragma solidity 0.4.24; import "./Ownable.sol"; import "../AppCoins.sol"; -import "../Advertisement.sol"; import "./StorageUser.sol"; import "./SingleAllowance.sol"; import "openzeppelin-solidity/contracts/math/SafeMath.sol"; From dc615d621772ad2004f617f022d313bc9e8e8e4b Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Tue, 26 Feb 2019 12:23:35 +0000 Subject: [PATCH 2/3] build(*): Script to flatten contracts' code to a single file just call Flattener.py original.sol destination.sol --- scripts/Flattener.py | 131 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 131 insertions(+) create mode 100755 scripts/Flattener.py diff --git a/scripts/Flattener.py b/scripts/Flattener.py new file mode 100755 index 0000000..8299431 --- /dev/null +++ b/scripts/Flattener.py @@ -0,0 +1,131 @@ +#!/usr/local/bin/python3 +import re +import sys +import os + +class SolFile: + def __init__(self,name,pragma,code,dependencies): + self.pragma = pragma + self.name = name + self.code = code + self.dependencies = dependencies + + def get_name(self): + return self.name + + def get_pragma(self): + return self.pragma + + def get_code(self): + return self.code + + def get_dependencies(self): + return self.dependencies + + def get_total_dependencies(self): + return len(self.dependencies) + +def find_imports(content): + libRegEx = r"import\s*\{.*\} from \"(.*)\"" + normalImportRegEx = r"import\s*\"(.*)\"" + result = re.findall(libRegEx,content) + result+= re.findall(normalImportRegEx,content) + return result + +def get_pragma(content): + pragmaRegEx = r"pragma .*;" + return re.findall(pragmaRegEx,content)[0]; + +def extract_code(content): + final_result = '' + codeDetectorRegEx = r".*" + result = re.findall(codeDetectorRegEx,content,re.MULTILINE) + + for line in result: + if line.startswith('import') or line.startswith('pragma') or line == "": + continue + removed_dev = re.sub(r".*@dev.*","",line) + final_result += removed_dev + "\n" + return final_result + +def get_file_content(filePath): + with open(filePath,"r") as f: + return f.read() + +def get_imported_files(initialdir,imports,dependencies,alreadyImported): + total_new_imports = [] + + for filePath in imports: + changed_dir = False + new_abs_imports = [] + + if filePath in alreadyImported: + continue + + filePath = os.path.abspath(filePath) + directoryPath = os.path.split(filePath)[0] + + # Checking if we are on the correct directory + if os.getcwd().split('/')[-1] != filePath.split('/')[-2]: + changed_dir = True + os.chdir(directoryPath) + + alreadyImported.append(filePath) + content = get_file_content(filePath) + new_imports = find_imports(content) + pragma = get_pragma(content) + + for imports in new_imports: + path = '' + if not imports.startswith('.'): + # Using node_modules if the contract is on an external library + path = initialdir+'/../node_modules/'+imports + else: + path = os.path.abspath(imports) + + new_abs_imports.append(path) + total_new_imports.append(path) + + code = extract_code(content) + solCode = SolFile(filePath,pragma,code,new_abs_imports) + + if solCode.get_total_dependencies() not in dependencies: + dependencies[solCode.get_total_dependencies()] = [] + dependencies[solCode.get_total_dependencies()].append(solCode) + + if changed_dir: + changed_dir = False + os.chdir(initialdir) + + return total_new_imports + +if __name__ == "__main__": + + dependencies = dict() + filePath = os.path.abspath(sys.argv[1]) + directoryPath = os.path.split(filePath)[0] + initialPath = os.getcwd() + os.chdir(directoryPath) + + imports = [filePath] + alreadyImported = [] + + while len(imports) != 0: + # if no new imports are found all dependencies are calculated + imports = get_imported_files(directoryPath,imports,dependencies,alreadyImported) + + dependencyNumbers = list(dependencies.keys()) + dependencyNumbers.sort() + + code = '' + + for key in dependencyNumbers: + for file in dependencies[key]: + #FIXME check dependencies within the same dependency number + if code == '': + code += file.get_pragma() + "\n\n"; + code += file.get_code() + "\n" + os.chdir(initialPath) + with open(sys.argv[2],'w+') as f: + f.write(code) + From 3b0320cbeece1610c13aadc538a5f2506babc8f9 Mon Sep 17 00:00:00 2001 From: Tiago Rosado Date: Tue, 26 Feb 2019 12:42:19 +0000 Subject: [PATCH 3/3] build(*): Add Help text when incorrect arguments are given to Flattener script --- scripts/Flattener.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/scripts/Flattener.py b/scripts/Flattener.py index 8299431..a3a8983 100755 --- a/scripts/Flattener.py +++ b/scripts/Flattener.py @@ -100,7 +100,13 @@ def get_imported_files(initialdir,imports,dependencies,alreadyImported): return total_new_imports if __name__ == "__main__": - + + if len(sys.argv) < 3: + print('use Flattener.py origin.sol dest.sol') + print('\torigin.sol is the original file which will be flattened') + print('\tdest.sol is the path of the new flattened code') + exit(1) + dependencies = dict() filePath = os.path.abspath(sys.argv[1]) directoryPath = os.path.split(filePath)[0]