/scripts/devSetup/node.py

https://gitlab.com/pankajmore/codecombat · Python · 143 lines · 124 code · 15 blank · 4 comment · 18 complexity · 3142f30dbba57360043d069987060a1b MD5 · raw file

  1. __author__ = u'schmatz'
  2. from downloader import Downloader
  3. import tarfile
  4. import errors
  5. from errors import DownloadCorruptionError
  6. import warnings
  7. import os
  8. from configuration import Configuration
  9. from dependency import Dependency
  10. import shutil
  11. from which import which
  12. import subprocess
  13. class Node(Dependency):
  14. def __init__(self,configuration):
  15. super(self.__class__, self).__init__(configuration)
  16. operating_system = configuration.system.operating_system
  17. self.config.directory.create_directory_in_tmp(u"node")
  18. if operating_system == u"mac":
  19. self.downloader = MacNodeDownloader(self)
  20. elif operating_system == u"win":
  21. self.downloader = WindowsNodeDownloader(self)
  22. self.config.directory.create_directory_in_bin(u"node") #TODO: Fix windows node installation
  23. elif operating_system == u"linux":
  24. self.downloader = LinuxNodeDownloader(self)
  25. @property
  26. def tmp_directory(self):
  27. return self.config.directory.tmp_directory
  28. @property
  29. def bin_directory(self):
  30. return self.config.directory.bin_directory
  31. def download_dependencies(self):
  32. self.downloader.download()
  33. self.downloader.decompress()
  34. def bashrc_string(self):
  35. return "COCO_NODE_PATH=" + self.config.directory.bin_directory + os.sep + u"node" + os.sep + "bin" + os.sep +"node"
  36. def install_dependencies(self):
  37. install_directory = self.config.directory.bin_directory + os.sep + u"node"
  38. #check for node here
  39. unzipped_node_path = self.findUnzippedNodePath()
  40. if self.config.system.operating_system in ["mac","linux"] and not which("node"):
  41. print "Copying node into /usr/local/bin/..."
  42. shutil.copy(unzipped_node_path + os.sep + "bin" + os.sep + "node","/usr/local/bin/")
  43. os.chmod("/usr/local/bin/node",0777)
  44. shutil.copytree(self.findUnzippedNodePath(),install_directory)
  45. wants_to_upgrade = True
  46. if self.check_if_executable_installed(u"npm"):
  47. warning_string = u"A previous version of npm has been found. \nYou may experience problems if you have a version of npm that's too old.Would you like to upgrade?(y/n) "
  48. from distutils.util import strtobool
  49. print warning_string
  50. #for bash script, you have to somehow redirect stdin to raw_input()
  51. user_input = raw_input()
  52. while True:
  53. try:
  54. wants_to_upgrade = strtobool(user_input)
  55. except:
  56. print u"Please enter y or n. "
  57. continue
  58. break
  59. if wants_to_upgrade:
  60. import urllib2, urllib
  61. print u"Retrieving npm update script..."
  62. npm_install_script_path = install_directory + os.sep + u"install.sh"
  63. urllib.urlretrieve(u"https://npmjs.org/install.sh",filename=npm_install_script_path)
  64. print u"Retrieved npm install script. Executing..."
  65. subprocess.call([u"sh", npm_install_script_path])
  66. print u"Updated npm version installed"
  67. def findUnzippedNodePath(self):
  68. return self.downloader.download_directory + os.sep + \
  69. (os.walk(self.downloader.download_directory).next()[1])[0]
  70. def check_if_executable_installed(self,name):
  71. executable_path = which(name)
  72. if executable_path:
  73. return True
  74. else:
  75. return False
  76. def check_node_version(self):
  77. version = subprocess.check_output(u"node -v")
  78. return version
  79. def check_npm_version(self):
  80. version = subprocess.check_output(u"npm -v")
  81. return version
  82. class NodeDownloader(Downloader):
  83. @property
  84. def download_url(self):
  85. raise NotImplementedError
  86. @property
  87. def download_directory(self):
  88. return self.dependency.tmp_directory + os.sep + u"node"
  89. @property
  90. def downloaded_file_path(self):
  91. return self.download_directory + os.sep + u"node.tgz"
  92. def download(self):
  93. print u"Downloading Node from URL " + self.download_url
  94. self.download_file(self.download_url,self.downloaded_file_path)
  95. self.check_download()
  96. def decompress(self):
  97. print u"Decompressing Node..."
  98. tfile = tarfile.open(self.downloaded_file_path)
  99. #TODO: make directory handler class
  100. tfile.extractall(self.download_directory)
  101. print u"Decompressed Node into " + self.download_directory
  102. def check_download(self):
  103. isFileValid = tarfile.is_tarfile(self.downloaded_file_path)
  104. if not isFileValid:
  105. raise DownloadCorruptionError(u"Node download was corrupted.")
  106. class LinuxNodeDownloader(NodeDownloader):
  107. @property
  108. def download_url(self):
  109. if self.dependency.config.mem_width == 64:
  110. return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x64.tar.gz"
  111. else:
  112. return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-linux-x86.tar.gz"
  113. class WindowsNodeDownloader(NodeDownloader):
  114. @property
  115. def download_url(self):
  116. raise NotImplementedError(u"Needs MSI to be executed to install npm")
  117. #"http://nodejs.org/dist/v0.10.24/x64/node-v0.10.24-x64.msi"
  118. if self.dependency.config.mem_width == 64:
  119. return u"http://nodejs.org/dist/v0.10.24/x64/node.exe"
  120. else:
  121. return u"http://nodejs.org/dist/v0.10.24/node.exe"
  122. class MacNodeDownloader(NodeDownloader):
  123. @property
  124. def download_url(self):
  125. if self.dependency.config.mem_width == 64:
  126. return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-darwin-x64.tar.gz"
  127. else:
  128. return u"http://nodejs.org/dist/v0.10.24/node-v0.10.24-darwin-x86.tar.gz"