home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / arm / prereq.py < prev    next >
Encoding:
Python Source  |  2012-05-18  |  6.9 KB  |  240 lines

  1. """
  2. Provides a warning and error code if python version isn't compatible.
  3. """
  4.  
  5. import os
  6. import sys
  7. import shutil
  8. import urllib
  9. import hashlib
  10. import tarfile
  11. import tempfile
  12.  
  13. # Library dependencies can be fetched on request. By default this is via
  14. # the following mirrors with their sha256 signatures checked.
  15. TORCTL_ARCHIVE = "http://www.atagar.com/arm/resources/deps/11-06-16/torctl.tar.gz"
  16. TORCTL_SIG = "5460adb1394c368ba492cc33d6681618b3d3062b3f5f70b2a87520fc291701c3"
  17. CAGRAPH_ARCHIVE = "http://www.atagar.com/arm/resources/deps/11-06-10/cagraph.tar.gz"
  18. CAGRAPH_SIG = "1439acd40ce016f4329deb216d86f36a749e4b8bf73a313a757396af6f95310d"
  19.  
  20. # optionally we can do an unverified fetch from the library's sources
  21. TORCTL_REPO = "git://git.torproject.org/pytorctl.git"
  22. CAGRAPH_TARBALL_URL = "http://cagraph.googlecode.com/files/cagraph-1.2.tar.gz"
  23. CAGRAPH_TARBALL_NAME = "cagraph-1.2.tar.gz"
  24. CAGRAPH_TARBALL_ROOT = "cagraph-1.2"
  25.  
  26. def isTorCtlAvailable():
  27.   """
  28.   True if TorCtl is already available on the platform, false otherwise.
  29.   """
  30.   
  31.   try:
  32.     import TorCtl
  33.     return True
  34.   except ImportError:
  35.     return False
  36.  
  37. def isCagraphAvailable():
  38.   """
  39.   True if cagraph is already available on the platform, false otherwise.
  40.   """
  41.   try:
  42.     import cagraph
  43.     return True
  44.   except ImportError:
  45.     return False
  46.  
  47. def promptTorCtlInstall():
  48.   """
  49.   Asks the user to install TorCtl. This returns True if it was installed and
  50.   False otherwise (if it was either declined or failed to be fetched).
  51.   """
  52.   
  53.   userInput = raw_input("Arm requires TorCtl to run, but it's unavailable. Would you like to install it? (y/n): ")
  54.   
  55.   # if user says no then terminate
  56.   if not userInput.lower() in ("y", "yes"): return False
  57.   
  58.   # attempt to install TorCtl, printing the issue if unsuccessful
  59.   try:
  60.     fetchLibrary(TORCTL_ARCHIVE, TORCTL_SIG)
  61.     
  62.     if not isTorCtlAvailable():
  63.       raise IOError("Unable to install TorCtl, sorry")
  64.     
  65.     print "TorCtl successfully installed"
  66.     return True
  67.   except IOError, exc:
  68.     print exc
  69.     return False
  70.  
  71. def promptCagraphInstall():
  72.   """
  73.   Asks the user to install cagraph. This returns True if it was installed and
  74.   False otherwise (if it was either declined or failed to be fetched).
  75.   """
  76.   
  77.   userInput = raw_input("Arm requires cagraph to run, but it's unavailable. Would you like to install it? (y/n): ")
  78.   
  79.   # if user says no then terminate
  80.   if not userInput.lower() in ("y", "yes"): return False
  81.   
  82.   # attempt to install cagraph, printing the issue if unsuccessful
  83.   try:
  84.     fetchLibrary(CAGRAPH_ARCHIVE, CAGRAPH_SIG)
  85.     
  86.     if not isCagraphAvailable():
  87.       raise IOError("Unable to install cagraph, sorry")
  88.     
  89.     print "cagraph successfully installed"
  90.     return True
  91.   except IOError, exc:
  92.     print exc
  93.     return False
  94.  
  95. def fetchLibrary(url, sig):
  96.   """
  97.   Downloads the given archive, verifies its signature, then installs the
  98.   library. This raises an IOError if any of these steps fail.
  99.   
  100.   Arguments:
  101.     url - url from which to fetch the gzipped tarball
  102.     sig - sha256 signature for the archive
  103.   """
  104.   
  105.   tmpDir = tempfile.mkdtemp()
  106.   destination = tmpDir + "/" + url.split("/")[-1]
  107.   urllib.urlretrieve(url, destination)
  108.   
  109.   # checks the signature, reading the archive in 256-byte chunks
  110.   m = hashlib.sha256()
  111.   fd = open(destination, "rb")
  112.   
  113.   while True:
  114.     data = fd.read(256)
  115.     if not data: break
  116.     m.update(data)
  117.   
  118.   fd.close()
  119.   actualSig = m.hexdigest()
  120.   
  121.   if sig != actualSig:
  122.     raise IOError("Signature of the library is incorrect (got '%s' rather than '%s')" % (actualSig, sig))
  123.   
  124.   # extracts the tarball
  125.   tarFd = tarfile.open(destination, 'r:gz')
  126.   tarFd.extractall("src/")
  127.   tarFd.close()
  128.   
  129.   # clean up the temporary contents (fails quietly if unsuccessful)
  130.   shutil.rmtree(destination, ignore_errors=True)
  131.  
  132. def installTorCtl():
  133.   """
  134.   Checks out the current git head release for TorCtl and bundles it with arm.
  135.   This raises an IOError if unsuccessful.
  136.   """
  137.   
  138.   if isTorCtlAvailable(): return
  139.   
  140.   # temporary destination for TorCtl's git clone, guarenteed to be unoccupied
  141.   # (to avoid conflicting with files that are already there)
  142.   tmpFilename = tempfile.mktemp("/torctl")
  143.   
  144.   # fetches TorCtl
  145.   exitStatus = os.system("git clone --quiet %s %s > /dev/null" % (TORCTL_REPO, tmpFilename))
  146.   if exitStatus: raise IOError("Unable to get TorCtl from %s. Is git installed?" % TORCTL_REPO)
  147.   
  148.   # the destination for TorCtl will be our directory
  149.   ourDir = os.path.dirname(os.path.realpath(__file__))
  150.   
  151.   # exports TorCtl to our location
  152.   exitStatus = os.system("(cd %s && git archive --format=tar --prefix=TorCtl/ master) | (cd %s && tar xf - 2> /dev/null)" % (tmpFilename, ourDir))
  153.   if exitStatus: raise IOError("Unable to install TorCtl to %s" % ourDir)
  154.   
  155.   # Clean up the temporary contents. This isn't vital so quietly fails in case
  156.   # of errors.
  157.   shutil.rmtree(tmpFilename, ignore_errors=True)
  158.  
  159. def installCagraph():
  160.   """
  161.   Downloads and extracts the cagraph tarball. This raises an IOError if
  162.   unsuccessful.
  163.   """
  164.   
  165.   if isCagraphAvailable(): return
  166.   
  167.   tmpDir = tempfile.mkdtemp()
  168.   tmpFilename = os.path.join(tmpDir, CAGRAPH_TARBALL_NAME)
  169.   
  170.   exitStatus = os.system("wget --quiet -P %s %s" % (tmpDir, CAGRAPH_TARBALL_URL))
  171.   if exitStatus: raise IOError("Unable to fetch cagraph from %s. Is wget installed?" % CAGRAPH_TARBALL_URL)
  172.   
  173.   # the destination for cagraph will be our directory
  174.   ourDir = os.path.dirname(os.path.realpath(__file__))
  175.   
  176.   # exports cagraph to our location
  177.   exitStatus = os.system("(cd %s && tar --strip-components=1 -xzf %s %s/cagraph)" % (ourDir, tmpFilename, CAGRAPH_TARBALL_ROOT))
  178.   if exitStatus: raise IOError("Unable to extract cagraph to %s" % ourDir)
  179.   
  180.   # Clean up the temporary contents. This isn't vital so quietly fails in case
  181.   # of errors.
  182.   shutil.rmtree(tmpDir, ignore_errors=True)
  183.  
  184. def allPrereq():
  185.   """
  186.   Requrements for both the cli and gui versions of arm.
  187.   """
  188.   
  189.   majorVersion = sys.version_info[0]
  190.   minorVersion = sys.version_info[1]
  191.   
  192.   if majorVersion > 2:
  193.     print("arm isn't compatible beyond the python 2.x series\n")
  194.     sys.exit(1)
  195.   elif majorVersion < 2 or minorVersion < 5:
  196.     print("arm requires python version 2.5 or greater\n")
  197.     sys.exit(1)
  198.   
  199.   if not isTorCtlAvailable():
  200.     isInstalled = promptTorCtlInstall()
  201.     if not isInstalled: sys.exit(1)
  202.  
  203. def cliPrereq():
  204.   """
  205.   Requirements for the cli arm interface.
  206.   """
  207.   
  208.   allPrereq()
  209.   
  210.   try:
  211.     import curses
  212.   except ImportError:
  213.     print("arm requires curses - try installing the python-curses package\n")
  214.     sys.exit(1)
  215.  
  216. def guiPrereq():
  217.   """
  218.   Requirements for the gui arm interface.
  219.   """
  220.   
  221.   allPrereq()
  222.   
  223.   try:
  224.     import gtk
  225.   except ImportError:
  226.     print("arm requires gtk - try installing the python-gtk2 package\n")
  227.     sys.exit(1)
  228.   
  229.   if not isCagraphAvailable():
  230.     isInstalled = promptCagraphInstall()
  231.     if not isInstalled: sys.exit(1)
  232.  
  233. if __name__ == '__main__':
  234.   isGui = "-g" in sys.argv or "--gui" in sys.argv
  235.   isBoth = "--both" in sys.argv
  236.   
  237.   if isGui or isBoth: guiPrereq()
  238.   if not isGui or isBoth: cliPrereq()
  239.  
  240.