Instapack

From TIP Wiki

Jump to: navigation, search

Instapack is a Python script that facilitates the task of packaging The Instagib Project's Game Media. It uses absolutely no external programs and is written in pure Python, as opposed to the previous bash/batch scripts that had to be written differently for different platforms and depended on external programs. Instapack allows for hashing of specific folders, zipping of specific folders, packaging (Which is basically hashing and zipping) of specific folders, and doing all of these as well. Instapack should run on any system that has Python installed.

It is not necessary to download the script, since the reason you'd be using it is to package the subversion media, and it already comes with it.

Windows

If you are on Windows, you'll first have to get the media from Subversion (Read Building From Subversion). You'll have to download the Python Windows Installer, then obviously install it. Once that's done, you should see the file instaget.py, simply double click on it and you'll be shown a menu of options:

  • 1. This option will hash and zip every package so that you could put them into your base folder
  • 2. This option will generate an MD5 hash for a specific package
  • 3. This option will zip up a specific package
  • 4. This option will hash and zip a specific package

Linux, Mac OS X, and Everything Else

You will simply have to open a terminal and go to the directory where instaget.py is, then type:

python instapack.py

And it'll give you output similar to the following:

Instapack: The Instagib Project Packager
Usage: instapack [SWITCH]
Switches:
  -h, --help            displays this help message
  -a, --all             packages every package
  --hash=PACKAGE        hashes the package (Don't include extension)
  --zip=PACKAGE         zips the package (Don't include extension)
  --package=PACKAGE     hashes and zips the package (Don't include extension)
Examples:
  Packaging everything:
    python instapack.py -a
  Hash a certain package:
    python instapack.py --hash=bots
  Zip a certain package:
    python instapack.py --zip=bots
  Package a certain package:
    python instapack.py --package=bots

That should be pretty self explanatory, if you have any problems ask in the forums.

Latest Revision

Here is the source for the latest revision. This is for you to look at, if you need the script, use the download link at the top:

#! /usr/bin/python
# Instaget - The Instagib Project Media Package
import os
import sys
from md5 import md5
import tempfile
import getopt
import shutil
import urllib2
 
# This hashes the media files
def md5sum(thefile):
    f = open(thefile, 'rb')
    hsh = md5()
    while 1:
       data = f.read(2048)
       if not data:
          break
       hsh.update(data)
    f.close()
    return hsh.hexdigest()
 
# This hashes the blah.pk3.md5 files
def md5file(filename):
    fh = open(filename, 'r')
    digest = md5()
    for line in fh:
        digest.update(line)
    fh.close()
    return digest.hexdigest()
 
# Download wget if it's not here already
def download(url):
    import urllib
    webFile = urllib.urlopen(url)
    localFile = open(url.split('/')[-1], 'w')
    localFile.write(webFile.read())
    webFile.close()
    localFile.close()
 
# This recursively returns a list of every single file
# in the path
def listDirs(path):
    theFiles = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.startswith(".") or file.endswith(".db"):
               continue
            theFiles.append(os.path.join(root, file))
        if '.svn' in dirs:
            dirs.remove('.svn')
    theFiles.sort()
 
    return theFiles
 
# This hashes the entire path and also creates the
# blah.pk3.md5 file
def hashDir(path):
    md5hash = path + "/" + path + ".pk3.md5"
 
    if os.path.exists(md5hash):
        os.remove(md5hash)
 
    files = listDirs(path)
    tmp = tempfile.mktemp()
    f = open(tmp, "w")
 
    for file in files:
        print >> f, md5sum(file) + " " + file.replace('\\', '/')
 
    f.close()
    d = open(path + "/" + path + ".pk3.md5", "w")
    print >> d, md5file(tmp) + " " + tmp
    d.close()
    shutil.copy(path + "/" + path + ".pk3.md5", ".")
 
# This zips up the entire path
def zipDir(path):
    if ((sys.platform != 'win32') or (sys.platform == 'win32' and os.path.exists("zip.exe"))):
        curDir = os.getcwd()
        os.chdir(path)
        if sys.platform == 'win32': zip = "..\\zip"
        else: zip = "zip"
        files = listDirs(".")
        for file in files:
            os.system(zip + " -r " + path + ".pk3 " + file + " -X")
 
        if sys.platform == 'win32':
           shutil.move(path + ".pk3", "..")
        else:
           os.system("mv -f " + path + ".pk3 ..")
        os.chdir("..")
        return
    else:
        os.system("wget http://instagib.jorgepena.be/downloads/tools/zip.exe -N")
        zipDir(path)
 
# Help
def Usage():
    print "Instapack: The Instagib Project Packager"
    print "Usage: instapack [SWITCH]"
    print "Switches:"
    print "  -h, --help\t\tdisplays this help message"
    print "  -a, --all\t\tpackages every package"
    print "  -s, --hash=PACKAGE\thashes the package (Don't include extension)"
    print "  -z, --zip=PACKAGE\tzips the package (Don't include extension)"
    print "  -p, --package=PACKAGE\thashes and zips the package (Don't include extension)"
    print "Examples:"
    print "  Packaging everything:"
    print "    python instapack.py -a"
    print "  Hash a certain package:"
    print "    python instapack.py --hash=bots"
    print "  Zip a certain package:"
    print "    python instapack.py --zip=bots"
    print "  Package a certain package:"
    print "    python instapack.py --package=bots"
 
# Package a path
def package(pack):
    hashDir(pack)
    zipDir(pack)
 
# Package everything
def all():
    hashDir("maps")
    hashDir("scripts")
    hashDir("sound")
    hashDir("textures")
    hashDir("ui")
    hashDir("bots")
    hashDir("media")
    hashDir("players")
 
    zipDir("maps")
    zipDir("scripts")
    zipDir("sound")
    zipDir("textures")
    zipDir("ui")
    zipDir("bots")
    zipDir("media")
    zipDir("players")
 
# Program beef
if __name__ == "__main__":
    try:
        opts, args = getopt.getopt(sys.argv[1:], 'haszp:', ["help", "all", "hash=", "zip=", "package="])
    except getopt.GetoptError:
        Usage()
        sys.exit(2)
 
    if not opts:
        if sys.platform == 'win32':
            if (not os.path.exists("wget.exe")):
                download("http://instagib.jorgepena.be/downloads/tools/wget.exe")
                print "Re-Run the program. You didn't have wget and it has just been downloaded."
                raw_input("Press enter to exit...")
                sys.exit(2)
 
            print "Type the number of the option you'd like to do:"
            print "1. All\t\tpackages everything"
            print "2. Hash\t\thash a specific package"
            print "3. Zip\t\tzip a specific package"
            print "4. Package\tpackage a specific package"
            option = int(raw_input("So what will it be? "))
 
            if option == 1:
                all()
            elif option == 2:
                pak = raw_input("What package? (Don't include .pk3): ")
                hashDir(pak)
            elif option == 3:
                pak = raw_input("What package? (Don't include .pk3): ")
                zipDir(pak)
            elif option == 4:
                pak = raw_input("What package? (Don't include .pk3): ")
                package(pak)
            else:
                print "What the heck? You entered a non-existant option!"
 
            raw_input("Press enter to exit...")
            sys.exit(2)
 
        Usage()
        sys.exit(2)
 
    pak = "None"
 
    for o, a in opts:
        if o in ("-h", "--help"):
            Usage()
            sys.exit(2)
        if o in ("-a", "--all"):
            all()
        if o in ("-s", "--hash"):
            hashDir(a)
        if o in ("-z", "--zip"):
            zipDir(a)
        if o in ("-p", "--package"):
            package(a)
 
    print "Done doing whatever it is you chose to do :D"
Personal tools