'Create or update the PO file for self.domain and `locale`.
Use contents of the existing `potfile`.
Use msgmerge and msgattrib GNU gettext utilities.'

def write_po_file(self, potfile, locale):

    basedir = os.path.join(os.path.dirname(potfile), locale, 'LC_MESSAGES')
    if (not os.path.isdir(basedir)):
        os.makedirs(basedir)
    pofile = os.path.join(basedir, ('%s.po' % self.domain))
    if os.path.exists(pofile):
        args = ((['msgmerge'] + self.msgmerge_options) + [pofile, potfile])
        (msgs, errors, status) = popen_wrapper(args)
        if errors:
            if (status != STATUS_OK):
                raise CommandError(('errors    happened    while    running    msgmerge\n%s' % errors))
            elif (self.verbosity > 0):
                self.stdout.write(errors)
    else:
        with open(potfile, 'r', encoding='utf-8') as fp:
            msgs = fp.read()
        if (not self.invoked_for_django):
            msgs = self.copy_plural_forms(msgs, locale)
    msgs = normalize_eols(msgs)
    msgs = msgs.replace(('#.    #-#-#-#-#        %s.pot    (PACKAGE    VERSION)        #-#-#-#-#\n' % self.domain), '')
    with open(pofile, 'w', encoding='utf-8') as fp:
        fp.write(msgs)
    if self.no_obsolete:
        args = ((['msgattrib'] + self.msgattrib_options) + ['-o', pofile, pofile])
        (msgs, errors, status) = popen_wrapper(args)
        if errors:
            if (status != STATUS_OK):
                raise CommandError(('errors    happened    while    running    msgattrib\n%s' % errors))
            elif (self.verbosity > 0):
                self.stdout.write(errors)
