20
20
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
21
22
22
import os , time , sys
23
- import bzrlib .branch
24
- import bzrlib .log
23
+ import py
25
24
26
25
header_template = """\
27
- # Copyright 2000-%s Michael Hudson-Doyle <[email protected] >%s
26
+ # Copyright 2000-%(lastyear) s Michael Hudson-Doyle <[email protected] >%(others) s
28
27
#
29
28
# All Rights Reserved
30
29
#
46
45
47
46
author_template = "\n #%s%%s" % (' ' * (header_template .index ("Michael" )+ 1 ),)
48
47
49
- branch , path = bzrlib .branch .Branch .open_containing (sys .argv [0 ])
50
- rev_tree = branch .basis_tree ()
51
- branch .lock_read ()
52
-
53
- def process (thing ):
54
- if os .path .isdir (thing ):
55
- for subthing in os .listdir (thing ):
56
- process (os .path .join (thing , subthing ))
57
- elif os .path .isfile (thing ):
58
- if thing [- 3 :] == '.py' :
59
- process_file (thing )
60
- else :
61
- print "W `%s' not file or directory" % (thing ,)
48
+
62
49
63
50
author_map = {
64
51
u'mwh' : None ,
52
+ u'micahel' : None ,
65
53
u'Michael Hudson <[email protected] >' :
None ,
66
54
u'arigo' : u"Armin Rigo" ,
67
55
u'antocuni' : u'Antonio Cuni' ,
56
+ u'anto' : u'Antonio Cuni' ,
68
57
u'bob' : u'Bob Ippolito' ,
69
58
u'fijal' : u'Maciek Fijalkowski' ,
70
59
u'agaynor' : u'Alex Gaynor' ,
71
60
u'hpk' : u'Holger Krekel' ,
61
+ u'Ronny' : u'Ronny Pfannschmidt' ,
62
+ u'amauryfa' : u"Amaury Forgeot d'Arc" ,
72
63
}
73
64
74
- def process_file (file ):
75
- ilines = open (file ).readlines ()
76
- file_id = rev_tree .path2id (file )
77
- rev_ids = [rev_id for (revno , rev_id , what )
78
- in bzrlib .log .find_touching_revisions (branch , file_id )]
79
- revs = branch .repository .get_revisions (rev_ids )
80
- revs = sorted (revs , key = lambda x :x .timestamp )
81
- modified_year = None
82
- for rev in reversed (revs ):
83
- if 'encopyright' not in rev .message :
84
- modified_year = time .gmtime (rev .timestamp )[0 ]
85
- break
65
+
66
+ def author_revs (path ):
67
+ proc = py .std .subprocess .Popen ([
68
+ 'hg' ,'log' , str (path ),
69
+ '--template' , '{author|user} {date}\n ' ,
70
+ '-r' , 'not keyword("encopyright")' ,
71
+ ], stdout = py .std .subprocess .PIPE )
72
+ output , _ = proc .communicate ()
73
+ lines = output .splitlines ()
74
+ for line in lines :
75
+ try :
76
+ name , date = line .split (None , 1 )
77
+ except ValueError :
78
+ pass
79
+ else :
80
+ if '-' in date :
81
+ date = date .split ('-' )[0 ]
82
+ yield name , float (date )
83
+
84
+
85
+ def process (path ):
86
+ ilines = path .readlines ()
87
+ revs = sorted (author_revs (path ), key = lambda x :x [1 ])
88
+ modified_year = time .gmtime (revs [- 1 ][1 ])[0 ]
86
89
if not modified_year :
87
- print 'E: no sensible modified_year found for %s' % file ,
90
+ print 'E: no sensible modified_year found for' , path
88
91
modified_year = time .gmtime (time .time ())[0 ]
89
- authors = set ()
90
- for rev in revs :
91
- authors .update (rev .get_apparent_authors ())
92
92
extra_authors = []
93
+ authors = set (rev [0 ] for rev in revs )
93
94
for a in authors :
94
95
if a not in author_map :
95
- print 'E: need real name for %r' % a
96
+ print 'E: need real name for' , a
96
97
ea = author_map .get (a )
97
98
if ea :
98
99
extra_authors .append (ea )
99
100
extra_authors .sort ()
100
- header = header_template % (modified_year , '' .join ([author_template % ea for ea in extra_authors ]))
101
+ header = header_template % {
102
+ 'lastyear' : modified_year ,
103
+ 'others' : '' .join ([author_template % ea for ea in extra_authors ])
104
+ }
101
105
header_lines = header .splitlines ()
102
106
prelines = []
103
107
old_copyright = []
104
108
105
109
if not ilines :
106
- print "W ignoring empty file `%s'" % ( file ,)
110
+ print "W ignoring empty file" , path
107
111
return
108
112
109
113
i = 0
@@ -123,8 +127,8 @@ def process_file(file):
123
127
if abs (len (old_copyright ) - len (header_lines )) < 2 + len (extra_authors ):
124
128
for x , y in zip (old_copyright , header_lines ):
125
129
if x [:- 1 ] != y :
126
- print "C change needed in" , file
127
- ofile = open (file , "w" )
130
+ print "C change needed in" , path
131
+ ofile = path . open ("w" )
128
132
for l in prelines :
129
133
ofile .write (l )
130
134
ofile .write (header + "\n " )
@@ -133,17 +137,21 @@ def process_file(file):
133
137
ofile .close ()
134
138
break
135
139
else :
136
- print "M no change needed in" , file
140
+ print "M no change needed in" , path
137
141
else :
138
142
print "A no (c) in" , file
139
- ofile = open (file , "w" )
140
- for l in prelines :
141
- ofile .write (l )
142
- ofile .write (header + "\n \n " )
143
- for l in ilines [len (prelines ):]:
144
- ofile .write (l )
145
- ofile .close ()
146
-
143
+ with path .open ("w" ) as ofile :
144
+ for l in prelines :
145
+ ofile .write (l )
146
+ ofile .write (header + "\n \n " )
147
+ for l in ilines [len (prelines ):]:
148
+ ofile .write (l )
149
+
147
150
148
151
for thing in sys .argv [1 :]:
149
- process (thing )
152
+ path = py .path .local (thing )
153
+ if path .check (dir = 1 ):
154
+ for item in path .visit ('*.py' ):
155
+ process (item )
156
+ elif path .check (file = 1 , ext = 'py' ):
157
+ process (path )
0 commit comments