File: fix_header.py

package info (click to toggle)
qastools 0.18.0-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,896 kB
  • ctags: 3,525
  • sloc: cpp: 35,509; python: 148; sh: 10; makefile: 4
file content (127 lines) | stat: -rwxr-xr-x 2,734 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import re
import sys
import shutil

script_dir = os.path.dirname ( __file__ )

src_dirs = [
	script_dir + "/../shared/src",
	script_dir + "/../qasconfig/src",
	script_dir + "/../qashctl/src",
	script_dir + "/../qasmixer/src",
]

cmd_arg_error = True
do_list_files = False
do_list_files_inverted = False
seriously_do_it = False

if ( len ( sys.argv ) > 1 ):
	if ( len ( sys.argv ) > 2 ):
		print "Too many arguments"
	else:
		cmd_arg_error = False
		if ( sys.argv[1] == '--help' ):
			print '''\
Options:
	--help  - Print a help text
	--list  - List matching files
	--list-invert - List not matching files
	--do-it - Actually change file headers\
'''
		elif ( sys.argv[1] == '--list' ):
			do_list_files = True
		elif ( sys.argv[1] == '--list-invert' ):
			do_list_files_inverted = True
		elif ( sys.argv[1] == '--do-it' ):
			seriously_do_it = True
		else:
			print ( "Unknown argument: " + sys.argv[1] )
			print ( "Try --help" )
			cmd_arg_error = True
			exit ( -1 )

if ( cmd_arg_error ):
	print ( "Try --help" )
	exit ( -1 )

file_list = []

# Find files in the source directories
for src_dir in src_dirs:
	for root, dirs, files in os.walk ( src_dir ):
		for fl in files:
			fl_full = root + "/" + fl
			fl_rel =  os.path.relpath ( fl_full, script_dir )
			file_list.append ( fl_rel )
file_list.sort()

# Source file name filter
filter_regs = [
	#re.compile ( ".*\.hpp$" ),
	re.compile ( ".*\.cpp$" )
]


file_list_filtered = []

for fname in file_list:
	#print fname
	for fl in filter_regs:
		if ( fl.match ( fname ) ):
			file_list_filtered.append ( fname )

hrepl_in = '''\
//
// C++ Implementation:
//
// Description:
//
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2011
//
// Copyright: See COPYING file that comes with this distribution
//
//\
'''

hrepl_out = '''\
//
// QasTools: Dektop toolset for the Linux sound system ALSA
//
// License: See COPYING file that comes with this source distribution
//
// Author: Sebastian Holtermann <sebholt@xwmw.org>, (C) 2012
//\
'''

hrepl_in = hrepl_in.replace ( '/', '\/' )
hrepl_in = hrepl_in.replace ( '(', '\(' )
hrepl_in = hrepl_in.replace ( ')', '\)' )
hrepl_in = hrepl_in.replace ( '+', '\+' )

#print hrepl_in
re_repl = re.compile ( hrepl_in )


tmp_file = "/tmp/header_fix.txt"
for fname in file_list_filtered:
	with open ( fname, 'r' ) as fhandle:
		fdata = fhandle.read()
	if ( re_repl.match ( fdata ) ):
		if ( do_list_files ):
			print fname

		if ( seriously_do_it ):
			print fname
			fdata_fixed = re_repl.sub ( hrepl_out, fdata )
			with open ( tmp_file, 'w' ) as fhandle:
				fhandle.write ( fdata_fixed )
			shutil.move ( tmp_file, fname )
	else:
		if ( do_list_files_inverted ):
			print fname