From be113f1365ffa02efe1fc2d0959d5f06029bc173 Mon Sep 17 00:00:00 2001 From: "maruel@chromium.org" Date: Thu, 1 Sep 2011 15:05:34 +0000 Subject: [PATCH] Make filename processing a static method. This is in preparation to support file rename, as a source_filename needs to be added and processed. R=dpranke@chromium.org BUG= TEST= Review URL: http://codereview.chromium.org/7809001 git-svn-id: svn://svn.chromium.org/chrome/trunk/tools/depot_tools@99185 0039d316-1c4b-4281-b951-d872f2087c98 --- patch.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/patch.py b/patch.py index 77bb159910..8bdeddb1e6 100644 --- a/patch.py +++ b/patch.py @@ -32,18 +32,21 @@ class FilePatchBase(object): is_new = False def __init__(self, filename): - self.filename = None - self._set_filename(filename) + self.filename = self._process_filename(filename) - def _set_filename(self, filename): - self.filename = filename.replace('\\', '/') + @staticmethod + def _process_filename(filename): + filename = filename.replace('\\', '/') # Blacklist a few characters for simplicity. for i in ('%', '$', '..', '\'', '"'): - if i in self.filename: - self._fail('Can\'t use \'%s\' in filename.' % i) + if i in filename: + raise UnsupportedPatchFormat( + filename, 'Can\'t use \'%s\' in filename.' % i) for i in ('/', 'CON', 'COM'): - if self.filename.startswith(i): - self._fail('Filename can\'t start with \'%s\'.' % i) + if filename.startswith(i): + raise UnsupportedPatchFormat( + filename, 'Filename can\'t start with \'%s\'.' % i) + return filename def get(self): # pragma: no coverage raise NotImplementedError('Nothing to grab') @@ -54,9 +57,11 @@ class FilePatchBase(object): relpath = relpath.replace('\\', '/') if relpath[0] == '/': self._fail('Relative path starts with %s' % relpath[0]) - self._set_filename(posixpath.join(relpath, self.filename)) + self.filename = self._process_filename( + posixpath.join(relpath, self.filename)) def _fail(self, msg): + """Shortcut function to raise UnsupportedPatchFormat.""" raise UnsupportedPatchFormat(self.filename, msg)