Skip to content

Commit

Permalink
update script: Add --reuse-hashes option to allow skipping hash recal…
Browse files Browse the repository at this point in the history
…culation when list.json already exists
  • Loading branch information
cameel committed Jul 28, 2020
1 parent e21e303 commit c22e150
Showing 1 changed file with 60 additions and 17 deletions.
77 changes: 60 additions & 17 deletions update
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ function updateCopy (srcRelativeToRoot, destRelativeToRoot) {
})
}

function readFile (dir, file) {
return fs.readFileSync(path.join(__dirname, dir, file))
}

function buildVersion (build) {
let version = build.version
if (build.prerelease && build.prerelease.length > 0) {
Expand All @@ -89,7 +85,10 @@ function buildVersion (build) {
return version
}

function makeEntry (dir, parsedFileName) {
function makeEntry (dir, parsedFileName, oldList) {
const pathRelativeToRoot = path.join(dir, parsedFileName[0])
const absolutePath = path.join(__dirname, pathRelativeToRoot)

const build = {
path: parsedFileName[0],
version: parsedFileName[1],
Expand All @@ -98,19 +97,45 @@ function makeEntry (dir, parsedFileName) {
}
build.longVersion = buildVersion(build)

const fileContent = readFile(dir, parsedFileName.path)
parsedFileName.longVersion = buildVersion(parsedFileName)
parsedFileName.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
parsedFileName.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]
return parsedFileName
if (oldList) {
const entries = oldList.builds.filter(entry => (entry.path === parsedFileName[0]))
if (entries) {
if (entries.length >= 2) {
throw Error("Found multiple list.json entries for binary '" + pathRelativeToRoot + "'")
} else if (entries.length === 1) {
build.keccak256 = entries[0].keccak256
build.urls = entries[0].urls
}
}
}

if (!build.keccak256 || !build.urls) {
const fileContent = fs.readFileSync(absolutePath)
build.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
build.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]

console.log("Hashing '" + pathRelativeToRoot + "'")
}

return build
}

function processDir (dir, listCallback) {
function processDir (dir, options, listCallback) {
fs.readdir(path.join(__dirname, dir), { withFileTypes: true }, function (err, files) {
if (err) {
throw err
}

let oldList
if (options.reuseHashes) {
try {
oldList = JSON.parse(fs.readFileSync(path.join(__dirname, dir, '/list.json')))
} catch (err) {
// Not being able to read the existing list is not a critical error.
// We'll just recreate it from scratch.
}
}

const binaryPrefix = (dir === '/bin' || dir === '/wasm' ? 'soljson' : 'solc-' + dir.slice(1))
const binaryExtension = {
'/bin': '.js',
Expand All @@ -135,7 +160,7 @@ function processDir (dir, listCallback) {
return file.match(new RegExp('^' + binaryPrefix + '-v([0-9.]*)(-([^+]*))?(\\+(.*))?' + escapedExtension + '$'))
})
.filter(function (version) { return version })
.map(function (pars) { return makeEntry(dir, pars) })
.map(function (pars) { return makeEntry(dir, pars, oldList) })
.sort(function (a, b) {
if (a.longVersion === b.longVersion) {
return 0
Expand Down Expand Up @@ -227,18 +252,36 @@ function processDir (dir, listCallback) {
})
}

function parseCommandLine () {
if (process.argv.length > 3) {
console.error('Expected at most one argument, got ' + (process.argv.length - 2))
process.exit(1)
}

if (process.argv.length === 3 && process.argv[2] !== '--reuse-hashes') {
console.error('Invalid argument: ' + process.argv[2])
process.exit(1)
}

return {
reuseHashes: process.argv.length === 3 && process.argv[2] === '--reuse-hashes'
}
}

const DIRS = [
'/bin',
'/linux-amd64',
'/macosx-amd64',
'/windows-amd64'
]

const options = parseCommandLine()

DIRS.forEach(function (dir) {
if (dir !== '/bin') {
processDir(dir)
processDir(dir, options)
} else {
processDir(dir, function (parsedList) {
processDir(dir, options, function (parsedList) {
// Any new releases added to bin/ need to be linked in other directories before we can start processing them.
parsedList.forEach(function (release) {
if (release.prerelease === undefined) {
Expand All @@ -257,8 +300,8 @@ DIRS.forEach(function (dir) {
}
})

processDir('/emscripten-asmjs')
processDir('/wasm', function (parsedList) {
processDir('/emscripten-asmjs', options)
processDir('/wasm', options, function (parsedList) {
// Any new releases added to wasm/ need to be linked in emscripten-wasm32/ first.
parsedList.forEach(function (release) {
if (release.prerelease === undefined) {
Expand All @@ -269,7 +312,7 @@ DIRS.forEach(function (dir) {
}
})

processDir('/emscripten-wasm32')
processDir('/emscripten-wasm32', options)
})
})
}
Expand Down

0 comments on commit c22e150

Please sign in to comment.