# Path: IApp.py
# class IApp(object):
#     '''
#     classdocs
#     '''
#     name = "Unknown"
#     cname = "Unknown"
#     databases = {}
#     known = False
#     dummyRE = type(re.compile("nfi"))
#     version = [-1]
#     
#     def get_versions(self):
#         return self.version
#     
#     def has_defaultversion(self):
#         if IDeviceVersion.DEFAULT_VERSION in self.version:
#             return True
#         return False
#     
#     def has_version(self,appversion):
#         if appversion in self.version:
#             return True
#         return False
#     
#     def get_packagename(self):
#         return self.name
#     
#     def get_canonicalname(self):
#         return self.cname
#     
#     def get_files(self):
#         return self.databases
#     
#     def get_file_info(self, dbname):
#         if len(self.databases) == 0:
#             return None
#         for thisdbname,defn in self.databases.iteritems():
#             if type(thisdbname) == type("str"):
#                 if thisdbname == dbname:
#                     return defn
#             elif type(thisdbname) == self.dummyRE:
#                 if thisdbname.match(dbname) != None:
#                     return defn
#         return None
#     
#     def set_packagename(self, name):
#         self.name = name
#     
#     def __init__(self):
#         return
# 
# Path: MiscUtils.py
# def genfilesig(filepath,hasher=DEFAULT_HASHER,bs=65536):
#         try:
#             f = open(filepath,"rb")
#             while True:
#                 buf = f.read(bs)
#                 if not buf:
#                     break
#                 hasher.update(buf)
#             f.close()
#             return hasher.hexdigest()
#         except:
#             print "Error opening file:" + filepath
#             return None
# 
# DEFAULT_HASHER = hashlib.sha256()
# 
# Path: ModuleImporter.py
# class Importer(object):
#     def __init__(self):
#         return
#     
#     def get_package_modules_dict(self, package_name, parent_obj, keyfield, 
#                                  args=None ):
#         return self._load_dict( [package_name], package_name + u".", parent_obj, 
#                                 keyfield, args)
#         
#     def get_package_modules(self, package_name, parent_obj, args=None ):
#         return self._load( [package_name], package_name + u".", parent_obj, 
#                            True, args )
#     
#     def get_package_modules_list(self, package_name, parent_obj ):
#         return self._load( [package_name], package_name + u".", parent_obj )
#     
#     def _load(self, path, prefix, parent_obj, instantiate=False, args=None ):
#         modules = []
#         for _, name, is_package in walk_packages( path, prefix=prefix ):
#             if is_package: continue
#             module = import_module( name )
#             for member in dir(module):
#                 if member == parent_obj.__class__.__name__: continue
#                 member_obj = getattr( module, member )
#                 if inspect.isclass( member_obj ) == False: continue
#                 if issubclass( member_obj, parent_obj.__class__ ):
#                     if instantiate:
#                         if args != None: obj = member_obj(*args)
#                         else: obj = member_obj()
#                         modules.append( obj )
#                     else:
#                         modules.append( member )
#         return modules
#     
#     def _load_dict(self, path, prefix, parent_obj, keyfield, args=None ):
#         modules = {}
#         for _, name, is_package in walk_packages( path, prefix=prefix ):
#             if is_package: continue
#             module = import_module( name )
#             for member in dir(module):
#                 if member == parent_obj.__class__.__name__: continue
#                 member_obj = getattr( module, member )
#                 if inspect.isclass( member_obj ) == False: continue
#                 if issubclass( member_obj, parent_obj.__class__ ):
#                     if args != None: tmpobj = member_obj(*args)
#                     else: tmpobj = member_obj()
#                     modules[getattr( tmpobj, keyfield )] = tmpobj
#         return modules


from IApp import IApp
from MimeGuesser import MimeGuesser
from os import listdir
from os.path import isfile, isdir, islink, join, basename, dirname
from MiscUtils import genfilesig, DEFAULT_HASHER
from ModuleImporter import Importer
import ExtractStore
import os
            return target_path
        return join(dirname(linkpath),target_path)
    
    def scan_single_app(self, approot_path, app):
        #print "[APP]: " + app.get_packagename()
        if self.outqueue != None:
            self.outqueue.put("[APP]: " + app.get_packagename())
        store_app = self.exstore.create_application(
                                app.get_packagename(), app.get_canonicalname())
        rootdir = store_app.add_directory(approot_path)
        store_app.add_root_stats( os.stat(approot_path) )
        for f in listdir(approot_path):
            filepath = join(approot_path,f)
            if isdir(filepath):
                dirobject = rootdir.add_dir(f)
                self.scan_single_app_dir(filepath, app, dirobject, store_app)
            elif islink(filepath):
                if filepath.endswith("lib"):
                    store_app.set_library_path(self.read_link(filepath))
            elif isfile(filepath):
                store_app.totalfiles += 1
                rootdir.add_file(self.handle_file(f,approot_path,app))
        return
    
    def scan_directory(self, root):
        for dfile in os.listdir(root):
            fqname = join(root,dfile)
            if isdir( fqname ):
                app = self.get_package( dfile )
                if app == None:

# Path: IApp.py
# class IApp(object):
#     '''
#     classdocs
#     '''
#     name = "Unknown"
#     cname = "Unknown"
#     databases = {}
#     known = False
#     dummyRE = type(re.compile("nfi"))
#     version = [-1]
#     
#     def get_versions(self):
#         return self.version
#     
#     def has_defaultversion(self):
#         if IDeviceVersion.DEFAULT_VERSION in self.version:
#             return True
#         return False
#     
#     def has_version(self,appversion):
#         if appversion in self.version:
#             return True
#         return False
#     
#     def get_packagename(self):
#         return self.name
#     
#     def get_canonicalname(self):
#         return self.cname
#     
#     def get_files(self):
#         return self.databases
#     
#     def get_file_info(self, dbname):
#         if len(self.databases) == 0:
#             return None
#         for thisdbname,defn in self.databases.iteritems():
#             if type(thisdbname) == type("str"):
#                 if thisdbname == dbname:
#                     return defn
#             elif type(thisdbname) == self.dummyRE:
#                 if thisdbname.match(dbname) != None:
#                     return defn
#         return None
#     
#     def set_packagename(self, name):
#         self.name = name
#     
#     def __init__(self):
#         return
# 
# Path: MiscUtils.py
# def genfilesig(filepath,hasher=DEFAULT_HASHER,bs=65536):
#         try:
#             f = open(filepath,"rb")
#             while True:
#                 buf = f.read(bs)
#                 if not buf:
#                     break
#                 hasher.update(buf)
#             f.close()
#             return hasher.hexdigest()
#         except:
#             print "Error opening file:" + filepath
#             return None
# 
# DEFAULT_HASHER = hashlib.sha256()
# 
# Path: ModuleImporter.py
# class Importer(object):
#     def __init__(self):
#         return
#     
#     def get_package_modules_dict(self, package_name, parent_obj, keyfield, 
#                                  args=None ):
#         return self._load_dict( [package_name], package_name + u".", parent_obj, 
#                                 keyfield, args)
#         
#     def get_package_modules(self, package_name, parent_obj, args=None ):
#         return self._load( [package_name], package_name + u".", parent_obj, 
#                            True, args )
#     
#     def get_package_modules_list(self, package_name, parent_obj ):
#         return self._load( [package_name], package_name + u".", parent_obj )
#     
#     def _load(self, path, prefix, parent_obj, instantiate=False, args=None ):
#         modules = []
#         for _, name, is_package in walk_packages( path, prefix=prefix ):
#             if is_package: continue
#             module = import_module( name )
#             for member in dir(module):
#                 if member == parent_obj.__class__.__name__: continue
#                 member_obj = getattr( module, member )
#                 if inspect.isclass( member_obj ) == False: continue
#                 if issubclass( member_obj, parent_obj.__class__ ):
#                     if instantiate:
#                         if args != None: obj = member_obj(*args)
#                         else: obj = member_obj()
#                         modules.append( obj )
#                     else:
#                         modules.append( member )
#         return modules
#     
#     def _load_dict(self, path, prefix, parent_obj, keyfield, args=None ):
#         modules = {}
#         for _, name, is_package in walk_packages( path, prefix=prefix ):
#             if is_package: continue
#             module = import_module( name )
#             for member in dir(module):
#                 if member == parent_obj.__class__.__name__: continue
#                 member_obj = getattr( module, member )
#                 if inspect.isclass( member_obj ) == False: continue
#                 if issubclass( member_obj, parent_obj.__class__ ):
#                     if args != None: tmpobj = member_obj(*args)
#                     else: tmpobj = member_obj()
#                     modules[getattr( tmpobj, keyfield )] = tmpobj
#         return modules


# Path: Android/ApplicationParser.py
from IApp import IApp
from MimeGuesser import MimeGuesser
from os import listdir
from os.path import isfile, isdir, islink, join, basename, dirname
from MiscUtils import genfilesig, DEFAULT_HASHER
from ModuleImporter import Importer
import ExtractStore
import os

            return target_path
        return join(dirname(linkpath),target_path)
    
    def scan_single_app(self, approot_path, app):
        #print "[APP]: " + app.get_packagename()
        if self.outqueue != None:
            self.outqueue.put("[APP]: " + app.get_packagename())
        store_app = self.exstore.create_application(
                                app.get_packagename(), app.get_canonicalname())
        rootdir = store_app.add_directory(approot_path)
        store_app.add_root_stats( os.stat(approot_path) )
        for f in listdir(approot_path):
            filepath = join(approot_path,f)
            if isdir(filepath):
                dirobject = rootdir.add_dir(f)
                self.scan_single_app_dir(filepath, app, dirobject, store_app)
            elif islink(filepath):
                if filepath.endswith("lib"):
                    store_app.set_library_path(self.read_link(filepath))
            elif isfile(filepath):
                store_app.totalfiles += 1
                rootdir.add_file(self.handle_file(f,approot_path,app))
        return
    
    def scan_directory(self, root):
        for dfile in os.listdir(root):
            fqname = join(root,dfile)
            if isdir( fqname ):
                app = self.get_package( dfile )
                if app == None:

                    app = IApp()