# Path: bento/core/pkg_objects.py
# class Executable(object):
#     @classmethod
#     def from_parse_dict(cls, d):
#         return cls(**d)
# 
#     @classmethod
#     def from_representation(cls, s):
#         if not "=" in s:
#             raise ValueError("s should be of the form name=module:function")
#         name, value = [j.strip() for j in s.split("=")]
#         if not ":" in value:
#             raise ValueError(
#                 "string representation should be of the form module:function, not %s"
#                 % value)
#         module, function = value.split(":", 1)
#         return cls(name, module, function)
# 
#     def __init__(self, name, module, function):
#         # FIXME: check that module is a module name ?
#         self.name = name
#         self.module = module
#         self.function = function
# 
#     # FIXME: this function should not really be here...
#     def representation(self):
#         return ":".join([self.module, self.function])
# 
#     # FIXME: this function should not really be here...
#     def full_representation(self):
#         return "%s = %s" % (self.name, self.representation())
# 
#     def __repr__(self):
#         return repr({"name": self.name, "module": self.module, "function": self.function})
# 
# Path: bento/commands/script_utils.py
# def nt_quote_arg(arg):
#     """Quote a command line argument according to Windows parsing
#     rules"""
# 
#     result = []
#     needquote = False
#     nb = 0
# 
#     needquote = (" " in arg) or ("\t" in arg)
#     if needquote:
#         result.append('"')
# 
#     for c in arg:
#         if c == '\\':
#             nb += 1
#         elif c == '"':
#             # double preceding backslashes, then add a \"
#             result.append('\\' * (nb*2) + '\\"')
#             nb = 0
#         else:
#             if nb:
#                 result.append('\\' * nb)
#                 nb = 0
#             result.append(c)
# 
#     if nb:
#         result.append('\\' * nb)
# 
#     if needquote:
#         result.append('\\' * nb)    # double the trailing backslashes
#         result.append('"')
# 
#     return ''.join(result)
# 
# def create_win32_script(name, executable, scripts_node):
#     script_text = SCRIPT_TEXT % {"python_exec": SYS_EXECUTABLE,
#             "module": executable.module,
#             "function": executable.function}
# 
#     wininst = False
#     header = get_script_header(SYS_EXECUTABLE, wininst)
# 
#     ext = '-script.py'
#     launcher = _CLI
# 
#     new_header = re.sub('(?i)pythonw.exe', 'python.exe', header)
# 
#     if os.path.exists(new_header[2:-1]) or sys.platform != 'win32':
#         hdr = new_header
#     else:
#         hdr = header
# 
#     fid = open(launcher, "rb")
#     try:
#         cnt = fid.read()
#     finally:
#         fid.close()
# 
#     def _write(name, cnt, mode):
#         target = scripts_node.make_node(name)
#         target.safe_write(cnt, "w%s" % mode)
#         return target
# 
#     nodes = []
#     nodes.append(_write(name + ext, hdr + script_text, 't'))
#     nodes.append(_write(name + ".exe", cnt, 'b'))
#     nodes.append(_write(name + ".exe.manifest", _LAUNCHER_MANIFEST % (name,), 't'))
# 
#     return nodes
# 
# def create_posix_script(name, executable, scripts_node):
#     header = "#!%(python_exec)s\n" % {"python_exec": SYS_EXECUTABLE}
#     cnt = SCRIPT_TEXT % {"python_exec": SYS_EXECUTABLE,
#             "module": executable.module,
#             "function": executable.function}
# 
#     n = scripts_node.make_node(name)
#     n.safe_write(header + cnt)
#     return [n]
# 
# def create_scripts(executables, bdir):
#     ret = {}
# 
#     for name, executable in executables.items():
#         if sys.platform == "win32":
#             ret[name] = create_win32_script(name, executable, bdir)
#         else:
#             ret[name] = create_posix_script(name, executable, bdir)
#     return ret


import os
import tempfile
import os.path as op
import mock
from bento.core.node \
    import \
        create_base_nodes
from bento.core.pkg_objects \
    import \
        Executable
from bento.compat.api.moves \
    import \
        unittest
from bento.commands.script_utils \
    import \
        nt_quote_arg, create_win32_script, create_posix_script, create_scripts




class TestMisc(unittest.TestCase):
    def test_nt_quote_arg(self):
        s = nt_quote_arg("foo")
        self.assertEqual(s, "foo")

        s = nt_quote_arg(" foo")
        self.assertEqual(s, '" foo"')


# Path: bento/core/pkg_objects.py
# class Executable(object):
#     @classmethod
#     def from_parse_dict(cls, d):
#         return cls(**d)
# 
#     @classmethod
#     def from_representation(cls, s):
#         if not "=" in s:
#             raise ValueError("s should be of the form name=module:function")
#         name, value = [j.strip() for j in s.split("=")]
#         if not ":" in value:
#             raise ValueError(
#                 "string representation should be of the form module:function, not %s"
#                 % value)
#         module, function = value.split(":", 1)
#         return cls(name, module, function)
# 
#     def __init__(self, name, module, function):
#         # FIXME: check that module is a module name ?
#         self.name = name
#         self.module = module
#         self.function = function
# 
#     # FIXME: this function should not really be here...
#     def representation(self):
#         return ":".join([self.module, self.function])
# 
#     # FIXME: this function should not really be here...
#     def full_representation(self):
#         return "%s = %s" % (self.name, self.representation())
# 
#     def __repr__(self):
#         return repr({"name": self.name, "module": self.module, "function": self.function})
# 
# Path: bento/commands/script_utils.py
# def nt_quote_arg(arg):
#     """Quote a command line argument according to Windows parsing
#     rules"""
# 
#     result = []
#     needquote = False
#     nb = 0
# 
#     needquote = (" " in arg) or ("\t" in arg)
#     if needquote:
#         result.append('"')
# 
#     for c in arg:
#         if c == '\\':
#             nb += 1
#         elif c == '"':
#             # double preceding backslashes, then add a \"
#             result.append('\\' * (nb*2) + '\\"')
#             nb = 0
#         else:
#             if nb:
#                 result.append('\\' * nb)
#                 nb = 0
#             result.append(c)
# 
#     if nb:
#         result.append('\\' * nb)
# 
#     if needquote:
#         result.append('\\' * nb)    # double the trailing backslashes
#         result.append('"')
# 
#     return ''.join(result)
# 
# def create_win32_script(name, executable, scripts_node):
#     script_text = SCRIPT_TEXT % {"python_exec": SYS_EXECUTABLE,
#             "module": executable.module,
#             "function": executable.function}
# 
#     wininst = False
#     header = get_script_header(SYS_EXECUTABLE, wininst)
# 
#     ext = '-script.py'
#     launcher = _CLI
# 
#     new_header = re.sub('(?i)pythonw.exe', 'python.exe', header)
# 
#     if os.path.exists(new_header[2:-1]) or sys.platform != 'win32':
#         hdr = new_header
#     else:
#         hdr = header
# 
#     fid = open(launcher, "rb")
#     try:
#         cnt = fid.read()
#     finally:
#         fid.close()
# 
#     def _write(name, cnt, mode):
#         target = scripts_node.make_node(name)
#         target.safe_write(cnt, "w%s" % mode)
#         return target
# 
#     nodes = []
#     nodes.append(_write(name + ext, hdr + script_text, 't'))
#     nodes.append(_write(name + ".exe", cnt, 'b'))
#     nodes.append(_write(name + ".exe.manifest", _LAUNCHER_MANIFEST % (name,), 't'))
# 
#     return nodes
# 
# def create_posix_script(name, executable, scripts_node):
#     header = "#!%(python_exec)s\n" % {"python_exec": SYS_EXECUTABLE}
#     cnt = SCRIPT_TEXT % {"python_exec": SYS_EXECUTABLE,
#             "module": executable.module,
#             "function": executable.function}
# 
#     n = scripts_node.make_node(name)
#     n.safe_write(header + cnt)
#     return [n]
# 
# def create_scripts(executables, bdir):
#     ret = {}
# 
#     for name, executable in executables.items():
#         if sys.platform == "win32":
#             ret[name] = create_win32_script(name, executable, bdir)
#         else:
#             ret[name] = create_posix_script(name, executable, bdir)
#     return ret


# Path: bento/commands/tests/test_script_utils.py
import os
import tempfile
import os.path as op
import mock
from bento.core.node \
    import \
        create_base_nodes
from bento.core.pkg_objects \
    import \
        Executable
from bento.compat.api.moves \
    import \
        unittest
from bento.commands.script_utils \
    import \
        nt_quote_arg, create_win32_script, create_posix_script, create_scripts





class TestMisc(unittest.TestCase):
    def test_nt_quote_arg(self):
        s = nt_quote_arg("foo")
        self.assertEqual(s, "foo")

        s = nt_quote_arg(" foo")
        self.assertEqual(s, '" foo"')


        s = nt_quote_arg("\ foo")