Unity 8
__init__.py
1 # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2 #
3 # Unity Autopilot Test Suite
4 # Copyright (C) 2012, 2013, 2014, 2015 Canonical
5 #
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #
19 
20 """unity autopilot tests and helpers - top level package."""
21 import os
22 import os.path
23 import subprocess
24 import sysconfig
25 
26 
27 class UnityException(Exception):
28  """Exception raised when there is an error with the Unity test helpers."""
29 
30 
31 def running_installed_tests():
32  binary_path = get_binary_path()
33  return binary_path.startswith('/usr')
34 
35 
36 def get_lib_path():
37  """Return the library path to use in this test run."""
38  if running_installed_tests():
39  lib_path = os.path.join(
40  "/usr/lib/",
41  sysconfig.get_config_var('MULTIARCH'),
42  "unity8"
43  )
44  else:
45  binary_path = get_binary_path()
46  lib_path = os.path.dirname(binary_path)
47  return lib_path
48 
49 
50 def get_default_extra_mock_libraries():
51  mocks_path = get_mocks_library_path()
52  return os.path.join(mocks_path, 'libusermetrics')
53 
54 
55 def get_mocks_library_path():
56  if running_installed_tests():
57  mock_path = "qml/mocks/"
58  else:
59  mock_path = os.path.join(
60  "../lib/",
61  sysconfig.get_config_var('MULTIARCH'),
62  "unity8/qml/mocks/"
63  )
64  lib_path = get_lib_path()
65  ld_library_path = os.path.abspath(
66  os.path.join(
67  lib_path,
68  mock_path,
69  )
70  )
71 
72  if not os.path.exists(ld_library_path):
73  raise RuntimeError(
74  "Expected library path does not exists: %s." % (ld_library_path)
75  )
76  return ld_library_path
77 
78 
79 def get_binary_path(binary="unity8"):
80  """Return the path to the specified binary."""
81  binary_path = os.path.abspath(
82  os.path.join(
83  os.path.dirname(__file__),
84  "../../../builddir/install/bin/%s" % binary
85  )
86  )
87  if not os.path.exists(binary_path):
88  try:
89  binary_path = subprocess.check_output(
90  ['which', binary],
91  universal_newlines=True,
92  ).strip()
93  except subprocess.CalledProcessError as e:
94  raise RuntimeError("Unable to locate %s binary: %r" % (binary, e))
95  return binary_path
96 
97 
98 def get_data_dirs(data_dirs_mock_enabled=True):
99  """Prepend a mock data path to XDG_DATA_DIRS."""
100  data_dirs = _get_xdg_env_path()
101  if data_dirs_mock_enabled:
102  mock_data_path = _get_full_mock_data_path()
103  if os.path.exists(mock_data_path):
104  if data_dirs is not None:
105  data_dirs = '{0}:{1}'.format(mock_data_path, data_dirs)
106  else:
107  data_dirs = mock_data_path
108  return data_dirs
109 
110 
111 def _get_full_mock_data_path():
112  if running_installed_tests():
113  data_path = "/usr/share/unity8/mocks/data"
114  else:
115  data_path = "../../mocks/data"
116  return os.path.abspath(
117  os.path.join(
118  os.path.dirname(__file__),
119  data_path
120  )
121  )
122 
123 
124 def _get_xdg_env_path():
125  path = os.getenv("XDG_DATA_DIRS")
126  if path is None:
127  path = _get_xdg_upstart_env()
128  return path
129 
130 
131 def _get_xdg_upstart_env():
132  try:
133  return subprocess.check_output([
134  "/sbin/initctl",
135  "get-env",
136  "--global",
137  "XDG_DATA_DIRS"
138  ], universal_newlines=True).rstrip()
139  except subprocess.CalledProcessError:
140  return None
141 
142 
143 def get_grid_size():
144  grid_size = os.getenv('GRID_UNIT_PX')
145  if grid_size is None:
146  raise RuntimeError(
147  "Environment variable GRID_UNIT_PX has not been set."
148  )
149  return int(grid_size)