#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005, TUBITAK/UEKAE
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Please read the COPYING file.

import os
import sys
import zipfile

def openZip(fileName):
    if not zipfile.is_zipfile(fileName):
        print "ZIP file required..."
        sys.exit()
    else:
        f = zipfile.ZipFile(fileName, "r")
        return f

def fillFileList(zipFile):
    filelist = []

    for file in zipFile.namelist():
        filelist.append(file)
    
    filelist.sort()
    return filelist

def uniq(alist):
    set = {}
    return [set.setdefault(e,e) for e in alist if e not in set]

if __name__ == "__main__":

    if sys.argv[1] == "dirs":
        f = openZip(sys.argv[2])
        dirlist =[]
        for file in fillFileList(f):
            dirlist.append(os.path.dirname(file))
    
        for dir in uniq(dirlist):
            print "<Path fileType=\"\">" + dir.replace("install/", "/") + "</Path>"
    elif sys.argv[1]:
        f = openZip(sys.argv[1])
        for file in fillFileList(f):
            if f.getinfo(file).external_attr == 2716663808:
                print file + " --> " + f.read(file)
            else:
                print file
    else:
        print "pisi file name required..."
        sys.exit()
