utils.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/python2 -O
  2. # -*- coding: utf-8 -*-
  3. # The Qubes OS Project, http://www.qubes-os.org
  4. #
  5. # Copyright (C) 2010 Joanna Rutkowska <joanna@invisiblethingslab.com>
  6. # Copyright (C) 2013 Marek Marczykowski <marmarek@invisiblethingslab.com>
  7. # Copyright (C) 2014 Wojtek Porczyk <woju@invisiblethingslab.com>
  8. #
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13. #
  14. # This program 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
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. #
  23. # FIXME: should be outside of QubesVM?
  24. def get_timezone(self):
  25. # fc18
  26. if os.path.islink('/etc/localtime'):
  27. return '/'.join(os.readlink('/etc/localtime').split('/')[-2:])
  28. # <=fc17
  29. elif os.path.exists('/etc/sysconfig/clock'):
  30. clock_config = open('/etc/sysconfig/clock', "r")
  31. clock_config_lines = clock_config.readlines()
  32. clock_config.close()
  33. zone_re = re.compile(r'^ZONE="(.*)"')
  34. for line in clock_config_lines:
  35. line_match = zone_re.match(line)
  36. if line_match:
  37. return line_match.group(1)
  38. else:
  39. # last resort way, some applications makes /etc/localtime
  40. # hardlink instead of symlink...
  41. tz_info = os.stat('/etc/localtime')
  42. if not tz_info:
  43. return None
  44. if tz_info.st_nlink > 1:
  45. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  46. '-inum', str(tz_info.st_ino)],
  47. stdout=subprocess.PIPE)
  48. tz_path = p.communicate()[0].strip()
  49. return tz_path.replace('/usr/share/zoneinfo/', '')
  50. return None