7 def Error(FileSpec, Message):
9 print "File", FileSpec + ":", Message
13 def traverse_and_process(DirList, Processor):
14 """Traverse the directory and process all .robot files found"""
21 Dir = Stack.pop() + "/"
23 List = os.listdir(Dir)
24 except (IOError, OSError), e:
25 Error(Dir, "Directory not accessible: " + str(e))
30 if stat.S_ISDIR(Stat.st_mode):
32 elif Item.endswith(".robot"):
36 def check_quietly(FileSpec):
38 Data = open(FileSpec).read()
39 except (IOError, OSError), e:
40 Error(FileSpec, "Not accessible: " + str(e))
42 TidyTool = robot.tidy.Tidy()
43 CleanedData = TidyTool.file(FileSpec)
44 if Data != CleanedData:
45 Error(FileSpec, "Found to be untidy")
49 Index = FileSpec.rfind("/")
52 FileName = FileSpec[Index + 1:]
53 sys.stdout.write(" " + FileName + "\r")
55 check_quietly(FileSpec)
56 sys.stdout.write(" " * (2 + len(FileName)) + "\r")
60 print "Processing file:", FileSpec
61 TidyTool = robot.tidy.Tidy()
63 CleanedData = TidyTool.file(FileSpec)
64 open(FileSpec, "w").write(CleanedData)
65 except (IOError, OSError), e:
66 Error(FileSpec, "Not accessible: " + str(e))
69 # TODO: Refactor the command line argument parsing to use argparse. Since I
70 # wanted to just quickly make this tool to get rid of manual robot.tidy
71 # runs I did not have time to create polished argparse based command
72 # line argument parsing code. Remember also to update the convenience
77 print "Usage:\ttidytool.py <command>k <dir1> [<dir2> <dir3> ...]"
79 print "where <command> is one of these:"
81 print "check\tCheck that the Robot test data is tidy."
82 print "quiet\tCheck quietly that the Robot test data is tidy."
83 print "tidy\tTidy the Robot test data."
85 print "The program traverses the specified directories, searching for"
86 print "Robot test data (.robot files) and performing the specified"
87 print "command on them."
90 if __name__ == "__main__":
95 DirList = sys.argv[2:]
96 if Command == "check":
98 elif Command == "quiet":
99 Processor = check_quietly
100 elif Command == "tidy":
103 print "Unrecognized command:", Command
105 ErrorsReported = False
106 traverse_and_process(DirList, Processor)
108 print "tidytool run FAILED !!!"