From 443592136b75509a93ee81d99bff7f760634b2bb Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Tue, 19 Jan 2016 08:41:35 +0100 Subject: [PATCH] px_romfs_pruner.py: PEP8ify and whitespace - Changed from 8 spaces indent back to 4 which I find appropriate for Python. - Fixed linelength to 80 chars. --- Tools/px_romfs_pruner.py | 71 +++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/Tools/px_romfs_pruner.py b/Tools/px_romfs_pruner.py index f77731be9c..b0235e577b 100644 --- a/Tools/px_romfs_pruner.py +++ b/Tools/px_romfs_pruner.py @@ -1,8 +1,7 @@ #!/usr/bin/env python ############################################################################ # -# Copyright (C) 2014 PX4 Development Team. All rights reserved. -# Author: Julian Oes +# Copyright (C) 2014-2015 PX4 Development Team. All rights reserved. # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions @@ -37,49 +36,53 @@ """ px_romfs_pruner.py: Delete all comments and newlines before ROMFS is converted to an image + +@author: Julian Oes """ from __future__ import print_function -import argparse, re +import argparse +import re import os def main(): + # Parse commandline arguments + parser = argparse.ArgumentParser(description="ROMFS pruner.") + parser.add_argument('--folder', action="store", + help="ROMFS scratch folder.") + args = parser.parse_args() - # Parse commandline arguments - parser = argparse.ArgumentParser(description="ROMFS pruner.") - parser.add_argument('--folder', action="store", help="ROMFS scratch folder.") - args = parser.parse_args() + print("Pruning ROMFS files.") - print("Pruning ROMFS files.") + # go through + for (root, dirs, files) in os.walk(args.folder): + for file in files: + # only prune text files + if ".zip" in file or ".bin" in file or ".swp" in file \ + or ".data" in file or ".DS_Store" in file \ + or file.startswith("."): + continue - # go through - for (root, dirs, files) in os.walk(args.folder): - for file in files: - # only prune text files - if ".zip" in file or ".bin" in file or ".swp" in file or ".data" in file or \ - ".DS_Store" in file or file.startswith("."): - continue + file_path = os.path.join(root, file) - file_path = os.path.join(root, file) - - # read file line by line - pruned_content = "" - with open(file_path, "rU") as f: - for line in f: - - # handle mixer files differently than startup files - if file_path.endswith(".mix"): - if line.startswith(("Z:", "M:", "R: ", "O:", "S:")): - pruned_content += line - else: - if not line.isspace() and not line.strip().startswith("#"): - pruned_content += line - # overwrite old scratch file - with open(file_path, "wb") as f: - pruned_content = re.sub("\r\n", "\n", pruned_content) - f.write(pruned_content.encode("ascii", errors='strict')) + # read file line by line + pruned_content = "" + with open(file_path, "rU") as f: + for line in f: + # handle mixer files differently than startup files + if file_path.endswith(".mix"): + if line.startswith(("Z:", "M:", "R: ", "O:", "S:")): + pruned_content += line + else: + if not line.isspace() \ + and not line.strip().startswith("#"): + pruned_content += line + # overwrite old scratch file + with open(file_path, "wb") as f: + pruned_content = re.sub("\r\n", "\n", pruned_content) + f.write(pruned_content.encode("ascii", errors='strict')) if __name__ == '__main__': - main() + main()