Warn about missing brkitr/coll/rbnf data corresponding to an included locale.

This makes it easier to check that we haven't accidentally forgotten to include
something. (At the moment, it warns that -large is missing the el and th break
iterator data, which we knew.)

I've also added an explicit error message for the case where you haven't set up
your environment, and made sure we exit with a failure status code on failure.

Bug: 2587066
Change-Id: Id1064b7d03f51909a3a7fc1fc28ce5c14003c678
diff --git a/stubdata/icu_dat_generator.py b/stubdata/icu_dat_generator.py
index c78ac1e..ae6019d 100755
--- a/stubdata/icu_dat_generator.py
+++ b/stubdata/icu_dat_generator.py
@@ -102,25 +102,44 @@
                 ICUDATA_DAT])
 
 
+def WriteIndex(path, list, cldr_version = None):
+  res_index = "res_index.txt"
+  empty_value = " {\"\"}\n"  # key-value pair for all locale entries
+
+  f = open(path, "w")
+  f.write("res_index:table(nofallback) {\n")
+  if cldr_version:
+    f.write("  CLDRVersion { %s }\n" % cldr_version)
+  f.write("  InstalledLocales {\n")
+  for item in list:
+    f.write(item + empty_value)
+
+  f.write("  }\n")
+  f.write("}\n")
+  f.close()
+
+
+def ShowMissing(whats, locales, data_dir_name, dat_file):
+  if not len(whats):
+    return
+  dat_file = os.path.basename(dat_file)
+  for missing in locales.difference(whats):
+    p = os.path.join(ANDROID_ROOT, "external", "icu4c", "data", data_dir_name,
+                     missing + ".txt")
+    if os.path.exists(p):
+      print "warning: %s exists but isn't included in %s" % (p, dat_file)
+
+
 # Open dat file such as icudt42l-us.txt.
 # Go through the list and generate res_index.txt for locales, brkitr,
 # coll and rbnf.
 def GenResIndex(dat_list_file_path):
   res_index = "res_index.txt"
-  header_locale = "res_index:table(nofallback) {\n CLDRVersion { "
-  header_locale += CLDR_VERSION + " }\n InstalledLocales {\n"
-  header = "res_index:table(nofallback) {\nInstalledLocales {\n"
-  footer = "    }\n}"
-  empty_value = " {\"\"}\n"  # key-value pair for all locale entries
 
-  locale_index = open(os.path.join(TMP_DAT_PATH, res_index), "w")
-  locale_index.write(header_locale)
-  brkitr_index = open(os.path.join(TMP_DAT_PATH, "brkitr", res_index), "w")
-  brkitr_index.write(header)
-  coll_index = open(os.path.join(TMP_DAT_PATH, "coll", res_index), "w")
-  coll_index.write(header)
-  rbnf_index = open(os.path.join(TMP_DAT_PATH, "rbnf", res_index), "w")
-  rbnf_index.write(header)
+  locales = set()
+  brkitrs = set()
+  colls = set()
+  rbnfs = set()
 
   for line in open(dat_list_file_path, "r"):
     if line.find("root.") >= 0:
@@ -129,45 +148,40 @@
       continue
     if line.find("_.res") >= 0:
       continue;
-    start = line.find("brkitr/")
-    if start >= 0:
+    if line.find("brkitr/") >= 0:
       end = line.find(".res")
       if end > 0:
-        brkitr_index.write(line[line.find("/")+1:end] + empty_value)
+        brkitrs.add(line[line.find("/")+1:end])
     elif line.find("coll/") >= 0:
-      start = line.find("coll/")
       end = line.find(".res")
       if end > 0:
-        coll_index.write(line[line.find("/")+1:end] + empty_value)
+        colls.add(line[line.find("/")+1:end])
     elif line.find("rbnf/") >= 0:
-      start = line.find("rbnf/")
       end = line.find(".res")
       if end > 0:
-        rbnf_index.write(line[line.find("/")+1:end] + empty_value)
+        rbnfs.add(line[line.find("/")+1:end])
     elif line.find(".res") >= 0:
       # We need to determine the resource is locale resource or misc resource.
       # To determine the locale resource, we assume max script length is 3.
       end = line.find(".res")
-      if end <= 3:
-        locale_index.write(line[:end] + empty_value)
-      elif line.find("_") <= 3:
-        if line.find("_") > 0:
-          locale_index.write(line[:end] + empty_value)
+      if end <= 3 or (line.find("_") <= 3 and line.find("_") > 0):
+        locales.add(line[:end])
 
-  locale_index.write(footer)
-  brkitr_index.write(footer)
-  coll_index.write(footer)
-  rbnf_index.write(footer)
-  locale_index.close()
-  brkitr_index.close()
-  coll_index.close()
-  rbnf_index.close()
+  ShowMissing(brkitrs, locales, "brkitr", dat_list_file_path)
+  ShowMissing(colls, locales, "coll", dat_list_file_path)
+  ShowMissing(rbnfs, locales, "rbnf", dat_list_file_path)
+
+  WriteIndex(os.path.join(TMP_DAT_PATH, res_index), locales, CLDR_VERSION)
+  WriteIndex(os.path.join(TMP_DAT_PATH, "brkitr", res_index), brkitrs)
+  WriteIndex(os.path.join(TMP_DAT_PATH, "coll", res_index), colls)
+  WriteIndex(os.path.join(TMP_DAT_PATH, "rbnf", res_index), rbnfs)
 
   # Call genrb to generate new res_index.res.
   InvokeIcuTool("genrb", TMP_DAT_PATH, [res_index])
   InvokeIcuTool("genrb", os.path.join(TMP_DAT_PATH, "brkitr"), [res_index])
   InvokeIcuTool("genrb", os.path.join(TMP_DAT_PATH, "coll"), [res_index])
-  InvokeIcuTool("genrb", os.path.join(TMP_DAT_PATH, "rbnf"), [res_index])
+  if len(rbnfs):
+    InvokeIcuTool("genrb", os.path.join(TMP_DAT_PATH, "rbnf"), [res_index])
 
 
 def CopyAndroidCnvFiles(icu_dat_path):
@@ -193,7 +207,7 @@
   global ICUDATA_DAT   # e.g. "icudt42l.dat"
   global CLDR_VERSION  # CLDR version. The value can be vary upon ICU release.
   global TMP_DAT_PATH  # temp directory to store all resource files and
-                       # intermittent dat files.
+                       # intermediate dat files.
   global HELP
   global VERBOSE
 
@@ -201,15 +215,18 @@
   if argc < 2:
     print "You must provide icu version number."
     print "Example: ./icu_dat_generator.py 4.2"
-    return
+    sys.exit(1)
   ICU_VERSION = sys.argv[1]
   version = GetIcuVersion(ICU_VERSION)
   if (version ==  -1):
     print sys.argv[1] + " is not a valid icu version number!"
-    return
+    sys.exit(1)
   ICUDATA = "icudt" + version + "l"
   CLDR_VERSION = "1.7"
   ANDROID_ROOT = os.environ.get("ANDROID_BUILD_TOP")
+  if not ANDROID_ROOT:
+    print "$ANDROID_BUILD_TOP not set! Run 'env_setup.sh'."
+    sys.exit(1)
   ICUDATA_DAT = ICUDATA + ".dat"
   HELP = False
   VERBOSE = False
@@ -219,11 +236,11 @@
   except getopt.error:
     print "Invalid option"
     PrintHelp()
-    return
+    sys.exit(1)
   for opt, arg in opts:
     if opt in ('-h', '--help'):
       PrintHelp()
-      return
+      sys.exit(1)
     elif opt in ('-v', '--verbose'):
       VERBOSE = True
 
@@ -233,7 +250,7 @@
   full_data_filename = os.path.join(icu_dat_path, ICUDATA + "-all.dat")
   if not os.path.isfile(full_data_filename):
     print "%s not present." % full_data_filename
-    return
+    sys.exit(1)
 
   # Create a temporary working directory.
   TMP_DAT_PATH = os.path.join(ANDROID_ROOT, "external", "icu4c", "tmp")