# Path: snap/metadata/package.py
# class Package:
#     """information about a package tracked by snap"""
# 
#     def __init__(self, name='', version=''):
#         '''initialize the package
# 
#         @param name - the name of the package
#         @param version - version of the package, currently not used
#         '''
# 
#         self.name = name
#         self.version = version
# 
# class PackagesRecordFile:
#     '''a snap package record file, contains list of packages installed, to restore'''
#     
#     def __init__(self, filename):
#         '''initialize the file
# 
#         @param filename - path to the file 
#         '''
# 
#         self.packagefile = filename
# 
#     def write(self, packages):
#         '''generate file containing record of packages
# 
#         @param packages - list of Packages to record
#         '''
#         f=open(self.packagefile, 'w')
#         f.write("<packages>")
#         for package in packages:
#             f.write('<package>' + xml.sax.saxutils.escape(package.name) + '</package>');
#         f.write("</packages>")
#         f.close()
# 
#     def read(self):
#         '''
#         read packages from the file
# 
#         @returns - list of instances of Package
#         '''
#         parser = xml.sax.make_parser()
#         parser.setFeature(xml.sax.handler.feature_namespaces, 0)
#         handler = _PackagesRecordFileParser()
#         parser.setContentHandler(handler)
#         parser.parse(self.packagefile)
#         return handler.packages
# 
# Path: snap/packageregistry.py
# class PackageRegistry:
#     PACKAGE_REGISTRY = {
#       'mysql-server' : { 'yum' : 'mysql-server',
#                          'apt' : 'mysql-server' },
#     
#       'mysql-client' : { 'yum' : 'mysql',
#                          'apt' : 'mysql-client' },
#     
#       'postgresql-server' : { 'yum' : 'postgresql-server',
#                               'apt' : 'postgresql' },
#     
#       'postgresql-client' : { 'yum' : 'postgresql',
#                               'apt' : 'postgresql-client' },
#     
#       'httpd' : { 'yum' : 'postgresql',
#                   'apt' : 'postgresql-client' }
#     }
# 
#     PACKAGE_SYSTEM_REGISTRY = { 'yum' : {}, 'apt' : {} }
#     for pkg in PACKAGE_REGISTRY:
#         PACKAGE_SYSTEM_REGISTRY['yum'][PACKAGE_REGISTRY[pkg]['yum']] = pkg
# 
#     def encode(package_system, package):
#         if package in PackageRegistry.PACKAGE_SYSTEM_REGISTRY[package_system]:
#             return PackageRegistry.PACKAGE_SYSTEM_REGISTRY[package_system][package]
#         return package
#     encode=staticmethod(encode)
# 
#     def decode(package_system, package):
#         if package in PackageRegistry.PACKAGE_REGISTRY:
#             return PackageRegistry.PACKAGE_REGISTRY[package][package_system]
#         return package
#     decode=staticmethod(decode)


import os
import apt
import sets
import snap
from snap.metadata.package import Package, PackagesRecordFile
from snap.packageregistry  import PackageRegistry
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.



class Sapt(snap.snapshottarget.SnapshotTarget):
    '''implements the snap! packages target backend using the apt package system'''

    def __init__(self):
        self.fs_root='/'

        self.cache = apt.Cache()
        self.cache.update()
        self.cache.open(None)

    def backup(self, basedir, include=[], exclude=[]):
        '''backup the packages installed locally'''
        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Backing up packages using apt backend");

        # retrieve installed packages
        packages=[]
        packagenames = set()
        for pkg in self.cache:
            pkg_name = pkg.name
            if pkg.is_installed and not pkg_name in packagenames:
                if snap.config.options.log_level_at_least('verbose'):
                    snap.callback.snapcallback.message("Backing up package " + pkg_name)
                packagenames.add(pkg_name)
                encoded_pkgname = PackageRegistry.encode('apt', pkg.name)

# Path: snap/metadata/package.py
# class Package:
#     """information about a package tracked by snap"""
# 
#     def __init__(self, name='', version=''):
#         '''initialize the package
# 
#         @param name - the name of the package
#         @param version - version of the package, currently not used
#         '''
# 
#         self.name = name
#         self.version = version
# 
# class PackagesRecordFile:
#     '''a snap package record file, contains list of packages installed, to restore'''
#     
#     def __init__(self, filename):
#         '''initialize the file
# 
#         @param filename - path to the file 
#         '''
# 
#         self.packagefile = filename
# 
#     def write(self, packages):
#         '''generate file containing record of packages
# 
#         @param packages - list of Packages to record
#         '''
#         f=open(self.packagefile, 'w')
#         f.write("<packages>")
#         for package in packages:
#             f.write('<package>' + xml.sax.saxutils.escape(package.name) + '</package>');
#         f.write("</packages>")
#         f.close()
# 
#     def read(self):
#         '''
#         read packages from the file
# 
#         @returns - list of instances of Package
#         '''
#         parser = xml.sax.make_parser()
#         parser.setFeature(xml.sax.handler.feature_namespaces, 0)
#         handler = _PackagesRecordFileParser()
#         parser.setContentHandler(handler)
#         parser.parse(self.packagefile)
#         return handler.packages
# 
# Path: snap/packageregistry.py
# class PackageRegistry:
#     PACKAGE_REGISTRY = {
#       'mysql-server' : { 'yum' : 'mysql-server',
#                          'apt' : 'mysql-server' },
#     
#       'mysql-client' : { 'yum' : 'mysql',
#                          'apt' : 'mysql-client' },
#     
#       'postgresql-server' : { 'yum' : 'postgresql-server',
#                               'apt' : 'postgresql' },
#     
#       'postgresql-client' : { 'yum' : 'postgresql',
#                               'apt' : 'postgresql-client' },
#     
#       'httpd' : { 'yum' : 'postgresql',
#                   'apt' : 'postgresql-client' }
#     }
# 
#     PACKAGE_SYSTEM_REGISTRY = { 'yum' : {}, 'apt' : {} }
#     for pkg in PACKAGE_REGISTRY:
#         PACKAGE_SYSTEM_REGISTRY['yum'][PACKAGE_REGISTRY[pkg]['yum']] = pkg
# 
#     def encode(package_system, package):
#         if package in PackageRegistry.PACKAGE_SYSTEM_REGISTRY[package_system]:
#             return PackageRegistry.PACKAGE_SYSTEM_REGISTRY[package_system][package]
#         return package
#     encode=staticmethod(encode)
# 
#     def decode(package_system, package):
#         if package in PackageRegistry.PACKAGE_REGISTRY:
#             return PackageRegistry.PACKAGE_REGISTRY[package][package_system]
#         return package
#     decode=staticmethod(decode)


# Path: snap/backends/packages/sapt.py
import os
import apt
import sets
import snap
from snap.metadata.package import Package, PackagesRecordFile
from snap.packageregistry  import PackageRegistry

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.



class Sapt(snap.snapshottarget.SnapshotTarget):
    '''implements the snap! packages target backend using the apt package system'''

    def __init__(self):
        self.fs_root='/'

        self.cache = apt.Cache()
        self.cache.update()
        self.cache.open(None)

    def backup(self, basedir, include=[], exclude=[]):
        '''backup the packages installed locally'''
        if snap.config.options.log_level_at_least('verbose'):
            snap.callback.snapcallback.message("Backing up packages using apt backend");

        # retrieve installed packages
        packages=[]
        packagenames = set()
        for pkg in self.cache:
            pkg_name = pkg.name
            if pkg.is_installed and not pkg_name in packagenames:
                if snap.config.options.log_level_at_least('verbose'):
                    snap.callback.snapcallback.message("Backing up package " + pkg_name)
                packagenames.add(pkg_name)
                encoded_pkgname = PackageRegistry.encode('apt', pkg.name)

                packages.append(Package(encoded_pkgname, pkg.versions[0].version))