devtools: add script to ease backport of renamed files
[dpdk.git] / devtools / update-patches.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: BSD-3-Clause
3 # Copyright(c) 2021 Intel Corporation
4
5 import os
6 import sys
7 import shutil
8 from os.path import abspath, dirname, join
9
10 def fixup_library_renames(contents):
11     """since library directory names have dropped the 'librte_' prefix,
12     add those prefixes back in for patches than need it"""
13     modified = False
14
15     # first get all the DPDK libs to build up replacement list
16     # stored in function attribute between calls
17     try:
18         libdirs = fixup_library_renames.libdirs
19     except AttributeError:
20         dpdk_libdir = abspath(join(dirname(sys.argv[0]), '..', 'lib'))
21         for root, dirs, files in os.walk(dpdk_libdir):
22             fixup_library_renames.libdirs = dirs
23             libdirs = dirs
24             break
25
26     for i in range(len(contents)):
27         # skip over any lines which don't have lib in it
28         if not "lib/" in contents[i]:
29             continue
30         for d in libdirs:
31             if f'lib/{d}' in contents[i]:
32                 modified = True
33                 contents[i] = contents[i].replace(f'lib/{d}', f'lib/librte_{d}')
34     return modified
35
36 def main():
37     "takes list of patches off argv and processes each"
38     for patch in sys.argv[1:]:
39         modified = False
40         with open(patch) as f:
41             contents = f.readlines()
42
43         modified |= fixup_library_renames(contents)
44         # other functions to change the patch go here
45
46         if not modified:
47             continue
48         shutil.copyfile(f'{patch}', f'{patch}.bak')
49         with open(patch, 'w') as f:
50             f.writelines(contents)
51
52 if __name__ == "__main__":
53     main()