Fanwang Meng commited on
Commit
c23466b
·
1 Parent(s): f52980f

Rename `DiverseSelector` to `selector` for updateheaders.py

Browse files
Files changed (1) hide show
  1. updateheaders.py +30 -31
updateheaders.py CHANGED
@@ -22,9 +22,9 @@
22
  # --
23
 
24
 
25
- from glob import glob
26
- from fnmatch import fnmatch
27
  import os
 
 
28
 
29
 
30
  def strip_header(lines, closing):
@@ -45,49 +45,49 @@ def strip_header(lines, closing):
45
 
46
  def fix_python(fn, lines, header_lines):
47
  # check if a shebang is present
48
- do_shebang = lines[0].startswith('#!')
49
  # remove the current header
50
- strip_header(lines, '# --\n')
51
  # add a pylint line for test files:
52
  # if os.path.basename(fn).startswith('test_'):
53
  # if not lines[1].startswith('#pylint: skip-file'):
54
  # lines.insert(1, '#pylint: skip-file\n')
55
  # add new header (insert in reverse order)
56
  for hline in header_lines[::-1]:
57
- lines.insert(0, ('# ' + hline).strip() + '\n')
58
  # add a source code encoding line
59
- lines.insert(0, '# -*- coding: utf-8 -*-\n')
60
  if do_shebang:
61
- lines.insert(0, '#!/usr/bin/env python\n')
62
 
63
 
64
  def fix_c(fn, lines, header_lines):
65
  # check for an exception line
66
  for line in lines:
67
- if 'no_update_headers' in line:
68
  return
69
  # remove the current header
70
- strip_header(lines, '//--\n')
71
  # add new header (insert must be in reverse order)
72
  for hline in header_lines[::-1]:
73
- lines.insert(0, ('// ' + hline).strip() + '\n')
74
 
75
 
76
  def fix_rst(fn, lines, header_lines):
77
  # check for an exception line
78
  for line in lines:
79
- if 'no_update_headers' in line:
80
  return
81
  # remove the current header
82
- strip_header(lines, ' : --\n')
83
  # add an empty line after header if needed
84
  if len(lines[1].strip()) > 0:
85
- lines.insert(1, '\n')
86
  # add new header (insert must be in reverse order)
87
  for hline in header_lines[::-1]:
88
- lines.insert(0, (' : ' + hline).rstrip() + '\n')
89
  # add comment instruction
90
- lines.insert(0, '..\n')
91
 
92
 
93
  def iter_subdirs(root):
@@ -96,39 +96,38 @@ def iter_subdirs(root):
96
 
97
 
98
  def main():
99
- source_dirs = ['.', 'doc', 'scripts', 'tools'] + \
100
- list(iter_subdirs('procrustes'))
101
 
102
  fixers = [
103
- ('*.py', fix_python),
104
- ('*.pxd', fix_python),
105
- ('*.pyx', fix_python),
106
- ('*.txt', fix_python),
107
- ('*.c', fix_c),
108
- ('*.cpp', fix_c),
109
- ('*.h', fix_c),
110
- ('*.rst', fix_rst),
111
  ]
112
 
113
- f = open('HEADER')
114
  header_lines = f.readlines()
115
  f.close()
116
 
117
  for sdir in source_dirs:
118
- print('Scanning:', sdir)
119
- for fn in glob(sdir + '/*.*'):
120
  if not os.path.isfile(fn):
121
  continue
122
  for pattern, fixer in fixers:
123
  if fnmatch(fn, pattern):
124
- print(' Fixing:', fn)
125
  with open(fn) as f:
126
  lines = f.readlines()
127
  fixer(fn, lines, header_lines)
128
- with open(fn, 'w') as f:
129
  f.writelines(lines)
130
  break
131
 
132
 
133
- if __name__ == '__main__':
134
  main()
 
22
  # --
23
 
24
 
 
 
25
  import os
26
+ from fnmatch import fnmatch
27
+ from glob import glob
28
 
29
 
30
  def strip_header(lines, closing):
 
45
 
46
  def fix_python(fn, lines, header_lines):
47
  # check if a shebang is present
48
+ do_shebang = lines[0].startswith("#!")
49
  # remove the current header
50
+ strip_header(lines, "# --\n")
51
  # add a pylint line for test files:
52
  # if os.path.basename(fn).startswith('test_'):
53
  # if not lines[1].startswith('#pylint: skip-file'):
54
  # lines.insert(1, '#pylint: skip-file\n')
55
  # add new header (insert in reverse order)
56
  for hline in header_lines[::-1]:
57
+ lines.insert(0, ("# " + hline).strip() + "\n")
58
  # add a source code encoding line
59
+ lines.insert(0, "# -*- coding: utf-8 -*-\n")
60
  if do_shebang:
61
+ lines.insert(0, "#!/usr/bin/env python\n")
62
 
63
 
64
  def fix_c(fn, lines, header_lines):
65
  # check for an exception line
66
  for line in lines:
67
+ if "no_update_headers" in line:
68
  return
69
  # remove the current header
70
+ strip_header(lines, "//--\n")
71
  # add new header (insert must be in reverse order)
72
  for hline in header_lines[::-1]:
73
+ lines.insert(0, ("// " + hline).strip() + "\n")
74
 
75
 
76
  def fix_rst(fn, lines, header_lines):
77
  # check for an exception line
78
  for line in lines:
79
+ if "no_update_headers" in line:
80
  return
81
  # remove the current header
82
+ strip_header(lines, " : --\n")
83
  # add an empty line after header if needed
84
  if len(lines[1].strip()) > 0:
85
+ lines.insert(1, "\n")
86
  # add new header (insert must be in reverse order)
87
  for hline in header_lines[::-1]:
88
+ lines.insert(0, (" : " + hline).rstrip() + "\n")
89
  # add comment instruction
90
+ lines.insert(0, "..\n")
91
 
92
 
93
  def iter_subdirs(root):
 
96
 
97
 
98
  def main():
99
+ source_dirs = [".", "doc", "scripts", "tools"] + list(iter_subdirs("procrustes"))
 
100
 
101
  fixers = [
102
+ ("*.py", fix_python),
103
+ ("*.pxd", fix_python),
104
+ ("*.pyx", fix_python),
105
+ ("*.txt", fix_python),
106
+ ("*.c", fix_c),
107
+ ("*.cpp", fix_c),
108
+ ("*.h", fix_c),
109
+ ("*.rst", fix_rst),
110
  ]
111
 
112
+ f = open("HEADER")
113
  header_lines = f.readlines()
114
  f.close()
115
 
116
  for sdir in source_dirs:
117
+ print("Scanning:", sdir)
118
+ for fn in glob(sdir + "/*.*"):
119
  if not os.path.isfile(fn):
120
  continue
121
  for pattern, fixer in fixers:
122
  if fnmatch(fn, pattern):
123
+ print(" Fixing:", fn)
124
  with open(fn) as f:
125
  lines = f.readlines()
126
  fixer(fn, lines, header_lines)
127
+ with open(fn, "w") as f:
128
  f.writelines(lines)
129
  break
130
 
131
 
132
+ if __name__ == "__main__":
133
  main()