23
23
24
24
from datetime import date
25
25
import glob
26
- import mmap
27
26
import os
28
27
import re
29
28
import shutil
30
29
import tempfile
31
30
31
+
32
32
def add_header (filepath , header , YEAR , AUTHORS ):
33
33
"""
34
34
Add or update header in source file
35
35
"""
36
36
tmpdir = tempfile .gettempdir ()
37
37
tmpfil = os .path .join (tmpdir , os .path .basename (filepath ) + '.bak' )
38
38
shutil .copy2 (filepath , tmpfil )
39
- with open (tmpfil , 'r+b ' ) as tmp :
39
+ with open (tmpfil , 'r' ) as tmp :
40
40
inpt = tmp .readlines ()
41
41
output = []
42
42
43
43
# Check if header is already present
44
44
present = re .compile ('PCMSolver, an API for the Polarizable Continuum Model' )
45
- if filter (present .search , inpt ):
45
+ if list ( filter (present .search , inpt ) ):
46
46
# Check if year and authors in current file are up to date
47
47
toupdate = re .compile (r'{0} (?!{1} {2}).*\n' .format ('Copyright \(C\)' , YEAR , AUTHORS ))
48
- if filter (toupdate .search , inpt ):
49
- print ('Updating header in {}' .format (filepath ))
48
+ if list ( filter (toupdate .search , inpt ) ):
49
+ print (( 'Updating header in {}' .format (filepath ) ))
50
50
# Check to preserve '#!' at the top of the file
51
51
if len (inpt ) > 0 and inpt [0 ].startswith ('#!' ):
52
52
output .append (inpt [0 ] + '\n ' )
53
53
inpt = inpt [1 :]
54
54
regex = re .compile (r'Copyright \(C\).*\n' )
55
55
repl = r'Copyright (C) ' + YEAR + ' ' + AUTHORS + '\n '
56
- output .extend (map ( lambda x : re .sub (regex , repl , x ), inpt ) )
56
+ output .extend ([ re .sub (regex , repl , x ) for x in inpt ] )
57
57
else :
58
- print ('Adding header in {}' .format (filepath ))
58
+ print (( 'Adding header in {}' .format (filepath ) ))
59
59
# Check to preserve '#!' at the top of the file
60
60
if len (inpt ) > 0 and inpt [0 ].startswith ('#!' ):
61
61
output .append (inpt [0 ] + '\n ' )
@@ -68,8 +68,8 @@ def add_header(filepath, header, YEAR, AUTHORS):
68
68
try :
69
69
f = open (filepath , 'w' )
70
70
f .writelines (output )
71
- except IOError , err :
72
- print ('Something went wrong trying to add header to {}: {}' .format (filepath , err ))
71
+ except IOError as err :
72
+ print (( 'Something went wrong trying to add header to {}: {}' .format (filepath , err ) ))
73
73
finally :
74
74
f .close ()
75
75
os .remove (tmpfil )
@@ -79,12 +79,12 @@ def prepare_header(stub, YEAR, AUTHORS):
79
79
"""
80
80
Update year and author information in license header template
81
81
"""
82
- with open (stub , 'r+b ' ) as l :
82
+ with open (stub , 'r' ) as l :
83
83
header = l .read ()
84
84
# Insert correct YEAR and AUTHORS in stub
85
85
rep = {'YEAR' : YEAR , 'AUTHORS' : AUTHORS }
86
- rep = dict ((re .escape (k ), v ) for k , v in rep .iteritems ())
87
- pattern = re .compile ("|" .join (rep .keys ()))
86
+ rep = dict ((re .escape (k ), v ) for k , v in rep .items ())
87
+ pattern = re .compile ("|" .join (list ( rep .keys () )))
88
88
header = pattern .sub (lambda m : rep [re .escape (m .group (0 ))], header )
89
89
return header
90
90
@@ -94,26 +94,26 @@ def file_license(attributes):
94
94
Obtain dictionary { file : license } from .gitattributes
95
95
"""
96
96
file_license = {}
97
- with open (attributes , 'r+b ' ) as f :
97
+ with open (attributes , 'r' ) as f :
98
98
# Read in .gitattributes
99
- tmp_mm = mmap . mmap ( f . fileno (), 0 , access = mmap . ACCESS_READ )
99
+ tmp = f . read ( )
100
100
# Removing all comment lines and other attributes
101
- pattern = re .compile ("|" . join ([ r'(?m)^\#.*\n?' , r' ^((?!licensefile).)*$']) )
102
- gitattributes = re .sub (pattern , '' , tmp_mm ).split ()
101
+ pattern = re .compile (r'(?m)^\#.*\n?| ^((?!licensefile).)*$' )
102
+ gitattributes = re .sub (pattern , '' , tmp ).split ()
103
103
# Obtain list of files
104
104
fil = [x for x in gitattributes if not 'licensefile' in x ]
105
105
# Remove licensefile= from strings
106
106
lic = [re .sub (r'licensefile\=' , '' , x ) for x in gitattributes if 'licensefile' in x ]
107
107
# Create list of blacklisted files
108
- blacklist = [fname for key , value in dict (zip (fil , lic )).items ()
108
+ blacklist = [fname for key , value in list ( dict (list ( zip (fil , lic ))) .items () )
109
109
if value == '!licensefile'
110
110
for fname in glob .glob (key )]
111
111
# Now create a dictionary with the files to be considered for
112
112
# license header manipulation
113
- file_license = { key : value
114
- for k , value in dict (zip (fil , lic )).items ()
115
- for key in glob .glob (k )
116
- if key not in blacklist }
113
+ file_license = {key : value
114
+ for k , value in list ( dict (list ( zip (fil , lic ))) .items () )
115
+ for key in glob .glob (k )
116
+ if key not in blacklist }
117
117
return file_license
118
118
119
119
@@ -129,7 +129,7 @@ def license_maintainer():
129
129
130
130
headerize = file_license (os .path .join (project_root_dir , '.gitattributes' ))
131
131
132
- for fname , license in headerize .items ():
132
+ for fname , license in list ( headerize .items () ):
133
133
# Prepare header
134
134
header = prepare_header (os .path .join (project_root_dir , license ), YEAR , AUTHORS )
135
135
add_header (fname , header , YEAR , AUTHORS )
0 commit comments