def _download_package(self, id):
     """downloads the specified package, if the download was successfull, it checks the package's md5 checksum
     versus the one stored in the package info for integretiy, and if succefull, dispatches a success message
     """
     package = self._available_packages.get(id)
     if package is None:
         raise PackageNotFound()
     try:
         downloadPath = os.path.join(self._path_manager.tmpPath,
                                     package.file)
         yield DownloaderWithProgress.download(url=package.downloadUrl,
                                               destination=downloadPath,
                                               object=package,
                                               refChecksum=package.fileHash)
         package.downloaded = True
         #update.installPath=self._addon_path
         self._send_signal("package_download_succeeded", package)
         log.msg("Successfully downloaded package ",
                 package.name,
                 system="Package Manager",
                 logLevel=logging.DEBUG)
     except Exception as inst:
         self._send_signal("package_download_failed", package)
         log.msg("Failed to download package",
                 package.name,
                 " error:",
                 inst,
                 system="Package Manager",
                 logLevel=logging.CRITICAL)
    def __init__(self,
                 pathManager=None,
                 packageCheckEnabled=False,
                 packageCheckFrequency=240):
        self._path_manager = pathManager
        self._signal_channel = "package_manager"
        self._signal_dispatcher = SignalDispatcher(self._signal_channel)
        self.signal_channel_prefix = "package_manager"

        pollapli_package = Package(name="Pollapli",
                                   description="core package",
                                   version="0.5.0",
                                   installed=True)
        pollapli_package.cid = uuid.UUID(
            "23b0d813-a6b2-461a-88ad-a7020ae67742")
        self._installed_packages = {pollapli_package.cid: pollapli_package}
        self._available_packages = {}

        self.package_check_frequency = packageCheckFrequency
        self.package_check_enabled = packageCheckEnabled
        self.package_list_url = "http://kaosat.net/pollapli/pollapli_packages.json"
        self._package_check = task.LoopingCall(self.refresh_packageList)

        self._addon_path = "."
        self.updates_path = "."
        self._max_download_attempts = 5
        self._downloader = DownloaderWithProgress()
 def refresh_packageList(self):
     """Fetches the remote package list, downloads it, and if there were any changes, packages
     the in memory package list accordingly
     """
     log.msg("checking for new packages : time", time.time(), logLevel=logging.CRITICAL)
     packageInfoPath = os.path.join(self._path_manager.tmpPath, "packages.txt") 
     try:
         yield DownloaderWithProgress.download(url=self.package_list_url, destination=packageInfoPath)
         self._parse_packageListFile()
     except Exception as inst:
         log.msg("Failed to download package master list: error:", inst, system="Package Manager", logLevel=logging.CRITICAL)
    def setUp(self):
        self._mockServerport = 8765
        self._fileServePath = ""
        self._testFileName = "TestFile.txt"
        self._downloadUrl = "http://localhost:%i/%s" % (self._mockServerport,
                                                        self._testFileName)

        self._downloader = DownloaderWithProgress()
        self._path_manager = PathManager()
        self._path_manager.tmpPath = "tmp"
        if not os.path.exists("tmp"):
            os.makedirs("tmp")
        try:
            if not os.path.exists(self._fileServePath):
                os.makedirs(self._fileServePath)
        except:
            pass

        self._start_mock_file_server(self._fileServePath)
        self._downloadPath = os.path.join(self._path_manager.tmpPath,
                                          self._testFileName)
        self._testFilePath = os.path.join(self._fileServePath,
                                          self._testFileName)
        self._write_mock_file(self._testFilePath)
 def _download_package(self, id):
     """downloads the specified package, if the download was successfull, it checks the package's md5 checksum
     versus the one stored in the package info for integretiy, and if succefull, dispatches a success message
     """
     package = self._available_packages.get(id)
     if package is None:
         raise PackageNotFound()
     try:
         downloadPath = os.path.join(self._path_manager.tmpPath,package.file)  
         yield DownloaderWithProgress.download(url = package.downloadUrl, destination= downloadPath, object=package, refChecksum=package.fileHash)
         package.downloaded = True
         #update.installPath=self._addon_path
         self._send_signal("package_download_succeeded", package)
         log.msg("Successfully downloaded package ",package.name,system="Package Manager",logLevel=logging.DEBUG)
     except Exception as inst:
         self._send_signal("package_download_failed", package)
         log.msg("Failed to download package",package.name," error:",inst,system="Package Manager",logLevel=logging.CRITICAL)
 def refresh_packageList(self):
     """Fetches the remote package list, downloads it, and if there were any changes, packages
     the in memory package list accordingly
     """
     log.msg("checking for new packages : time",
             time.time(),
             logLevel=logging.CRITICAL)
     packageInfoPath = os.path.join(self._path_manager.tmpPath,
                                    "packages.txt")
     try:
         yield DownloaderWithProgress.download(url=self.package_list_url,
                                               destination=packageInfoPath)
         self._parse_packageListFile()
     except Exception as inst:
         log.msg("Failed to download package master list: error:",
                 inst,
                 system="Package Manager",
                 logLevel=logging.CRITICAL)
 def setUp(self):
     self._mockServerport = 8765
     self._fileServePath = ""
     self._testFileName = "TestFile.txt"
     self._downloadUrl = "http://localhost:%i/%s" %(self._mockServerport,self._testFileName)
     
     self._downloader = DownloaderWithProgress()
     self._path_manager = PathManager()
     self._path_manager.tmpPath = "tmp"
     if not os.path.exists("tmp"):
         os.makedirs("tmp")       
     try: 
         if not os.path.exists(self._fileServePath):
             os.makedirs(self._fileServePath)
     except:pass  
     
     self._start_mock_file_server(self._fileServePath)
     self._downloadPath = os.path.join(self._path_manager.tmpPath,self._testFileName)
     self._testFilePath = os.path.join(self._fileServePath,self._testFileName)
     self._write_mock_file(self._testFilePath)
class DownloaderTest(unittest.TestCase):
    def setUp(self):
        self._mockServerport = 8765
        self._fileServePath = ""
        self._testFileName = "TestFile.txt"
        self._downloadUrl = "http://localhost:%i/%s" % (self._mockServerport,
                                                        self._testFileName)

        self._downloader = DownloaderWithProgress()
        self._path_manager = PathManager()
        self._path_manager.tmpPath = "tmp"
        if not os.path.exists("tmp"):
            os.makedirs("tmp")
        try:
            if not os.path.exists(self._fileServePath):
                os.makedirs(self._fileServePath)
        except:
            pass

        self._start_mock_file_server(self._fileServePath)
        self._downloadPath = os.path.join(self._path_manager.tmpPath,
                                          self._testFileName)
        self._testFilePath = os.path.join(self._fileServePath,
                                          self._testFileName)
        self._write_mock_file(self._testFilePath)

    def tearDown(self):
        self._stop_mock_file_server()
        os.remove(self._testFileName)
        if os.path.exists("tmp"):
            shutil.rmtree("tmp")
        if os.path.exists(self._fileServePath):
            shutil.rmtree(self._fileServePath)

    @defer.inlineCallbacks
    def test_download_file(self):
        yield self._downloader.download(url=self._downloadUrl,
                                        destination=self._downloadPath)
        expFilePath = self._downloadPath
        self.assertTrue(os.path.exists(expFilePath))

    @defer.inlineCallbacks
    def test_download_file_with_progress_update(self):
        obsUpdatableObject = MockUpdatableObject()
        expUpdatableObject = MockUpdatableObject(100)
        expFilePath = self._downloadPath

        yield self._downloader.download(url=self._downloadUrl,
                                        destination=self._downloadPath,
                                        object=obsUpdatableObject)

        self.assertEquals(obsUpdatableObject, expUpdatableObject)
        self.assertTrue(os.path.exists(expFilePath))

    @defer.inlineCallbacks
    def test_download_file_with_checksum(self):
        obsUpdatableObject = MockUpdatableObject()
        expFilePath = self._downloadPath
        yield self._downloader.download(
            url=self._downloadUrl,
            destination=self._downloadPath,
            object=obsUpdatableObject,
            refChecksum="18c0864b36d60f6036bf8eeab5c1fe7d")
        self.assertTrue(os.path.exists(expFilePath))

    def test_download_file_with_checksumError(self):
        obsUpdatableObject = MockUpdatableObject()
        d = self._downloader.download(
            url=self._downloadUrl,
            destination=self._downloadPath,
            object=obsUpdatableObject,
            refChecksum="18c0864b36d60f6036bf8eeab5c1fe7f")
        return self.assertFailure(d, Exception)

    def _start_mock_file_server(self, fileServePath):
        self._handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        self.httpd = SocketServer.TCPServer(("", self._mockServerport),
                                            self._handler)
        print("serving on port %i" % self._mockServerport)
        self._t = Thread(target=self.httpd.serve_forever).start()

    def _stop_mock_file_server(self):
        self.httpd.shutdown()
        self.httpd.server_close()
        print("stopped serving on port %i" % self._mockServerport)

    def _write_mock_file(self, path):
        f = open(path, "w")
        f.write("somecontent")
        f.close()
class DownloaderTest(unittest.TestCase):    
    def setUp(self):
        self._mockServerport = 8765
        self._fileServePath = ""
        self._testFileName = "TestFile.txt"
        self._downloadUrl = "http://localhost:%i/%s" %(self._mockServerport,self._testFileName)
        
        self._downloader = DownloaderWithProgress()
        self._path_manager = PathManager()
        self._path_manager.tmpPath = "tmp"
        if not os.path.exists("tmp"):
            os.makedirs("tmp")       
        try: 
            if not os.path.exists(self._fileServePath):
                os.makedirs(self._fileServePath)
        except:pass  
        
        self._start_mock_file_server(self._fileServePath)
        self._downloadPath = os.path.join(self._path_manager.tmpPath,self._testFileName)
        self._testFilePath = os.path.join(self._fileServePath,self._testFileName)
        self._write_mock_file(self._testFilePath)
        
    def tearDown(self):
        self._stop_mock_file_server()
        os.remove(self._testFileName)
        if os.path.exists("tmp"):
            shutil.rmtree("tmp")
        if os.path.exists(self._fileServePath): 
            shutil.rmtree(self._fileServePath)
            
    @defer.inlineCallbacks
    def test_download_file(self):
        yield self._downloader.download(url = self._downloadUrl ,destination = self._downloadPath)
        expFilePath = self._downloadPath
        self.assertTrue(os.path.exists(expFilePath))
            
    @defer.inlineCallbacks
    def test_download_file_with_progress_update(self):
        obsUpdatableObject = MockUpdatableObject()
        expUpdatableObject = MockUpdatableObject(100)
        expFilePath = self._downloadPath
        
        yield self._downloader.download(url = self._downloadUrl ,destination = self._downloadPath, object = obsUpdatableObject)
        
        self.assertEquals(obsUpdatableObject,expUpdatableObject)
        self.assertTrue(os.path.exists(expFilePath))
        
    @defer.inlineCallbacks
    def test_download_file_with_checksum(self):
        obsUpdatableObject = MockUpdatableObject()
        expFilePath = self._downloadPath        
        yield self._downloader.download(url = self._downloadUrl ,destination = self._downloadPath, object = obsUpdatableObject, refChecksum = "18c0864b36d60f6036bf8eeab5c1fe7d")
        self.assertTrue(os.path.exists(expFilePath))
    
    def test_download_file_with_checksumError(self):
        obsUpdatableObject = MockUpdatableObject()
        d = self._downloader.download(url = self._downloadUrl ,destination = self._downloadPath, object = obsUpdatableObject, refChecksum = "18c0864b36d60f6036bf8eeab5c1fe7f")
        return self.assertFailure(d, Exception)
        
    def _start_mock_file_server(self,fileServePath):
        self._handler = SimpleHTTPServer.SimpleHTTPRequestHandler
        self.httpd = SocketServer.TCPServer(("", self._mockServerport), self._handler)
        print ("serving on port %i" %self._mockServerport)
        self._t=Thread(target=self.httpd.serve_forever).start()    
            
    def _stop_mock_file_server(self):
        self.httpd.shutdown()
        self.httpd.server_close()
        print ("stopped serving on port %i" %self._mockServerport)
        
    def _write_mock_file(self,path):
        f = open(path,"w") 
        f.write("somecontent")
        f.close()