File size: 1,457 Bytes
9fa5305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# -*- coding: utf-8 -*- 
# @Time : 2023/7/16 20:19 
# @Author : Yajing Zheng
# @Email: yj.zheng@pku.edu.cn
# @File : path.py
# here put the import lib
import os


def seek_file(search_dirs, filename):
    search_dir_split = split_path_into_pieces(search_dirs)
    dir_level = len(search_dir_split)

    for i_dir in range(0, dir_level):
        if i_dir > 0:
            search_dir_split.pop(-1)
        # search_dir = os.path.join(str(search_dir_split[0:-i_dir]))
        search_dir = os.path.join(*search_dir_split)
        for root, dirs, files in os.walk(search_dir):
            if filename in files:
                print('{0}/{1}'.format(root, filename))
                filepath = os.path.join(root, filename)
                return filepath


def split_path_into_pieces(path: str):
    pieces = []
    if path[-1] == '/':
        path = path[0:-1]

    while True:
        splits = os.path.split(path)
        if splits[0] == '':
            pieces.insert(0, splits[-1])
            break
        if splits[-1] == '':
            pieces.insert(0, splits[0])
            break
        pieces.insert(0, splits[-1])
        path = splits[0]

    return pieces

def replace_identifier(path: list, src: str, dst: str):
    new_path = []
    for piece in path:
        added_piece = piece
        if piece == src:
            added_piece = dst
        new_path.append(added_piece)

    return new_path