-
Notifications
You must be signed in to change notification settings - Fork 9
/
BatchTidy.py
181 lines (146 loc) · 4.34 KB
/
BatchTidy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/python
# -*- coding: utf-8 -*-
# BatchTidy.py
# Kevin Horton . 2008 Mar 27
"""Run pythontidy on globbed list of arguments, making backups.
For example:
> ./BatchTidy --suffix=\"~\" *.py
"""
# 2009 Oct 27 . ccr . Call PythonTidy through PythonTidyWrapper.
# . Skip copy if backup already exists.
# . Abort on error.
# . Add option to print shell script rather than run
# . tidy in real time.
from __future__ import division
import os
import sys
from optparse import OptionParser
ZERO = 0
SPACE = ' '
NULL = ''
NUL = '\x00'
NA = -1
PROC_PATH = os.path.split(sys.argv[ZERO])[ZERO]
PROC = os.path.join(PROC_PATH, 'PythonTidyWrapper.py')
def parse_options():
"""Parse the command line options.
"""
global OPTIONS, ARGS
usage = 'usage: %prog [options] arg'
parser = OptionParser(usage=usage)
parser.add_option(
'-d',
'--dry_run',
action='store_true',
dest='dry_run',
default=False,
help='dry_run mode. Sends output to stdout.',
)
parser.add_option(
'-i',
'--in_place',
action='store_true',
dest='in_place',
default=False,
help='modify files in place mode. Each file is overwritten with no backup.',
)
parser.add_option( # 2009 Oct 27
'-r',
'--restore',
action='store_true',
dest='restore',
default=False,
help='restore mode. Replace files with their untidy backups.',
)
parser.add_option(
'-s',
'--suffix',
dest='suffix',
default='.bak',
help='suffix for file backup. (Defaults to ".bak")',
)
parser.add_option( # 2009 Oct 27
'-l',
'--list_only',
action='store_true',
dest='list_only',
default=False,
help='list mode. Generate a shellscript on stdout.',
)
parser.add_option( # 2009 Oct 27
'-u',
'--ini_file',
dest='ini_file',
default=None,
help='PythonTidyWrapper ini_file.',
)
(OPTIONS, ARGS) = parser.parse_args()
if len(ARGS) < 1:
parser.error('Please specify the files to tidy.')
return
class Process(object): # 2009 Oct 27
def __init__(self, command):
self.command = command
return
def run(self):
print self.command
if OPTIONS.list_only:
pass
else:
result = os.system(self.command)
if result is ZERO:
pass
else:
print '#Error: Command failed.'
sys.exit(0x10)
return self
class ProcessTidy(Process):
def __init__(self, *parms):
parms = list(parms)
if OPTIONS.ini_file is None:
pass
else:
ini_file = '"%s"' % OPTIONS.ini_file
parms.insert(ZERO, ini_file)
parms.insert(ZERO, '-u')
parms.insert(ZERO, PROC)
Process.__init__(self, SPACE.join(parms))
return
class ProcessMakeExecutable(Process):
def __init__(self, file_name):
Process.__init__(self, 'chmod +x %s' % file_name)
return
class ProcessBackup(Process):
def __init__(self, file_name, bu_name):
Process.__init__(
self,
'if test ! -e %s; then cp -a %s %s; fi' % (bu_name, file_name, bu_name),
)
return
class ProcessRestore(Process):
def __init__(self, file_name, bu_name):
Process.__init__(self, 'mv %s %s' % (bu_name, file_name))
return
def tidy():
"""Run pythontidy on the specified files.
"""
for arg in ARGS:
arg_quote = '"%s"' % arg
if OPTIONS.dry_run:
ProcessTidy(arg_quote).run() # Results to stdout.
elif OPTIONS.in_place:
ProcessTidy(arg_quote, arg_quote).run()
ProcessMakeExecutable(arg_quote).run()
elif OPTIONS.restore:
bu_file = '"%s%s"' % (arg, OPTIONS.suffix)
ProcessRestore(arg_quote, bu_file).run()
else:
bu_file = '"%s%s"' % (arg, OPTIONS.suffix)
ProcessBackup(arg_quote, bu_file).run()
ProcessTidy(arg_quote, arg_quote).run()
ProcessMakeExecutable(arg_quote).run()
return
if __name__ == '__main__':
parse_options()
tidy()
# Fin!