# Path: lib/package_resolver.py
# class PackageResolver(threading.Thread):
#     def __init__(self, package_control, packages, to_remove, callback):
#         self.package_control = package_control
#         self.packages = packages
#         self.to_remove = to_remove
#         self.callback = callback
#         threading.Thread.__init__(self)
# 
#     def run(self):
#         for pkg in self.to_remove:
#             self.package_control.remove_package(pkg)
# 
#         for pkg in self.packages:
#             self.package_control.install_package(pkg)
# 
#         self.callback()


import sys
import os
import sublime
from .package_resolver import PackageResolver
        from package_control.package_manager import PackageManager
        from package_control.package_cleanup import PackageCleanup
class PackageController:
    user_dir = None

    def __init__(self):
        sublime_dir = os.path.dirname(sublime.packages_path())
        installed_packages_dir = os.path.join(sublime_dir, 'Installed Packages')
        packages_dir = os.path.join(sublime_dir, 'Packages')

        self.user_dir = os.path.join(packages_dir, 'User')

        package_control = os.path.join(installed_packages_dir, "Package Control.sublime-package")
        sys.path.append(package_control)

        self.package_control = PackageManager()

    def install_packages(self, packages, callback=lambda: True, wipe_others=True):
        if wipe_others:
            installed_packages = self.package_control.list_packages()

            to_remove = []
            for pkg in installed_packages:
                if pkg not in packages:
                    to_remove.append(pkg)

        to_install = []
        for pkg in packages:
            if pkg in installed_packages:
                continue
            to_install.append(pkg)


# Path: lib/package_resolver.py
# class PackageResolver(threading.Thread):
#     def __init__(self, package_control, packages, to_remove, callback):
#         self.package_control = package_control
#         self.packages = packages
#         self.to_remove = to_remove
#         self.callback = callback
#         threading.Thread.__init__(self)
# 
#     def run(self):
#         for pkg in self.to_remove:
#             self.package_control.remove_package(pkg)
# 
#         for pkg in self.packages:
#             self.package_control.install_package(pkg)
# 
#         self.callback()


# Path: lib/package_controller.py
import sys
import os
import sublime
from .package_resolver import PackageResolver
        from package_control.package_manager import PackageManager
        from package_control.package_cleanup import PackageCleanup

class PackageController:
    user_dir = None

    def __init__(self):
        sublime_dir = os.path.dirname(sublime.packages_path())
        installed_packages_dir = os.path.join(sublime_dir, 'Installed Packages')
        packages_dir = os.path.join(sublime_dir, 'Packages')

        self.user_dir = os.path.join(packages_dir, 'User')

        package_control = os.path.join(installed_packages_dir, "Package Control.sublime-package")
        sys.path.append(package_control)

        self.package_control = PackageManager()

    def install_packages(self, packages, callback=lambda: True, wipe_others=True):
        if wipe_others:
            installed_packages = self.package_control.list_packages()

            to_remove = []
            for pkg in installed_packages:
                if pkg not in packages:
                    to_remove.append(pkg)

        to_install = []
        for pkg in packages:
            if pkg in installed_packages:
                continue
            to_install.append(pkg)


        PackageResolver(self.package_control, to_install, to_remove, callback).start()