'Creates or updates the PO file for self.domain and :param locale:.
Uses contents of the existing :param potfile:.
Uses mguniq, msgmerge, and msgattrib GNU gettext utilities.'

def write_po_file(self, potfile, locale):

    args = ['msguniq', '--to-code=utf-8']
    if self.wrap:
        args.append(self.wrap)
    if self.location:
        args.append(self.location)
    args.append(potfile)
    (msgs, errors, status) = popen_wrapper(args)
    if errors:
        if (status != STATUS_OK):
            raise CommandError(('errors    happened    while    running    msguniq\n%s' % errors))
        elif (self.verbosity > 0):
            self.stdout.write(errors)
    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' % str(self.domain)))
    if os.path.exists(pofile):
        with open(potfile, 'w') as fp:
            fp.write(msgs)
        args = ['msgmerge', '-q']
        if self.wrap:
            args.append(self.wrap)
        if self.location:
            args.append(self.location)
        args.extend([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)
    elif (not self.invoked_for_django):
        msgs = self.copy_plural_forms(msgs, locale)
    msgs = msgs.replace(('#.    #-#-#-#-#        %s.pot    (PACKAGE    VERSION)        #-#-#-#-#\n' % self.domain), '')
    with open(pofile, 'w') as fp:
        fp.write(msgs)
    if self.no_obsolete:
        args = ['msgattrib', '-o', pofile, '--no-obsolete']
        if self.wrap:
            args.append(self.wrap)
        if self.location:
            args.append(self.location)
        args.append(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)
