<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_doc_from_tmpfile(self, doc_id, tmpfi, parent_sha, auth_info, commit_msg='', doctype_display_name="document"):
 """Given a doc_id, temporary filename of content, branch and auth_info """

        gh_user, author = get_user_author(auth_info)
        doc_filepath = self.path_for_doc(doc_id)
        doc_dir = os.path.split(doc_filepath)[0]
        if parent_sha is None:
            self.checkout_master()
            parent_sha = self.get_master_sha()
        branch = self.create_or_checkout_branch(gh_user, doc_id, parent_sha)

        # build complete (probably type-specific) commit message
        default_commit_msg = "Update %s '%s' via OpenTree API" % (doctype_display_name, doc_id)
        if commit_msg:
            commit_msg = "%s\n\n(%s)" % (commit_msg, default_commit_msg)
        else:
            commit_msg = default_commit_msg

        # create a doc directory if this is a new document  EJM- what if it isn't?
        if not os.path.isdir(doc_dir):
            os.makedirs(doc_dir)

        if os.path.exists(doc_filepath):
            prev_file_sha = self.get_blob_sha_for_file(doc_filepath)
        else:
            prev_file_sha = None
        shutil.copy(tmpfi.name, doc_filepath)
        self._add_and_commit(doc_filepath, author, commit_msg)
        new_sha = git(self.gitdir, self.gitwd, "rev-parse", "HEAD")
        _LOG.debug('Committed document "{i}" to branch "{b}" commit SHA: "{s}"'.format(i=doc_id,
                                                                                       b=branch,
                                                                                       s=new_sha.strip()))
        return {'commit_sha': new_sha.strip(),
                'branch': branch,
                'prev_file_sha': prev_file_sha,
                }