bin/misc-functions.sh: use pipe for find ${D}${D}
[portage.git] / bin / chpathtool.py
1 #!/usr/bin/python
2 # Copyright 2011-2012 Gentoo Foundation
3 # Distributed under the terms of the GNU General Public License v2
4
5 import io
6 import optparse
7 import os
8 import stat
9 import sys
10
11 CONTENT_ENCODING = "utf_8"
12 FS_ENCODING = "utf_8"
13
14 try:
15         import magic
16 except ImportError:
17         magic = None
18 else:
19         try:
20                 magic.MIME_TYPE
21         except AttributeError:
22                 # magic module seems to be broken
23                 magic = None
24
25 class IsTextFile(object):
26
27         def __init__(self):
28                 if magic is not None:
29                         self._call = self._is_text_magic
30                         self._m = magic.open(magic.MIME_TYPE)
31                         self._m.load()
32                 else:
33                         self._call = self._is_text_encoding
34                         self._encoding = CONTENT_ENCODING
35
36         def __call__(self, filename):
37                 """
38                 Returns True if the given file is a text file, and False otherwise.
39                 """
40                 return self._call(filename)
41
42         def _is_text_magic(self, filename):
43                 mime_type = self._m.file(filename)
44                 return mime_type.startswith("text/")
45
46         def _is_text_encoding(self, filename):
47                 try:
48                         for line in io.open(filename, mode='r', encoding=self._encoding):
49                                 pass
50                 except UnicodeDecodeError:
51                         return False
52                 return True
53
54 def chpath_inplace(filename, is_text_file, old, new):
55         """
56         Returns True if any modifications were made, and False otherwise.
57         """
58
59         modified = False
60         orig_stat = os.lstat(filename)
61         try:
62                 f = io.open(filename, buffering=0, mode='r+b')
63         except IOError:
64                 try:
65                         orig_mode = stat.S_IMODE(os.lstat(filename).st_mode)
66                 except OSError as e:
67                         sys.stderr.write("%s: %s\n" % (e, filename))
68                         return
69                 temp_mode = 0o200 | orig_mode
70                 os.chmod(filename, temp_mode)
71                 try:
72                         f = io.open(filename, buffering=0, mode='r+b')
73                 finally:
74                         os.chmod(filename, orig_mode)
75
76         len_old = len(old)
77         len_new = len(new)
78         matched_byte_count = 0
79         while True:
80                 in_byte = f.read(1)
81
82                 if not in_byte:
83                         break
84
85                 if in_byte == old[matched_byte_count]:
86                         matched_byte_count += 1
87                         if matched_byte_count == len_old:
88                                 modified = True
89                                 matched_byte_count = 0
90                                 end_position = f.tell()
91                                 start_position = end_position - len_old
92                                 if not is_text_file:
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)
97                                         while True:
98                                                 if f.read(1) != b'/':
99                                                         break
100                                                 num_to_write += 1
101                                                 f.seek(f.tell() - 2)
102
103                                         # pad with as many leading slashes as necessary
104                                         while num_to_write > len_new:
105                                                 f.write(b'/')
106                                                 num_to_write -= 1
107                                         f.write(new)
108                                 else:
109                                         remainder = f.read()
110                                         f.seek(start_position)
111                                         f.write(new)
112                                         if remainder:
113                                                 f.write(remainder)
114                                                 f.truncate()
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
121
122         f.close()
123         if modified:
124                 if sys.hexversion >= 0x3030000:
125                         orig_mtime = orig_stat.st_mtime_ns
126                         os.utime(filename, ns=(orig_mtime, orig_mtime))
127                 else:
128                         orig_mtime = orig_stat[stat.ST_MTIME]
129                         os.utime(filename, (orig_mtime, orig_mtime))
130         return modified
131
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):]
136                 os.unlink(filename)
137                 os.symlink(new_target, filename)
138                 os.lchown(filename, st.st_uid, st.st_gid)
139
140 def main(argv):
141
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:])
145
146         if len(args) != 3:
147                 parser.error("3 args required, got %s" % (len(args),))
148
149         location, old, new = args
150
151         is_text_file = IsTextFile()
152
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)
159
160         st = os.lstat(location)
161
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)
166                                 try:
167                                         st = os.lstat(filename)
168                                 except OSError:
169                                         pass
170                                 else:
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)
176
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)
182
183         return os.EX_OK
184
185 if __name__ == "__main__":
186         sys.exit(main(sys.argv))