OLD | NEW |
1 # coding=utf8 | 1 # coding=utf8 |
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 """Utility functions to handle patches.""" | 5 """Utility functions to handle patches.""" |
6 | 6 |
7 import posixpath | 7 import posixpath |
8 import os | 8 import os |
9 import re | 9 import re |
10 | 10 |
(...skipping 14 matching lines...) Expand all Loading... |
25 class FilePatchBase(object): | 25 class FilePatchBase(object): |
26 """Defines a single file being modified. | 26 """Defines a single file being modified. |
27 | 27 |
28 '/' is always used instead of os.sep for consistency. | 28 '/' is always used instead of os.sep for consistency. |
29 """ | 29 """ |
30 is_delete = False | 30 is_delete = False |
31 is_binary = False | 31 is_binary = False |
32 is_new = False | 32 is_new = False |
33 | 33 |
34 def __init__(self, filename): | 34 def __init__(self, filename): |
35 self.filename = None | 35 self.filename = self._process_filename(filename) |
36 self._set_filename(filename) | |
37 | 36 |
38 def _set_filename(self, filename): | 37 @staticmethod |
39 self.filename = filename.replace('\\', '/') | 38 def _process_filename(filename): |
| 39 filename = filename.replace('\\', '/') |
40 # Blacklist a few characters for simplicity. | 40 # Blacklist a few characters for simplicity. |
41 for i in ('%', '$', '..', '\'', '"'): | 41 for i in ('%', '$', '..', '\'', '"'): |
42 if i in self.filename: | 42 if i in filename: |
43 self._fail('Can\'t use \'%s\' in filename.' % i) | 43 raise UnsupportedPatchFormat( |
| 44 filename, 'Can\'t use \'%s\' in filename.' % i) |
44 for i in ('/', 'CON', 'COM'): | 45 for i in ('/', 'CON', 'COM'): |
45 if self.filename.startswith(i): | 46 if filename.startswith(i): |
46 self._fail('Filename can\'t start with \'%s\'.' % i) | 47 raise UnsupportedPatchFormat( |
| 48 filename, 'Filename can\'t start with \'%s\'.' % i) |
| 49 return filename |
47 | 50 |
48 def get(self): # pragma: no coverage | 51 def get(self): # pragma: no coverage |
49 raise NotImplementedError('Nothing to grab') | 52 raise NotImplementedError('Nothing to grab') |
50 | 53 |
51 def set_relpath(self, relpath): | 54 def set_relpath(self, relpath): |
52 if not relpath: | 55 if not relpath: |
53 return | 56 return |
54 relpath = relpath.replace('\\', '/') | 57 relpath = relpath.replace('\\', '/') |
55 if relpath[0] == '/': | 58 if relpath[0] == '/': |
56 self._fail('Relative path starts with %s' % relpath[0]) | 59 self._fail('Relative path starts with %s' % relpath[0]) |
57 self._set_filename(posixpath.join(relpath, self.filename)) | 60 self.filename = self._process_filename( |
| 61 posixpath.join(relpath, self.filename)) |
58 | 62 |
59 def _fail(self, msg): | 63 def _fail(self, msg): |
| 64 """Shortcut function to raise UnsupportedPatchFormat.""" |
60 raise UnsupportedPatchFormat(self.filename, msg) | 65 raise UnsupportedPatchFormat(self.filename, msg) |
61 | 66 |
62 | 67 |
63 class FilePatchDelete(FilePatchBase): | 68 class FilePatchDelete(FilePatchBase): |
64 """Deletes a file.""" | 69 """Deletes a file.""" |
65 is_delete = True | 70 is_delete = True |
66 | 71 |
67 def __init__(self, filename, is_binary): | 72 def __init__(self, filename, is_binary): |
68 super(FilePatchDelete, self).__init__(filename) | 73 super(FilePatchDelete, self).__init__(filename) |
69 self.is_binary = is_binary | 74 self.is_binary = is_binary |
(...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
342 for patch in self.patches: | 347 for patch in self.patches: |
343 patch.set_relpath(relpath) | 348 patch.set_relpath(relpath) |
344 | 349 |
345 def __iter__(self): | 350 def __iter__(self): |
346 for patch in self.patches: | 351 for patch in self.patches: |
347 yield patch | 352 yield patch |
348 | 353 |
349 @property | 354 @property |
350 def filenames(self): | 355 def filenames(self): |
351 return [p.filename for p in self.patches] | 356 return [p.filename for p in self.patches] |
OLD | NEW |