forked from peter17/pijnu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileUpdater.py
95 lines (79 loc) · 2.92 KB
/
fileUpdater.py
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
# -*- coding: utf8 -*-
'''
Copyright 2009 Denis Derman <denis.spir@gmail.com> (former developer)
Copyright 2011-2012 Peter Potrowl <peter017@gmail.com> (current developer)
This file is part of Pijnu.
Pijnu is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pijnu is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with Pijnu. If not, see <http://www.gnu.org/licenses/>.
'''
'''
Tool to insert a notice in selected files of recursively walked dirs
See more info in variable 'info' below.
'''
from pijnu.library.tools import fileText, fileNames
import sys, os
end = sys.exit
# tool func to replace or remove specific text in files
def changeText(filename, text, newtext=""):
''' *Change* (or *remove*) text in this file.
-- called by changeFiles '''
filetext = fileText(filename)
if text in filetext:
filetext = filetext.replace(text, newtext)
f = file(filename, 'w')
f.write(filetext)
f.close()
print "file: %s" % filename
def changeTexts(text, newtext="", dir=None, ext="py"):
# parameters
if dir is None:
dir = os.getcwd()
# run
print "=== file walk ==="
filenames = fileNames(dir, ext)
print "\n=== changing files ==="
for filename in filenames:
changeText(filename, text, newtext)
# tool func to insert notice in files
def insertNotice(filename, notice, linecount):
''' Insert notice in this file. '''
notice_lines = ["%s\n" % l for l in notice.splitlines()]
# compose new text version
filelines = file(filename).readlines()
textlines = filelines[0:linecount] + notice_lines + filelines[linecount:]
text = "".join(textlines)
# write it back
f = file(filename, 'w')
f.write(text)
f.close()
print "file: %s" % filename
def insertNotices(notice_filename, linecount=2, dir=None, ext="py"):
# params
if dir is None:
dir = os.getcwd()
try:
notice = fileText(notice_filename)
notice_start = '\n'.join(notice.splitlines()[:7])
except IOError:
notice = notice_start = None
# now, run?
print ("\ndir: %s\next: %s\nlinecount: %s\nfirst text lines:\n%s\n"
% (dir, ext, linecount, notice_start))
answer = raw_input(">>> write notice in files? (y/n)")
if not (answer.lower() == 'y'):
end()
# yes, run!
print "=== file walk ==="
print linecount, dir
filenames = fileNames(dir, ext)
print "\n=== writing notice ==="
for filename in filenames:
insertNotice(filename, notice, linecount)