utils.py 5.8 KB

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