install.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #! /usr/bin/env python
  2. #
  3. # This file is part of stov, written by Helmut Pozimski in 2012.
  4. #
  5. # stov is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, version 2 of the License.
  8. #
  9. # stov is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with stov. If not, see <http://www.gnu.org/licenses/>.
  16. # -*- coding: utf8 -*-
  17. from __future__ import print_function, unicode_literals
  18. import sys
  19. import subprocess
  20. import os
  21. import shutil
  22. """A simple installation script which checks if all requirements are met
  23. and copies all files of stov in the necessary locations
  24. """
  25. def main():
  26. """Check if a valid Path option is set or print the help if called"""
  27. try:
  28. sys.argv[1]
  29. except IndexError:
  30. print("No argument given, please give the path to install the program to"
  31. " or --help")
  32. sys.exit(2)
  33. if sys.argv[1] == "-h" or sys.argv[1] == "--help":
  34. print("""Usage: ./install.py OPTION
  35. Valid options are:
  36. -h or --help: Print this help screen
  37. $PATH: Define the Path to install to, e.g. /usr/local or /usr/share
  38. If installation fails, please check if the given path is correct and
  39. you can write to it.""")
  40. sys.exit(0)
  41. else:
  42. installpath = sys.argv[1]
  43. if os.access(installpath, os.F_OK) is not True:
  44. print >> sys.stderr, ("The directory does not exist, "
  45. "cancelling installation!")
  46. sys.exit(1)
  47. elif os.access(installpath, os.W_OK) is not True:
  48. print >> sys.stderr, ("You have no write permission"
  49. "for the defined directory, cancelling installation")
  50. sys.exit(1)
  51. """First check if all requirements are met and alert the user if
  52. they are not.
  53. """
  54. if sys.version_info < (2, 6):
  55. print >> sys.stderr, ("Your python version is below 2.6! You might still"
  56. "be able to run the program but use it at your own risk!")
  57. else:
  58. print("Checking running python version: OK")
  59. try:
  60. subprocess.check_call(["youtube-dl", "-h"], stdout=subprocess.PIPE)
  61. except subprocess.CalledProcessError:
  62. print >> sys.stderr, ("Could not find youtube-dl, it either does not exist,"
  63. " is not readable or not executable. Please note that "
  64. "youtube-dl is not needed for the program to run but is"
  65. " needed to use the download option which won't work otherwise."
  66. " If youtube-dl isn't found automatically, you may also enter "
  67. "the path to it in the configuration file.")
  68. else:
  69. print("Checking for youtube-dl: OK")
  70. """Create the subdirectory for the installation process"""
  71. programpath = os.path.join(installpath, "stov")
  72. files = ["stov.py", "subscription.py", "youtube.py", "configuration.py",
  73. "outputhelper.py", "youtubeAPI.py"]
  74. directories = ["locale"]
  75. installation_success = True
  76. """Start the installation process by copying files into the directory"""
  77. try:
  78. if os.access(programpath, os.F_OK) is not True:
  79. os.mkdir(programpath, 0o775)
  80. else:
  81. print("Program directory already exists, existing "
  82. "files will be overwritten!")
  83. except OSError:
  84. print("Directory could not be created, cancelling installation",
  85. file=sys.stderr)
  86. else:
  87. for i in files:
  88. sys.stdout.write("Copying file %s into program directory: " % i)
  89. try:
  90. shutil.copy(i, programpath)
  91. except IOError:
  92. sys.stdout.write("FAIL\n")
  93. install_success = False
  94. else:
  95. sys.stdout.write("OK\n")
  96. for i in directories:
  97. sys.stdout.write("Copying directory %s into program directory: " % i)
  98. if os.access(os.path.join(programpath, i), os.F_OK):
  99. shutil.rmtree(os.path.join(programpath, i))
  100. try:
  101. shutil.copytree(i, os.path.join(programpath, i))
  102. except IOError:
  103. sys.stdout.write("FAIL\n")
  104. install_success = False
  105. else:
  106. sys.stdout.write("OK\n")
  107. binpath = os.path.join(installpath, "bin")
  108. os.chdir(programpath)
  109. if os.access(binpath, os.F_OK & os.W_OK) and installation_success is True:
  110. try:
  111. os.symlink(os.path.join(programpath, "stov.py"),
  112. os.path.join(binpath, "stov"))
  113. except OSError:
  114. print("Symlink in bin-directory already exists")
  115. print("stov has been installed successfully to "
  116. + programpath + " and a symlink has been "
  117. "created in " + binpath + ", you should "
  118. "be able to run the program by typing \"stov\"")
  119. elif installation_success is True:
  120. print("stov has been installed successfully to %s but the installer "
  121. "couldn't find a suitable bin directory, you need to call stov.py"
  122. " with it's absolute path" % programpath)
  123. else:
  124. print("One or more installation steps failed. Please check this manually "
  125. "and try again")
  126. if __name__ == "__main__":
  127. main()
  128. else:
  129. print("This script needs to run on command line, please do not import it",
  130. file=sys.stderr)