utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/python2 -O
  2. # vim: fileencoding=utf-8
  3. #
  4. # The Qubes OS Project, https://www.qubes-os.org/
  5. #
  6. # Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  7. # Copyright (C) 2013-2015 Marek Marczykowski-Górecki
  8. # <marmarek@invisiblethingslab.com>
  9. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  10. #
  11. # This program is free software; you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation; either version 2 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License along
  22. # with this program; if not, write to the Free Software Foundation, Inc.,
  23. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. #
  25. import os
  26. import re
  27. import subprocess
  28. import docutils
  29. import docutils.core
  30. import docutils.io
  31. import qubes.exc
  32. def get_timezone():
  33. # fc18
  34. if os.path.islink('/etc/localtime'):
  35. return '/'.join(os.readlink('/etc/localtime').split('/')[-2:])
  36. # <=fc17
  37. elif os.path.exists('/etc/sysconfig/clock'):
  38. clock_config = open('/etc/sysconfig/clock', "r")
  39. clock_config_lines = clock_config.readlines()
  40. clock_config.close()
  41. zone_re = re.compile(r'^ZONE="(.*)"')
  42. for line in clock_config_lines:
  43. line_match = zone_re.match(line)
  44. if line_match:
  45. return line_match.group(1)
  46. else:
  47. # last resort way, some applications makes /etc/localtime
  48. # hardlink instead of symlink...
  49. tz_info = os.stat('/etc/localtime')
  50. if not tz_info:
  51. return None
  52. if tz_info.st_nlink > 1:
  53. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  54. '-inum', str(tz_info.st_ino)],
  55. stdout=subprocess.PIPE)
  56. tz_path = p.communicate()[0].strip()
  57. return tz_path.replace('/usr/share/zoneinfo/', '')
  58. return None
  59. def format_doc(docstring):
  60. '''Return parsed documentation string, stripping RST markup.
  61. '''
  62. if not docstring:
  63. return ''
  64. # pylint: disable=unused-variable
  65. output, pub = docutils.core.publish_programmatically(
  66. source_class=docutils.io.StringInput,
  67. source=' '.join(docstring.strip().split()),
  68. source_path=None,
  69. destination_class=docutils.io.NullOutput, destination=None,
  70. destination_path=None,
  71. reader=None, reader_name='standalone',
  72. parser=None, parser_name='restructuredtext',
  73. writer=None, writer_name='null',
  74. settings=None, settings_spec=None, settings_overrides=None,
  75. config_section=None, enable_exit_status=None)
  76. return pub.writer.document.astext()
  77. # FIXME those are wrong, k/M/G are SI prefixes and means 10**3
  78. # maybe adapt https://code.activestate.com/recipes/578019
  79. def parse_size(size):
  80. units = [
  81. ('K', 1024), ('KB', 1024),
  82. ('M', 1024*1024), ('MB', 1024*1024),
  83. ('G', 1024*1024*1024), ('GB', 1024*1024*1024),
  84. ]
  85. size = size.strip().upper()
  86. if size.isdigit():
  87. return int(size)
  88. for unit, multiplier in units:
  89. if size.endswith(unit):
  90. size = size[:-len(unit)].strip()
  91. return int(size)*multiplier
  92. raise qubes.exc.QubesException("Invalid size: {0}.".format(size))