From 8626ce5d88ad453817bd4d322964196ebfda4043 Mon Sep 17 00:00:00 2001 From: Luigi Toscano Date: Mon, 17 Dec 2018 18:01:02 +0100 Subject: [PATCH] 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 --- os_api_ref/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/os_api_ref/__init__.py b/os_api_ref/__init__.py index 2fbdf40..c5ea2a2 100644 --- a/os_api_ref/__init__.py +++ b/os_api_ref/__init__.py @@ -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