PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/headphones/versioncheck.py

https://github.com/ersw1187/headphones
Python | 204 lines | 134 code | 63 blank | 7 comment | 41 complexity | ab86a11d2f54ce18ac45f4960cb0aae4 MD5 | raw file
  1. import platform, subprocess, re, os
  2. import urllib2
  3. import tarfile
  4. import headphones
  5. from headphones import logger, version
  6. from lib.pygithub import github
  7. def runGit(args):
  8. if headphones.GIT_PATH:
  9. git_locations = ['"'+headphones.GIT_PATH+'"']
  10. else:
  11. git_locations = ['git']
  12. if platform.system().lower() == 'darwin':
  13. git_locations.append('/usr/local/git/bin/git')
  14. output = err = None
  15. for cur_git in git_locations:
  16. cmd = cur_git+' '+args
  17. try:
  18. logger.debug('Trying to execute: "' + cmd + '" with shell in ' + headphones.PROG_DIR)
  19. p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=headphones.PROG_DIR)
  20. output, err = p.communicate()
  21. logger.debug('Git output: ' + output)
  22. except OSError:
  23. logger.debug('Command ' + cmd + ' didn\'t work, couldn\'t find git')
  24. continue
  25. if 'not found' in output or "not recognized as an internal or external command" in output:
  26. logger.debug('Unable to find git with command ' + cmd)
  27. output = None
  28. elif 'fatal:' in output or err:
  29. logger.error('Git returned bad info. Are you sure this is a git installation?')
  30. output = None
  31. elif output:
  32. break
  33. return (output, err)
  34. def getVersion():
  35. if version.HEADPHONES_VERSION.startswith('build '):
  36. headphones.INSTALL_TYPE = 'win'
  37. # Don't have a way to update exe yet, but don't want to set VERSION to None
  38. return 'Windows Install'
  39. elif os.path.isdir(os.path.join(headphones.PROG_DIR, '.git')):
  40. headphones.INSTALL_TYPE = 'git'
  41. output, err = runGit('rev-parse HEAD')
  42. if not output:
  43. logger.error('Couldn\'t find latest installed version.')
  44. return None
  45. cur_commit_hash = output.strip()
  46. if not re.match('^[a-z0-9]+$', cur_commit_hash):
  47. logger.error('Output doesn\'t look like a hash, not using it')
  48. return None
  49. return cur_commit_hash
  50. else:
  51. headphones.INSTALL_TYPE = 'source'
  52. version_file = os.path.join(headphones.PROG_DIR, 'version.txt')
  53. if not os.path.isfile(version_file):
  54. return None
  55. fp = open(version_file, 'r')
  56. current_version = fp.read().strip(' \n\r')
  57. fp.close()
  58. if current_version:
  59. return current_version
  60. else:
  61. return None
  62. def checkGithub():
  63. commits_behind = 0
  64. cur_commit = headphones.CURRENT_VERSION
  65. latest_commit = None
  66. gh = github.GitHub()
  67. for curCommit in gh.commits.forBranch('rembo10', 'headphones', version.HEADPHONES_VERSION):
  68. if not latest_commit:
  69. latest_commit = curCommit.id
  70. if not cur_commit:
  71. break
  72. if curCommit.id == cur_commit:
  73. break
  74. commits_behind += 1
  75. headphones.LATEST_VERSION = latest_commit
  76. headphones.COMMITS_BEHIND = commits_behind
  77. if headphones.LATEST_VERSION == headphones.CURRENT_VERSION:
  78. logger.info('Headphones is already up-to-date.')
  79. return latest_commit
  80. def update():
  81. if headphones.INSTALL_TYPE == 'win':
  82. logger.info('Windows .exe updating not supported yet.')
  83. pass
  84. elif headphones.INSTALL_TYPE == 'git':
  85. output, err = runGit('pull origin ' + version.HEADPHONES_VERSION)
  86. if not output:
  87. logger.error('Couldn\'t download latest version')
  88. for line in output.split('\n'):
  89. if 'Already up-to-date.' in line:
  90. logger.info('No update available, not updating')
  91. logger.info('Output: ' + str(output))
  92. elif line.endswith('Aborting.'):
  93. logger.error('Unable to update from git: '+line)
  94. logger.info('Output: ' + str(output))
  95. else:
  96. tar_download_url = 'http://github.com/rembo10/headphones/tarball/'+version.HEADPHONES_VERSION
  97. update_dir = os.path.join(headphones.PROG_DIR, 'update')
  98. version_path = os.path.join(headphones.PROG_DIR, 'version.txt')
  99. try:
  100. logger.info('Downloading update from: '+tar_download_url)
  101. data = urllib2.urlopen(tar_download_url)
  102. except (IOError, URLError):
  103. logger.error("Unable to retrieve new version from "+tar_download_url+", can't update")
  104. return
  105. download_name = data.geturl().split('/')[-1]
  106. tar_download_path = os.path.join(headphones.PROG_DIR, download_name)
  107. # Save tar to disk
  108. f = open(tar_download_path, 'wb')
  109. f.write(data.read())
  110. f.close()
  111. # Extract the tar to update folder
  112. logger.info('Extracing file' + tar_download_path)
  113. tar = tarfile.open(tar_download_path)
  114. tar.extractall(update_dir)
  115. tar.close()
  116. # Delete the tar.gz
  117. logger.info('Deleting file' + tar_download_path)
  118. os.remove(tar_download_path)
  119. # Find update dir name
  120. update_dir_contents = [x for x in os.listdir(update_dir) if os.path.isdir(os.path.join(update_dir, x))]
  121. if len(update_dir_contents) != 1:
  122. logger.error(u"Invalid update data, update failed: "+str(update_dir_contents))
  123. return
  124. content_dir = os.path.join(update_dir, update_dir_contents[0])
  125. # walk temp folder and move files to main folder
  126. for dirname, dirnames, filenames in os.walk(content_dir):
  127. dirname = dirname[len(content_dir)+1:]
  128. for curfile in filenames:
  129. old_path = os.path.join(content_dir, dirname, curfile)
  130. new_path = os.path.join(headphones.PROG_DIR, dirname, curfile)
  131. if os.path.isfile(new_path):
  132. os.remove(new_path)
  133. os.renames(old_path, new_path)
  134. # Update version.txt
  135. try:
  136. ver_file = open(version_path, 'w')
  137. ver_file.write(headphones.LATEST_VERSION)
  138. ver_file.close()
  139. except IOError, e:
  140. logger.error(u"Unable to write for curCommit in gh.commits.forBranch file, update not complete: "+ex(e))
  141. return