Exemple #1
0
def IdentifyPublishedDb(db_name):
    """Identifies type of published database.

  Args:
    db_name: database name that is actually an DB asset path.
  Returns:
    (db_path, db_type): database path, type tuple.
  Raises:
    exceptions.Error in case of short database name.
  """
    if not db_name:
        raise exceptions.Error(
            "Internal Error - database name is an empty string.")

    db_path = os.path.normpath(db_name)
    db_type = None

    if db_path[-5:] == "/gedb":
        db_type = basic_types.DbType.TYPE_GE
    elif db_path[-6:] == "/mapdb":
        db_type = basic_types.DbType.TYPE_MAP
    else:
        db_ext = db_path[-4:]
        if db_ext == ".glb":
            db_type = basic_types.DbType.TYPE_GLB
        elif db_ext == ".glm":
            db_type = basic_types.DbType.TYPE_GLM
        elif db_ext == ".glc":
            db_type = basic_types.DbType.TYPE_GLC
        else:
            raise exceptions.Error(
                "Internal Error - Invalid database type for %s." % db_path)

    # Check for short database name for Fusion DB.
    if (db_type == basic_types.DbType.TYPE_GE
            or db_type == basic_types.DbType.TYPE_MAP):
        if db_name[0] != "/":
            raise exceptions.Error(
                "Internal Error - The short database name (%s) cannot be used."
                % db_name)

    return (db_path, db_type)
  def __ParsePostInput(self, post_input, request):
    """Parses POST request input.

    Args:
      post_input: POST request input.
      request: request object to collect exptracted parameters.
    Raises:
      Error exception.
    """
    post_dct = urlparse.parse_qs(post_input)
    # TODO: parsing POST request.

    raise exceptions.Error(
        "Internal Error - HTTP POST Request is not supported.")
Exemple #3
0
def LocalTransfer(src_path,
                  dest_path,
                  force_copy=False,
                  prefer_copy=False,
                  allow_symlinks=True):
    """Transfers specified source file to specified destination path.

  Args:
    src_path: source file path.
    dest_path: destination file path.
    force_copy: whether to force a copy.
    prefer_copy: whether copying is preferred.
    allow_symlinks: whether symlink is allowed.
  Returns:
    whether transfer was successful.
  """
    try:
        # Ensure that source file exists and has read permission.
        # Have to check since it is allowed to create a link to a non-existing file.
        if not os.path.exists(src_path):
            raise exceptions.Error(
                "Source file has no read permission or does not exist: %s" %
                src_path)
        elif not os.access(src_path, os.R_OK):
            raise exceptions.Error("No read permission: %s" % src_path)

        # Ensure that the destination parent directory exists.
        if not os.path.exists(os.path.dirname(dest_path)):
            os.makedirs(os.path.dirname(dest_path))
        elif os.path.exists(dest_path):
            # Delete existing destination file.
            os.unlink(dest_path)

        # If force_copy is set to Y then just copy.
        # Note: force_copy can be set by user (parameter for
        # console tool). Use case is a disconnected publishing.
        if force_copy:
            shutil.copy2(src_path, dest_path)
            return True

        # force_copy are not set to Y, then try a hard-link
        # at first.
        # Not: os.link tries to create hard link, and if it fails,
        # it then creates a copy.
        try:
            os.link(src_path, dest_path)
            # Compare the inode numbers to verify whether we get a copy
            # or a hard link.
            if os.path.exists(dest_path) and os.path.samefile(
                    src_path, dest_path):
                return True
        except OSError:
            pass

        # Hard-link failed.
        # Note: we set prefer_copy if src_path is a path in temp.
        # directory or file is going to be updated then (e.g. layers.js).
        if prefer_copy:
            # os.link() can make a copy, so we check for existence.
            if not os.path.exists(dest_path):
                shutil.copy2(src_path, dest_path)
            return True
        else:
            # if allow-symlinks is set to Y then try to create a symlink.
            if allow_symlinks:
                try:
                    # os.link() can make a copy, so we check for existence.
                    if os.path.exists(dest_path):
                        os.unlink(dest_path)

                    os.symlink(src_path, dest_path)
                    return True
                except OSError:
                    pass

            # Symlink is not allowed or failed. Copy is the last choice.
            # os.link() can make a copy so we check for existence.
            if not os.path.exists(dest_path):
                shutil.copy2(src_path, dest_path)
            return True
    except (OSError, IOError, shutil.Error, exceptions.Error) as e:
        logging.error("Local transfer failed.\n %s", e)
    except Exception as e:
        logging.error("Local transfer failed.\n Unknown error: %s", e)

    return False