<commit_before>from __future__ import print_function
from __future__ import absolute_import
from builtins import range
import sys

import re

from argparse.cwl_tool import CWLTool


def load_conflicting_package(name, not_name, local_module):
    """Load a conflicting package
    Some assumptions are made, namely that your package includes the "official"
    one as part of the name. E.g. gxargparse/argparse, you would call this with:

        >>> real_argparse = load_conflicting_package('argpargase', 'gxargparse',
        ...                                          sys.modules[load_conflicting_package.__module__])

     http://stackoverflow.com/a/6032023
    """
    import imp

    for i in range(0, 100):
        random_name = 'random_name_%d' % (i,)
        if random_name not in sys.modules:
            break
        else:
            random_name = None
    if random_name is None:
        raise RuntimeError("Couldn't manufacture an unused module name.")
    # NB: This code is unlikely to work for nonstdlib overrides.
    # This will hold the correct sys.path for the REAL argparse
    for path in sys.path:
        try:
            (f, pathname, desc) = imp.find_module(name, [path])
            if not_name not in pathname and desc[2] == 1:
                module = imp.load_module(random_name, f, pathname, desc)
                f.close()
                return sys.modules[random_name]
        except:
            # Many sys.paths won't contain the module of interest
            pass
    return None

ap = load_conflicting_package('argparse', 'gxargparse', sys.modules[load_conflicting_package.__module__])
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
from . import argparse_galaxy_translation as agt
from . import argparse_cwl_translation as act
from . import cwl_tool as cwlt

# This fetches a reference to ourselves
__selfmodule__ = sys.modules[load_conflicting_package.__module__]
# Static list of imports
__argparse_exports__ = ['HelpFormatter', 'RawDescriptionHelpFormatter',
                        'ArgumentDefaultsHelpFormatter', 'FileType',
                        'SUPPRESS', 'OPTIONAL', 'ZERO_OR_MORE', 'ONE_OR_MORE',
                        'PARSER', 'REMAINDER', '_UNRECOGNIZED_ARGS_ATTR',
                        '_VersionAction']

# Set the attribute on ourselves.
for x in __argparse_exports__:
    setattr(__selfmodule__, x, getattr(ap, x))

tools = []


class ArgumentParser(ap.ArgumentParser):

    # argument_list = []

    def __init__(self,
                 prog=None,
                 usage=None,
                 description=None,
                 epilog=None,
                 parents=[],
                 formatter_class=HelpFormatter,
                 prefix_chars='-',
                 fromfile_prefix_chars=None,
                 argument_default=None,
                 conflict_handler='error',
                 add_help=True):

        self.argument_list = []
        tools.append(self)
        super(ArgumentParser, self).__init__(prog=prog,
                                             usage=usage,
                                             description=description,
                                             epilog=epilog,
                                             parents=parents,
                                             formatter_class=formatter_class,
                                             prefix_chars=prefix_chars,
                                             fromfile_prefix_chars=fromfile_prefix_chars,
                                             argument_default=argument_default,
                                             conflict_handler=conflict_handler,
                                             add_help=add_help)

    def add_argument(self, *args, **kwargs):
        result = ap.ArgumentParser.add_argument(self, *args, **kwargs)
        self.argument_list.append(result)

    def parse_args(self, *args, **kwargs):
        if '--generate_cwl_tool' in sys.argv:
            # assuming all passed arguments are either commands or argparse2cwl flags
            command = ''
            for arg in sys.argv:
                if not arg.startswith('--'):
                    command += '{0} '.format(arg.split('/')[-1])
                else:
                    command = command.strip()
                    kwargs['command'] = command
                    break
            if '-f' in sys.argv:
                kwargs['path'] = sys.argv[sys.argv.index('-f')+1]
            if '--basecommand' in sys.argv:
                kwargs['basecommand'] = sys.argv[sys.argv.index('--basecommand')+1]
            self.parse_args_cwl(*args, **kwargs)
        # TODO: reorganize to a separate CLI
        elif '--generate_galaxy_xml' in sys.argv:
            self.parse_args_galaxy_nouse(*args, **kwargs)
        else:
            return ap.ArgumentParser.parse_args(self, *args, **kwargs)

    def parse_args_cwl(self, *args, **kwargs):
        for argp in tools:
            # make subparser description out of its help message
            if argp._subparsers:<commit_msg>Add support for shell bang to base command<commit_after>from __future__ import print_function
from __future__ import absolute_import
from builtins import range
import sys

import re

from argparse.cwl_tool import CWLTool


def load_conflicting_package(name, not_name, local_module):
    """Load a conflicting package
    Some assumptions are made, namely that your package includes the "official"
    one as part of the name. E.g. gxargparse/argparse, you would call this with:

        >>> real_argparse = load_conflicting_package('argpargase', 'gxargparse',
        ...                                          sys.modules[load_conflicting_package.__module__])

     http://stackoverflow.com/a/6032023
    """
    import imp

    for i in range(0, 100):
        random_name = 'random_name_%d' % (i,)
        if random_name not in sys.modules:
            break
        else:
            random_name = None
    if random_name is None:
        raise RuntimeError("Couldn't manufacture an unused module name.")
    # NB: This code is unlikely to work for nonstdlib overrides.
    # This will hold the correct sys.path for the REAL argparse
    for path in sys.path:
        try:
            (f, pathname, desc) = imp.find_module(name, [path])
            if not_name not in pathname and desc[2] == 1:
                module = imp.load_module(random_name, f, pathname, desc)
                f.close()
                return sys.modules[random_name]
        except:
            # Many sys.paths won't contain the module of interest
            pass
    return None

ap = load_conflicting_package('argparse', 'gxargparse', sys.modules[load_conflicting_package.__module__])
import galaxyxml.tool as gxt
import galaxyxml.tool.parameters as gxtp
from . import argparse_galaxy_translation as agt
from . import argparse_cwl_translation as act
from . import cwl_tool as cwlt

# This fetches a reference to ourselves
__selfmodule__ = sys.modules[load_conflicting_package.__module__]
# Static list of imports
__argparse_exports__ = ['HelpFormatter', 'RawDescriptionHelpFormatter',
                        'ArgumentDefaultsHelpFormatter', 'FileType',
                        'SUPPRESS', 'OPTIONAL', 'ZERO_OR_MORE', 'ONE_OR_MORE',
                        'PARSER', 'REMAINDER', '_UNRECOGNIZED_ARGS_ATTR',
                        '_VersionAction']

# Set the attribute on ourselves.
for x in __argparse_exports__:
    setattr(__selfmodule__, x, getattr(ap, x))

tools = []


class ArgumentParser(ap.ArgumentParser):

    # argument_list = []

    def __init__(self,
                 prog=None,
                 usage=None,
                 description=None,
                 epilog=None,
                 parents=[],
                 formatter_class=HelpFormatter,
                 prefix_chars='-',
                 fromfile_prefix_chars=None,
                 argument_default=None,
                 conflict_handler='error',
                 add_help=True):

        self.argument_list = []
        tools.append(self)
        super(ArgumentParser, self).__init__(prog=prog,
                                             usage=usage,
                                             description=description,
                                             epilog=epilog,
                                             parents=parents,
                                             formatter_class=formatter_class,
                                             prefix_chars=prefix_chars,
                                             fromfile_prefix_chars=fromfile_prefix_chars,
                                             argument_default=argument_default,
                                             conflict_handler=conflict_handler,
                                             add_help=add_help)

    def add_argument(self, *args, **kwargs):
        result = ap.ArgumentParser.add_argument(self, *args, **kwargs)
        self.argument_list.append(result)

    def parse_args(self, *args, **kwargs):
        if '--generate_cwl_tool' in sys.argv:
            # assuming all passed arguments are either commands or argparse2cwl flags
            command = ''
            shebang = re.search(r'\./\w*?.py$', sys.argv[0])
            for arg in sys.argv:
                if not arg.startswith('--'):
                    command += '{0} '.format(arg.split('/')[-1])
                else:
                    command = command.strip()
                    kwargs['command'] = command
                    break
            if '-f' in sys.argv:
                kwargs['path'] = sys.argv[sys.argv.index('-f')+1]
            if '--basecommand' in sys.argv:
                kwargs['basecommand'] = sys.argv[sys.argv.index('--basecommand')+1]
            elif shebang:
                kwargs['basecommand'] = shebang.group(0)
            self.parse_args_cwl(*args, **kwargs)
        # TODO: reorganize to a separate CLI
        elif '--generate_galaxy_xml' in sys.argv:
            self.parse_args_galaxy_nouse(*args, **kwargs)
        else:
            return ap.ArgumentParser.parse_args(self, *args, **kwargs)

    def parse_args_cwl(self, *args, **kwargs):
        for argp in tools:
            # make subparser description out of its help message
            if argp._subparsers:
{'fraction_non_alphanumeric': 0.08587472165746926, 'fraction_numerical': 0.0036789621454158196, 'mean_word_length': 1.7495342028214, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '877378', 'n_tokens_mistral': 2719, 'n_tokens_neox': 2620, 'n_words': 681}