def find_move(self, board, debug=False): # Should we flip the board to make sure it always gets it from white? vec_board = board_to_vec( board if board.turn == chess.WHITE else board.mirror()) vec_board = tensor_sketch(vec_board, self.sketches) probs = self.move_clf.predict_proba([vec_board])[0] score = self.score_clf.predict([vec_board])[0] for n, (mp, from_to) in enumerate(sorted((-p, ft) for ft, p in enumerate(probs))): to_square, from_square = divmod(from_to, 64) if board.turn == chess.BLACK: from_square = chess.square_mirror(from_square) to_square = chess.square_mirror(to_square) move = chess.Move(from_square, to_square) if move in board.legal_moves: print(f'Choice move {n}, p={-mp}') return move, score
def choose_sense(self, sense_actions: List[Square], move_actions: List[chess.Move], seconds_left: float) -> \ Optional[Square]: """ The method to implement your sensing strategy. The chosen sensing action should be returned from this function. I.e. the value returned is the square at the center of the 3x3 sensing area you want to sense. The returned square must be one of the squares in the `sense_actions` parameter. You can pass instead of sensing by returning `None` from this function. Move actions are provided through `move_actions` in case you want to sense based on a potential move. Called after :meth:`handle_opponent_move_result()`. Example implementation: :: def choose_sense(self, sense_actions: List[Square], move_actions: List[chess.Move], seconds_left: float) -> Square: return random.choice(sense_actions) :param sense_actions: A :class:`list` containing the valid squares to sense over. :param move_actions: A :class:`list` containing the valid moves that can be returned in :meth:`choose_move()`. :param seconds_left: The time in seconds you have left to use in the game. :return: a :class:`Square` that is the center of the 3x3 sensing area you want to get information about. """ pass
def choose_move(self, move_actions: List[chess.Move], seconds_left: float) -> Optional[chess.Move]: """ The method to implement your movement strategy. The chosen movement action should be returned from this function. I.e. the value returned is the move to make. The returned move must be one of the moves in the `move_actions` parameter. The pass move is legal, and is executed by returning `None` from this method. Called after :meth:`handle_sense_result()`. Example implementation: :: def choose_move(self, move_actions: List[chess.Move], seconds_left: float) -> Optional[chess.Move]: return random.choice(move_actions) :param move_actions: A `list` containing the valid :class:`chess.Move` you can choose. :param seconds_left: The time in seconds you have left to use in the game. :return: The :class:`chess.Move` to make. """ pass
def play_sense(game: Game, player: Player, sense_actions: List[Square], move_actions: List[chess.Move]): """ Runs the sense phase for `player` in `game`. Does the following sequentially: #. Get the sensing action using :meth:`Player.choose_sense`. #. Apply the sense action using :meth:`Game.sense`. #. Give the result of the sense action to player using :meth:`Player.handle_sense_result`. :param game: The :class:`Game` that `player` is playing in. :param player: The :class:`Player` whose turn it is. :param sense_actions: The possible sense actions for `player`. :param move_actions: The possible move actions for `player`. """ sense = player.choose_sense(sense_actions, move_actions, game.get_seconds_left()) sense_result = game.sense(sense) player.handle_sense_result(sense_result)
def is_illegal_castle(board: chess.Board, move: chess.Move) -> bool: if not board.is_castling(move): return False # illegal without kingside rights if board.is_kingside_castling(move) and not board.has_kingside_castling_rights(board.turn): return True # illegal without queenside rights if board.is_queenside_castling(move) and not board.has_queenside_castling_rights(board.turn): return True # illegal if any pieces are between king & rook rook_square = chess.square(7 if board.is_kingside_castling(move) else 0, chess.square_rank(move.from_square)) between_squares = chess.SquareSet(chess.between(move.from_square, rook_square)) if any(map(lambda s: board.piece_at(s), between_squares)): return True # its legal return False
def default(self, o): if isinstance(o, chess.Piece): return { 'type': 'Piece', 'value': o.symbol(), } elif isinstance(o, chess.Move): return { 'type': 'Move', 'value': o.uci(), } elif isinstance(o, chess.Board): return { 'type': 'Board', 'value': o.fen(), } elif isinstance(o, WinReason): return { 'type': 'WinReason', 'value': o.name, } return super().default(o)
def requested_move(self, turn: Turn) -> Optional[chess.Move]: """ Get the requested move action on the given turn. Examples: >>> history.requested_move(Turn(WHITE, 0)) Move(E2, E4) >>> history.requested_move(Turn(BLACK, 0)) Move(E7, E5) >>> history.requested_move(Turn(WHITE, 1)) None :param turn: The :class:`Turn` in question. :return: If the player requested to move, then the requested move action as a :class:`chess.Move`, otherwise `None` if the player requested to pass. """ self._validate_turn(turn, self._requested_moves) return self._requested_moves[turn.color][turn.turn_number]
def taken_move(self, turn: Turn) -> Optional[chess.Move]: """ Get the executed move action on the given turn. This may be different than the requested move, as the requested move may not be legal. Examples: >>> history.requested_move(Turn(WHITE, 0)) Move(E2, D3) >>> history.taken_move(Turn(WHITE, 0)) None >>> history.requested_move(Turn(WHITE, 1)) Move(E2, E4) >>> history.taken_move(Turn(WHITE, 1)) Move(E2, E3) :param turn: The :class:`Turn` in question. :return: `None` if the player requested to pass or made an illegal move, the executed move action as a :class:`chess.Move` otherwise. """ self._validate_turn(turn, self._taken_moves) return self._taken_moves[turn.color][turn.turn_number]
def truth_fen_before_move(self, turn: Turn) -> str: """ Get the truth state of the board as a fen string before the move was executed on the given turn. Use :meth:`truth_board_before_move` if you want the truth board as a :class:`chess.Board` object. Examples: >>> history.truth_fen_before_move(Turn(WHITE, 0)) "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -" >>> history.taken_move(Turn(WHITE, 0)) Move(E2, E4) >>> history.truth_fen_before_move(Turn(BLACK, 0)) "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -" :param turn: The :class:`Turn` in question. :return: The fen of the truth board. """ self._validate_turn(turn, self._fens_before_move) return self._fens_before_move[turn.color][turn.turn_number]
def truth_board_before_move(self, turn: Turn) -> chess.Board: """ Get the truth state of the board as a :class:`chess.Board` before the move was executed on the given turn. Use :meth:`truth_fen_before_move` if you want the truth board as a fen string. Examples: >>> history.truth_board_before_move(Turn(WHITE, 0)) Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -") >>> history.taken_move(Turn(WHITE, 0)) Move(E2, E4) >>> history.truth_fen_before_move(Turn(BLACK, 0)) Board("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -") :param turn: The :class:`Turn` in question. :return: A :class:`chess.Board` object. """ self._validate_turn(turn, self._fens_before_move) board = chess.Board(self._fens_before_move[turn.color][turn.turn_number]) board.turn = turn.color return board
def truth_fen_after_move(self, turn: Turn) -> str: """ Get the truth state of the board as a fen string after the move was executed on the given turn. Use :meth:`truth_board_after_move` if you want the truth board as a :class:`chess.Board` object. Examples: >>> history.truth_fen_before_move(Turn(WHITE, 0)) "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -" >>> history.taken_move(Turn(WHITE, 0)) Move(E2, E4) >>> history.truth_fen_after_move(Turn(WHITE, 0)) "rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -" :param turn: The :class:`Turn` in question. :return: The fen of the truth board. """ self._validate_turn(turn, self._fens_after_move) return self._fens_after_move[turn.color][turn.turn_number]
def truth_board_after_move(self, turn: Turn) -> chess.Board: """ Get the truth state of the board as a :class:`chess.Board` after the move was executed on the given turn. Use :meth:`truth_fen_after_move` if you want the truth board as a fen string. Examples: >>> history.truth_board_before_move(Turn(WHITE, 0)) Board("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -") >>> history.taken_move(Turn(WHITE, 0)) Move(E2, E4) >>> history.truth_fen_after_move(Turn(WHITE, 0)) Board("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq -") :param turn: The :class:`Turn` in question. :return: A :class:`chess.Board` object. """ self._validate_turn(turn, self._fens_after_move) board = chess.Board(self._fens_after_move[turn.color][turn.turn_number]) board.turn = turn.color return board
def collect(self, get_turn_data_fn: Callable[[Turn], T], turns: Iterable[Turn]) -> Iterable[T]: """ Collect data from multiple turns using any of :meth:`sense`, :meth:`sense_result`, :meth:`requested_move`, :meth:`taken_move`, :meth:`capture_square`, :meth:`move_result`, :meth:`truth_fen_before_move`, :meth:`truth_board_before_move`, :meth:`truth_fen_after_move`, or :meth:`truth_board_after_move`. Examples: >>> history.collect(history.sense, [Turn(WHITE, 0), Turn(BLACK, 0)]) [E7, E2] >>> history.collect(history.requested_move, history.turns(WHITE)) [Move(E2, E4), Move(D1, H5), ...] :param get_turn_data_fn: One of the getter functions of the history object. :param turns: The turns in question. :return: A list of the data, where each element is the value of the getter function on the corresponding turn. """ if get_turn_data_fn not in [self.sense, self.sense_result, self.requested_move, self.taken_move, self.capture_square, self.move_result, self.truth_board_before_move, self.truth_board_after_move, self.truth_fen_before_move, self.truth_fen_after_move]: raise ValueError('get_turn_data_fn must be one of the history getter functions') for turn in turns: yield get_turn_data_fn(turn)
def update_text_box(self, window, msg, is_hide): """ Update text elements """ best_move = None msg_str = str(msg) if not 'bestmove ' in msg_str: if 'info_all' in msg_str: info_all = ' '.join(msg_str.split()[0:-1]).strip() msg_line = '{}\n'.format(info_all) window.FindElement('search_info_all_k').Update( '' if is_hide else msg_line) else: # Best move can be None because engine dies try: best_move = chess.Move.from_uci(msg.split()[1]) except Exception: logging.exception('Engine sent {}.'.format(best_move)) sg.Popup('Engine error, it sent a {} bestmove.\n'.format( best_move) + 'Back to Neutral mode, it is better to ' 'change engine {}.'.format( self.opp_id_name), icon=ico_path[platform]['pecg'], title=BOX_TITLE) return best_move
def mirror_move(move): return chess.Move(chess.square_mirror(move.from_square), chess.square_mirror(move.to_square), move.promotion)
def handle_move_result(self, requested_move: Optional[chess.Move], taken_move: Optional[chess.Move], captured_opponent_piece: bool, capture_square: Optional[Square]): """ Provides the result of the movement action. The `requested_move` is the move returned from :meth:`choose_move()`, and is provided for ease of use. `taken_move` is the move that was actually performed. Note that `taken_move`, can be different from `requested_move`, due to the uncertainty aspect. Called after :meth:`choose_move()`. Example implementation: :: def handle_move_result(self, requested_move: chess.Move, taken_move: chess.Move, captured_opponent_piece: bool, capture_square: Optional[Square]): if taken_move is not None: self.board.push(taken_move) Note: In the case of playing games on a server, this method is invoked during your opponents turn. This means in most cases this method will not use your play time. However if the opponent finishes their turn before this method completes, then time will be counted against you. :param requested_move: The :class:`chess.Move` you requested in :meth:`choose_move()`. :param taken_move: The :class:`chess.Move` that was actually applied by the game if it was a valid move, otherwise `None`. :param captured_opponent_piece: If `taken_move` resulted in a capture, then `True`, otherwise `False`. :param capture_square: If a capture occurred, then the :class:`Square` that the opponent piece was taken on, otherwise `None`. """ pass
def play_move(game: Game, player: Player, move_actions: List[chess.Move], end_turn_last=False): """ Runs the move phase for `player` in `game`. Does the following sequentially: #. Get the moving action using :meth:`Player.choose_move`. #. Apply the moving action using :meth:`Game.move`. #. Ends the current player's turn using :meth:`Game.end_turn`. #. Give the result of the moveaction to player using :meth:`Player.handle_move_result`. If `end_turn_last` is True, then :meth:`Game.end_turn` is called last instead of before :meth:`Player.handle_move_result`. :param game: The :class:`Game` that `player` is playing in. :param player: The :class:`Player` whose turn it is. :param move_actions: The possible move actions for `player`. :param end_turn_last: Flag indicating whether to call :meth:`Game.end_turn` before or after :meth:`Player.handle_move_result` """ move = player.choose_move(move_actions, game.get_seconds_left()) requested_move, taken_move, opt_enemy_capture_square = game.move(move) if not end_turn_last: game.end_turn() player.handle_move_result(requested_move, taken_move, opt_enemy_capture_square is not None, opt_enemy_capture_square) if end_turn_last: game.end_turn()
def is_psuedo_legal_castle(board: chess.Board, move: chess.Move): return board.is_castling(move) and not is_illegal_castle(board, move)
def slide_move(board: chess.Board, move: chess.Move) -> Optional[chess.Move]: psuedo_legal_moves = list(board.generate_pseudo_legal_moves()) squares = list(chess.SquareSet(chess.between(move.from_square, move.to_square))) + [move.to_square] squares = sorted(squares, key=lambda s: chess.square_distance(s, move.from_square), reverse=True) for slide_square in squares: revised = chess.Move(move.from_square, slide_square, move.promotion) if revised in psuedo_legal_moves: return revised return None
def capture_square_of_move(board: chess.Board, move: Optional[chess.Move]) -> Optional[Square]: capture_square = None if move is not None and board.is_capture(move): if board.is_en_passant(move): # taken from :func:`chess.Board.push()` down = -8 if board.turn == chess.WHITE else 8 capture_square = board.ep_square + down else: capture_square = move.to_square return capture_square
def moves_without_opponent_pieces(board: chess.Board) -> List[chess.Move]: """Generates moves on `board` with the opponent's pieces removed.""" return list(without_opponent_pieces(board).generate_pseudo_legal_moves())
def _object_hook(self, obj): if 'type' in obj: if obj['type'] == 'Piece': return chess.Piece.from_symbol(obj['value']) elif obj['type'] == 'Move': return chess.Move.from_uci(obj['value']) elif obj['type'] == 'Board': return chess.Board(fen=obj['value']) elif obj['type'] == 'WinReason': return WinReason[obj['value']] return obj
def store_move(self, color: Color, requested_move: Optional[chess.Move], taken_move: Optional[chess.Move], opt_capture_square: Optional[Square]): self._requested_moves[color].append(requested_move) self._taken_moves[color].append(taken_move) self._capture_squares[color].append(opt_capture_square)
def move_coordinates(): if not s.board.is_game_over(): source = int(request.args.get('from', default='')) target = int(request.args.get('to', default='')) promotion = True if request.args.get('promotion', default='') == 'true' else False move = s.board.san(chess.Move(source, target, promotion=chess.QUEEN if promotion else None)) if move is not None and move != "": print("human moves", move) try: s.board.push_san(move) computer_move(s, v) except Exception: traceback.print_exc() response = app.response_class( response=s.board.fen(), status=200 ) return response print("GAME IS OVER") response = app.response_class( response="game over", status=200 ) return response
def calculate_computer_move(board: chess.Board, engine: Any) -> chess.Move: """Calculates the computer's move. Parameters: - board: The board object before the move. - engine: The UCI engine object. Returns: The computer's move object. """ engine.position(board) best_move_and_ponder_move = engine.go(movetime=(3000)) return best_move_and_ponder_move[0]
def make_move_reponse( last_board: chess.Board, new_board: chess.Board, move: chess.Move ) -> str: """Makes a response string for after a move is made. Parameters: - last_board: The board object before the move. - new_board: The board object after the move. - move: The move object. Returns: The move response string. """ return ( 'The board was like this:\n\n' '{}' '\n\n\n' 'Then *{}* moved *{}*:\n\n' '{}' '\n\n\n' 'Now it\'s **{}**\'s turn.' '\n\n\n' '{}' ).format( make_str(last_board, new_board.turn), 'white' if last_board.turn else 'black', last_board.san(move), make_str(new_board, new_board.turn), 'white' if new_board.turn else 'black', make_footer() )
def label_to_move (label): """Maps from our 4d-label format to a move.""" return chess.Move ( index_to_square(label[0:2]), index_to_square(label[2:4]) )
def __init__(self, path): ft = self.ft = fasttext.load_model(path) vectors = (ft.get_output_matrix() @ ft.get_input_matrix().T).T rows, _cols = vectors.shape # Add counts and evals vectors = np.hstack([ np.ones(rows).reshape(rows, 1), vectors]) # maybe its an occ model? self.occ = False # Start with bias. No bias for eval. bias = np.hstack([[0], vectors[0]]) # Parse remaining words piece_to_vec = defaultdict(lambda: 0) castling = {} for w, v in zip(ft.words[1:], vectors[1:]): sq = getattr(chess, w[:2].upper()) if w.endswith('-Occ'): self.occ = True for color in chess.COLORS: for piece_type in chess.PIECE_TYPES: piece_to_vec[piece_type, color, sq] += np.hstack([[0], v]) elif w.endswith('-C'): e = pst.castling[sq] castling[sq] = np.hstack([[e], v]) else: p = chess.Piece.from_symbol(w[2]) e = pst.piece[p.piece_type-1] * (1 if p.color else -1) e += pst.pst[0 if p.color else 1][p.piece_type-1][sq] #print(w[2], p, e) piece_to_vec[p.piece_type, p.color, sq] += np.hstack([[e], v]) # Convert to two-colours # We keep a record of the board from both perspectives piece_to_vec2 = {} for (piece_type, color, sq), v in piece_to_vec.items(): inv = piece_to_vec[piece_type, not color, chess.square_mirror(sq)] piece_to_vec2[piece_type, color, sq] = np.vstack([v, inv]) self.bias = np.vstack([bias, bias]) self.piece_to_vec = piece_to_vec2 self.castling = {sq: np.vstack([v, castling[chess.square_mirror(sq)]]) for sq, v in castling.items()} # Parse labels self.moves = [chess.Move.from_uci(label_uci[len('__label__'):]) for label_uci in ft.labels] # Adding 2 to the move ids, since the first entry will be the count, # and the second entry will be the evaluation self.move_to_id = {move: i + 2 for i, move in enumerate(self.moves)}
def apply(self, vec, board, move): """ Should be called prior to pushing move to board. Applies the move to the vector. """ # Remove from square. piece_type = board.piece_type_at(move.from_square) color = board.turn vec -= self.piece_to_vec[piece_type, color, move.from_square] # Update castling rights. old_castling_rights = board.clean_castling_rights() new_castling_rights = old_castling_rights & ~chess.BB_SQUARES[ move.to_square] & ~chess.BB_SQUARES[move.from_square] if piece_type == chess.KING: new_castling_rights &= ~chess.BB_RANK_1 if color else ~chess.BB_RANK_8 # Castling rights can only have been removed for sq in chess.scan_forward(old_castling_rights ^ new_castling_rights): vec -= self.castling[sq] # Remove pawns captured en passant. if piece_type == chess.PAWN and move.to_square == board.ep_square: down = -8 if board.turn == chess.WHITE else 8 capture_square = board.ep_square + down vec -= self.piece_to_vec[chess.PAWN, not board.turn, capture_square] # Move rook during castling. if piece_type == chess.KING: if move.from_square == chess.E1: if move.to_square == chess.G1: vec -= self.piece_to_vec[chess.ROOK, color, chess.H1] vec += self.piece_to_vec[chess.ROOK, color, chess.F1] if move.to_square == chess.C1: vec -= self.piece_to_vec[chess.ROOK, color, chess.A1] vec += self.piece_to_vec[chess.ROOK, color, chess.D1] if move.from_square == chess.E8: if move.to_square == chess.G8: vec -= self.piece_to_vec[chess.ROOK, color, chess.H8] vec += self.piece_to_vec[chess.ROOK, color, chess.F8] if move.to_square == chess.C8: vec -= self.piece_to_vec[chess.ROOK, color, chess.A8] vec += self.piece_to_vec[chess.ROOK, color, chess.D8] # Capture captured_piece_type = board.piece_type_at(move.to_square) if captured_piece_type: vec -= self.piece_to_vec[captured_piece_type, not color, move.to_square] # Put the piece on the target square. vec += self.piece_to_vec[move.promotion or piece_type, color, move.to_square] return vec
def __init__(self, theme, engine_config_file, user_config_file, gui_book_file, computer_book_file, human_book_file, is_use_gui_book, is_random_book, max_book_ply, max_depth=MAX_DEPTH): self.theme = theme self.user_config_file = user_config_file self.engine_config_file = engine_config_file self.gui_book_file = gui_book_file self.computer_book_file = computer_book_file self.human_book_file = human_book_file self.max_depth = max_depth self.is_use_gui_book = is_use_gui_book self.is_random_book = is_random_book self.max_book_ply = max_book_ply self.opp_path_and_file = None self.opp_file = None self.opp_id_name = None self.adviser_file = None self.adviser_path_and_file = None self.adviser_id_name = None self.adviser_hash = 128 self.adviser_threads = 1 self.adviser_movetime_sec = 10 self.pecg_auto_save_game = 'pecg_auto_save_games.pgn' self.my_games = 'pecg_my_games.pgn' self.repertoire_file = {'white': 'pecg_white_repertoire.pgn', 'black': 'pecg_black_repertoire.pgn'} self.init_game() self.fen = None self.psg_board = None self.menu_elem = None self.engine_id_name_list = [] self.engine_file_list = [] self.username = 'Human' self.human_base_time_ms = 5 * 60 * 1000 # 5 minutes self.human_inc_time_ms = 10 * 1000 # 10 seconds self.human_period_moves = 0 self.human_tc_type = 'fischer' self.engine_base_time_ms = 3 * 60 * 1000 # 5 minutes self.engine_inc_time_ms = 2 * 1000 # 10 seconds self.engine_period_moves = 0 self.engine_tc_type = 'fischer' # Default board color is brown self.sq_light_color = '#F0D9B5' self.sq_dark_color = '#B58863' # Move highlight, for brown board self.move_sq_light_color = '#E8E18E' self.move_sq_dark_color = '#B8AF4E' self.gui_theme = 'Reddit' self.is_save_time_left = False self.is_save_user_comment = True
def downloadDemo(which): try: downloadDir = tempfile.mkdtemp() archivePath = "{}/svviz-data.zip".format(downloadDir) # logging.info("Downloading...") downloadWithProgress("http://svviz.github.io/svviz/assets/examples/{}.zip".format(which), archivePath) logging.info("Decompressing...") archive = zipfile.ZipFile(archivePath) archive.extractall("{}".format(downloadDir)) if not os.path.exists("svviz-examples"): os.makedirs("svviz-examples/") shutil.move("{temp}/{which}".format(temp=downloadDir, which=which), "svviz-examples/") except Exception as e: print("error downloading and decompressing example data: {}".format(e)) return False if not os.path.exists("svviz-examples"): print("error finding example data after download and decompression") return False return True
def _remote_init(working_dir): global pickle import pickle import sys import shutil import os if not os.path.isdir(working_dir): os.mkdir(working_dir) sys.path.append(working_dir) shutil.move("_common.py", working_dir) shutil.move("_gdb.py", working_dir) shutil.move("cmds.gdb", working_dir) # setup CERT exploitable exp_lib_dir = os.path.join(working_dir, "exploitable", "lib") os.makedirs(exp_lib_dir) shutil.move("exploitable.py", os.path.join(working_dir, "exploitable")) shutil.move("__init__.py", exp_lib_dir) shutil.move("analyzers.py", exp_lib_dir) shutil.move("classifier.py", exp_lib_dir) shutil.move("elf.py", exp_lib_dir) shutil.move("gdb_wrapper.py", exp_lib_dir) shutil.move("rules.py", exp_lib_dir) shutil.move("tools.py", exp_lib_dir) shutil.move("versions.py", exp_lib_dir) os.chdir(working_dir) global _common global _gdb import _common import _gdb
def restore_config_file(): os.remove(zmirror_file('config.py')) os.remove(zmirror_file('custom_func.py')) if os.path.exists(zmirror_file('config.py._unittest_raw')): shutil.move(zmirror_file('config.py._unittest_raw'), zmirror_file('config.py')) if os.path.exists(zmirror_file('custom_func.py._unittest_raw')): shutil.move(zmirror_file('custom_func.py._unittest_raw'), zmirror_file('custom_func.py')) try: os.remove(zmirror_file('ip_whitelist.txt')) except: pass try: os.remove(zmirror_file('ip_whitelist.log')) except: pass try: os.remove(zmirror_file('automatic_domains_whitelist.log')) except: pass
def __init__(self, tarpath, basepath='', cache_path=None, tarball_name=None): OSPath.__init__(self, basepath, cache_path=cache_path) object.__setattr__(self, 'tarball_path', tarpath) object.__setattr__(self, 'base_path', basepath) if cache_path == tarpath: # we need to prep the path tarfl = os.path.split(tarpath)[1] cpath = os.path.join(cache_path, 'contents') tarpath = os.path.join(cache_path, tarfl) if os.path.isfile(cache_path): # we need to move things around td = tmpdir(delete=True) tmpfl = os.path.join(td,tarfl) shutil.move(cache_path, tmpfl) if not os.path.isdir(cpath): os.makedirs(cpath, mode=0o755) shutil.move(tmpfl, tarpath) object.__setattr__(self, 'tarball_path', tarpath) object.__setattr__(self, 'cache_path', cpath) # get the tarball 'name' flnm = os.path.split(self.tarball_path if tarball_name is None else tarball_name)[-1] tarball_name = flnm.split('.tar')[0] object.__setattr__(self, 'tarball_name', tarball_name)
def setup_logging(log_loc): if os.path.exists(log_loc): shutil.move(log_loc, log_loc + "_" + str(int(os.path.getctime(log_loc)))) os.makedirs(log_loc) log_file = '{}/benchmark.log'.format(log_loc) LOGGER = logging.getLogger('benchmark') LOGGER.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s %(levelname)s:%(name)s %(message)s') file_handler = logging.FileHandler(log_file) console_handler = logging.StreamHandler() file_handler.setFormatter(formatter) console_handler.setFormatter(formatter) LOGGER.addHandler(file_handler) LOGGER.addHandler(console_handler) return LOGGER
def test_dyld_library_path_lookups(): # Test that DYLD_LIBRARY_PATH can be used to find libs during # delocation with TempDirWithoutEnvVars('DYLD_LIBRARY_PATH') as tmpdir: # Copy libs into a temporary directory subtree = pjoin(tmpdir, 'subtree') all_local_libs = _make_libtree(subtree) liba, libb, libc, test_lib, slibc, stest_lib = all_local_libs # move libb and confirm that test_lib doesn't work hidden_dir = 'hidden' os.mkdir(hidden_dir) new_libb = os.path.join(hidden_dir, os.path.basename(LIBB)) shutil.move(libb, new_libb) assert_raises(RuntimeError, back_tick, [test_lib]) # Update DYLD_LIBRARY_PATH and confirm that we can now # successfully delocate test_lib os.environ['DYLD_LIBRARY_PATH'] = hidden_dir delocate_path('subtree', 'deplibs') back_tick(test_lib)
def get_return_label(self): """Get the GSX return label for this part.""" if self.return_label.name == "": # Return label not yet set, get it... label = gsxws.Return(self.return_order).get_label(self.part_number) filename = "%s_%s.pdf" % (self.return_order, self.part_number) tmp_fp = os.path.join(settings.TEMP_ROOT, filename) # move the label to local temp to avoid # django.security.SuspiciousFileOperation shutil.move(label.returnLabelFileData, tmp_fp) f = File(open(tmp_fp, 'r')) self.return_label = f self.save() self.return_label.save(filename, f) f.closed os.remove(tmp_fp) return self.return_label.read()
def replace_old_db_with_new(self): global con global DATABASE_PATH temp_db_path = TEMP_PATH + '/angry_database.db' dir_path = os.path.dirname(DATABASE_PATH) if not os.path.exists(temp_db_path): return if not os.path.exists(dir_path): os.makedirs(dir_path) if con: con.close() shutil.move(temp_db_path, DATABASE_PATH) con = sqlite3.connect(DATABASE_PATH, check_same_thread=False) con.create_function("regexp", 2, regexp)
def testPyCmds(): start = time.time() helix = cmds.polyHelix(**HELIX_OPTS) pHelix = helix[0] size = cmds.polyEvaluate(v=True) for i in xrange(size): x = RAND.uniform(LOW, HIGH) attrib = '%s.vtx[%s]' % (pHelix, i) cmds.move(x, attrib, x=True) cmds.delete(pHelix) end = time.time() return end-start
def loadstate(): global STATE, STATEFILE if os.getenv("HOME") is not None: STATEFILE = os.path.join(os.getenv("HOME"), ".epr") if os.path.isdir(os.path.join(os.getenv("HOME"), ".config")): configdir = os.path.join(os.getenv("HOME"), ".config", "epr") os.makedirs(configdir, exist_ok=True) if os.path.isfile(STATEFILE): if os.path.isfile(os.path.join(configdir, "config")): os.remove(os.path.join(configdir, "config")) shutil.move(STATEFILE, os.path.join(configdir, "config")) STATEFILE = os.path.join(configdir, "config") elif os.getenv("USERPROFILE") is not None: STATEFILE = os.path.join(os.getenv("USERPROFILE"), ".epr") else: STATEFILE = os.devnull if os.path.exists(STATEFILE): with open(STATEFILE, "r") as f: STATE = json.load(f)
def trs_matching(node=None, sel_org=True): global matching_obj global matching_mode global child_comp_flag #print matching_mode, matching_obj mode = matching_mode if node is None: mached_obj = cmds.ls(sl=True, l=True, type='transform') else: #print 'muched obj', node mached_obj = node if not mached_obj: if sel_org: finish_matching() return else: if isinstance(mached_obj, list): mached_obj = mached_obj[0] #print 'trs matching :', mached_obj scl = cmds.xform(mached_obj, q=True, s=True, ws=True) rot = cmds.xform(mached_obj, q=True, ro=True, ws=True) pos = cmds.xform(mached_obj, q=True, t=True, ws=True) for obj in matching_obj: if mode == 'scale' or mode == 'all': cmds.scale(1.0, 1.0, 1.0, obj, pcp=True) ws_scl = cmds.xform(obj, q=True, s=True, ws=True) cmds.scale(scl[0]/ws_scl[0], scl[1]/ws_scl[1], scl[2]/ws_scl[2], obj, pcp=child_comp_flag) if mode == 'rotate' or mode == 'all': cmds.rotate(rot[0], rot[1], rot[2], obj, ws=True, pcp=child_comp_flag) if mode == 'translate' or mode == 'all': cmds.move(pos[0], pos[1], pos[2], obj, ws=True, pcp=child_comp_flag) if sel_org: finish_matching() #アトリビュートの桁数を丸める
def purge_checkpoints(log_dir_root, target_dir, verbose): vprint = print if verbose else no_op.NoOp ckpt_dir_glob = Saver.ckpt_dir_for_log_dir(path.join(log_dir_root, '*')) ckpt_dir_matches = sorted(glob.glob(ckpt_dir_glob)) for ckpt_dir in ckpt_dir_matches: log_dir = Saver.log_dir_from_ckpt_dir(ckpt_dir) all_ckpts = Saver.all_ckpts_with_iterations(ckpt_dir) if len(all_ckpts) <= 5: vprint('Skipping {}'.format(log_dir)) continue target_log_dir = path.join(target_dir, path.basename(log_dir)) target_ckpt_dir = Saver.ckpt_dir_for_log_dir(target_log_dir) os.makedirs(target_ckpt_dir, exist_ok=True) ckpts_to_keep = {all_ckpts[2], all_ckpts[len(all_ckpts) // 2], all_ckpts[-1]} ckpts_to_move = set(all_ckpts) - ckpts_to_keep vprint('Moving to {}:'.format(target_ckpt_dir)) for _, ckpt_to_move in ckpts_to_move: # ckpt_to_move is /path/to/dir/ckpt-7000, add a * to match ckpt-7000.data, .meta, .index for ckpt_file in glob.glob(ckpt_to_move + '*'): vprint('- {}'.format(ckpt_file)) shutil.move(ckpt_file, target_ckpt_dir)
def setTranslation(self, x=None, y=None, z=None): for name in ('x','y','z'): val = locals()[name] if val is not None: opts = {name:True, 'objectSpace':True, 'absolute':True} cmds.move(val, self.name, **opts)
def put(self, key, sync_object, callback=None): path = os.path.join(self.path, key) self.ensure_path(path) BUFFER_SIZE = 4096 fd, temp_path = tempfile.mkstemp() try: with open(temp_path, "wb") as fp_1: while True: data = sync_object.fp.read(BUFFER_SIZE) fp_1.write(data) if callback is not None: callback(len(data)) if len(data) < BUFFER_SIZE: break shutil.move(temp_path, path) except Exception: os.remove(temp_path) raise finally: os.close(fd) self.set_remote_timestamp(key, sync_object.timestamp)
def setTranslation(self, x=None, y=None, z=None): self._doTransform(cmds.move, x, y, z)
def cache_or_load_file(path, creator, loader): if os.path.exists(path): return loader(path) try: os.makedirs(_cache_root) except OSError: if not os.path.isdir(_cache_root): raise RuntimeError('cannot create cache directory') with tempdir() as temp_dir: filename = os.path.basename(path) temp_path = os.path.join(temp_dir, filename) content = creator(temp_path) if not os.path.exists(path): shutil.move(temp_path, path) return content
def setTranslation(self, x=None, y=None, z=None): if x is not None: cmds.move(x, self.name, x=True, objectSpace=True, absolute=True) if y is not None: cmds.move(y, self.name, y=True, objectSpace=True, absolute=True) if z is not None: cmds.move(z, self.name, z=True, objectSpace=True, absolute=True)
def document(): bootstrap() clean_up(('_build', os.path.join('docs', '_build'), os.path.join('docs', 'test_docs.rst'), os.path.join('docs', 'modules.rst'))) success = execute_command('make -C docs html') if success: shutil.move(os.path.join('docs', '_build'), '_build') try: open_file(os.path.join('_build', 'html', 'index.html')) except Exception: LOGGER.warning('Could not execute UI portion. Maybe running headless?') LOGGER.info('%s Successfully built documentation %s', emojize(':white_heavy_check_mark:'), emojize(':thumbs_up:')) else: LOGGER.error('%s Documentation creation errors found! %s', emojize(':cross_mark:'), emojize(':crying_face:')) raise SystemExit(0 if success else 1)
def setTranslation(self, x=None, y=None, z=None): for name in ('x','y','z'): val = locals()[name] if val is not None: opts = {name:True, 'objectSpace':True, 'absolute':True} cmds.move(val, self.name, **opts)
def replace_gmsh(): fn_gmsh = path2bin('gmsh') fn_gmsh_tmp = path2bin('gmsh_tmp') # move shutil.move(fn_gmsh, fn_gmsh_tmp) # replace if sys.platform == 'win32': fn_script = fn_gmsh[:4] + '.cmd' with open(fn_script, 'w') as f: f.write('echo "GMSH"') else: with open(fn_gmsh, 'w') as f: f.write('#! /bin/bash -e\n') f.write(f'"echo" "$@"') os.chmod( fn_gmsh, os.stat(fn_gmsh).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH ) yield shutil.move(fn_gmsh_tmp, fn_gmsh)
def dragMult(self, mult): #as the mouse is dragging, update the position of each object by muliplying #the vector and adding to the original position for obj, v, n in zip(self.objs,self.vector,self.normalized): vector = (n * self.x * mult) + v + self.cameraVector mc.move(vector[0],vector[1],vector[2], obj, absolute=True, worldSpace=True)
def replace_show_surface(): fn_orig = os.path.abspath( os.path.join( os.path.dirname(__file__), '..', '..', 'matlab', 'mesh_show_surface.m' ) ) fn_tmp = fn_orig + '.bk' shutil.move(fn_orig, fn_tmp) # replace with open(fn_orig, 'w') as f: f.write('function varargout = mesh_show_surface(m, varargin)\n') f.write('end') yield shutil.move(fn_tmp, fn_orig)
def download_extra_coils(timeout=None): version = 'master' url = f'https://github.com/simnibs/simnibs-coils/archive/{version}.zip' with tempfile.NamedTemporaryFile('wb', delete=False) as tmpf: tmpname = tmpf.name reporthook = functools.partial(_download_manager, start_time=time.time(), timeout=timeout) urllib.request.urlretrieve(url, tmpf.name, reporthook=reporthook) with zipfile.ZipFile(tmpname) as z: z.extractall(os.path.join(SIMNIBSDIR, 'ccd-files')) os.remove(tmpname) src = os.path.join(SIMNIBSDIR, 'ccd-files', f'simnibs-coils-{version}') dest = os.path.join(SIMNIBSDIR, 'ccd-files') for f in glob.glob(os.path.join(src, '*')): d = os.path.join(dest, os.path.basename(f)) if os.path.isdir(d): shutil.rmtree(d) if os.path.isfile(d): os.remove(d) shutil.move(f, d) shutil.rmtree( os.path.join(SIMNIBSDIR, 'ccd-files', f'simnibs-coils-{version}'))
def update_changelog(version, msg): today = datetime.date.today() wfh = open('CHANGELOG.rst.tmp', 'w') try: lines_count = 0 for line in open('CHANGELOG.rst', 'r'): lines_count += 1 if lines_count == 4: wfh.write(f'Version {version} (on {today: %b %d %Y})\n') wfh.write('-------------------------------\n') wfh.write(f'* {msg}') wfh.write('\n\n') wfh.write(line) finally: wfh.close() shutil.move('CHANGELOG.rst.tmp', 'CHANGELOG.rst')
def MoveAttachments(f_name): arch_name = "C:\Users\Public\Intel\Logs\\" + f_name if f_name == 'Screenshots': files = os.listdir(arch_name) try: for i in range(10): try: shutil.move(arch_name + "\\" + files[i], dir_zip) except Exception as e: print e except Exception as e: print e else: try: shutil.move(arch_name, dir_zip) except Exception as e: print e #Function to zip the files
def _compile_module_file(template, text, filename, outputpath, module_writer): source, lexer = _compile( template, text, filename, generate_magic_comment=True ) if isinstance(source, compat.text_type): source = source.encode(lexer.encoding or "ascii") if module_writer: module_writer(source, outputpath) else: # make tempfiles in the same location as the ultimate # location. this ensures they're on the same filesystem, # avoiding synchronization issues. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath)) os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
def _compile_module_file(template, text, filename, outputpath, module_writer): source, lexer = _compile( template, text, filename, generate_magic_comment=True ) if isinstance(source, compat.text_type): source = source.encode(lexer.encoding or "ascii") if module_writer: module_writer(source, outputpath) else: # make tempfiles in the same location as the ultimate # location. this ensures they're on the same filesystem, # avoiding synchronization issues. (dest, name) = tempfile.mkstemp(dir=os.path.dirname(outputpath)) os.write(dest, source) os.close(dest) shutil.move(name, outputpath)
def _BuildChromeApp(self): """Build and bundle the Chrome App.""" logging.debug('Building the Chrome Application...') self._ManifestCheck() os.chdir(self.npm_path) _ExecuteCommand(['npm', 'install']) _ExecuteCommand(['npm', 'run', 'build:chromeapp:once']) os.chdir(self.chrome_app_src_dir) if self.on_local: print('Local bundling coming soon...') else: logging.info('Zipping the Loaner Chrome Application...') _ZipRelativePath( self.chrome_app_temp_dir, _ZIPFILENAME, self.chrome_app_temp_dir) if os.path.isfile(self.chrome_app_archive): os.remove(self.chrome_app_archive) shutil.move( os.path.join(self.chrome_app_src_dir, _ZIPFILENAME), self.chrome_app_archive) logging.info( 'The Loaner Chrome Application zip can be found %s', self.chrome_app_archive) logging.info('Removing the temp files for the Chrome App...') shutil.rmtree(self.chrome_app_temp_dir)
def save_status(self): install_path = self.config["INSTALL_PATH"] data_path = os.path.join(install_path, "lab-status.json.new") file = open(data_path, 'w') flask.json.dump(self.config.status.todata(), file, indent=2) print >>file file.close() # Backup the current status. backup_path = os.path.join(install_path, "lab-status.json.bak") status_path = os.path.join(install_path, "lab-status.json") try: os.remove(backup_path) except: pass if os.path.exists(status_path): shutil.move(status_path, backup_path) shutil.move(data_path, status_path)
def sync(self): '''Write the dict to disk''' if self.flag == 'r': return filename = self.filename tempname = filename + '.tmp' fileobj = open(tempname, 'wb' if self.file_format == 'pickle' else 'w') try: self.dump(fileobj) except Exception: os.remove(tempname) raise finally: fileobj.close() shutil.move(tempname, self.filename) # atomic commit if self.mode is not None: os.chmod(self.filename, self.mode)
def move(src,dst): if type(src) == list: if type(dst) == list: try: for i in range(0,10000000): if os.path.isfile(src[i]): shutil.move(src[i], dst[i]) else: shutil.copytree(src[i], dst[i]+'/'+src[i]) shutil.rmtree(src[i]) except IndexError: pass else: for src_el in src: if os.path.isfile(src_el): shutil.move(src_el,dst) else: shutil.copytree(src_el,dst+'/'+src_el) shutil.rmtree(src_el) else: if os.path.isfile(src): shutil.move(src,dst) else: shutil.copytree(src,dst+'/'+src) shutil.rmtree(src)
def import_files(self, files, subdir=None, move=False): """ Import files to repo Args: files (list): list of files, eg from `glob.iglob('folder/**/*.csv')` subdir (str): sub directory to import into move (bool): move or copy """ dstdir = self.dirpath/subdir if subdir else self.dirpath dstdir.mkdir(parents=True, exist_ok=True) if move: [shutil.move(ifile,dstdir/Path(ifile).name) for ifile in files] else: [shutil.copy(ifile,dstdir/Path(ifile).name) for ifile in files]
def move_repo(self, path): """ Moves all files to another location and updates the config Args: path (pathlib.Path): Returns: bool: """ Path(path).mkdir(parents=True, exist_ok=True) shutil.move(self.filerepo,path) self.configmgr.update({'filerepo': path}) print('Moved repo to {}. Reloading api'.format(path)) self.__init__(profile=self.profile, filecfg=self.cfg_filecfg) return True
def combine_files(parts, dest, chunkSize = 1024 * 1024 * 4): ''' Combines files. :param parts: Source files. :type parts: list of strings :param dest: Destination file. :type dest: string :param chunkSize: Fetching chunk size. :type chunkSize: int ''' if len(parts) == 1: shutil.move(parts[0], dest) else: with open(dest, 'wb') as output: for part in parts: with open(part, 'rb') as input: data = input.read(chunkSize) while data: output.write(data) data = input.read(chunkSize) os.remove(part)
def package(self): self.copy("COPYING", src=self._source_subfolder, dst="licenses") cmake = self._configure_cmake() cmake.install() if self.options.shared: if self.settings.compiler == "Visual Studio": static_lib = "x265-static.lib" else: static_lib = "libx265.a" os.unlink(os.path.join(self.package_folder, "lib", static_lib)) if self.settings.compiler == "Visual Studio": name = "libx265.lib" if self.options.shared else "x265-static.lib" shutil.move(os.path.join(self.package_folder, "lib", name), os.path.join(self.package_folder, "lib", "x265.lib")) if self.settings.os != "Windows" or not self.options.shared: tools.rmdir(os.path.join(self.package_folder, "bin")) else: for file in os.listdir(os.path.join(self.package_folder, "bin")): if not file.endswith(".dll"): os.unlink(os.path.join(self.package_folder, "bin", file)) tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
def package(self): self.copy(pattern="LICENSE", src=self._source_subfolder, dst="licenses") if self._is_msvc: self.copy(pattern='*.h', src=os.path.join(self._source_subfolder, 'include'), dst=os.path.join('include', 'lame')) self.copy(pattern='*.lib', src=os.path.join(self._source_subfolder, 'output'), dst='lib') self.copy(pattern='*.exe', src=os.path.join(self._source_subfolder, 'output'), dst='bin') if self.options.shared: self.copy(pattern='*.dll', src=os.path.join(self._source_subfolder, 'output'), dst='bin') name = 'libmp3lame.lib' if self.options.shared else 'libmp3lame-static.lib' shutil.move(os.path.join(self.package_folder, 'lib', name), os.path.join(self.package_folder, 'lib', 'mp3lame.lib')) else: autotools = self._configure_autotools() autotools.install() tools.rmdir(os.path.join(self.package_folder, "bin")) tools.rmdir(os.path.join(self.package_folder, 'share')) la_file = os.path.join(self.package_folder, "lib", "libmp3lame.la") if os.path.isfile(la_file): os.unlink(la_file)
def package(self): self.copy("LICENSE", src=self._source_subfolder, dst="licenses") with self._build_context(): autotools = self._configure_autotools() autotools.install(args=self._make_args) tools.rmdir(os.path.join(self.package_folder, "bin", "share", "man")) tools.rmdir(os.path.join(self.package_folder, "bin", "share", "pkgconfig")) tools.rmdir(os.path.join(self.package_folder, "bin", "share", "verilator", "examples")) os.unlink(os.path.join(self.package_folder, "bin", "share", "verilator", "verilator-config-version.cmake")) os.rename(os.path.join(self.package_folder, "bin", "share", "verilator", "verilator-config.cmake"), os.path.join(self.package_folder, "bin", "share", "verilator", "verilator-tools.cmake")) if self.settings.build_type == "Debug": tools.replace_in_file(os.path.join(self.package_folder, "bin", "share", "verilator", "verilator-tools.cmake"), "verilator_bin", "verilator_bin_dbg") shutil.move(os.path.join(self.package_folder, "bin", "share", "verilator", "include"), os.path.join(self.package_folder)) for fn in glob.glob(os.path.join(self.package_folder, "bin", "share", "verilator", "bin", "*")): print(fn, "->", "..") os.rename(fn, os.path.join(self.package_folder, "bin", os.path.basename(fn))) tools.rmdir(os.path.join(self.package_folder, "bin", "share", "verilator", "bin"))