All includes are relative now #147 #164

Merged
muellerr merged 2 commits from gaisser/fsfw:gaisser_relative_paths into master 2020-08-18 12:07:41 +02:00
Owner

Fixes issue #147 Relative paths instead of absolut ones. The framework does not need to be in a specific folder.

BUT, a config folder is still needed at the moment!

Fixes issue #147 Relative paths instead of absolut ones. The framework does not need to be in a specific folder. BUT, a config folder is still needed at the moment!
gaisser added the
feature
label 2020-08-13 21:10:45 +02:00
Owner

Is there a sed script to apply these changes?

Is there a sed script to apply these changes?
Author
Owner

No, its super hacky python.

import os


def read_all_files(folder):
    for file in os.listdir(folder):
        if file in [".git", ".idea", ".project"]:
            continue
        if ".py" in file:
            continue
        if os.path.isdir(os.path.join(folder, file)):
            read_all_files(os.path.join(folder, file))
            continue
        print(os.path.join(folder, file))
        write = False
        with open(os.path.join(folder, file), "r") as in_file:
            rows = [x for x in in_file]
            for index, row in enumerate(rows):
                if "#include" in row:
                    if folder[2:] in row:
                        if "<config/" in row:
                            continue
                        write = True
                        row = row.replace(">", "\"")
                        row = row.replace("<framework", "\"")
                        row = row.replace(folder[1:] + "/", "")
                        rows[index] = row
                        print(row)
                    elif "<framework" in row:
                        how_deep = folder[2:].count("/") + 1
                        write = True
                        row = row.replace(">", "\"")
                        str = [".." for i in range(how_deep)]
                        dots = "/".join(str)
                        row = row.replace("<framework", "\"" + dots)
                        rows[index] = row
                        print(row)
        if write:
            with open(os.path.join(folder, file), "w") as in_file:
                print("Write file %s" % (os.path.join(folder, file)))
                in_file.writelines(rows)


read_all_files(".")

No, its super hacky python. ``` python import os def read_all_files(folder): for file in os.listdir(folder): if file in [".git", ".idea", ".project"]: continue if ".py" in file: continue if os.path.isdir(os.path.join(folder, file)): read_all_files(os.path.join(folder, file)) continue print(os.path.join(folder, file)) write = False with open(os.path.join(folder, file), "r") as in_file: rows = [x for x in in_file] for index, row in enumerate(rows): if "#include" in row: if folder[2:] in row: if "<config/" in row: continue write = True row = row.replace(">", "\"") row = row.replace("<framework", "\"") row = row.replace(folder[1:] + "/", "") rows[index] = row print(row) elif "<framework" in row: how_deep = folder[2:].count("/") + 1 write = True row = row.replace(">", "\"") str = [".." for i in range(how_deep)] dots = "/".join(str) row = row.replace("<framework", "\"" + dots) rows[index] = row print(row) if write: with open(os.path.join(folder, file), "w") as in_file: print("Write file %s" % (os.path.join(folder, file))) in_file.writelines(rows) read_all_files(".") ```
gaisser added the
API Change
label 2020-08-18 12:07:01 +02:00
muellerr was assigned by gaisser 2020-08-18 12:07:08 +02:00
muellerr closed this pull request 2020-08-18 12:07:41 +02:00
Owner

Thanks!

Variation to select folders explicitely (in my case, only framework and config folder):

import os


def main():
    read_all_files(".", True)


def read_all_files(folder, root_folder: bool):
    for file in os.listdir(folder):
        if file in [".git", ".idea", ".project", ".settings"]:
            continue
        if ".py" in file:
            continue
        if file not in ["framework", "config"] and root_folder:
            continue
        if os.path.isdir(os.path.join(folder, file)):
            read_all_files(os.path.join(folder, file), False)
            continue
        print(os.path.join(folder, file))
        write = False
        with open(os.path.join(folder, file), "r") as in_file:
            rows = [x for x in in_file]
            for index, row in enumerate(rows):
                if "#include" in row:
                    if folder[2:] in row:
                        if "<config/" in row:
                            continue
                        write = True
                        row = row.replace(">", "\"")
                        row = row.replace("<framework", "\"")
                        row = row.replace(folder[1:] + "/", "")
                        rows[index] = row
                        print(row)
                    elif "<framework" in row:
                        how_deep = folder[2:].count("/") + 1
                        write = True
                        row = row.replace(">", "\"")
                        str = [".." for i in range(how_deep)]
                        dots = "/".join(str)
                        row = row.replace("<framework", "\"" + dots)
                        rows[index] = row
                        print(row)
        if write:
            with open(os.path.join(folder, file), "w") as in_file:
                print("Write file %s" % (os.path.join(folder, file)))
                in_file.writelines(rows)


read_all_files(".", True)

Somehow still has issues for nested directories for me..

Thanks! Variation to select folders explicitely (in my case, only framework and config folder): ``` import os def main(): read_all_files(".", True) def read_all_files(folder, root_folder: bool): for file in os.listdir(folder): if file in [".git", ".idea", ".project", ".settings"]: continue if ".py" in file: continue if file not in ["framework", "config"] and root_folder: continue if os.path.isdir(os.path.join(folder, file)): read_all_files(os.path.join(folder, file), False) continue print(os.path.join(folder, file)) write = False with open(os.path.join(folder, file), "r") as in_file: rows = [x for x in in_file] for index, row in enumerate(rows): if "#include" in row: if folder[2:] in row: if "<config/" in row: continue write = True row = row.replace(">", "\"") row = row.replace("<framework", "\"") row = row.replace(folder[1:] + "/", "") rows[index] = row print(row) elif "<framework" in row: how_deep = folder[2:].count("/") + 1 write = True row = row.replace(">", "\"") str = [".." for i in range(how_deep)] dots = "/".join(str) row = row.replace("<framework", "\"" + dots) rows[index] = row print(row) if write: with open(os.path.join(folder, file), "w") as in_file: print("Write file %s" % (os.path.join(folder, file))) in_file.writelines(rows) read_all_files(".", True) ``` Somehow still has issues for nested directories for me..
Owner
# Move this file into root folder of project !
import os


def main():
    read_all_files(".", True)


def read_all_files(folder, root_folder: bool, nesting_depth: int = 0):
    for file in os.listdir(folder):
        if file in [".git", ".idea", ".project", ".settings"]:
            continue
        if ".py" in file:
            continue
        if file not in ["framework"] and root_folder:
            continue
        if os.path.isdir(os.path.join(folder, file)):
            read_all_files(os.path.join(folder, file), False, nesting_depth + 1)
            continue
        print(os.path.join(folder, file))
        write = False
        with open(os.path.join(folder, file), "r") as in_file:
            rows = [x for x in in_file]
            for index, row in enumerate(rows):
                if "#include" in row:
                    if folder[2:] in row:
                        if "<config/" in row:
                            continue
                        write = True
                        row = row.replace(">", "\"")
                        row = row.replace("<framework", "\"")
                        row = row.replace(folder[1:] + "/", "")
                        rows[index] = row
                        print("Replacement 1" + str(row))
                    elif "<framework" in row:
                        how_deep = nesting_depth - 1
                        write = True
                        row = row.replace(">", "\"")
                        string = [".." for i in range(how_deep)]
                        dots = "/".join(string)
                        row = row.replace("<framework", "\"" + dots)
                        rows[index] = row
                        print("Replacement 2: " + str(row))
        if write:
            with open(os.path.join(folder, file), "w") as in_file:
                print("Write file %s" % (os.path.join(folder, file)))
                in_file.writelines(rows)


read_all_files(".", True)

This worked for me, had to be in root folder of project though

``` # Move this file into root folder of project ! import os def main(): read_all_files(".", True) def read_all_files(folder, root_folder: bool, nesting_depth: int = 0): for file in os.listdir(folder): if file in [".git", ".idea", ".project", ".settings"]: continue if ".py" in file: continue if file not in ["framework"] and root_folder: continue if os.path.isdir(os.path.join(folder, file)): read_all_files(os.path.join(folder, file), False, nesting_depth + 1) continue print(os.path.join(folder, file)) write = False with open(os.path.join(folder, file), "r") as in_file: rows = [x for x in in_file] for index, row in enumerate(rows): if "#include" in row: if folder[2:] in row: if "<config/" in row: continue write = True row = row.replace(">", "\"") row = row.replace("<framework", "\"") row = row.replace(folder[1:] + "/", "") rows[index] = row print("Replacement 1" + str(row)) elif "<framework" in row: how_deep = nesting_depth - 1 write = True row = row.replace(">", "\"") string = [".." for i in range(how_deep)] dots = "/".join(string) row = row.replace("<framework", "\"" + dots) rows[index] = row print("Replacement 2: " + str(row)) if write: with open(os.path.join(folder, file), "w") as in_file: print("Write file %s" % (os.path.join(folder, file))) in_file.writelines(rows) read_all_files(".", True) ``` This worked for me, had to be in root folder of project though
gaisser deleted branch gaisser_relative_paths 2020-08-18 19:29:34 +02:00
Sign in to join this conversation.
No description provided.