utils.py 2.1 KB

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