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))
43 Data = Data.decode("utf8")
45 Error(FileSpec, "Has invalid UTF8 data")
47 TidyTool = robot.tidy.Tidy()
48 CleanedData = TidyTool.file(FileSpec)
49 if Data != CleanedData:
50 Error(FileSpec, "Found to be untidy")
54 Index = FileSpec.rfind("/")
57 FileName = FileSpec[Index + 1:]
58 sys.stdout.write(" " + FileName + "\r")
60 check_quietly(FileSpec)
61 sys.stdout.write(" " * (2 + len(FileName)) + "\r")
65 print "Processing file:", FileSpec
66 TidyTool = robot.tidy.Tidy()
68 CleanedData = TidyTool.file(FileSpec)
69 CleanedData = CleanedData.encode("utf8")
70 open(FileSpec, "w").write(CleanedData)
71 except (IOError, OSError), e:
72 Error(FileSpec, "Not accessible: " + str(e))
75 # TODO: Refactor the command line argument parsing to use argparse. Since I
76 # wanted to just quickly make this tool to get rid of manual robot.tidy
77 # runs I did not have time to create polished argparse based command
78 # line argument parsing code. Remember also to update the convenience
83 print "Usage:\ttidytool.py <command>k <dir1> [<dir2> <dir3> ...]"
85 print "where <command> is one of these:"
87 print "check\tCheck that the Robot test data is tidy."
88 print "quiet\tCheck quietly that the Robot test data is tidy."
89 print "tidy\tTidy the Robot test data."
91 print "The program traverses the specified directories, searching for"
92 print "Robot test data (.robot files) and performing the specified"
93 print "command on them."
96 if __name__ == "__main__":
100 Command = sys.argv[1]
101 DirList = sys.argv[2:]
102 if Command == "check":
104 elif Command == "quiet":
105 Processor = check_quietly
106 elif Command == "tidy":
109 print "Unrecognized command:", Command
111 ErrorsReported = False
112 traverse_and_process(DirList, Processor)
114 print "tidytool run FAILED !!!"