def Create3dLayerInfoFile(self, layers_info, base_glb_info):
    """Build 3d layerinfo.txt file."""
    layer_info_content = ""
    dbroot_layer_info_content = ""
    layer_index = 1
    if (base_glb_info and
        "base_glb_path" in base_glb_info.keys() and
        base_glb_info["base_glb_path"]):
      layer_info_content += "%s %s %s %d 0\n" % (
          "base_globe", base_glb_info["base_glb_path"], "IMAGE", layer_index)
      dbroot_layer_info_content = layer_info_content

    layer_index += 1
    layer_info_files = self.LayerInfoFiles(layer_index, "IMAGE", layers_info)
    layer_info_content = "%s%s" % (layer_info_content, layer_info_files)

    layer_info_content += "%s %s %s 0 0\n" % (
        "dbRoot", REL_META_DBROOT_FILE, "DBROOT")

    utils.CreateFile(self.layers_info_file, layer_info_content)

    dbroot_layer_info_content += (self.DbrootLayerInfo(layer_index,
                                                       "IMAGE",
                                                       layers_info))

    utils.CreateFile(self.dbroot_layers_info_file, dbroot_layer_info_content)
 def Create2dLayerInfoFile(self, layers_info, glms, projection):
   """Build 2d layerinfo.txt file."""
   layer_info_content = self.LayerInfoFiles(
       GLM_STARTING_INDEX, "MAP", glms)
   self.CreateMapJson(GLM_STARTING_INDEX, layers_info, projection)
   layer_info_content += "map.json maps/map.json FILE 0 0\n"
   utils.CreateFile(self.layers_info_file, layer_info_content)
  def AssembleGlc(self, form_):
    """Assemble a 2d or 3d glc based on given parameters."""
    utils.PrintAndLog("Assembling ...")
    try:
      spec_json = form_.getvalue_json("glc_assembly_spec")
      spec = json.loads(spec_json)
    except Exception as e:
      utils.PrintAndLog("Failed to parse glc assembly json:" + str(e))
      raise e

    msg = ""
    try:
      # Prepare (create and copy) directories.
      base = form_.getvalue_path("base")
      name = form_.getvalue_filename("name")
      path = form_.getvalue_path("path")
      overwriting = form_.getvalue("overwrite")
      logger = self.PrepareAssembler(base)

      # Extra check to prevent XSS attack.
      if self.IsPathInvalid({"base": base,
                             "path": os.path.dirname(path),
                            }):
        return ""

      if overwriting:
        utils.PrintAndLog("*** Overwriting %s ***" % name, logger)
        try:
          self.DeleteGlc(path)
          utils.PrintAndLog("SUCCESS", logger, None)
        except IOError:
          utils.PrintAndLog("FAILED", logger, None)
          raise

      # Create file to reserve name.
      utils.PrintAndLog("Reserve file: %s" % path, logger)
      fp = open(path, "w")
      fp.close()
      utils.PrintAndLog("SUCCESS", logger, None)

      layers_info = spec["layers"]
      if spec["is_2d"]:
        try:
          glms = spec["glms"]
        except KeyError:
          glms = None
      else:
        try:
          base_glb_info = spec["base_glb"]
        except KeyError:
          base_glb_info = None

      utils.PrintAndLog("Create info file: %s" % self.info_file, logger)
      os.chdir(self.base_path)
      utils.CreateInfoFile(self.info_file,
                           utils.HtmlEscape(spec["description"]))
      utils.PrintAndLog("SUCCESS", logger, None)

      utils.PrintAndLog("Create polygon file: %s" % self.polygon_file, logger)
      utils.CreateFile(self.polygon_file, spec["polygon"])
      utils.PrintAndLog("SUCCESS", logger, None)

      if spec["is_2d"]:
        utils.PrintAndLog("Building 2d glc at %s ..." % path, logger)
        self.Assemble2dGlcFiles(layers_info, glms, logger)
      else:
        utils.PrintAndLog("Building 3d glc at %s ..." % path, logger)
        self.Assemble3dGlcFiles(layers_info, base_glb_info, logger)

      # --make_copy is needed because we are using another volume.
      os_cmd = ("%s/geportableglcpacker "
                "--layer_info=\"%s\" "
                "--output=\"%s\" --make_copy"
                % (COMMAND_DIR, self.layers_info_file, self.temp_glc))
      utils.ExecuteCmdInBackground(os_cmd, logger)

    except OsCommandError as e:
      utils.PrintAndLog("Error: Unable to run OS command.", logger)
      msg = "FAILED"
    except:
      utils.PrintAndLog("Exception: {0}".format(traceback.format_exc()))
      msg = "FAILED"
    return msg