2014-12-29 12:46:16 +01:00
|
|
|
#!/usr/bin/python2 -O
|
2015-01-19 18:03:23 +01:00
|
|
|
# vim: fileencoding=utf-8
|
2014-12-29 12:46:16 +01:00
|
|
|
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# The Qubes OS Project, https://www.qubes-os.org/
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# Copyright (C) 2010-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2013-2015 Marek Marczykowski-Górecki
|
|
|
|
# <marmarek@invisiblethingslab.com>
|
|
|
|
# Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
2015-01-19 18:03:23 +01:00
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2014-12-29 12:46:16 +01:00
|
|
|
#
|
|
|
|
|
2015-01-19 18:14:15 +01:00
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
|
2015-01-23 18:37:40 +01:00
|
|
|
import docutils
|
2015-06-23 19:02:58 +02:00
|
|
|
import docutils.core
|
|
|
|
import docutils.io
|
2015-01-23 18:37:40 +01:00
|
|
|
|
2014-12-29 12:46:16 +01:00
|
|
|
|
2015-01-13 15:40:43 +01:00
|
|
|
def get_timezone():
|
2014-12-29 12:46:16 +01:00
|
|
|
# fc18
|
|
|
|
if os.path.islink('/etc/localtime'):
|
|
|
|
return '/'.join(os.readlink('/etc/localtime').split('/')[-2:])
|
|
|
|
# <=fc17
|
|
|
|
elif os.path.exists('/etc/sysconfig/clock'):
|
|
|
|
clock_config = open('/etc/sysconfig/clock', "r")
|
|
|
|
clock_config_lines = clock_config.readlines()
|
|
|
|
clock_config.close()
|
|
|
|
zone_re = re.compile(r'^ZONE="(.*)"')
|
|
|
|
for line in clock_config_lines:
|
|
|
|
line_match = zone_re.match(line)
|
|
|
|
if line_match:
|
|
|
|
return line_match.group(1)
|
|
|
|
else:
|
|
|
|
# last resort way, some applications makes /etc/localtime
|
|
|
|
# hardlink instead of symlink...
|
|
|
|
tz_info = os.stat('/etc/localtime')
|
|
|
|
if not tz_info:
|
|
|
|
return None
|
|
|
|
if tz_info.st_nlink > 1:
|
|
|
|
p = subprocess.Popen(['find', '/usr/share/zoneinfo',
|
|
|
|
'-inum', str(tz_info.st_ino)],
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
tz_path = p.communicate()[0].strip()
|
|
|
|
return tz_path.replace('/usr/share/zoneinfo/', '')
|
|
|
|
return None
|
|
|
|
|
2015-01-23 18:37:40 +01:00
|
|
|
|
|
|
|
def format_doc(docstring):
|
|
|
|
'''Return parsed documentation string, stripping RST markup.
|
|
|
|
'''
|
|
|
|
|
|
|
|
if not docstring:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
# pylint: disable=unused-variable
|
|
|
|
output, pub = docutils.core.publish_programmatically(
|
|
|
|
source_class=docutils.io.StringInput,
|
|
|
|
source=' '.join(docstring.strip().split()),
|
|
|
|
source_path=None,
|
|
|
|
destination_class=docutils.io.NullOutput, destination=None,
|
|
|
|
destination_path=None,
|
|
|
|
reader=None, reader_name='standalone',
|
|
|
|
parser=None, parser_name='restructuredtext',
|
|
|
|
writer=None, writer_name='null',
|
|
|
|
settings=None, settings_spec=None, settings_overrides=None,
|
|
|
|
config_section=None, enable_exit_status=None)
|
|
|
|
return pub.writer.document.astext()
|
2015-10-01 22:14:35 +02:00
|
|
|
|
|
|
|
# FIXME those are wrong, k/M/G are SI prefixes and means 10**3
|
|
|
|
# maybe adapt https://code.activestate.com/recipes/578019
|
|
|
|
def parse_size(size):
|
|
|
|
units = [ ('K', 1024), ('KB', 1024),
|
|
|
|
('M', 1024*1024), ('MB', 1024*1024),
|
|
|
|
('G', 1024*1024*1024), ('GB', 1024*1024*1024),
|
|
|
|
]
|
|
|
|
|
|
|
|
size = size.strip().upper()
|
|
|
|
if size.isdigit():
|
|
|
|
return int(size)
|
|
|
|
|
|
|
|
for unit, multiplier in units:
|
|
|
|
if size.endswith(unit):
|
|
|
|
size = size[:-len(unit)].strip()
|
|
|
|
return int(size)*multiplier
|
|
|
|
|
|
|
|
raise QubesException("Invalid size: {0}.".format(size))
|