utils.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. def get_timezone():
  26. # fc18
  27. if os.path.islink('/etc/localtime'):
  28. return '/'.join(os.readlink('/etc/localtime').split('/')[-2:])
  29. # <=fc17
  30. elif os.path.exists('/etc/sysconfig/clock'):
  31. clock_config = open('/etc/sysconfig/clock', "r")
  32. clock_config_lines = clock_config.readlines()
  33. clock_config.close()
  34. zone_re = re.compile(r'^ZONE="(.*)"')
  35. for line in clock_config_lines:
  36. line_match = zone_re.match(line)
  37. if line_match:
  38. return line_match.group(1)
  39. else:
  40. # last resort way, some applications makes /etc/localtime
  41. # hardlink instead of symlink...
  42. tz_info = os.stat('/etc/localtime')
  43. if not tz_info:
  44. return None
  45. if tz_info.st_nlink > 1:
  46. p = subprocess.Popen(['find', '/usr/share/zoneinfo',
  47. '-inum', str(tz_info.st_ino)],
  48. stdout=subprocess.PIPE)
  49. tz_path = p.communicate()[0].strip()
  50. return tz_path.replace('/usr/share/zoneinfo/', '')
  51. return None