""" wheels2dmg command module
"""
from __future__ import division, print_function

import sys
import os
from argparse import ArgumentParser, RawDescriptionHelpFormatter

from .piputils import recon_pip_args, get_requirements
from .pkgbuilders import (insert_template_path, PkgWriter)

# Defaults
PYTHON_VERSION='2.7'


def get_parser():
    parser = ArgumentParser(
        description=
"Make dmg installer for Python.org Python from Python wheels",
        epilog=
"""Make DMG installer from wheels

* Collect ``get-pip.py`` installer script;
* Collect needed wheels using "pip wheel" command, including pip, setuptools;
* Write directory to DMG containing wheel packages;
* Write "postinstall" script to install pip, then install wheels;
* Fold "postinstall" script into ".pkg" double click installer;
* Package wheel directory and pkg installer into DMG file.

There must be at least one REQ_SPEC or REQUIREMENT.
""",
        formatter_class=RawDescriptionHelpFormatter)
    parser.add_argument('pkg_name', type=str, help='root name of installer')
    parser.add_argument('pkg_version', type=str, help='version of installer')
    parser.add_argument('req_specs', type=str, nargs='*', default=[],
                        help='pip requirement specifiers', metavar='REQ_SPEC')
    parser.add_argument('--python-version',  type=str, default=PYTHON_VERSION,
                        help='Python version in major.minor format, e.g "3.4"')
    parser.add_argument('--get-pip-url', type=str,
                        help='URL or local path to "get-pip.py" (default is '
                        'to download from canonical URL')
    parser.add_argument('--dmg-build-dir', type=str,
                        help='Path to write dmg contents to (default is to '
                        'use a temporary directory)')
    parser.add_argument('--scratch-dir', type=str,
                        help='Path to write scratch files to '
                        'default is to use a temporary directory)')
    parser.add_argument('--dmg-out-dir', type=str, default=os.getcwd(),
                        help='Directory to which we write dmg disk image '
                        '(default is current directory)')
    parser.add_argument('--template-dir', type=str,
                        help='Alternative directory containing jinja '
                        'templates for installer files')
    parser.add_argument('--pkg-id-root', type=str,
                        help='Package id root for installing package receipt '
                        '(default is "com.github.MacPython")')
    # The rest of the arguments are destined for pip wheel / install calls
    parser.add_argument('--requirement', '-r', type=str, action='append',
                        help='pip requirement file(s)', metavar='REQUIREMENT')
    parser.add_argument('--index-url', '-i', type=str,
                        help='base URL of Python index (see pip install)')
    parser.add_argument('--extra-index-url', type=str, action='append',
                        help='extra URLs of Python indices (see pip)')
    parser.add_argument('--no-index',  action='store_true',
                        help='disable search of pip indices when fetching '
                        'packages to make installer')
    parser.add_argument('--find-links', '-f', type=str, action='append',
                        help='locations to find packages to make installer')
    return parser


def main():
    # parse the command line
    parser = get_parser()
    args = parser.parse_args()
    # Split pip command line options into requirements and other parameters
    req_params, fetch_params = recon_pip_args(args)
    # We need at least one requirement
    if len(req_params) == 0:
        parser.print_help()
        sys.exit(12)
    if not args.template_dir is None:
        insert_template_path(args.template_dir)
    reqs = get_requirements(args.req_specs, args.requirement)
    pkg_writer = PkgWriter(args.pkg_name,
                           args.pkg_version,
                           args.python_version,
                           reqs,
                           req_params,
                           fetch_params,
                           args.get_pip_url,
                           args.dmg_build_dir,
                           args.scratch_dir,
                           pkg_id_root = args.pkg_id_root)
    pkg_writer.write_dmg(args.dmg_out_dir)

@@ -184,16 +184,33 @@
 s import
+ make_pip_parser,
  recon_p
@@ -235,16 +235,16 @@
 rements%0A
-
 from .pk
@@ -1143,157 +1143,8 @@
 r')%0A
-    parser.add_argument('req_specs', type=str, nargs='*', default=%5B%5D,%0A                        help='pip requirement specifiers', metavar='REQ_SPEC')%0A
     
@@ -2437,862 +2437,38 @@
     
-# The rest of the arguments are destined for pip wheel / install calls%0A    parser.add_argument('--requirement', '-r', type=str, action='append',%0A                        help='pip requirement file(s)', metavar='REQUIREMENT')%0A    parser.add_argument('--index-url', '-i', type=str,%0A                        help='base URL of Python index (see pip install)')%0A    parser.add_argument('--extra-index-url', type=str, action='append',%0A                        help='extra URLs of Python indices (see pip)')%0A    parser.add_argument('--no-index',  action='store_true',%0A                        help='disable search of pip indices when fetching '%0A                        'packages to make installer')%0A    parser.add_argument('--find-links', '-f', type=str, action='append',%0A                        help='locations to find packages to make installer')%0A    return parser
+return make_pip_parser(parser)
 %0A%0A%0Ad

use pip argument adder for command line parser