def main(filename: str): read_file = ReadFile(filename) read_file.load_file() n, m, B, T, F, products, materials = read_file.split_file() solver = Solver(n, m, B, T, F, products, materials) solver.init_variables() solver.create_restriction() solver.set_function_objective() solver.print_solution()
def concatenateFile(file_path, output_path, temp_folder): size = ReadFile.getNumberOfPieces(file_path) filenames = [] for i in range(size): piece_hash = ReadFile.getNHashPiece(file_path, i) op = "{}/{}".format(temp_folder, piece_hash) filenames.append(op) with open(output_path, 'wb') as output_file: for files in filenames: with open(files, 'rb') as f: shutil.copyfileobj(f, output_file)
def run(self): # First message needs to be the handshake data = self.connection.recv(BUFFER_SIZE) print(data) data = data.decode("utf-8") print(data) self.connection.send(b"ok") info_hash = Messages.getInfoHash(data) print(info_hash) path = FileTable.getByInfoHash(info_hash) print(path) file = open(path, "r") while True: data = self.connection.recv(BUFFER_SIZE) data = data.decode("utf-8") mt = int(Messages.getMessageType(data)) if (mt == 1): pieceNumber = int(Messages.getNPiece(data)) piece = ReadFile.getNPiece(file, pieceNumber, 65536) print(piece) self.connection.send(piece.encode()) if (mt == 2): break file.close() self.connection.send(b"finished") self.connection.close()
def getRemainingPieces(self): q = queue.Queue() op = "{}{}".format(os.path.splitext(self.output_path)[0], "/temp") size = ReadFile.getNumberOfPieces(self.file_path) if(os.path.isdir(op)): for i in range(size): piece_hash = ReadFile.getNHashPiece(self.file_path, i) aux = "{}/{}".format(op, piece_hash) if not os.path.exists(aux): q.put(i) else: os.makedirs(op) for i in range(size): q.put(i) return q
def start_download(self): tracker_ip = "http://{}/announce".format(ReadFile.getTrackerIp(self.file_path)) hostname = socket.gethostname() myip = socket.gethostbyname(hostname) peerId = ReadConfig.getPeerId(CONFIG_PATH) info_hash = ReadFile.getInfoHash(self.file_path) port = ReadConfig.getPort(CONFIG_PATH) peers = Peers.getPeers(tracker_ip, info_hash, myip, port, 0, peerId) piece_length = ReadFile.getPieceLength(self.file_path) q = self.getRemainingPieces() for p in peers: if(q.empty()): break s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((p['ip'], int(p['port']))) handshake = Messages.handshake(info_hash, peerId) s.send(handshake.encode()) data = s.recv(piece_length) if(data != b'ok'): s.close() continue cnt = 0 while not q.empty(): elem = q.get() msg = '0001{}'.format(str(elem)) s.send(msg.encode()) data = s.recv(piece_length) if(self.verifyHash(data, elem)): self.saveFile(data.decode('utf-8'), elem) cnt = 0 else: q.put(elem) cnt = cnt + 1 if cnt == 10: break s.send(b"0002") data = s.recv(piece_length) s.close() temp = "{}{}".format(os.path.splitext(self.output_path)[0], "/temp") ConcatenateFile.concatenateFile(self.file_path, self.output_path, temp) temp = os.path.splitext(self.output_path)[0] shutil.rmtree(temp)
def saveFile(self, data, n): piece_hash = ReadFile.getNHashPiece(self.file_path, n) op = "{}/temp/{}".format(os.path.splitext(self.output_path)[0], piece_hash) try: file = open(op, "w") file.write(data) file.close() except Exception as e: raise e
class Main: rf = ReadFile() wf = WriteFile() # if dir does not exist create it rf.create_dir() # if file does not exist, create it and do the following steps if rf.create_txt(): # write a basic table into the file wf.print_file() # read the file and print it into the console rf.read_txt()
def verifyHash(self, data, n): piece_hash = ReadFile.getNHashPiece(self.file_path, n) m = hashlib.sha256() m.update(data) return m.hexdigest() == piece_hash
def teste(): ManagerFile.deleta_cria_file() log = ReadFile.leitura_arquivo() return render_template("teste.html", log=log)
def set_screen_layout(self): # Set screen layout that will be drawn by curses # It is main game screen screen_layout = ReadFile(self.file_name) return screen_layout.get_content()