snakefood 改


上一小節提到 snakefood 可產生模組間兩兩關係的檔案。因此筆者撰寫一小程式讀取該檔案,並建立專案模組依存架構。

from colors import color

ref_list = []
ordered_list = []

with open("d.txt") as f:
    for line in f:
        nline = line.strip('\n').split(',')
        caller = nline[0]
        callee = nline[1].strip()
        ref_list.append([caller, callee])


# print module_list


def dive(i):
    # root_caller = ref_list[i][0].replace("'", "")
    root_callee = ref_list[i][1].replace("'", "")

    counter = 0

    for item in ref_list:
        ref_caller = item[0].replace("'", "")
        ref_callee = item[1].replace("'", "")

        if root_callee == ref_caller:
            if "viper" in ref_callee:
                ordered_list.append([ref_caller, ref_callee, 1])
                dive(counter)

            elif "modules" in ref_callee:
                ordered_list.append([ref_caller, ref_callee, 31])
                dive(counter)

        elif root_callee in ref_caller:
            if "viper" in ref_callee:
                ordered_list.append([ref_caller, ref_callee, 32])
            dive(counter)

        counter = counter + 1

dive(0)


# print ordered_call_list

# find dependencies

ordered_list_len = len(ordered_list)

for n in range(0, ordered_list_len):
    next_item_index = n + 1
    if next_item_index < ordered_list_len:
        root_callee = ordered_list[n][1]
        next_caller = ordered_list[n+1][0]
        if root_callee == next_caller:
            # make next_caller's color code 33
            ordered_list[n+1][2] = 33


# print ordered_list

for n in range(0, ordered_list_len):
    color_code = ordered_list[n][2]
    connection_found = 33
    packages_found = 32
    lower_bound = 0
    callee_to_inspect = n - 2

    caller = ordered_list[n][0]
    callee = ordered_list[n][1]
    previous_2_callee = ordered_list[n-2][1]

    connection = '\t\t\t-> {}'.format(callee)
    packages = '\t\t[p][m]{} \n\t\t\t-> {}'.format(caller, callee)
    another_dep_found = '\t\t\t[d]{}\n\t\t\t->{}'.format(caller, callee)
    normal = '[r]{}\n\t-> {}'.format(caller, callee)

    if color_code == connection_found:
        print color(connection, color_code)
    elif color_code == packages_found:
        print color(packages, color_code)
    else:
        # look for other dependencies in the same module
        # in viper, same module appears at previous 2 callee
        if callee_to_inspect > lower_bound:
            if caller == previous_2_callee:
                print color(another_dep_found, color_code)
            else:
                print color(normal, color_code)
        else:
print color(normal, color_code)

說在前頭,此程式還在非常早期且粗糙的版本。尚待筆者重構。在此先簡單說明程式邏輯:

  1. 讀取 snakefood 產生之 log 並將 caller 與 callee 關係存入 list。[caller, callee]。再將每一段關係存入一更大的 list ref_list。因此,log 處理完,我們便可看到 list 如 [[caller1, callee1], [caller2, callee2],...]。
  2. 接著撰寫一 dive() function,將 ref_list 中的 lists,依照呼叫順序重排,並標上 color code。身份不同,顏色不同。
  3. 尋找關聯,若有找到 dependency,則改變 color code 至 33。
  4. 依據處理結果,將 module dependency 印出。

印出的部份結果如下圖所示:

[r]:起始點

[p][m]:package module

[d]:同一隻程式內 import 了其他模組。例如 session.py import 了 objects.py 跟 out.py

顏色管理

綠色:package module

黃色:終點

results matching ""

    No results matching ""