tidyup 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #**************************************************************************
  4. # Copyright (C) 2011, Paul Lutus *
  5. # *
  6. # This program is free software; you can redistribute it and/or modify *
  7. # it under the terms of the GNU General Public License as published by *
  8. # the Free Software Foundation; either version 2 of the License, or *
  9. # (at your option) any later version. *
  10. # *
  11. # This program is distributed in the hope that it will be useful, *
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. # GNU General Public License for more details. *
  15. # *
  16. # You should have received a copy of the GNU General Public License *
  17. # along with this program; if not, write to the *
  18. # Free Software Foundation, Inc., *
  19. # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  20. #**************************************************************************
  21. import re, sys
  22. PVERSION = '1.0'
  23. class BeautifyBash:
  24. def __init__(self):
  25. self.tab_str = ' '
  26. self.tab_size = 4
  27. def read_file(self,fp):
  28. with open(fp) as f:
  29. return f.read()
  30. def write_file(self,fp,data):
  31. with open(fp,'w') as f:
  32. f.write(data)
  33. def beautify_string(self,data,path = ''):
  34. tab = 0
  35. case_stack = []
  36. in_here_doc = False
  37. defer_ext_quote = False
  38. in_ext_quote = False
  39. ext_quote_string = ''
  40. here_string = ''
  41. output = []
  42. line = 1
  43. for record in re.split('\n',data):
  44. record = record.rstrip()
  45. stripped_record = record.strip()
  46. # collapse multiple quotes between ' ... '
  47. test_record = re.sub(r'\'.*?\'','',stripped_record)
  48. # collapse multiple quotes between " ... "
  49. test_record = re.sub(r'".*?"','',test_record)
  50. # collapse multiple quotes between ` ... `
  51. test_record = re.sub(r'`.*?`','',test_record)
  52. # collapse multiple quotes between \` ... ' (weird case)
  53. test_record = re.sub(r'\\`.*?\'','',test_record)
  54. # strip out any escaped single characters
  55. test_record = re.sub(r'\\.','',test_record)
  56. # remove '#' comments
  57. test_record = re.sub(r'(\A|\s)(#.*)','',test_record,1)
  58. if(not in_here_doc):
  59. if(re.search('<<-?',test_record)):
  60. here_string = re.sub('.*<<-?\s*[\'|"]?([_|\w]+)[\'|"]?.*','\\1',stripped_record,1)
  61. in_here_doc = (len(here_string) > 0)
  62. if(in_here_doc): # pass on with no changes
  63. output.append(record)
  64. # now test for here-doc termination string
  65. if(re.search(here_string,test_record) and not re.search('<<',test_record)):
  66. in_here_doc = False
  67. else: # not in here doc
  68. if(in_ext_quote):
  69. if(re.search(ext_quote_string,test_record)):
  70. # provide line after quotes
  71. test_record = re.sub('.*%s(.*)' % ext_quote_string,'\\1',test_record,1)
  72. in_ext_quote = False
  73. else: # not in ext quote
  74. if(re.search(r'(\A|\s)(\'|")',test_record)):
  75. # apply only after this line has been processed
  76. defer_ext_quote = True
  77. ext_quote_string = re.sub('.*([\'"]).*','\\1',test_record,1)
  78. # provide line before quote
  79. test_record = re.sub('(.*)%s.*' % ext_quote_string,'\\1',test_record,1)
  80. if(in_ext_quote):
  81. # pass on unchanged
  82. output.append(record)
  83. else: # not in ext quote
  84. inc = len(re.findall('(\s|\A|;)(case|then|do)(;|\Z|\s)',test_record))
  85. inc += len(re.findall('(\{|\(|\[)',test_record))
  86. outc = len(re.findall('(\s|\A|;)(esac|fi|done|elif)(;|\)|\||\Z|\s)',test_record))
  87. outc += len(re.findall('(\}|\)|\])',test_record))
  88. if(re.search(r'\besac\b',test_record)):
  89. if(len(case_stack) == 0):
  90. sys.stderr.write(
  91. 'File %s: error: "esac" before "case" in line %d.\n' % (path,line)
  92. )
  93. else:
  94. outc += case_stack.pop()
  95. # sepcial handling for bad syntax within case ... esac
  96. if(len(case_stack) > 0):
  97. if(re.search('\A[^(]*\)',test_record)):
  98. # avoid overcount
  99. outc -= 2
  100. case_stack[-1] += 1
  101. if(re.search(';;',test_record)):
  102. outc += 1
  103. case_stack[-1] -= 1
  104. # an ad-hoc solution for the "else" keyword
  105. else_case = (0,-1)[re.search('^(else)',test_record) != None]
  106. net = inc - outc
  107. tab += min(net,0)
  108. extab = tab + else_case
  109. extab = max(0,extab)
  110. output.append((self.tab_str * self.tab_size * extab) + stripped_record)
  111. tab += max(net,0)
  112. if(defer_ext_quote):
  113. in_ext_quote = True
  114. defer_ext_quote = False
  115. if(re.search(r'\bcase\b',test_record)):
  116. case_stack.append(0)
  117. line += 1
  118. error = (tab != 0)
  119. if(error):
  120. sys.stderr.write('File %s: error: indent/outdent mismatch: %d.\n' % (path,tab))
  121. return '\n'.join(output), error
  122. def beautify_file(self,path):
  123. error = False
  124. if(path == '-'):
  125. data = sys.stdin.read()
  126. result,error = self.beautify_string(data,'(stdin)')
  127. sys.stdout.write(result)
  128. else: # named file
  129. data = self.read_file(path)
  130. result,error = self.beautify_string(data,path)
  131. if(data != result):
  132. self.write_file(path,result)
  133. return error
  134. def main(self):
  135. error = False
  136. sys.argv.pop(0)
  137. if(len(sys.argv) < 1):
  138. sys.stderr.write('usage: shell script filenames or \"-\" for stdin.\n')
  139. else:
  140. for path in sys.argv:
  141. error |= self.beautify_file(path)
  142. sys.exit((0,1)[error])
  143. # if not called as a module
  144. if(__name__ == '__main__'):
  145. BeautifyBash().main()