 c538d536c8
			
		
	
	
		c538d536c8
		
	
	
	
	
		
			
			This is part of fixing qvm-start. qmemman was moved with minimal touching, mainly module names. Moved function parsing human-readable sizes from core2. This function is wrong, because it treats k/M/G as 1024-based, but leave it for now.
		
			
				
	
	
		
			104 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
| #!/usr/bin/python2 -O
 | |
| # vim: fileencoding=utf-8
 | |
| 
 | |
| #
 | |
| # The Qubes OS Project, https://www.qubes-os.org/
 | |
| #
 | |
| # 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.
 | |
| #
 | |
| # 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.
 | |
| #
 | |
| # 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.
 | |
| #
 | |
| 
 | |
| import os
 | |
| import re
 | |
| import subprocess
 | |
| 
 | |
| import docutils
 | |
| import docutils.core
 | |
| import docutils.io
 | |
| 
 | |
| 
 | |
| def get_timezone():
 | |
|     # 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
 | |
| 
 | |
| 
 | |
| 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()
 | |
| 
 | |
| # 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))
 |