2 # Copyright 2011-2012 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
11 CONTENT_ENCODING = "utf_8"
21 except AttributeError:
22 # magic module seems to be broken
25 class IsTextFile(object):
29 self._call = self._is_text_magic
30 self._m = magic.open(magic.MIME_TYPE)
33 self._call = self._is_text_encoding
34 self._encoding = CONTENT_ENCODING
36 def __call__(self, filename):
38 Returns True if the given file is a text file, and False otherwise.
40 return self._call(filename)
42 def _is_text_magic(self, filename):
43 mime_type = self._m.file(filename)
44 return mime_type.startswith("text/")
46 def _is_text_encoding(self, filename):
48 for line in io.open(filename, mode='r', encoding=self._encoding):
50 except UnicodeDecodeError:
54 def chpath_inplace(filename, is_text_file, old, new):
56 Returns True if any modifications were made, and False otherwise.
60 orig_stat = os.lstat(filename)
62 f = io.open(filename, buffering=0, mode='r+b')
65 orig_mode = stat.S_IMODE(os.lstat(filename).st_mode)
67 sys.stderr.write("%s: %s\n" % (e, filename))
69 temp_mode = 0o200 | orig_mode
70 os.chmod(filename, temp_mode)
72 f = io.open(filename, buffering=0, mode='r+b')
74 os.chmod(filename, orig_mode)
78 matched_byte_count = 0
85 if in_byte == old[matched_byte_count]:
86 matched_byte_count += 1
87 if matched_byte_count == len_old:
89 matched_byte_count = 0
90 end_position = f.tell()
91 start_position = end_position - len_old
93 # search backwards for leading slashes written by
94 # a previous invocation of this tool
95 num_to_write = len_old
96 f.seek(start_position - 1)
103 # pad with as many leading slashes as necessary
104 while num_to_write > len_new:
110 f.seek(start_position)
115 f.seek(start_position + len_new)
116 elif matched_byte_count > 0:
117 # back up an try to start a new match after
118 # the first byte of the previous partial match
119 f.seek(f.tell() - matched_byte_count)
120 matched_byte_count = 0
124 if sys.hexversion >= 0x3030000:
125 orig_mtime = orig_stat.st_mtime_ns
126 os.utime(filename, ns=(orig_mtime, orig_mtime))
128 orig_mtime = orig_stat[stat.ST_MTIME]
129 os.utime(filename, (orig_mtime, orig_mtime))
132 def chpath_inplace_symlink(filename, st, old, new):
133 target = os.readlink(filename)
134 if target.startswith(old):
135 new_target = new + target[len(old):]
137 os.symlink(new_target, filename)
138 os.lchown(filename, st.st_uid, st.st_gid)
142 usage = "%s [options] <location> <old> <new>" % (os.path.basename(argv[0],))
143 parser = optparse.OptionParser(usage=usage)
144 options, args = parser.parse_args(argv[1:])
147 parser.error("3 args required, got %s" % (len(args),))
149 location, old, new = args
151 is_text_file = IsTextFile()
153 if not isinstance(location, bytes):
154 location = location.encode(FS_ENCODING)
155 if not isinstance(old, bytes):
156 old = old.encode(FS_ENCODING)
157 if not isinstance(new, bytes):
158 new = new.encode(FS_ENCODING)
160 st = os.lstat(location)
162 if stat.S_ISDIR(st.st_mode):
163 for parent, dirs, files in os.walk(location):
164 for filename in files:
165 filename = os.path.join(parent, filename)
167 st = os.lstat(filename)
171 if stat.S_ISREG(st.st_mode):
172 chpath_inplace(filename,
173 is_text_file(filename), old, new)
174 elif stat.S_ISLNK(st.st_mode):
175 chpath_inplace_symlink(filename, st, old, new)
177 elif stat.S_ISREG(st.st_mode):
178 chpath_inplace(location,
179 is_text_file(location), old, new)
180 elif stat.S_ISLNK(st.st_mode):
181 chpath_inplace_symlink(location, st, old, new)
185 if __name__ == "__main__":
186 sys.exit(main(sys.argv))