def test_many_download(self): q = Queue() q2 = Queue() selects = {} for i in range(10): choice = random.choice(list(test_urls.keys())) if choice in selects.keys(): selects[choice] += 1 else: selects[choice] = 1 q.put((i, {"url": test_urls[choice]["url"], "dest": test_dest})) worker = Worker({"wait_task": 1, "wait_retry": 0, "max_retry": 1}, q, q2) worker.start() worker.join() self.assertTrue(q.empty()) for key, value in selects.items(): path = test_urls[key]["path"] dirname = FileManager.get_dirname(path) basename = FileManager.get_basename(path) for i in range(value): filepath = os.path.join(dirname, "{}_{}".format(i, basename)) if i != 0 else path self.assertTrue(os.path.exists(filepath)) with open(filepath, "rb") as f: data = f.read() self.assertEqual(hashlib.md5(data).hexdigest(), test_urls[key]["md5"]) FileManager.remove_file(filepath)
def rename_file(self, dest): """ Rename downloaded file from random name to name identified in url. Filename will have incremental number at the front if filename exist. dest: destination path """ filename = self.progress.get("filename") dirname = FileManager.get_dirname(dest) # Get filename to rename the file # Increment number at the front of filename if filename exist new_dest = FileManager.join_path(dirname, filename) if FileManager.is_path_exists(new_dest): new_dest = FileManager.generate_filepath(new_dest) FileManager.rename_file(dest, new_dest) # Notify success work self.progress["filename"] = FileManager.get_basename(new_dest) self.progress["state"] = "Success" self.progresses.put((self.i, self.progress.copy()))