1*7f2fe78bSCy Schubertfrom optparse import OptionParser 2*7f2fe78bSCy Schubertimport os 3*7f2fe78bSCy Schubertimport re 4*7f2fe78bSCy Schubertimport sys 5*7f2fe78bSCy Schubert 6*7f2fe78bSCy Schubertstyles = { 7*7f2fe78bSCy Schubert "bsd": 8*7f2fe78bSCy Schubert "/* -*- mode: c; c-file-style: \"bsd\"; indent-tabs-mode: t -*- */\n", 9*7f2fe78bSCy Schubert "krb5": 10*7f2fe78bSCy Schubert "/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */\n" 11*7f2fe78bSCy Schubert } 12*7f2fe78bSCy Schubert 13*7f2fe78bSCy Schubertdef dofile(fname, style): 14*7f2fe78bSCy Schubert changed = False 15*7f2fe78bSCy Schubert newname = fname + ".new" 16*7f2fe78bSCy Schubert infile = open(fname) 17*7f2fe78bSCy Schubert outfile = open(newname, "w") 18*7f2fe78bSCy Schubert first = next(infile) 19*7f2fe78bSCy Schubert if (first != style): 20*7f2fe78bSCy Schubert changed = True 21*7f2fe78bSCy Schubert outfile.write(style) 22*7f2fe78bSCy Schubert if re.match(r"""\s*/\*\s*-\*-.*-\*-\s*\*/""", first): 23*7f2fe78bSCy Schubert # Replace first line if it was already a local variables line. 24*7f2fe78bSCy Schubert pass 25*7f2fe78bSCy Schubert else: 26*7f2fe78bSCy Schubert outfile.write(first) 27*7f2fe78bSCy Schubert 28*7f2fe78bSCy Schubert # Simply copy remaining lines. 29*7f2fe78bSCy Schubert for line in infile: 30*7f2fe78bSCy Schubert outfile.write(line) 31*7f2fe78bSCy Schubert 32*7f2fe78bSCy Schubert infile.close() 33*7f2fe78bSCy Schubert outfile.close() 34*7f2fe78bSCy Schubert 35*7f2fe78bSCy Schubert if changed: 36*7f2fe78bSCy Schubert os.rename(newname, fname) 37*7f2fe78bSCy Schubert else: 38*7f2fe78bSCy Schubert os.remove(newname) 39*7f2fe78bSCy Schubert 40*7f2fe78bSCy Schubertparser = OptionParser() 41*7f2fe78bSCy Schubertparser.add_option("--cstyle", action="store", dest="style", 42*7f2fe78bSCy Schubert choices=("bsd", "krb5"), default="krb5") 43*7f2fe78bSCy Schubert(options, args) = parser.parse_args() 44*7f2fe78bSCy Schubert 45*7f2fe78bSCy Schubertfor fname in args: 46*7f2fe78bSCy Schubert print(fname) 47*7f2fe78bSCy Schubert dofile(fname, styles[options.style]) 48