5
5
"""
6
6
from __future__ import with_statement
7
7
8
+ import logging
9
+ from optparse import OptionParser
8
10
import os
9
11
import re
10
12
import shutil
11
13
import sys
12
14
13
- src , dest = sys .argv [1 :]
15
+ def usage ():
16
+ print >> sys .stderr , 'Usage: pde2py [-f|--force] srcdir destdir'
17
+ sys .exit (1 )
18
+
19
+ parser = OptionParser ()
20
+ parser .add_option ("-f" , "--force" ,
21
+ action = "store_true" , dest = "force" , default = False ,
22
+ help = "don't print status messages to stdout" )
23
+
24
+ (opts , args ) = parser .parse_args ()
25
+
26
+ if len (args ) < 2 :
27
+ usage ()
28
+
29
+ src , dest = args
14
30
if not (os .path .exists (src ) and os .path .isdir (src )):
15
- raise Exception ( "I expect the first argument to be the source directory." )
31
+ usage ( )
16
32
if os .path .exists (dest ):
17
33
shutil .rmtree (dest )
18
34
os .makedirs (dest )
19
35
20
36
def copy_dir (s , d ):
21
37
if not os .path .exists (d ):
22
38
os .mkdir (d )
23
- for file in os .listdir (s ):
24
- if file [0 ] == '.' :
39
+ for f in os .listdir (s ):
40
+ if f [0 ] == '.' :
25
41
continue
26
- copy (os .path .join (s , file ), os .path .join (d , file ))
42
+ copy (os .path .join (s , f ), os .path .join (d , f ))
27
43
28
44
def copy_file (s , d , xform = None ):
29
45
with open (s , 'rb' ) as f :
30
46
text = f .read ()
31
47
if xform :
32
48
(d , text ) = xform (d , text )
33
49
if os .path .exists (d ):
34
- raise Exception ("I refuse to overwrite %s." % d )
50
+ if opts .force :
51
+ logging .info ('Overwriting %s.' % d )
52
+ else :
53
+ logging .warning ('Not overwriting %s.' % d )
54
+ else :
55
+ logging .info ('Writing %s.' % d )
35
56
with open (d , 'wb' ) as f :
36
57
f .write (text )
37
58
@@ -54,7 +75,7 @@ def xform_py(d, text):
54
75
text = text .replace ('new ' , '' )
55
76
text = text .replace ('true' , 'True' )
56
77
text = text .replace ('false' , 'False' )
57
- text = text .replace ('this.' , 'self.' )
78
+ text = text .replace ('this.' , 'self.' )
58
79
return (d , text )
59
80
60
81
def copy (s , d ):
0 commit comments