pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Python PasteBin - Home - Help - Archive

Posted by instaget on March Sat 24th 11:14 PM - Never Expires
download | new post

  1. #! /usr/bin/python
  2.  
  3. import os
  4. import sys
  5. from getopt import getopt
  6. import zipfile
  7. from glob import glob
  8.  
  9. def getPk3Hash(thezip):
  10.    # This retrieves the md5 sum from the md5hash file in the zip file
  11.    f = zipfile.ZipFile(thezip, "r")
  12.    line = f.read(thezip[5:] + ".md5")
  13.    return line[:32]
  14.  
  15. def downloadHashes():
  16.    # Will need to get all of the file.zip.md5 files
  17.    os.system("wget -N http://instagib.jorgepena.be/downloads/packages/ -nd -r -l1 --no-parent -A md5")
  18.  
  19. def downloadAllPk3s():
  20.    os.system("wget -nd -r -l1 --no-parent -A pk3 http://instagib.jorgepena.be/downloads/packages/")
  21.    try:
  22.       mode = os.stat("base")[ST_MODE]
  23.       if S_ISDIR(mode):
  24.          os.system("mv -v *.pk3 base/")
  25.    except OSError:
  26.       os.system("mkdir base")
  27.       os.system("mv -v *.pk3 base/")
  28.  
  29. def downloadPk3(thefile):
  30.    # This downloads the updated file
  31.    url = "http://instagib.jorgepena.be/downloads/packages/" + thefile
  32.    dest = "base/" + thefile
  33.    print "Downloading " + thefile + " from " + url + " to " + dest
  34.    os.system("wget -P base/ " + url + " -N")
  35.  
  36. def Usage():
  37.    print "Instaget: The Instagib Project Updater"
  38.    print "Usage: instaget [SWITCH]"
  39.    print "Switches:"
  40.    print "  -h, --help\t\tdisplays this help message"
  41.    print "  -i, --initial\t\tdownloads all of the pk3s"
  42.    print "  -c, --check\t\tchecks a specific package"
  43.    print "  -a, --all\t\tchecks all of the packages"
  44.    print "  -d, --download\tdownloads the specific pk3"
  45.    print "Examples:"
  46.    print "  Downloading all of the pk3s for the first time:"
  47.    print "    python instaget.py -i"
  48.    print "  Check only one of the pk3s:"
  49.    print "    python instaget.py -c players.pk3"
  50.    print "  Check all of the pk3s:"
  51.    print "    python instaget.py -a"
  52.    print "  Download a specific pk3:"
  53.    print "    python instaget.py -d players.pk3"
  54.  
  55. def onlyCheck(pk3):
  56.    if os.path.exists(pk3):
  57.       print "Checking " + pk3 + "..."
  58.       f = open(pk3[5:] + ".md5", "r")
  59.       rHash = f.readline()[:32]
  60.       lHash = getPk3Hash(pk3)
  61.       print "---------------------------------------------"
  62.       print "File: " + pk3
  63.       print "Remote Hash: " + rHash
  64.       print "Local Hash:  " + lHash
  65.  
  66.       if (lHash != rHash):
  67.          print "MD5 Hash Mismatch..."
  68.          failed = failed + 1
  69.          downloadPk3(pk3[5:])
  70.       else:
  71.          print "Successful match"
  72.    else:
  73.       print "The file doesn't even exist. Try using the -d switch"
  74.  
  75. def doAll():
  76.    # We should first download the hashes
  77.    downloadHashes()
  78.    failed = 0 # This will check how many updates are needed
  79.  
  80.    pk3s = glob("base/*.pk3")
  81.    for pk3 in pk3s:
  82.       f = open(pk3[5:] + ".md5", "r")
  83.       rHash = f.readline()[:32]
  84.       lHash = getPk3Hash(pk3)
  85.       print "---------------------------------------------"
  86.       print "File: " + pk3
  87.       print "Remote Hash: " + rHash
  88.       print "Local Hash:  " + lHash
  89.  
  90.       if (lHash != rHash):
  91.          print "MD5 Hash Mismatch..."
  92.          failed = failed + 1
  93.          downloadPk3(pk3[5:])
  94.       else:
  95.          print "Successful match"
  96.  
  97.    print "---------------------------------------------"
  98.    if failed == 0:
  99.       print "No updates available"
  100.    else:
  101.       print "Downloaded %i updates" % failed
  102.    
  103.    # Remove the .md5 files
  104.    print "Removing the MD5 Hash files..."
  105.    os.system("rm -v *.md5")
  106.  
  107. if __name__ == "__main__":
  108.    try:
  109.       opts, args = getopt(sys.argv[1:], ':hicad', ["help", "check"])
  110.    except getopt.GetoptError:
  111.       Usage()
  112.       sys.exit(2)
  113.    
  114.    if not opts:
  115.       Usage()
  116.       sys.exit(2)
  117.    
  118.    package = "None"
  119.    
  120.    for o, a in opts:
  121.       if o in ("-h", "--help"):
  122.          Usage()
  123.       if o in ("-i", "--initial"):
  124.          downloadAllPk3s()
  125.       if o in ("-c", "--check"):
  126.          onlyCheck(a)
  127.       if o in ("-a", "--all"):
  128.          doAll()
  129.       if o in ("-d", "--download"):
  130.          downloadPk3(a)
  131.  
  132.    if not glob("base/*.pk3"):
  133.       print "No media archives (pk3s) present, now dowloading them all..."
  134.       downloadAllPk3s()
  135.    else:
  136.       doAll()

Submit a correction or amendment below. (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
Syntax Highlighting:
To highlight particular lines, prefix each line with @@
Pressing TAB inserts 3 spaces