from bento.core.package import \
        PackageDescription
from bento.conv import \
        write_pkg_info

from bento.commands.errors \
    import \
        UsageException
from bento.commands.core import \
        Command, SCRIPT_NAME

class BuildPkgInfoCommand(Command):
    long_descr = """\
Purpose: generate PKG-INFO file
Usage:   bentomaker build_pkg_info [OPTIONS]"""
    short_descr = "generate PKG-INFO file."
    common_options = Command.common_options + [
        {"opts": ["-o", "--output"], "dest": "output", "help": "Output file for PKG-INFO"},
    ]

    def run(self, ctx):
        o, a = self.parser.parse_args(ctx.get_command_arguments())
        if o.help:
            self.parser.print_help()
            return

        if len(a) < 1:
            raise UsageException("%s: error: %s subcommand require an argument" \
                    % (SCRIPT_NAME, "parse"))
        else:
            filename = a[0]

        if not o.output:
            pkg_info = "PKG-INFO"
        else:
            pkg_info = o.output

        pkg = PackageDescription.from_file(filename)

        fid = open(pkg_info, "w")
        try:
            write_pkg_info(pkg, fid)
        finally:
            fid.close()

@@ -1,20 +1,37 @@
+import optparse%0A%0A
 from bento.core.pack
@@ -490,18 +490,24 @@
     
-%7B%22
 opt
-s%22: %5B
+parse.Option(
 %22-o%22
@@ -522,19 +522,15 @@
 put%22
-%5D
 , 
-%22
 dest
-%22: 
+=
 %22out
@@ -539,16 +539,13 @@
 t%22, 
-%22
 help
-%22: 
+=
 %22Out
@@ -570,9 +570,9 @@
 NFO%22
-%7D
+)
 ,%0A  

fix options definition in build_pkg_info.