/src/utils.py

https://github.com/atareao/Touchpad-Indicator
Python | 71 lines | 42 code | 9 blank | 20 comment | 10 complexity | 26666af181d16dda9246d5bc41387993 MD5 | raw file
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This file is part of Touchpad-Indicator
  5. #
  6. # Copyright (C) 2010-2019 Lorenzo Carbonell<lorenzo.carbonell.cerezo@gmail.com>
  7. # Copyright (C) 2010-2012 Miguel Angel Santamaría Rogado<leibag@gmail.com>
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (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, see <http://www.gnu.org/licenses/>.
  21. import subprocess
  22. import shlex
  23. from os.path import isfile, join, basename
  24. import glob
  25. def get_version():
  26. command = 'lsb_release -c'
  27. po = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
  28. stderr=subprocess.PIPE, shell=False)
  29. out, err = po.communicate()
  30. return_code = po.wait()
  31. if return_code == 0:
  32. return out.decode().split('Codename:\t')[1].replace('\n', '')
  33. return None
  34. def is_package_installed(package_name):
  35. command = 'dpkg-query --show --showformat="${db:Status-Status}\n" "%s"' % (
  36. package_name)
  37. po = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE,
  38. stderr=subprocess.PIPE, shell=False)
  39. out, err = po.communicate()
  40. return_code = po.wait()
  41. if return_code == 0:
  42. return True
  43. return False
  44. def is_ppa_repository_added(repository):
  45. if repository.find('/') and repository.startswith('ppa:'):
  46. repository = repository[4:]
  47. firstpart, secondpart = repository.split('/')
  48. mypath = '/etc/apt/sources.list.d'
  49. onlyfiles = [basename(f).replace('.list', '') for f in
  50. glob.glob(join(mypath, '*.list'))
  51. if isfile(join(mypath, f))]
  52. for element in onlyfiles:
  53. if element.startswith(firstpart) and \
  54. element[len(firstpart):].find(secondpart) > -1:
  55. return True
  56. return False
  57. if __name__ == '__main__':
  58. print(is_package_installed('my-weather-indicator'))
  59. print(is_package_installed('xserver-xorg-input-libinput'))
  60. print(get_version())
  61. print(is_ppa_repository_added('ppa:atareao/atareao'))
  62. print(exists_psmouse())