123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- import re, sys
-
- PVERSION = '1.0'
-
- class BeautifyBash:
-
- def __init__(self):
- self.tab_str = ' '
- self.tab_size = 4
-
- def read_file(self,fp):
- with open(fp) as f:
- return f.read()
-
- def write_file(self,fp,data):
- with open(fp,'w') as f:
- f.write(data)
-
- def beautify_string(self,data,path = ''):
- tab = 0
- case_stack = []
- in_here_doc = False
- defer_ext_quote = False
- in_ext_quote = False
- ext_quote_string = ''
- here_string = ''
- output = []
- line = 1
- for record in re.split('\n',data):
- record = record.rstrip()
- stripped_record = record.strip()
-
-
- test_record = re.sub(r'\'.*?\'','',stripped_record)
-
- test_record = re.sub(r'".*?"','',test_record)
-
- test_record = re.sub(r'`.*?`','',test_record)
-
- test_record = re.sub(r'\\`.*?\'','',test_record)
-
- test_record = re.sub(r'\\.','',test_record)
-
- test_record = re.sub(r'(\A|\s)(#.*)','',test_record,1)
- if(not in_here_doc):
- if(re.search('<<-?',test_record)):
- here_string = re.sub('.*<<-?\s*[\'|"]?([_|\w]+)[\'|"]?.*','\\1',stripped_record,1)
- in_here_doc = (len(here_string) > 0)
- if(in_here_doc):
- output.append(record)
-
- if(re.search(here_string,test_record) and not re.search('<<',test_record)):
- in_here_doc = False
- else:
- if(in_ext_quote):
- if(re.search(ext_quote_string,test_record)):
-
- test_record = re.sub('.*%s(.*)' % ext_quote_string,'\\1',test_record,1)
- in_ext_quote = False
- else:
- if(re.search(r'(\A|\s)(\'|")',test_record)):
-
- defer_ext_quote = True
- ext_quote_string = re.sub('.*([\'"]).*','\\1',test_record,1)
-
- test_record = re.sub('(.*)%s.*' % ext_quote_string,'\\1',test_record,1)
- if(in_ext_quote):
-
- output.append(record)
- else:
- inc = len(re.findall('(\s|\A|;)(case|then|do)(;|\Z|\s)',test_record))
- inc += len(re.findall('(\{|\(|\[)',test_record))
- outc = len(re.findall('(\s|\A|;)(esac|fi|done|elif)(;|\)|\||\Z|\s)',test_record))
- outc += len(re.findall('(\}|\)|\])',test_record))
- if(re.search(r'\besac\b',test_record)):
- if(len(case_stack) == 0):
- sys.stderr.write(
- 'File %s: error: "esac" before "case" in line %d.\n' % (path,line)
- )
- else:
- outc += case_stack.pop()
-
- if(len(case_stack) > 0):
- if(re.search('\A[^(]*\)',test_record)):
-
- outc -= 2
- case_stack[-1] += 1
- if(re.search(';;',test_record)):
- outc += 1
- case_stack[-1] -= 1
-
- else_case = (0,-1)[re.search('^(else)',test_record) != None]
- net = inc - outc
- tab += min(net,0)
- extab = tab + else_case
- extab = max(0,extab)
- output.append((self.tab_str * self.tab_size * extab) + stripped_record)
- tab += max(net,0)
- if(defer_ext_quote):
- in_ext_quote = True
- defer_ext_quote = False
- if(re.search(r'\bcase\b',test_record)):
- case_stack.append(0)
- line += 1
- error = (tab != 0)
- if(error):
- sys.stderr.write('File %s: error: indent/outdent mismatch: %d.\n' % (path,tab))
- return '\n'.join(output), error
-
- def beautify_file(self,path):
- error = False
- if(path == '-'):
- data = sys.stdin.read()
- result,error = self.beautify_string(data,'(stdin)')
- sys.stdout.write(result)
- else:
- data = self.read_file(path)
- result,error = self.beautify_string(data,path)
- if(data != result):
- self.write_file(path,result)
- return error
-
- def main(self):
- error = False
- sys.argv.pop(0)
- if(len(sys.argv) < 1):
- sys.stderr.write('usage: shell script filenames or \"-\" for stdin.\n')
- else:
- for path in sys.argv:
- error |= self.beautify_file(path)
- sys.exit((0,1)[error])
-
-
- if(__name__ == '__main__'):
- BeautifyBash().main()
|