PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/deps/misc/get_pys60.py

http://pygame-symbian-s60.googlecode.com/
Python | 317 lines | 308 code | 8 blank | 1 comment | 4 complexity | d45ce2114f541df78a5aa659f12199e9 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, LGPL-2.1, LGPL-2.0, GPL-2.0
  1. """PyS60 SDK installer script for downloading the latest PyS60 release from Garage"""
  2. import os
  3. import sys
  4. import urllib
  5. import glob
  6. import zipfile
  7. import tarfile
  8. GARAGE = "https://garage.maemo.org"
  9. PYS60_DOWNLOAD_PAGE = GARAGE + "/frs/?group_id=854"
  10. TARGET = "pys60_installer"
  11. INSTALLED = "installed.txt"
  12. def check():
  13. "Check that EPOCROOT is defined correctly"
  14. er = os.environ["EPOCROOT"]
  15. if not os.path.exists( er ):
  16. raise SystemExit("Missing EPOCROOT")
  17. if not er.endswith("\\"):
  18. er += "\\"
  19. if not os.path.exists( er + "epoc32" ):
  20. raise SystemExit("Invalid EPOCROOT")
  21. os.environ["EPOCROOT"] = er.replace("\\", "/")
  22. def update_installed(filename):
  23. if not is_installed(filename):
  24. f = open(TARGET + INSTALLED, 'a')
  25. f.write(filename + "\n")
  26. f.close()
  27. def is_installed(filename):
  28. try:
  29. f = open(TARGET + INSTALLED)
  30. except IOError:
  31. return False
  32. lines = f.readlines()
  33. f.close()
  34. for line in lines:
  35. if filename in line:
  36. return True
  37. return False
  38. def install(options):
  39. "Extract the downloaded archives to Symbian SDK"
  40. # Scan for latest PyS60 version in the archive folder
  41. latest = (0,0,0)
  42. for name in glob.glob(TARGET + "Python*SDK*.zip"):
  43. name = os.path.basename(name)
  44. parts = name.split("_")
  45. ver = tuple( map(int, parts[1].split(".") ))
  46. if ver > latest:
  47. latest = ver
  48. if latest != (0,0,0):
  49. latest = ".".join( [str(x) for x in latest ] )
  50. print "Latest PyS60 version in local repository detected:", latest
  51. else:
  52. latest = "" # Install any
  53. # Extract files from archives.
  54. for name in glob.glob(TARGET + "*.*"):
  55. archive = name
  56. name = os.path.basename( name )
  57. if name.endswith(".zip"):
  58. zfile = zipfile.ZipFile(archive, "r")
  59. infolist = zfile.infolist()
  60. #import pdb;pdb.set_trace()
  61. if ( name.startswith("Python") and "SDK" in name) and ( latest not in name or options.sdk not in name):
  62. print archive, "skipped. Invalid SDK version."
  63. continue
  64. elif name.endswith(".tar.gz") and latest in name:
  65. tfile = tarfile.open(archive, 'r:gz')
  66. infolist = tfile.getmembers()
  67. else:
  68. print name, "skipped."
  69. continue
  70. if is_installed(archive):
  71. if not options.force_install:
  72. print name, "skipped. Already installed."
  73. continue
  74. else:
  75. print name, "already installed but install forced."
  76. print "Extracting", archive
  77. erase = ""
  78. counter = 0.0
  79. prev = 0.0
  80. total = float(len( infolist ))
  81. for info in infolist:
  82. percent = int( counter / total * 100 )
  83. if percent != prev:
  84. # Write backspaces to erase previous progress
  85. sys.stdout.write(erase)
  86. percent = "%d\\%d(%d%%)" % ( counter, total, percent )
  87. prev = percent
  88. sys.stdout.write(percent)
  89. erase = len( percent ) * '\b'
  90. if type(info) == tarfile.TarInfo:
  91. #import pdb;pdb.set_trace()
  92. name = info.name
  93. file = tfile.extractfile(info)
  94. else:
  95. file = zfile
  96. name = info.filename
  97. path = os.environ["EPOCROOT"] + name
  98. if "PythonForS60" in archive and options.tools_path:
  99. path = os.path.join( options.tools_path, name )
  100. if name.endswith("/") or file is None:
  101. if not os.path.exists(path):
  102. os.mkdir(path)
  103. #print "Dir created:", path
  104. continue
  105. #print path
  106. fout = open(path, "wb")
  107. if type(info) == tarfile.TarInfo:
  108. data = file.read( )
  109. while len(data) > 0:
  110. fout.write(data)
  111. data = file.read()
  112. else:
  113. data = zfile.read(name)
  114. fout.write(data)
  115. fout.close()
  116. counter += 1
  117. update_installed(archive)
  118. sys.stdout.write(erase)
  119. #print "Done"
  120. def download(options):
  121. """Download PyS60 archives from Garage
  122. - Latest OpenC SDK
  123. - PyS60 headers and libraries
  124. - PyS60 packaging tool
  125. """
  126. f = urllib.urlopen( PYS60_DOWNLOAD_PAGE )
  127. data = f.read()
  128. f.close()
  129. # Find latest release
  130. key = "shownotes.php"
  131. s = data.index(key)
  132. e = data.index("shownotes.php", s + len(key) )
  133. data = data[s:e]
  134. files = {}
  135. lines = data.split()
  136. for line in lines:
  137. if "href" not in line: continue
  138. #print line
  139. try:
  140. url, name = line.split(">")[:2]
  141. url = url.split('"')[1]
  142. name = name.split("<")[0]
  143. files[name] = GARAGE + url
  144. except ValueError:
  145. # Done
  146. pass
  147. # Download FP2 SDK
  148. for name in files:
  149. downloadable = False
  150. if "OpenC" in name and "plugin" in name and options.openc:
  151. downloadable = True
  152. elif "PythonForS60" in name and name.endswith(".tar.gz"):
  153. downloadable = True
  154. elif options.sdk in name and "Python" in name and name.endswith(".zip"):
  155. downloadable = True
  156. if downloadable:
  157. filename = TARGET + name
  158. if os.path.exists(filename):
  159. if not options.force_download:
  160. print filename, "exists. Skipped."
  161. continue
  162. else:
  163. print filename, "exists, but download forced."
  164. print "Downloading", name, files[name]
  165. # Could use urlretrieve, but me wants progressinfo :b
  166. #urllib.urlretrieve( files[name], name )
  167. fout = open( filename , 'wb')
  168. f = urllib.urlopen( files[name] )
  169. fullsize = int( f.headers["Content-length"] )
  170. data = f.read(2048)
  171. size = len(data)
  172. while len(data):
  173. size += len( data )
  174. fout.write( data )
  175. progress = "%d\\%d(%2d%%)" % ( size, fullsize, size / float(fullsize) * 100 )
  176. sys.stdout.write( progress )
  177. data = f.read(2048)
  178. sys.stdout.write( len(progress) * "\b" )
  179. f.close()
  180. fout.close()
  181. def main():
  182. global TARGET
  183. check()
  184. TARGET = os.environ["EPOCROOT"] + TARGET + "/"
  185. from optparse import OptionParser
  186. parser = OptionParser()
  187. parser.add_option("-s", "--sdk",
  188. dest="sdk", default="FP2",
  189. help="Download correct PyS60 SDK for S60 SDK:[%default]")
  190. parser.add_option("-t", "--target-dir",
  191. dest="target_dir", default=TARGET,
  192. help="Folder where the files are downloaded:[%default]")
  193. parser.add_option("", "--tools-path", default=None,
  194. help="Folder where the PythonForS60 tools are installed:[%default]")
  195. parser.add_option("-f", "--force-download",
  196. action="store_true", dest="force_download", default=False,
  197. help="Download new files even if they exist.")
  198. parser.add_option("-F", "--force-install",
  199. action="store_true", dest="force_install", default=False,
  200. help="Install new files even if they are already installed.")
  201. parser.add_option("", "--no-download",
  202. action="store_false", dest="download", default=True,
  203. help="Don't download new files.")
  204. parser.add_option("", "--no-install",
  205. action="store_false", dest="install", default=True,
  206. help="Don't install the files.")
  207. parser.add_option("", "--no-openc",
  208. action="store_false", dest="openc", default=True,
  209. help="Don't get new OpenC.")
  210. parser.add_option("-c", "--clean",
  211. action="store_true", dest="clean", default=False,
  212. help="Clean the archive folder and exit." )
  213. (options, args) = parser.parse_args()
  214. TARGET = options.target_dir
  215. if not TARGET.endswith("\\"):
  216. TARGET += "\\"
  217. if options.clean:
  218. files = glob.glob(TARGET + "*.*")
  219. for file in files:
  220. os.remove(file)
  221. os.remove(TARGET)
  222. return
  223. if not os.path.exists( TARGET ):
  224. os.mkdir(TARGET)
  225. if options.download:
  226. download(options)
  227. if options.install:
  228. install(options)
  229. main()