Unity 8
 All Classes Functions
__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 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 emulators - top level package."""
21 import os
22 import os.path
23 import subprocess
24 import sysconfig
25 
26 
27 def running_installed_tests():
28  binary_path = get_binary_path()
29  return binary_path.startswith('/usr')
30 
31 
32 def get_lib_path():
33  """Return the library path to use in this test run."""
34  if running_installed_tests():
35  lib_path = os.path.join(
36  "/usr/lib/",
37  sysconfig.get_config_var('MULTIARCH'),
38  "unity8"
39  )
40  else:
41  binary_path = get_binary_path()
42  lib_path = os.path.dirname(binary_path)
43  return lib_path
44 
45 
46 def get_default_extra_mock_libraries():
47  mocks_path = get_mocks_library_path()
48  return os.path.join(mocks_path, 'libusermetrics')
49 
50 
51 def get_mocks_library_path():
52  if running_installed_tests():
53  mock_path = "qml/mocks/"
54  else:
55  mock_path = os.path.join(
56  "../lib/",
57  sysconfig.get_config_var('MULTIARCH'),
58  "unity8/qml/mocks/"
59  )
60  lib_path = get_lib_path()
61  ld_library_path = os.path.abspath(
62  os.path.join(
63  lib_path,
64  mock_path,
65  )
66  )
67 
68  if not os.path.exists(ld_library_path):
69  raise RuntimeError(
70  "Expected library path does not exists: %s." % (ld_library_path)
71  )
72  return ld_library_path
73 
74 
75 def get_binary_path(binary="unity8"):
76  """Return the path to the specified binary."""
77  binary_path = os.path.abspath(
78  os.path.join(
79  os.path.dirname(__file__),
80  "../../../builddir/install/bin/%s" % binary
81  )
82  )
83  if not os.path.exists(binary_path):
84  try:
85  binary_path = subprocess.check_output(
86  ['which', binary],
87  universal_newlines=True,
88  ).strip()
89  except subprocess.CalledProcessError as e:
90  raise RuntimeError("Unable to locate %s binary: %r" % (binary, e))
91  return binary_path
92 
93 
94 def get_data_dirs(data_dirs_mock_enabled=True):
95  """Prepend a mock data path to XDG_DATA_DIRS."""
96  data_dirs = _get_xdg_env_path()
97  if data_dirs_mock_enabled:
98  mock_data_path = _get_full_mock_data_path()
99  if os.path.exists(mock_data_path):
100  if data_dirs is not None:
101  data_dirs = '{0}:{1}'.format(mock_data_path, data_dirs)
102  else:
103  data_dirs = mock_data_path
104  return data_dirs
105 
106 
107 def _get_full_mock_data_path():
108  if running_installed_tests():
109  data_path = "/usr/share/unity8/mocks/data"
110  else:
111  data_path = "../../mocks/data"
112  return os.path.abspath(
113  os.path.join(
114  os.path.dirname(__file__),
115  data_path
116  )
117  )
118 
119 
120 def _get_xdg_env_path():
121  path = os.getenv("XDG_DATA_DIRS")
122  if path is None:
123  path = _get_xdg_upstart_env()
124  return path
125 
126 
127 def _get_xdg_upstart_env():
128  try:
129  return subprocess.check_output([
130  "/sbin/initctl",
131  "get-env",
132  "--global",
133  "XDG_DATA_DIRS"
134  ], universal_newlines=True).rstrip()
135  except subprocess.CalledProcessError:
136  return None
137 
138 
139 def get_grid_size():
140  grid_size = os.getenv('GRID_UNIT_PX')
141  if grid_size is None:
142  raise RuntimeError(
143  "Environment variable GRID_UNIT_PX has not been set."
144  )
145  return int(grid_size)