def generic_commit_and_try_merge2master_wf(git_action,
                                           file_content,
                                           doc_id,
                                           auth_info,
                                           parent_sha,
                                           commit_msg='',
                                           merged_sha=None,
                                           doctype_display_name="document"):
    """Actually make a local Git commit and push it to our remote
    """
    # _LOG.debug('generic_commit_and_try_merge2master_wf: doc_id="{s}" \
    #            parent_sha="{p}" merged_sha="{m}"'.format(
    #            s=doc_id, p=parent_sha, m=merged_sha))
    merge_needed = False
    fc = tempfile.NamedTemporaryFile()
    # N.B. we currently assume file_content is text/JSON, or should be serialized from a dict
    try:
        if is_str_type(file_content):
            fc.write(file_content)
        else:
            write_as_json(file_content, fc)
        fc.flush()
        try:
            max_file_size = git_action.max_file_size
        except:
            max_file_size = None
        if max_file_size is not None:
            file_size = os.stat(fc.name).st_size
            if file_size > max_file_size:
                m = 'Commit of {t} "{i}" had a file size ({a} bytes) which ' \
                    'exceeds the maximum size allowed ({b} bytes).'
                m = m.format(t=doctype_display_name, i=doc_id, a=file_size, b=max_file_size)
                raise GitWorkflowError(m)
        f = "Could not acquire lock to write to %s #%s" % (doctype_display_name, doc_id)
        acquire_lock_raise(git_action, fail_msg=f)
        try:
            try:
                commit_resp = git_action.write_doc_from_tmpfile(doc_id,
                                                                fc,
                                                                parent_sha,
                                                                auth_info,
                                                                commit_msg,
                                                                doctype_display_name)
            except Exception as e:
                _LOG.exception('write_doc_from_tmpfile exception')
                raise GitWorkflowError("Could not write to %s #%s ! Details: \n%s" %
                                       (doctype_display_name, doc_id, e.message))
            written_fp = git_action.path_for_doc(doc_id)
            branch_name = commit_resp['branch']
            new_sha = commit_resp['commit_sha']
            _LOG.debug('write of {t} {i} on parent {p} returned = {c}'.format(t=doctype_display_name,
                                                                              i=doc_id,
                                                                              p=parent_sha,
                                                                              c=str(commit_resp)))
            m_resp = _do_merge2master_commit(git_action,
                                             new_sha,
                                             branch_name,
                                             written_fp,
                                             merged_sha=merged_sha,
                                             prev_file_sha=commit_resp.get('prev_file_sha'))
            new_sha, branch_name, merge_needed = m_resp
        finally:
            git_action.release_lock()
    finally:
        fc.close()
    # What other useful information should be returned on a successful write?
    r = {
        "error": 0,
        "resource_id": doc_id,
        "branch_name": branch_name,
        "description": "Updated %s #%s" % (doctype_display_name, doc_id),
        "sha": new_sha,
        "merge_needed": merge_needed,
    }
    _LOG.debug('returning {r}'.format(r=str(r)))
    return r
def generic_commit_and_try_merge2master_wf ( git_action , file_content , doc_id , auth_info , parent_sha , commit_msg = '' , merged_sha = None , doctype_display_name = "document" ) : merge_needed = False fc = tempfile . NamedTemporaryFile ( ) try : if is_str_type ( file_content ) : fc . write ( file_content ) else : write_as_json ( file_content , fc ) fc . flush ( ) try : max_file_size = git_action . max_file_size except : max_file_size = None if max_file_size is not None : file_size = os . stat ( fc . name ) . st_size if file_size > max_file_size : m = 'Commit of {t} "{i}" had a file size ({a} bytes) which ' 'exceeds the maximum size allowed ({b} bytes).' m = m . format ( t = doctype_display_name , i = doc_id , a = file_size , b = max_file_size ) raise GitWorkflowError ( m ) f = "Could not acquire lock to write to %s #%s" % ( doctype_display_name , doc_id ) acquire_lock_raise ( git_action , fail_msg = f ) try : try : commit_resp = git_action . write_doc_from_tmpfile ( doc_id , fc , parent_sha , auth_info , commit_msg , doctype_display_name ) except Exception as e : _LOG . exception ( 'write_doc_from_tmpfile exception' ) raise GitWorkflowError ( "Could not write to %s #%s ! Details: \n%s" % ( doctype_display_name , doc_id , e . message ) ) written_fp = git_action . path_for_doc ( doc_id ) branch_name = commit_resp [ 'branch' ] new_sha = commit_resp [ 'commit_sha' ] _LOG . debug ( 'write of {t} {i} on parent {p} returned = {c}' . format ( t = doctype_display_name , i = doc_id , p = parent_sha , c = str ( commit_resp ) ) ) m_resp = _do_merge2master_commit ( git_action , new_sha , branch_name , written_fp , merged_sha = merged_sha , prev_file_sha = commit_resp . get ( 'prev_file_sha' ) ) new_sha , branch_name , merge_needed = m_resp finally : git_action . release_lock ( ) finally : fc . close ( ) r = { "error" : 0 , "resource_id" : doc_id , "branch_name" : branch_name , "description" : "Updated %s #%s" % ( doctype_display_name , doc_id ) , "sha" : new_sha , "merge_needed" : merge_needed , } _LOG . debug ( 'returning {r}' . format ( r = str ( r ) ) ) return r
Actually make a local Git commit and push it to our remote