Skip to content

Commit ce22f02

Browse files
authored
Merge pull request #50 from ethereum/update-script-reuse-hashes
Add --reuse-hashes option to update script
2 parents 26910cf + 68b1834 commit ce22f02

File tree

1 file changed

+91
-39
lines changed

1 file changed

+91
-39
lines changed

update

+91-39
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ const swarmhash = require('swarmhash')
1111
// This script updates the index files list.js and list.txt in the directories containing binaries,
1212
// as well as the 'latest' and 'nightly' symlinks/files.
1313

14+
function generateLegacyListJS (builds, releases) {
15+
return `
16+
var soljsonSources = ${JSON.stringify(builds, null, 2)};
17+
var soljsonReleases = ${JSON.stringify(releases, null, 2)};
18+
19+
if (typeof(module) !== 'undefined')
20+
module.exports = {
21+
'allVersions': soljsonSources,
22+
'releases': soljsonReleases
23+
};
24+
`
25+
}
26+
1427
function updateSymlinkSync (linkPathRelativeToRoot, targetRelativeToLink) {
1528
const absoluteLinkPath = path.join(__dirname, linkPathRelativeToRoot)
1629
let linkString
@@ -61,25 +74,66 @@ function updateCopy (srcRelativeToRoot, destRelativeToRoot) {
6174
})
6275
}
6376

64-
function processDir (dir, listCallback) {
77+
function buildVersion (build) {
78+
let version = build.version
79+
if (build.prerelease && build.prerelease.length > 0) {
80+
version += '-' + build.prerelease
81+
}
82+
if (build.build && build.build.length > 0) {
83+
version += '+' + build.build
84+
}
85+
return version
86+
}
87+
88+
function makeEntry (dir, parsedFileName, oldList) {
89+
const pathRelativeToRoot = path.join(dir, parsedFileName[0])
90+
const absolutePath = path.join(__dirname, pathRelativeToRoot)
91+
92+
const build = {
93+
path: parsedFileName[0],
94+
version: parsedFileName[1],
95+
prerelease: parsedFileName[3],
96+
build: parsedFileName[5]
97+
}
98+
build.longVersion = buildVersion(build)
99+
100+
if (oldList) {
101+
const entries = oldList.builds.filter(entry => (entry.path === parsedFileName[0]))
102+
if (entries) {
103+
if (entries.length >= 2) {
104+
throw Error("Found multiple list.json entries for binary '" + pathRelativeToRoot + "'")
105+
} else if (entries.length === 1) {
106+
build.keccak256 = entries[0].keccak256
107+
build.urls = entries[0].urls
108+
}
109+
}
110+
}
111+
112+
if (!build.keccak256 || !build.urls) {
113+
const fileContent = fs.readFileSync(absolutePath)
114+
build.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
115+
build.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]
116+
117+
console.log("Hashing '" + pathRelativeToRoot + "'")
118+
}
119+
120+
return build
121+
}
122+
123+
function processDir (dir, options, listCallback) {
65124
fs.readdir(path.join(__dirname, dir), { withFileTypes: true }, function (err, files) {
66125
if (err) {
67126
throw err
68127
}
69128

70-
function buildVersion (build) {
71-
var version = build.version
72-
if (build.prerelease && build.prerelease.length > 0) {
73-
version += '-' + build.prerelease
74-
}
75-
if (build.build && build.build.length > 0) {
76-
version += '+' + build.build
129+
let oldList
130+
if (options.reuseHashes) {
131+
try {
132+
oldList = JSON.parse(fs.readFileSync(path.join(__dirname, dir, '/list.json')))
133+
} catch (err) {
134+
// Not being able to read the existing list is not a critical error.
135+
// We'll just recreate it from scratch.
77136
}
78-
return version
79-
}
80-
81-
function readFile (file) {
82-
return fs.readFileSync(path.join(__dirname, dir, file))
83137
}
84138

85139
const binaryPrefix = (dir === '/bin' || dir === '/wasm' ? 'soljson' : 'solc-' + dir.slice(1))
@@ -106,14 +160,7 @@ function processDir (dir, listCallback) {
106160
return file.match(new RegExp('^' + binaryPrefix + '-v([0-9.]*)(-([^+]*))?(\\+(.*))?' + escapedExtension + '$'))
107161
})
108162
.filter(function (version) { return version })
109-
.map(function (pars) { return { path: pars[0], version: pars[1], prerelease: pars[3], build: pars[5] } })
110-
.map(function (pars) {
111-
const fileContent = readFile(pars.path)
112-
pars.longVersion = buildVersion(pars)
113-
pars.keccak256 = '0x' + ethUtil.keccak(fileContent).toString('hex')
114-
pars.urls = ['bzzr://' + swarmhash(fileContent).toString('hex')]
115-
return pars
116-
})
163+
.map(function (pars) { return makeEntry(dir, pars, oldList) })
117164
.sort(function (a, b) {
118165
if (a.longVersion === b.longVersion) {
119166
return 0
@@ -205,18 +252,36 @@ function processDir (dir, listCallback) {
205252
})
206253
}
207254

255+
function parseCommandLine () {
256+
if (process.argv.length > 3) {
257+
console.error('Expected at most one argument, got ' + (process.argv.length - 2))
258+
process.exit(1)
259+
}
260+
261+
if (process.argv.length === 3 && process.argv[2] !== '--reuse-hashes') {
262+
console.error('Invalid argument: ' + process.argv[2])
263+
process.exit(1)
264+
}
265+
266+
return {
267+
reuseHashes: process.argv.length === 3 && process.argv[2] === '--reuse-hashes'
268+
}
269+
}
270+
208271
const DIRS = [
209272
'/bin',
210273
'/linux-amd64',
211274
'/macosx-amd64',
212275
'/windows-amd64'
213276
]
214277

278+
const options = parseCommandLine()
279+
215280
DIRS.forEach(function (dir) {
216281
if (dir !== '/bin') {
217-
processDir(dir)
282+
processDir(dir, options)
218283
} else {
219-
processDir(dir, function (parsedList) {
284+
processDir(dir, options, function (parsedList) {
220285
// Any new releases added to bin/ need to be linked in other directories before we can start processing them.
221286
parsedList.forEach(function (release) {
222287
if (release.prerelease === undefined) {
@@ -235,8 +300,8 @@ DIRS.forEach(function (dir) {
235300
}
236301
})
237302

238-
processDir('/emscripten-asmjs')
239-
processDir('/wasm', function (parsedList) {
303+
processDir('/emscripten-asmjs', options)
304+
processDir('/wasm', options, function (parsedList) {
240305
// Any new releases added to wasm/ need to be linked in emscripten-wasm32/ first.
241306
parsedList.forEach(function (release) {
242307
if (release.prerelease === undefined) {
@@ -247,21 +312,8 @@ DIRS.forEach(function (dir) {
247312
}
248313
})
249314

250-
processDir('/emscripten-wasm32')
315+
processDir('/emscripten-wasm32', options)
251316
})
252317
})
253318
}
254319
})
255-
256-
function generateLegacyListJS (builds, releases) {
257-
return `
258-
var soljsonSources = ${JSON.stringify(builds, null, 2)};
259-
var soljsonReleases = ${JSON.stringify(releases, null, 2)};
260-
261-
if (typeof(module) !== 'undefined')
262-
module.exports = {
263-
'allVersions': soljsonSources,
264-
'releases': soljsonReleases
265-
};
266-
`
267-
}

0 commit comments

Comments
 (0)