def PrepareAssembler(self, base_path):
    """Set up build directory and paths to be used by assembler."""
    self.base_path = base_path
    self.maps_data_dir = MAPS_DATA_DIR % self.base_path
    self.earth_data_dir = EARTH_DATA_DIR % self.base_path
    self.search_dir = SEARCH_DIR % self.base_path
    self.kml_dir = KML_DIR % self.base_path
    self.polygon_file = POLYGON_FILE % self.base_path
    self.meta_dbroot_file = META_DBROOT_FILE % self.base_path
    self.map_json_file = MAP_JSON_FILE % self.base_path
    self.extract_map_json_file = EXTRACT_MAP_JSON_FILE % self.base_path
    self.layers_info_file = LAYERS_INFO_FILE % self.base_path
    self.dbroot_layers_info_file = DBROOT_LAYERS_INFO_FILE % self.base_path
    self.info_file = INFO_FILE % self.base_path
    self.temp_glc = TEMP_GLC_FILE % self.base_path

    logger = self.GetLogger()
    utils.PrintAndLog("Prepare assembler...", logger)
    utils.PrintAndLog("Base dir: %s" % self.base_path, logger)
    utils.CreateDirectory(self.base_path)
    utils.CopyDirectory("%s/earth" % TEMPLATE_DIR, self.earth_data_dir, logger)
    utils.CopyDirectory("%s/maps" % TEMPLATE_DIR, self.maps_data_dir, logger)
    utils.CreateDirectory(self.search_dir)
    utils.CreateDirectory(self.kml_dir)
    utils.PrintAndLog("Prepare assembler done.", logger)
    return logger
Example #2
0
 def WritePolygonFile(self, polygon, logger):
     with open(self.polygon_file, "w") as fp:
         # Check XML validity and standardize representation
         utils.PrintAndLog("Checking polygon")
         xml = etree.ElementTree(etree.fromstring(str(polygon)))
         utils.PrintAndLog("Writing polygon")
         xml.write(fp, xml_declaration=True, encoding='UTF-8')
         utils.PrintAndLog("SUCCESS", logger, None)
  def PrepareDisassembler(self, base_path, glc_path):
    """Set up build directory and paths to be used by disassembler."""
    self.base_path = base_path
    self.output_dir = base_path
    self.glc_path = glc_path
    self.info_file = INFO_FILE_LOG % self.base_path
   
    logger = self.GetLogger()
    utils.PrintAndLog("Prepare disassembler...", logger)
    utils.PrintAndLog("Output dir: %s" % self.base_path, logger)
    utils.PrintAndLog("Glc path: %s" % self.glc_path, logger)

    utils.PrintAndLog("Prepare disassembler done.", logger)
    return logger
 def IsPathInvalid(self, paths):
   """Prevent XSS attack by double-checking paths are real file paths."""
   for path in paths:
     if not os.path.exists(paths[path]):
       utils.PrintAndLog("ERROR: invalid \'%s\': %s" % (path, paths[path]))
       return True
   return False
    def CleanUp(self, form_):
        """Move temp glc to final location and remove temp folder if needed."""
        base = form_.getvalue_path("base")
        temp_glc = TEMP_GLC_FILE % base
        final_glc = form_.getvalue_path("globe")
        cleanup = form_.getvalue("cleanup") is not None
        logger = self.GetLogger(base)
        utils.PrintAndLog(
            "Moving temp glc %s -> %s ..." % (temp_glc, final_glc), logger)
        shutil.move(temp_glc, final_glc)
        utils.PrintAndLog("SUCCESS", logger, None)

        if cleanup:
            utils.PrintAndLog("Deleting %s" % base, logger)
            shutil.rmtree(base)
            utils.PrintAndLog("SUCCESS", logger, None)

        return ""
  def Assemble2dGlcFiles(self, layers_info, glms, logger):
    """Assemble glms into a glc."""
    first_glm_json = self.GetJson(glms[0]['path'])

    # Verify all GLMs have the same projection:
    warned_about_projections = False
    for i in xrange(1, len(glms)):
      glm_json = self.GetJson(glms[i]['path'])
      if glm_json['projection'] != first_glm_json['projection']:
        if not warned_about_projections:
          utils.PrintAndLog(
            """<span class="warning">WARNING: GLMs with different projections, clients may display mismatched layers.</span>""",
            logger, None)
          utils.PrintAndLog(
            "  First file {0}, projection: {1}".format(glms[0]['path'], first_glm_json['projection']),
            logger, None)
          warned_about_projections = True
        utils.PrintAndLog(
          "  File {0}, projection: {1}".format(glms[i]['path'], glm_json['projection']),
          logger, None)

    self.Create2dLayerInfoFile(layers_info, glms, first_glm_json['projection'])
  def ExtractFilelistFromGlx(self, glc_path, regex):
    """Extract given file from glc file and save it to the given path."""
    logger = self.GetLogger()
    os_cmd = ("%s/geglxinfo "
              "--glx=\"%s\" "
              "--list_files"
              % (COMMAND_DIR, glc_path))
    glx_info = utils.RunCmd(os_cmd)

    # Checking for errors and logging them if there where any
    if not glx_info[0] and len(glx_info) == 2:
      utils.PrintAndLog("ERROR: '%s' running command: '%s'" % (glx_info[1], os_cmd), logger)
      return []

    glx_files = []
    for line in glx_info:
      match = regex.match(line)
      if match:
        glx_files.append(match.groups()[0])
    return glx_files
  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