예제 #1
0
def mergepo(o, a, b):
    workingpath = os.getcwd()

    ofile = pofile(os.path.join(workingpath, o))
    odict = makedict(ofile)
    afile = pofile(os.path.join(workingpath, a))
    adict = makedict(afile)
    bfile = pofile(os.path.join(workingpath, b))
    bdict = makedict(bfile)

    temp_dir = tempfile.mkdtemp('', 'gitmergepo-')
    #temp_file = tempfile.mkstemp(dir=temp_dir)
    temp_po = POFile()
    conflicts = 0

    # Process all the ancestor entries
    for oentry in ofile:
        aentry = adict.get(oentry.msgid, None)
        bentry = bdict.get(oentry.msgid, None)
        result = merge_entry(oentry, aentry, bentry, b)
        odict[oentry.msgid] = result[0]
        delete_key(oentry.msgid, adict)
        delete_key(oentry.msgid, bdict)

        if result[1]:
            conflicts += 1

    # Process newly created entries in a & b
    for key, value in adict.iteritems():
        bentry = bdict.get(key, None)
        result = merge_entry(None, value, bentry, b)
        odict[key] = result[0]
        delete_key(key, bdict)

        if result[1]:
            conflicts += 1

    # Process newly created entries in b
    for key, value in bdict.iteritems():
        odict[value.msgid] = value

    # Append all entries to the temp po file
    for item in sorted(odict.iteritems()):
        temp_po.append(item[1])

    # Export the merged po file
    temp_po.metadata = afile.metadata
    temp_po.header = 'Merged by git-mergepo ...' + temp_po.header
    #temp_po.save(fpath=temp_file[1])
    temp_po.save(fpath=a)
    shutil.rmtree(temp_dir)

    #print 'Dicts: ', len(odict), ', ', len(adict), ', ', len(bdict)
    #print 'Path: ', workingpath
    #print 'Conflicts: ', conflicts

    # If conflicts were found we exit with status 1
    if conflicts > 0:
        print '\n!!! TRANSLATION CONFLICTS: %s !!!\n' % conflicts
        sys.exit(1)
    sys.exit(0)