This file is indexed.

/usr/lib/python3/dist-packages/autopilot/tests/functional/fixtures.py is in python3-autopilot-tests 1.4+14.04.20140416-0ubuntu1.

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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Autopilot Functional Test Tool
# Copyright (C) 2014 Canonical
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

"""Fixtures for the autopilot functional test suite."""

from __future__ import absolute_import

import logging
import os
import stat
from shutil import rmtree
import tempfile
from textwrap import dedent

from fixtures import Fixture


logger = logging.getLogger(__name__)


class ExecutableScript(Fixture):
    """Write some text to a file on disk and make it executable."""

    def __init__(self, script, extension=".py"):
        """Initialise the fixture.

        :param script: The contents of the script file.
        :param extension: The desired extension on the script file.

        """
        super(ExecutableScript, self).__init__()
        self._script = script
        self._extension = extension

    def setUp(self):
        super(ExecutableScript, self).setUp()
        with tempfile.NamedTemporaryFile(
            suffix=self._extension,
            mode='w',
            delete=False
        ) as f:
            f.write(self._script)
            self.path = f.name
        self.addCleanup(os.unlink, self.path)

        os.chmod(self.path, os.stat(self.path).st_mode | stat.S_IXUSR)


class TempDesktopFile(Fixture):

    def __init__(self, type=None, exec_=None, name=None, icon=None):
        """Create a TempDesktopFile instance.

        Parameters control the contents of the created desktop file. Default
        values will create a desktop file with bogus contents.

        :param type: The type field in the created file. Defaults to
            'Application'.
        :param exec_: The path to the file to execute.
        :param name: The name of the application being launched. Defaults to
            "Test App".
        """
        super(TempDesktopFile, self).__init__()
        type_line = type if type is not None else "Application"
        exec_line = exec_ if exec_ is not None else "Not Important"
        name_line = name if name is not None else "Test App"
        icon_line = icon if icon is not None else "Not Important"
        self._file_contents = dedent(
            """\
            [Desktop Entry]
            Type={}
            Exec={}
            Name={}
            Icon={}
            """.format(type_line, exec_line, name_line, icon_line)
        )

    def setUp(self):
        super(TempDesktopFile, self).setUp()
        path_created = TempDesktopFile._ensure_desktop_dir_exists()
        self._desktop_file_path = self._create_desktop_file(
            self._file_contents,
        )

        self.addCleanup(
            TempDesktopFile._remove_desktop_file_components,
            path_created,
            self._desktop_file_path,
        )

    def get_desktop_file_path(self):
        return self._desktop_file_path

    def get_desktop_file_id(self):
        return os.path.splitext(os.path.basename(self._desktop_file_path))[0]

    @staticmethod
    def _ensure_desktop_dir_exists():
        desktop_file_dir = TempDesktopFile._desktop_file_dir()
        if not os.path.exists(desktop_file_dir):
            return TempDesktopFile._create_desktop_file_dir(desktop_file_dir)
        return ''

    @staticmethod
    def _desktop_file_dir():
        return os.path.join(
            os.getenv('HOME'),
            '.local',
            'share',
            'applications'
        )

    @staticmethod
    def _create_desktop_file_dir(desktop_file_dir):
        """Create the directory specified.

        Returns the component of the path that did not exist, or the empty
        string if the entire path already existed.

        """
        # We might be creating more than just the leaf directory, so we need to
        # keep track of what doesn't already exist and remove it when we're
        # done. Defaults to removing the full path
        path_to_delete = ""
        if not os.path.exists(desktop_file_dir):
            path_to_delete = desktop_file_dir
        full_path, leaf = os.path.split(desktop_file_dir)
        while leaf != "":
            if not os.path.exists(full_path):
                path_to_delete = full_path
            full_path, leaf = os.path.split(full_path)

        try:
            os.makedirs(desktop_file_dir)
        except OSError:
            logger.warning("Directory already exists: %s" % desktop_file_dir)
        return path_to_delete

    @staticmethod
    def _remove_desktop_file_components(created_path, created_file):
        if created_path != "":
            rmtree(created_path)
        else:
            os.remove(created_file)

    @staticmethod
    def _create_desktop_file(file_contents):
        _, tmp_file_path = tempfile.mkstemp(
            suffix='.desktop',
            dir=TempDesktopFile._desktop_file_dir()
        )
        with open(tmp_file_path, 'w') as desktop_file:
            desktop_file.write(file_contents)
        return tmp_file_path