install.py 4.8 KB

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