This file is indexed.

/usr/lib/python3/dist-packages/testpath/commands.py is in python3-testpath 0.2-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import contextlib
import json
import os
import sys
import tempfile

__all__ = ['MockCommand', 'assert_calls']

commands_dir = None
recording_dir = None

def prepend_to_path(dir):
    os.environ['PATH'] = dir + os.pathsep + os.environ['PATH']

def remove_from_path(dir):
    path_dirs = os.environ['PATH'].split(os.pathsep)
    path_dirs.remove(dir)
    os.environ['PATH'] = os.pathsep.join(path_dirs)


_record_run = """#!{python}
import os, sys
import json

cmd = os.path.basename(__file__)
with open(os.path.join({recording_dir!r}, cmd), 'a') as f:
    json.dump({{'env': dict(os.environ),
               'argv': sys.argv,
               'cwd': os.getcwd()}},
              f)
    f.write('\x1e') # ASCII record separator
"""

# TODO: Overlapping calls to the same command may interleave writes.

class MockCommand(object):
    """Context manager to mock a system command.
    
    The mock command will be written to a directory at the front of $PATH,
    taking precedence over any existing command with the same name.
    
    By specifying content as a string, you can determine what running the
    command will do. The default content records each time the command is
    called and exits: you can access these records with mockcmd.get_calls().
    
    On Windows, the specified content will be run by the Python interpreter in
    use. On Unix, it should start with a shebang (``#!/path/to/interpreter``).
    """
    def __init__(self, name, content=None):
        self.name = name
        self.content = content
    
    def _write_cmd_file(self):
        c = '"{python}" "%~dp0\{pyfile}" %*\r\n'
        path = os.path.join(commands_dir, self.name+'.cmd')
        with open(path, 'w') as f:
            f.write(c.format(python=sys.executable, pyfile=self.name+'.py'))

    @property
    def _cmd_path(self):
        # Can only be used once commands_dir has been set
        p = os.path.join(commands_dir, self.name)
        if os.name == 'nt':
            p += '.py'
        return p

    def __enter__(self):
        global commands_dir, recording_dir
        if commands_dir is None:
            commands_dir = tempfile.mkdtemp()
        if recording_dir is None:
            recording_dir = tempfile.mkdtemp()

        if os.path.isfile(self._cmd_path):
            raise EnvironmentError("Command %r already exists at %s" %
                                            (self.name, self._cmd_path))
        
        if commands_dir not in os.environ['PATH'].split(os.pathsep):
            prepend_to_path(commands_dir)
        
        if self.content is None:
            self.content = _record_run.format(python=sys.executable,
                                              recording_dir=recording_dir)

        with open(self._cmd_path, 'w') as f:
            f.write(self.content)
        
        if os.name == 'nt':
            self._write_cmd_file()
        else:
            os.chmod(self._cmd_path, 0o755) # Set executable bit
        
        return self
    
    def __exit__(self, etype, evalue, tb):
        os.remove(self._cmd_path)
        if os.name == 'nt':
            os.remove(os.path.join(commands_dir, self.name+'.cmd'))
        if not os.listdir(commands_dir):
            remove_from_path(commands_dir)

    def get_calls(self):
        """Get a list of calls made to this mocked command.
        
        This relies on the default script content, so it will return an
        empty list if you specified a different content parameter.
        
        For each time the command was run, the list will contain a dictionary
        with keys argv, env and cwd.
        """
        if recording_dir is None:
            return []
        record_file = os.path.join(recording_dir, self.name)
        if not os.path.isfile(record_file):
            return []
        
        with open(record_file, 'r') as f:
            # 1E is ASCII record separator, last chunk is empty
            chunks = f.read().split('\x1e')[:-1]
        
        return [json.loads(c) for c in chunks]


@contextlib.contextmanager
def assert_calls(cmd, args=None):
    """Assert that a block of code runs the given command.
    
    If args is passed, also check that it was called at least once with the
    given arguments (not including the command name).
    
    Use as a context manager, e.g.::
    
        with assert_calls('git'):
            some_function_wrapping_git()
            
        with assert_calls('git', ['add', myfile]):
            some_other_function()
    """
    with MockCommand(cmd) as mc:
        yield
    
    calls = mc.get_calls()
    assert calls != [], "Command %r was not called" % cmd

    if args is not None:
        if not any(args == c['argv'][1:] for c in calls):
            msg = ["Command %r was not called with specified args (%r)" %
                            (cmd, args),
                   "It was called with these arguments: "]
            for c in calls:
                msg.append('  %r' % c['argv'][1:])
            raise AssertionError('\n'.join(msg))