Check version in desugar library configuration

Test: tools/archive.py --dry-run --dry-run-output ooo
Bug: 134732760
Change-Id: Iac8967775c8edff79f9851863059365c1fc452fb
diff --git a/tools/archive_desugar_jdk_libs.py b/tools/archive_desugar_jdk_libs.py
index 6edbd98..3ca25a6 100755
--- a/tools/archive_desugar_jdk_libs.py
+++ b/tools/archive_desugar_jdk_libs.py
@@ -55,12 +55,8 @@
       raise Exception('Version file '
           + VERSION_FILE + ' is expected to have exactly one line')
     version = lines[0].strip()
-    reg = re.compile('^([0-9]+)\\.([0-9]+)\\.([0-9]+)$')
-    if not reg.match(version):
-      raise Exception('Invalid version \''
-            + version
-            + '\' in version file '
-            + VERSION_FILE)
+    utils.check_basic_semver_version(
+        version, 'in version file ' + VERSION_FILE)
     return version
 
 
diff --git a/tools/utils.py b/tools/utils.py
index 72cdbd4..152c1e7 100644
--- a/tools/utils.py
+++ b/tools/utils.py
@@ -558,4 +558,15 @@
     if not version:
       raise Exception(
           'No "version" found in ' + utils.DESUGAR_CONFIGURATION)
+    check_basic_semver_version(version, 'in ' + DESUGAR_CONFIGURATION)
     return version
+
+# Check that the passed string is formatted as a basic semver version (x.y.z)
+# See https://semver.org/.
+def check_basic_semver_version(version, error_context = ''):
+    reg = re.compile('^([0-9]+)\\.([0-9]+)\\.([0-9]+)$')
+    if not reg.match(version):
+      raise Exception("Invalid version '"
+            + version
+            + "'"
+            + (' ' + error_context) if len(error_context) > 0 else '')