1f69ca293aceea89149a34ace5d026835d4e95d3
[integration/test.git] / tools / robot_check / tidytool.py
1 import os
2 import sys
3 import stat
4 import robot.tidy
5
6
7 def Error(FileSpec, Message):
8     global ErrorsReported
9     print "File", FileSpec + ":", Message
10     ErrorsReported = True
11
12
13 def traverse_and_process(DirList, Processor):
14     """Traverse the directory and process all .robot files found"""
15     Stack = []
16     for Dir in DirList:
17         if Dir[-1] == "/":
18             Dir = Dir[:-1]
19         Stack.append(Dir)
20     while len(Stack) > 0:
21         Dir = Stack.pop() + "/"
22         try:
23             List = os.listdir(Dir)
24         except (IOError, OSError), e:
25             Error(Dir, "Directory not accessible: " + str(e))
26             continue
27         for Item in List:
28             Spec = Dir + Item
29             Stat = os.stat(Spec)
30             if stat.S_ISDIR(Stat.st_mode):
31                 Stack.append(Spec)
32             elif Item.endswith(".robot"):
33                 Processor(Spec)
34
35
36 def check_quietly(FileSpec):
37     try:
38         Data = open(FileSpec).read()
39     except (IOError, OSError), e:
40         Error(FileSpec, "Not accessible: " + str(e))
41         return
42     TidyTool = robot.tidy.Tidy()
43     CleanedData = TidyTool.file(FileSpec)
44     if Data != CleanedData:
45         Error(FileSpec, "Found to be untidy")
46
47
48 def check(FileSpec):
49     Index = FileSpec.rfind("/")
50     FileName = FileSpec
51     if Index >= 0:
52         FileName = FileSpec[Index + 1:]
53     sys.stdout.write("  " + FileName + "\r")
54     sys.stdout.flush()
55     check_quietly(FileSpec)
56     sys.stdout.write(" " * (2 + len(FileName)) + "\r")
57
58
59 def tidy(FileSpec):
60     print "Processing file:", FileSpec
61     TidyTool = robot.tidy.Tidy()
62     try:
63         CleanedData = TidyTool.file(FileSpec)
64         open(FileSpec, "w").write(CleanedData)
65     except (IOError, OSError), e:
66         Error(FileSpec, "Not accessible: " + str(e))
67
68
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
73 #       scripts.
74
75
76 def usage():
77     print "Usage:\ttidytool.py <command>k <dir1> [<dir2> <dir3> ...]"
78     print
79     print "where <command> is one of these:"
80     print
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."
84     print
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."
88
89
90 if __name__ == "__main__":
91     if len(sys.argv) < 2:
92         usage()
93         raise SystemExit
94     Command = sys.argv[1]
95     DirList = sys.argv[2:]
96     if Command == "check":
97         Processor = check
98     elif Command == "quiet":
99         Processor = check_quietly
100     elif Command == "tidy":
101         Processor = tidy
102     else:
103         print "Unrecognized command:", Command
104         sys.exit(1)
105     ErrorsReported = False
106     traverse_and_process(DirList, Processor)
107     if ErrorsReported:
108         print "tidytool run FAILED !!!"
109         sys.exit(1)