Generate stable targets rather than random

A random value can still easily hit collision, and at least
one was found easily on the gates (gone with a recheck).
Moreover, the usage of a random target does not allow for ensuring
reproducible builds: the usage of an hash build from the node
content makes sure that the target is stable across different
runs of api-ref generation.

Change-Id: I3fcd8a4e5b0a66c9dbf34f4a4c472f3c93c46bb8
This commit is contained in:
Luigi Toscano 2018-12-17 18:01:02 +01:00
parent 4e56d09daf
commit 8626ce5d88
1 changed files with 6 additions and 3 deletions

View File

@ -11,8 +11,8 @@
# under the License.
from collections import OrderedDict
import hashlib
import os
import random
import re
from docutils import nodes
@ -199,8 +199,11 @@ class RestMethodDirective(rst.Directive):
# We need to build a temporary target that we can replace
# later in the processing to get the TOC to resolve correctly.
temp_target = "%s-%d-selector" % (node['target'],
random.randint(1, 1000))
# SHA-1 is used even if collisions are possible, because
# they are still unlikely to occurr and it is way shorter
# than stronger SHAs.
node_hash = hashlib.sha1(str(node).encode('utf-8')).hexdigest()
temp_target = "%s-%s-selector" % (node['target'], node_hash)
target = nodes.target(ids=[temp_target])
self.state.add_target(temp_target, '', target, lineno)
section += node