utils.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 socket
  30. import subprocess
  31. import tempfile
  32. from contextlib import contextmanager, suppress
  33. import pkg_resources
  34. import docutils
  35. import docutils.core
  36. import docutils.io
  37. import qubes.exc
  38. LOGGER = logging.getLogger('qubes.utils')
  39. def get_timezone():
  40. if os.path.islink('/etc/localtime'):
  41. tz_path = '/'.join(os.readlink('/etc/localtime').split('/'))
  42. return tz_path.split('zoneinfo/')[1]
  43. # last resort way, some applications makes /etc/localtime
  44. # hardlink instead of symlink...
  45. tz_info = os.stat('/etc/localtime')
  46. if not tz_info:
  47. return None
  48. if tz_info.st_nlink > 1:
  49. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  50. '-inum', str(tz_info.st_ino), '-print', '-quit'],
  51. stdout=subprocess.PIPE)
  52. tz_path = p.communicate()[0].strip()
  53. return tz_path.replace(b'/usr/share/zoneinfo/', b'')
  54. return None
  55. def format_doc(docstring):
  56. '''Return parsed documentation string, stripping RST markup.
  57. '''
  58. if not docstring:
  59. return ''
  60. # pylint: disable=unused-variable
  61. output, pub = docutils.core.publish_programmatically(
  62. source_class=docutils.io.StringInput,
  63. source=' '.join(docstring.strip().split()),
  64. source_path=None,
  65. destination_class=docutils.io.NullOutput, destination=None,
  66. destination_path=None,
  67. reader=None, reader_name='standalone',
  68. parser=None, parser_name='restructuredtext',
  69. writer=None, writer_name='null',
  70. settings=None, settings_spec=None, settings_overrides=None,
  71. config_section=None, enable_exit_status=None)
  72. return pub.writer.document.astext()
  73. def parse_size(size):
  74. units = [
  75. ('K', 1000), ('KB', 1000),
  76. ('M', 1000 * 1000), ('MB', 1000 * 1000),
  77. ('G', 1000 * 1000 * 1000), ('GB', 1000 * 1000 * 1000),
  78. ('Ki', 1024), ('KiB', 1024),
  79. ('Mi', 1024 * 1024), ('MiB', 1024 * 1024),
  80. ('Gi', 1024 * 1024 * 1024), ('GiB', 1024 * 1024 * 1024),
  81. ]
  82. size = size.strip().upper()
  83. if size.isdigit():
  84. return int(size)
  85. for unit, multiplier in units:
  86. if size.endswith(unit.upper()):
  87. size = size[:-len(unit)].strip()
  88. return int(size) * multiplier
  89. raise qubes.exc.QubesException("Invalid size: {0}.".format(size))
  90. def mbytes_to_kmg(size):
  91. if size > 1024:
  92. return "%d GiB" % (size / 1024)
  93. return "%d MiB" % size
  94. def kbytes_to_kmg(size):
  95. if size > 1024:
  96. return mbytes_to_kmg(size / 1024)
  97. return "%d KiB" % size
  98. def bytes_to_kmg(size):
  99. if size > 1024:
  100. return kbytes_to_kmg(size / 1024)
  101. return "%d B" % size
  102. def size_to_human(size):
  103. """Humane readable size, with 1/10 precision"""
  104. if size < 1024:
  105. return str(size)
  106. if size < 1024 * 1024:
  107. return str(round(size / 1024.0, 1)) + ' KiB'
  108. if size < 1024 * 1024 * 1024:
  109. return str(round(size / (1024.0 * 1024), 1)) + ' MiB'
  110. return str(round(size / (1024.0 * 1024 * 1024), 1)) + ' GiB'
  111. def urandom(size):
  112. rand = os.urandom(size)
  113. if rand is None:
  114. raise IOError('failed to read urandom')
  115. return hashlib.sha512(rand).digest()
  116. def get_entry_point_one(group, name):
  117. epoints = tuple(pkg_resources.iter_entry_points(group, name))
  118. if not epoints:
  119. raise KeyError(name)
  120. if len(epoints) > 1:
  121. raise TypeError(
  122. 'more than 1 implementation of {!r} found: {}'.format(name,
  123. ', '.join('{}.{}'.format(ep.module_name, '.'.join(ep.attrs))
  124. for ep in epoints)))
  125. return epoints[0].load()
  126. def random_string(length=5):
  127. ''' Return random string consisting of ascii_leters and digits '''
  128. return ''.join(random.choice(string.ascii_letters + string.digits)
  129. for _ in range(length))
  130. def systemd_notify():
  131. '''Notify systemd'''
  132. nofity_socket = os.getenv('NOTIFY_SOCKET')
  133. if not nofity_socket:
  134. return
  135. if nofity_socket.startswith('@'):
  136. nofity_socket = '\0' + nofity_socket[1:]
  137. sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  138. sock.connect(nofity_socket)
  139. sock.sendall(b'READY=1')
  140. sock.close()
  141. def match_vm_name_with_special(vm, name):
  142. '''Check if *vm* matches given name, which may be specified as @tag:...
  143. or @type:...'''
  144. if name.startswith('@tag:'):
  145. return name[len('@tag:'):] in vm.tags
  146. if name.startswith('@type:'):
  147. return name[len('@type:'):] == vm.__class__.__name__
  148. return name == vm.name
  149. @contextmanager
  150. def replace_file(dst, *, permissions, close_on_success=True,
  151. logger=LOGGER, log_level=logging.DEBUG):
  152. ''' Yield a tempfile whose name starts with dst. If the block does
  153. not raise an exception, apply permissions and persist the
  154. tempfile to dst (which is allowed to already exist). Otherwise
  155. ensure that the tempfile is cleaned up.
  156. '''
  157. tmp_dir, prefix = os.path.split(dst + '~')
  158. tmp = tempfile.NamedTemporaryFile(dir=tmp_dir, prefix=prefix, delete=False)
  159. try:
  160. yield tmp
  161. tmp.flush()
  162. os.fchmod(tmp.fileno(), permissions)
  163. os.fsync(tmp.fileno())
  164. if close_on_success:
  165. tmp.close()
  166. rename_file(tmp.name, dst, logger=logger, log_level=log_level)
  167. except:
  168. try:
  169. tmp.close()
  170. finally:
  171. remove_file(tmp.name, logger=logger, log_level=log_level)
  172. raise
  173. def rename_file(src, dst, *, logger=LOGGER, log_level=logging.DEBUG):
  174. ''' Durably rename src to dst. '''
  175. os.rename(src, dst)
  176. dst_dir = os.path.dirname(dst)
  177. src_dir = os.path.dirname(src)
  178. fsync_path(dst_dir)
  179. if src_dir != dst_dir:
  180. fsync_path(src_dir)
  181. logger.log(log_level, 'Renamed file: %r -> %r', src, dst)
  182. def remove_file(path, *, logger=LOGGER, log_level=logging.DEBUG):
  183. ''' Durably remove the file at path, if it exists. Return whether
  184. we removed it. '''
  185. with suppress(FileNotFoundError):
  186. os.remove(path)
  187. fsync_path(os.path.dirname(path))
  188. logger.log(log_level, 'Removed file: %r', path)
  189. return True
  190. return False
  191. def fsync_path(path):
  192. fd = os.open(path, os.O_RDONLY) # works for a file or a directory
  193. try:
  194. os.fsync(fd)
  195. finally:
  196. os.close(fd)
  197. @asyncio.coroutine
  198. def coro_maybe(value):
  199. if asyncio.iscoroutine(value):
  200. return (yield from value)
  201. return value
  202. @asyncio.coroutine
  203. def void_coros_maybe(values):
  204. ''' Ignore elements of the iterable values that are not coroutine
  205. objects. Run all coroutine objects to completion, concurrent
  206. with each other. If there were exceptions, raise the leftmost
  207. one (not necessarily chronologically first). Return nothing.
  208. '''
  209. coros = [val for val in values if asyncio.iscoroutine(val)]
  210. if coros:
  211. done, _ = yield from asyncio.wait(coros)
  212. for task in done:
  213. task.result() # re-raises exception if task failed