utils.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  6. # <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This library is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU Lesser General Public
  11. # License as published by the Free Software Foundation; either
  12. # version 2.1 of the License, or (at your option) any later version.
  13. #
  14. # This library is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # Lesser General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU Lesser General Public
  20. # License along with this library; if not, see <https://www.gnu.org/licenses/>.
  21. #
  22. import asyncio
  23. import hashlib
  24. import logging
  25. import random
  26. import string
  27. import os
  28. import os.path
  29. import re
  30. import socket
  31. import subprocess
  32. import tempfile
  33. from contextlib import contextmanager, suppress
  34. import pkg_resources
  35. import docutils
  36. import docutils.core
  37. import docutils.io
  38. import qubes.exc
  39. LOGGER = logging.getLogger('qubes.utils')
  40. def get_timezone():
  41. # fc18
  42. if os.path.islink('/etc/localtime'):
  43. tz_path = '/'.join(os.readlink('/etc/localtime').split('/'))
  44. return tz_path.split('zoneinfo/')[1]
  45. # <=fc17
  46. if os.path.exists('/etc/sysconfig/clock'):
  47. clock_config = open('/etc/sysconfig/clock', "r")
  48. clock_config_lines = clock_config.readlines()
  49. clock_config.close()
  50. zone_re = re.compile(r'^ZONE="(.*)"')
  51. for line in clock_config_lines:
  52. line_match = zone_re.match(line)
  53. if line_match:
  54. return line_match.group(1)
  55. # last resort way, some applications makes /etc/localtime
  56. # hardlink instead of symlink...
  57. tz_info = os.stat('/etc/localtime')
  58. if not tz_info:
  59. return None
  60. if tz_info.st_nlink > 1:
  61. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  62. '-inum', str(tz_info.st_ino), '-print', '-quit'],
  63. stdout=subprocess.PIPE)
  64. tz_path = p.communicate()[0].strip()
  65. return tz_path.replace(b'/usr/share/zoneinfo/', b'')
  66. return None
  67. def format_doc(docstring):
  68. '''Return parsed documentation string, stripping RST markup.
  69. '''
  70. if not docstring:
  71. return ''
  72. # pylint: disable=unused-variable
  73. output, pub = docutils.core.publish_programmatically(
  74. source_class=docutils.io.StringInput,
  75. source=' '.join(docstring.strip().split()),
  76. source_path=None,
  77. destination_class=docutils.io.NullOutput, destination=None,
  78. destination_path=None,
  79. reader=None, reader_name='standalone',
  80. parser=None, parser_name='restructuredtext',
  81. writer=None, writer_name='null',
  82. settings=None, settings_spec=None, settings_overrides=None,
  83. config_section=None, enable_exit_status=None)
  84. return pub.writer.document.astext()
  85. def parse_size(size):
  86. units = [
  87. ('K', 1000), ('KB', 1000),
  88. ('M', 1000 * 1000), ('MB', 1000 * 1000),
  89. ('G', 1000 * 1000 * 1000), ('GB', 1000 * 1000 * 1000),
  90. ('Ki', 1024), ('KiB', 1024),
  91. ('Mi', 1024 * 1024), ('MiB', 1024 * 1024),
  92. ('Gi', 1024 * 1024 * 1024), ('GiB', 1024 * 1024 * 1024),
  93. ]
  94. size = size.strip().upper()
  95. if size.isdigit():
  96. return int(size)
  97. for unit, multiplier in units:
  98. if size.endswith(unit.upper()):
  99. size = size[:-len(unit)].strip()
  100. return int(size) * multiplier
  101. raise qubes.exc.QubesException("Invalid size: {0}.".format(size))
  102. def mbytes_to_kmg(size):
  103. if size > 1024:
  104. return "%d GiB" % (size / 1024)
  105. return "%d MiB" % size
  106. def kbytes_to_kmg(size):
  107. if size > 1024:
  108. return mbytes_to_kmg(size / 1024)
  109. return "%d KiB" % size
  110. def bytes_to_kmg(size):
  111. if size > 1024:
  112. return kbytes_to_kmg(size / 1024)
  113. return "%d B" % size
  114. def size_to_human(size):
  115. """Humane readable size, with 1/10 precision"""
  116. if size < 1024:
  117. return str(size)
  118. if size < 1024 * 1024:
  119. return str(round(size / 1024.0, 1)) + ' KiB'
  120. if size < 1024 * 1024 * 1024:
  121. return str(round(size / (1024.0 * 1024), 1)) + ' MiB'
  122. return str(round(size / (1024.0 * 1024 * 1024), 1)) + ' GiB'
  123. def urandom(size):
  124. rand = os.urandom(size)
  125. if rand is None:
  126. raise IOError('failed to read urandom')
  127. return hashlib.sha512(rand).digest()
  128. def get_entry_point_one(group, name):
  129. epoints = tuple(pkg_resources.iter_entry_points(group, name))
  130. if not epoints:
  131. raise KeyError(name)
  132. if len(epoints) > 1:
  133. raise TypeError(
  134. 'more than 1 implementation of {!r} found: {}'.format(name,
  135. ', '.join('{}.{}'.format(ep.module_name, '.'.join(ep.attrs))
  136. for ep in epoints)))
  137. return epoints[0].load()
  138. def random_string(length=5):
  139. ''' Return random string consisting of ascii_leters and digits '''
  140. return ''.join(random.choice(string.ascii_letters + string.digits)
  141. for _ in range(length))
  142. def systemd_notify():
  143. '''Notify systemd'''
  144. nofity_socket = os.getenv('NOTIFY_SOCKET')
  145. if not nofity_socket:
  146. return
  147. if nofity_socket.startswith('@'):
  148. nofity_socket = '\0' + nofity_socket[1:]
  149. sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  150. sock.connect(nofity_socket)
  151. sock.sendall(b'READY=1')
  152. sock.close()
  153. def match_vm_name_with_special(vm, name):
  154. '''Check if *vm* matches given name, which may be specified as @tag:...
  155. or @type:...'''
  156. if name.startswith('@tag:'):
  157. return name[len('@tag:'):] in vm.tags
  158. if name.startswith('@type:'):
  159. return name[len('@type:'):] == vm.__class__.__name__
  160. return name == vm.name
  161. @contextmanager
  162. def replace_file(dst, *, permissions, close_on_success=True,
  163. logger=LOGGER, log_level=logging.DEBUG):
  164. ''' Yield a tempfile whose name starts with dst. If the block does
  165. not raise an exception, apply permissions and persist the
  166. tempfile to dst (which is allowed to already exist). Otherwise
  167. ensure that the tempfile is cleaned up.
  168. '''
  169. tmp_dir, prefix = os.path.split(dst + '~')
  170. tmp = tempfile.NamedTemporaryFile(dir=tmp_dir, prefix=prefix, delete=False)
  171. try:
  172. yield tmp
  173. tmp.flush()
  174. os.fchmod(tmp.fileno(), permissions)
  175. os.fsync(tmp.fileno())
  176. if close_on_success:
  177. tmp.close()
  178. rename_file(tmp.name, dst, logger=logger, log_level=log_level)
  179. except:
  180. try:
  181. tmp.close()
  182. finally:
  183. remove_file(tmp.name, logger=logger, log_level=log_level)
  184. raise
  185. def rename_file(src, dst, *, logger=LOGGER, log_level=logging.DEBUG):
  186. ''' Durably rename src to dst. '''
  187. os.rename(src, dst)
  188. dst_dir = os.path.dirname(dst)
  189. src_dir = os.path.dirname(src)
  190. fsync_path(dst_dir)
  191. if src_dir != dst_dir:
  192. fsync_path(src_dir)
  193. logger.log(log_level, 'Renamed file: %r -> %r', src, dst)
  194. def remove_file(path, *, logger=LOGGER, log_level=logging.DEBUG):
  195. ''' Durably remove the file at path, if it exists. Return whether
  196. we removed it. '''
  197. with suppress(FileNotFoundError):
  198. os.remove(path)
  199. fsync_path(os.path.dirname(path))
  200. logger.log(log_level, 'Removed file: %r', path)
  201. return True
  202. return False
  203. def fsync_path(path):
  204. fd = os.open(path, os.O_RDONLY) # works for a file or a directory
  205. try:
  206. os.fsync(fd)
  207. finally:
  208. os.close(fd)
  209. @asyncio.coroutine
  210. def coro_maybe(value):
  211. if asyncio.iscoroutine(value):
  212. return (yield from value)
  213. return value
  214. @asyncio.coroutine
  215. def void_coros_maybe(values):
  216. ''' Ignore elements of the iterable values that are not coroutine
  217. objects. Run all coroutine objects to completion, concurrent
  218. with each other. If there were exceptions, raise the leftmost
  219. one (not necessarily chronologically first). Return nothing.
  220. '''
  221. coros = [val for val in values if asyncio.iscoroutine(val)]
  222. if coros:
  223. done, _ = yield from asyncio.wait(coros)
  224. for task in done:
  225. task.result() # re-raises exception if task failed