summaryrefslogtreecommitdiff
path: root/src/rename-logic.py
blob: 6f6a4ec6a8fc3f8f594863dc8fc649befcca9df7 (plain)
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
#!/usr/bin/env python3

from os.path import dirname, basename, isfile, isdir, exists
from os import listdir
import sys

"""

This file simply implements the src_path to dst_path conversion logic
just for test. file_fill() and file_fill_recursive() in file.c
implements this logic.

"""


def recursive(src, rel_path, dst, dst_should_dir, replace_dir_name):
    
    if isfile(src):
        if dst_should_dir:
            print("{} => {}/{}{}".format(src, dst, rel_path, basename(src)))
        else:
            print("{} => {}{}".format(src, rel_path, dst))
        return
        
    # src is directory
    for f in listdir(src):
        next_src = "{}/{}".format(src, f)
        if replace_dir_name and dst_should_dir:
            next_rel_path = ""
        else:
            next_rel_path = "{}{}/".format(rel_path, basename(src))
        recursive(next_src, next_rel_path, dst, dst_should_dir, False)


def fill_dst(srclist, dst):
    dst_must_dir = len(srclist) > 1
    for src in srclist:
        dst_should_dir = isdir(src) | isdir(dst)
        replace_dir_name = not isdir(dst)
        recursive(src, "", dst, dst_should_dir | dst_must_dir, replace_dir_name)


def main():
    if (len(sys.argv) < 2):
        print("usage: {} source ... target".format(sys.argv[0]))
    fill_dst(sys.argv[1:len(sys.argv) - 1], sys.argv[len(sys.argv) - 1])

main()