utils.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 random
  25. import string
  26. import os
  27. import re
  28. import socket
  29. import subprocess
  30. import pkg_resources
  31. import docutils
  32. import docutils.core
  33. import docutils.io
  34. import qubes.exc
  35. def get_timezone():
  36. # fc18
  37. if os.path.islink('/etc/localtime'):
  38. tz_path = '/'.join(os.readlink('/etc/localtime').split('/'))
  39. return tz_path.split('zoneinfo/')[1]
  40. # <=fc17
  41. if os.path.exists('/etc/sysconfig/clock'):
  42. clock_config = open('/etc/sysconfig/clock', "r")
  43. clock_config_lines = clock_config.readlines()
  44. clock_config.close()
  45. zone_re = re.compile(r'^ZONE="(.*)"')
  46. for line in clock_config_lines:
  47. line_match = zone_re.match(line)
  48. if line_match:
  49. return line_match.group(1)
  50. # last resort way, some applications makes /etc/localtime
  51. # hardlink instead of symlink...
  52. tz_info = os.stat('/etc/localtime')
  53. if not tz_info:
  54. return None
  55. if tz_info.st_nlink > 1:
  56. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  57. '-inum', str(tz_info.st_ino), '-print', '-quit'],
  58. stdout=subprocess.PIPE)
  59. tz_path = p.communicate()[0].strip()
  60. return tz_path.replace(b'/usr/share/zoneinfo/', b'')
  61. return None
  62. def format_doc(docstring):
  63. '''Return parsed documentation string, stripping RST markup.
  64. '''
  65. if not docstring:
  66. return ''
  67. # pylint: disable=unused-variable
  68. output, pub = docutils.core.publish_programmatically(
  69. source_class=docutils.io.StringInput,
  70. source=' '.join(docstring.strip().split()),
  71. source_path=None,
  72. destination_class=docutils.io.NullOutput, destination=None,
  73. destination_path=None,
  74. reader=None, reader_name='standalone',
  75. parser=None, parser_name='restructuredtext',
  76. writer=None, writer_name='null',
  77. settings=None, settings_spec=None, settings_overrides=None,
  78. config_section=None, enable_exit_status=None)
  79. return pub.writer.document.astext()
  80. def parse_size(size):
  81. units = [
  82. ('K', 1000), ('KB', 1000),
  83. ('M', 1000 * 1000), ('MB', 1000 * 1000),
  84. ('G', 1000 * 1000 * 1000), ('GB', 1000 * 1000 * 1000),
  85. ('Ki', 1024), ('KiB', 1024),
  86. ('Mi', 1024 * 1024), ('MiB', 1024 * 1024),
  87. ('Gi', 1024 * 1024 * 1024), ('GiB', 1024 * 1024 * 1024),
  88. ]
  89. size = size.strip().upper()
  90. if size.isdigit():
  91. return int(size)
  92. for unit, multiplier in units:
  93. if size.endswith(unit.upper()):
  94. size = size[:-len(unit)].strip()
  95. return int(size) * multiplier
  96. raise qubes.exc.QubesException("Invalid size: {0}.".format(size))
  97. def mbytes_to_kmg(size):
  98. if size > 1024:
  99. return "%d GiB" % (size / 1024)
  100. return "%d MiB" % size
  101. def kbytes_to_kmg(size):
  102. if size > 1024:
  103. return mbytes_to_kmg(size / 1024)
  104. return "%d KiB" % size
  105. def bytes_to_kmg(size):
  106. if size > 1024:
  107. return kbytes_to_kmg(size / 1024)
  108. return "%d B" % size
  109. def size_to_human(size):
  110. """Humane readable size, with 1/10 precision"""
  111. if size < 1024:
  112. return str(size)
  113. if size < 1024 * 1024:
  114. return str(round(size / 1024.0, 1)) + ' KiB'
  115. if size < 1024 * 1024 * 1024:
  116. return str(round(size / (1024.0 * 1024), 1)) + ' MiB'
  117. return str(round(size / (1024.0 * 1024 * 1024), 1)) + ' GiB'
  118. def urandom(size):
  119. rand = os.urandom(size)
  120. if rand is None:
  121. raise IOError('failed to read urandom')
  122. return hashlib.sha512(rand).digest()
  123. def get_entry_point_one(group, name):
  124. epoints = tuple(pkg_resources.iter_entry_points(group, name))
  125. if not epoints:
  126. raise KeyError(name)
  127. if len(epoints) > 1:
  128. raise TypeError(
  129. 'more than 1 implementation of {!r} found: {}'.format(name,
  130. ', '.join('{}.{}'.format(ep.module_name, '.'.join(ep.attrs))
  131. for ep in epoints)))
  132. return epoints[0].load()
  133. def random_string(length=5):
  134. ''' Return random string consisting of ascii_leters and digits '''
  135. return ''.join(random.choice(string.ascii_letters + string.digits)
  136. for _ in range(length))
  137. def systemd_notify():
  138. '''Notify systemd'''
  139. nofity_socket = os.getenv('NOTIFY_SOCKET')
  140. if not nofity_socket:
  141. return
  142. if nofity_socket.startswith('@'):
  143. nofity_socket = '\0' + nofity_socket[1:]
  144. sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
  145. sock.connect(nofity_socket)
  146. sock.sendall(b'READY=1')
  147. sock.close()
  148. def match_vm_name_with_special(vm, name):
  149. '''Check if *vm* matches given name, which may be specified as @tag:...
  150. or @type:...'''
  151. if name.startswith('@tag:'):
  152. return name[len('@tag:'):] in vm.tags
  153. if name.startswith('@type:'):
  154. return name[len('@type:'):] == vm.__class__.__name__
  155. return name == vm.name
  156. @asyncio.coroutine
  157. def coro_maybe(value):
  158. if asyncio.iscoroutine(value):
  159. return (yield from value)
  160. return value
  161. @asyncio.coroutine
  162. def void_coros_maybe(values):
  163. ''' Ignore elements of the iterable values that are not coroutine
  164. objects. Run all coroutine objects to completion, in parallel
  165. to each other. If there were exceptions, re-raise the leftmost
  166. one (not necessarily chronologically first). Return nothing.
  167. '''
  168. coros = [val for val in values if asyncio.iscoroutine(val)]
  169. if coros:
  170. done, _ = yield from asyncio.wait(coros)
  171. for task in done:
  172. task.result() # re-raises exception if task failed