'Subcommand for updating existing message catalogs from a template.
:param argv: the command arguments
:since: version 0.9'

def update(self, argv):

    parser = OptionParser(usage=(self.usage % ('update', '')), description=self.commands['update'])
    parser.add_option('--domain', '-D', dest='domain', help="domain    of    PO    file    (default    '%default')")
    parser.add_option('--input-file', '-i', dest='input_file', metavar='FILE', help='name    of    the    input    file')
    parser.add_option('--output-dir', '-d', dest='output_dir', metavar='DIR', help='path    to    output    directory')
    parser.add_option('--output-file', '-o', dest='output_file', metavar='FILE', help="name    of    the    output    file    (default    '<output_dir>/<locale>/LC_MESSAGES/<domain>.po')")
    parser.add_option('--locale', '-l', dest='locale', metavar='LOCALE', help='locale    of    the    translations    catalog')
    parser.add_option('--ignore-obsolete', dest='ignore_obsolete', action='store_true', help='do    not    include    obsolete    messages    in    the    output    (default    %default)')
    parser.add_option('--no-fuzzy-matching', '-N', dest='no_fuzzy_matching', action='store_true', help='do    not    use    fuzzy    matching    (default    %default)')
    parser.add_option('--previous', dest='previous', action='store_true', help='keep    previous    msgids    of    translated    messages    (default    %default)')
    parser.set_defaults(domain='messages', ignore_obsolete=False, no_fuzzy_matching=False, previous=False)
    (options, args) = parser.parse_args(argv)
    if (not options.input_file):
        parser.error('you    must    specify    the    input    file')
    if ((not options.output_file) and (not options.output_dir)):
        parser.error('you    must    specify    the    output    file    or    directory')
    if (options.output_file and (not options.locale)):
        parser.error('you    must    specify    the    locale')
    if (options.no_fuzzy_matching and options.previous):
        options.previous = False
    po_files = []
    if (not options.output_file):
        if options.locale:
            po_files.append((options.locale, os.path.join(options.output_dir, options.locale, 'LC_MESSAGES', (options.domain + '.po'))))
        else:
            for locale in os.listdir(options.output_dir):
                po_file = os.path.join(options.output_dir, locale, 'LC_MESSAGES', (options.domain + '.po'))
                if os.path.exists(po_file):
                    po_files.append((locale, po_file))
    else:
        po_files.append((options.locale, options.output_file))
    domain = options.domain
    if (not domain):
        domain = os.path.splitext(os.path.basename(options.input_file))[0]
    infile = open(options.input_file, 'U')
    try:
        template = read_po(infile)
    finally:
        infile.close()
    if (not po_files):
        parser.error('no    message    catalogs    found')
    for (locale, filename) in po_files:
        self.log.info('updating    catalog    %r    based    on    %r', filename, options.input_file)
        infile = open(filename, 'U')
        try:
            catalog = read_po(infile, locale=locale, domain=domain)
        finally:
            infile.close()
        catalog.update(template, options.no_fuzzy_matching)
        tmpname = os.path.join(os.path.dirname(filename), (tempfile.gettempprefix() + os.path.basename(filename)))
        tmpfile = open(tmpname, 'w')
        try:
            try:
                write_po(tmpfile, catalog, ignore_obsolete=options.ignore_obsolete, include_previous=options.previous)
            finally:
                tmpfile.close()
        except:
            os.remove(tmpname)
            raise
        try:
            os.rename(tmpname, filename)
        except OSError:
            os.remove(filename)
            shutil.copy(tmpname, filename)
            os.remove(tmpname)
