utils.py 2.2 KB

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