Skip to content

Instantly share code, notes, and snippets.

@imlucas
Created May 24, 2009 00:25
Show Gist options
  • Select an option

  • Save imlucas/116889 to your computer and use it in GitHub Desktop.

Select an option

Save imlucas/116889 to your computer and use it in GitHub Desktop.
import os, sys, getopt, zipfile
from elementtree import ElementTree as ET
"""
Build a zip for your app to test it.
Make sure to install ET so we can read attributes of our app from the descriptor
file. This makes sure that you only have to edit things in one place and the
behaviour of packagaing your app will always be what you want it to ( that is of
course you dont't update your descriptor file :))
sudo apt=-get install python-elementtree
"""
class BoxeePackager:
def __init__(self):
self.app_id = ''
self.app_name = ''
self.app_version = ''
self.zip_filename = ''
self.app_descriptor_path = ''
self.app_path = ''
def run(self):
if len(sys.argv) < 2:
print "Usage: python package_app.py {app-id}\n\n";
sys.exit(2)
my_dir = os.path.dirname(os.path.realpath(sys.argv[0]))
self.app_name = sys.argv[1]
self.app_path = my_dir+"/"+self.app_name+"/"
self.app_descriptor_path = self.app_path + "descriptor.xml"
# Check and make sure some basics are there
if os.path.isdir(self.app_path) != True:
print "Error: " + self.app_path + " is not a directory. Spelled that right?"
sys.exit(3)
if os.path.exists(self.app_descriptor_path) != True:
print "Error: No descriptor file found :( should be at " + self.app_descriptor_path + ". Whats up? You need that."
sys.exit(4)
# Parse the descriptor
try:
tree = ET.parse(self.app_descriptor_path)
except Exception, inst:
print "Unexpected error opening %s: %s" % (self.app_descriptor_path, inst)
sys.exit(5)
self.app_id = tree.findtext('id')
self.app_version = tree.findtext('version')
self.app_name = tree.findtext('name')
self.zip_filename = self.app_id + "-" + self.app_version + ".zip"
if os.path.exists(self.zip_filename):
os.remove(self.zip_filename)
z = zipfile.ZipFile(self.zip_filename, "w")
files = self.dirEntries(self.app_path, True)
for file in files:
z.write(file, file.replace(my_dir+"/"+self.app_id+"/", ''))
z.close()
print os.path.realpath(self.zip_filename)
def dirEntries(self, dir_name, subdir, *args):
fileList = []
for file in os.listdir(dir_name):
dirfile = os.path.join(dir_name, file)
if os.path.isfile(dirfile):
if not args:
fileList.append(dirfile)
else:
if os.path.splitext(dirfile)[1][1:] in args:
fileList.append(dirfile)
# recursively access file names in subdirectories
elif os.path.isdir(dirfile) and subdir:
fileList.extend(self.dirEntries(dirfile, subdir, *args))
return fileList
def main():
b = BoxeePackager()
b.run()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment