Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. import os
  2. import argparse
  3.  
  4.  
  5. DELIM = '='
  6.  
  7.  
  8. def replace_in_files(src, dst, replaces):
  9.     for root, dirs, files in os.walk(src):
  10.         for fname in files:
  11.             full_src_path = os.path.join(root, fname)
  12.             relpath = os.path.relpath(full_src_path, src)
  13.             full_dst_path = os.path.join(dst, relpath)
  14.  
  15.             with open(full_src_path) as f:
  16.                 data = f.read()
  17.            
  18.             for src, dst in replaces.items():
  19.                 data = data.replace(src, dst)
  20.            
  21.             dirname, _ = os.path.split(full_dst_path)
  22.             os.makedirs(dirname)
  23.  
  24.             with open(full_dst_path, 'w') as f:
  25.                 f.write(data)
  26.  
  27. def main():
  28.     parser = argparse.ArgumentParser()
  29.     parser.add_argument('--fname', help='fname')
  30.     parser.add_argument('--src_path', help='src_path')
  31.     parser.add_argument('--dst_path', help='src_path')
  32.  
  33.     args = parser.parse_args()
  34.  
  35.     replaces = {}
  36.     for line in open(args.fname):
  37.         src, dst = [s.strip() for s in line.split(DELIM)]
  38.         replaces[src] = dst
  39.  
  40.     replace_in_files(args.src_path, args.dst_path, replaces)
  41.  
  42.  
  43. if __name__ == '__main__':
  44.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement