Merge lp:~mandel/ubuntuone-client/legal_linux_chars into lp:ubuntuone-client

Proposed by Manuel de la Peña
Status: Merged
Approved by: Manuel de la Peña
Approved revision: 1042
Merged at revision: 1009
Proposed branch: lp:~mandel/ubuntuone-client/legal_linux_chars
Merge into: lp:ubuntuone-client
Prerequisite: lp:~mandel/ubuntuone-client/illegal_windows_chars
Diff against target: 277 lines (+64/-35)
3 files modified
tests/platform/windows/test_os_helper.py (+22/-15)
ubuntuone/platform/windows/filesystem_notifications.py (+4/-2)
ubuntuone/platform/windows/os_helper.py (+38/-18)
To merge this branch: bzr merge lp:~mandel/ubuntuone-client/legal_linux_chars
Reviewer Review Type Date Requested Status
Roberto Alsina (community) Approve
Natalia Bidart (community) Approve
Review via email: mp+65285@code.launchpad.net

Commit message

Allow to send the events to the state machine that do not have the utf8 special chars.

Description of the change

Allow to send the events to the state machine that do not have the utf8 special chars. To test do:

python C:\Python27\Scripts\u1trial tests\platform\windows\test_os_helper.py

To post a comment you must log in.
Revision history for this message
Natalia Bidart (nataliabidart) :
review: Approve
Revision history for this message
Roberto Alsina (ralsina) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'tests/platform/windows/test_os_helper.py'
--- tests/platform/windows/test_os_helper.py 2011-06-20 16:50:13 +0000
+++ tests/platform/windows/test_os_helper.py 2011-06-21 10:21:47 +0000
@@ -20,8 +20,9 @@
20import errno20import errno
2121
22from ubuntuone.platform.windows.os_helper import (22from ubuntuone.platform.windows.os_helper import (
23 _remove_windows_illegal_chars,23 remove_windows_illegal_chars,
24 access,24 access,
25 is_illegal_path,
25 listdir,26 listdir,
26 make_dir,27 make_dir,
27 open_file,28 open_file,
@@ -29,6 +30,7 @@
29 remove_dir,30 remove_dir,
30 remove_file,31 remove_file,
31 rename,32 rename,
33 remove_utf8_chars,
32 set_no_rights,34 set_no_rights,
33 set_file_readwrite,35 set_file_readwrite,
34 stat_path,36 stat_path,
@@ -46,7 +48,7 @@
46 self.basedir = self.mktemp('test_root')48 self.basedir = self.mktemp('test_root')
47 self.testfile = os.path.join(self.basedir, "test_file")49 self.testfile = os.path.join(self.basedir, "test_file")
48 self.testfile += ''.join(WINDOWS_ILLEGAL_CHARS_MAP)50 self.testfile += ''.join(WINDOWS_ILLEGAL_CHARS_MAP)
49 self.fd = open(_remove_windows_illegal_chars(self.testfile), 'w')51 self.fd = open(remove_windows_illegal_chars(self.testfile), 'w')
5052
51 def test_path_file_exist_yes(self):53 def test_path_file_exist_yes(self):
52 """Test that the file exists."""54 """Test that the file exists."""
@@ -55,7 +57,7 @@
55 def test_path_file_exist_no(self):57 def test_path_file_exist_no(self):
56 """Test that the file doesn't exist."""58 """Test that the file doesn't exist."""
57 self.fd.close()59 self.fd.close()
58 os.remove(_remove_windows_illegal_chars(self.testfile))60 os.remove(remove_windows_illegal_chars(self.testfile))
59 self.assertFalse(path_exists(self.testfile))61 self.assertFalse(path_exists(self.testfile))
6062
61 def test_path_dir_exist_yes(self):63 def test_path_dir_exist_yes(self):
@@ -65,7 +67,7 @@
65 def test_path_dir_exist_no(self):67 def test_path_dir_exist_no(self):
66 """Test that the dir doesn't exist."""68 """Test that the dir doesn't exist."""
67 self.fd.close()69 self.fd.close()
68 os.remove(_remove_windows_illegal_chars(self.testfile))70 os.remove(remove_windows_illegal_chars(self.testfile))
69 self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))71 self.assertFalse(path_exists(os.path.join(self.basedir, 'nodir')))
7072
71 def test_remove_file(self):73 def test_remove_file(self):
@@ -73,7 +75,7 @@
73 self.fd.close()75 self.fd.close()
74 remove_file(self.testfile)76 remove_file(self.testfile)
75 self.assertFalse(os.path.exists(77 self.assertFalse(os.path.exists(
76 _remove_windows_illegal_chars(self.testfile)))78 remove_windows_illegal_chars(self.testfile)))
7779
78 def test_remove_dir(self):80 def test_remove_dir(self):
79 """Test the remove dir."""81 """Test the remove dir."""
@@ -143,23 +145,23 @@
143 """Rename a file."""145 """Rename a file."""
144 testfile1 = os.path.join(self.basedir, 'testfile1>')146 testfile1 = os.path.join(self.basedir, 'testfile1>')
145 testfile2 = os.path.join(self.basedir, 'testfile2<')147 testfile2 = os.path.join(self.basedir, 'testfile2<')
146 open(_remove_windows_illegal_chars(testfile1), 'w').close()148 open(remove_windows_illegal_chars(testfile1), 'w').close()
147 rename(testfile1, testfile2)149 rename(testfile1, testfile2)
148 self.assertFalse(os.path.exists(150 self.assertFalse(os.path.exists(
149 _remove_windows_illegal_chars(testfile1)))151 remove_windows_illegal_chars(testfile1)))
150 self.assertTrue(os.path.exists(152 self.assertTrue(os.path.exists(
151 _remove_windows_illegal_chars(testfile2)))153 remove_windows_illegal_chars(testfile2)))
152154
153 def test_rename_dir(self):155 def test_rename_dir(self):
154 """Rename a dir."""156 """Rename a dir."""
155 testdir1 = os.path.join(self.basedir, 'testdir1>')157 testdir1 = os.path.join(self.basedir, 'testdir1>')
156 testdir2 = os.path.join(self.basedir, 'testdir2<')158 testdir2 = os.path.join(self.basedir, 'testdir2<')
157 os.mkdir(_remove_windows_illegal_chars(testdir1))159 os.mkdir(remove_windows_illegal_chars(testdir1))
158 rename(testdir1, testdir2)160 rename(testdir1, testdir2)
159 self.assertFalse(os.path.exists(161 self.assertFalse(os.path.exists(
160 _remove_windows_illegal_chars(testdir1)))162 remove_windows_illegal_chars(testdir1)))
161 self.assertTrue(os.path.exists(163 self.assertTrue(os.path.exists(
162 _remove_windows_illegal_chars(testdir2)))164 remove_windows_illegal_chars(testdir2)))
163165
164 def test_listdir(self):166 def test_listdir(self):
165 """Return a list of the files in a dir."""167 """Return a list of the files in a dir."""
@@ -177,7 +179,7 @@
177179
178 def test_access_ro(self):180 def test_access_ro(self):
179 """Test access on a file with read only permission."""181 """Test access on a file with read only permission."""
180 fixed_path = _remove_windows_illegal_chars(self.testfile)182 fixed_path = remove_windows_illegal_chars(self.testfile)
181 os.chmod(fixed_path, 0o444)183 os.chmod(fixed_path, 0o444)
182 self.assertTrue(access(self.testfile))184 self.assertTrue(access(self.testfile))
183 os.chmod(fixed_path, 0o664)185 os.chmod(fixed_path, 0o664)
@@ -190,7 +192,7 @@
190192
191 def test_stat_normal(self):193 def test_stat_normal(self):
192 """Test on a normal file."""194 """Test on a normal file."""
193 self.assertEqual(os.stat(_remove_windows_illegal_chars(self.testfile)),195 self.assertEqual(os.stat(remove_windows_illegal_chars(self.testfile)),
194 stat_path(self.testfile))196 stat_path(self.testfile))
195197
196 def test_stat_no_path(self):198 def test_stat_no_path(self):
@@ -207,7 +209,7 @@
207 def test_path_exists_file_no(self):209 def test_path_exists_file_no(self):
208 """The file is not there."""210 """The file is not there."""
209 self.fd.close()211 self.fd.close()
210 os.remove(_remove_windows_illegal_chars(self.testfile))212 os.remove(remove_windows_illegal_chars(self.testfile))
211 self.assertFalse(path_exists(self.testfile))213 self.assertFalse(path_exists(self.testfile))
212214
213 def test_path_exists_dir_yes(self):215 def test_path_exists_dir_yes(self):
@@ -217,4 +219,9 @@
217 def test_path_exists_dir_no(self):219 def test_path_exists_dir_no(self):
218 """The dir is not there."""220 """The dir is not there."""
219 self.fd.close()221 self.fd.close()
220 self.assertFalse(path_exists(os.path.join(self.basedir, 'subdir')))
221\ No newline at end of file222\ No newline at end of file
223 self.assertFalse(path_exists(os.path.join(self.basedir, 'subdir')))
224
225 def test_remove_utf8_chars(self):
226 """Test that the path does not have utf chars."""
227 path = remove_utf8_chars(remove_windows_illegal_chars(self.testfile))
228 self.assertFalse(is_illegal_path(path))
222\ No newline at end of file229\ No newline at end of file
223230
=== modified file 'ubuntuone/platform/windows/filesystem_notifications.py'
--- ubuntuone/platform/windows/filesystem_notifications.py 2011-06-17 09:25:12 +0000
+++ ubuntuone/platform/windows/filesystem_notifications.py 2011-06-21 10:21:47 +0000
@@ -67,7 +67,8 @@
67 GeneralINotifyProcessor)67 GeneralINotifyProcessor)
68from ubuntuone.platform.windows.os_helper import (68from ubuntuone.platform.windows.os_helper import (
69 LONG_PATH_PREFIX,69 LONG_PATH_PREFIX,
70 listdir)70 listdir,
71 remove_utf8_chars)
71from ubuntuone import logger72from ubuntuone import logger
72from ubuntuone.platform.windows.os_helper import longpath73from ubuntuone.platform.windows.os_helper import longpath
73# our logging level74# our logging level
@@ -275,7 +276,8 @@
275 # add the diff events to the q so that the can be processed no276 # add the diff events to the q so that the can be processed no
276 # matter the speed.277 # matter the speed.
277 for action, file in results:278 for action, file in results:
278 full_filename = os.path.join(self._path, file)279 full_filename = remove_utf8_chars(os.path.join(self._path,
280 file))
279 self._raw_events_queue.put((full_filename, action))281 self._raw_events_queue.put((full_filename, action))
280 self.log.debug('Added %s to raw events queue.',282 self.log.debug('Added %s to raw events queue.',
281 (full_filename, action))283 (full_filename, action))
282284
=== modified file 'ubuntuone/platform/windows/os_helper.py'
--- ubuntuone/platform/windows/os_helper.py 2011-06-20 23:59:33 +0000
+++ ubuntuone/platform/windows/os_helper.py 2011-06-21 10:21:47 +0000
@@ -126,23 +126,22 @@
126def _check_winerror_123(exception, cb, path, *args, **kwargs):126def _check_winerror_123(exception, cb, path, *args, **kwargs):
127 """Checks if an exception is a 123 error and executes the cb."""127 """Checks if an exception is a 123 error and executes the cb."""
128 if getattr(exception, "winerror", None) == 123 :128 if getattr(exception, "winerror", None) == 123 :
129 path = _remove_windows_illegal_chars(path)129 path = remove_windows_illegal_chars(path)
130 return cb(path, *args, **kwargs)130 return cb(path, *args, **kwargs)
131 else:131 else:
132 raise exception132 raise exception
133133
134134
135def _is_illegal_path(path):135def _contains_chars(path, chars):
136 """Return if the path is illegal in the system."""136 """Returns if one of the chars is in the path."""
137 if LONG_PATH_PREFIX in path:137 if LONG_PATH_PREFIX in path:
138 path = path.replace(LONG_PATH_PREFIX, '')138 path = path.replace(LONG_PATH_PREFIX, '')
139 return any(c in WINDOWS_ILLEGAL_CHARS_MAP139 return any(c in chars for c in os.path.splitdrive(path)[1])
140 for c in os.path.splitdrive(path)[1])140
141141
142142def _modify_chars(path, should_apply, chars):
143def _remove_windows_illegal_chars(path):
144 """Remove illegal chars and return a valid windows path."""143 """Remove illegal chars and return a valid windows path."""
145 if not _is_illegal_path(path):144 if not should_apply(path):
146 return path145 return path
147 prefix = False146 prefix = False
148 # do not split the path if you have a long path prefix since the147 # do not split the path if you have a long path prefix since the
@@ -156,7 +155,7 @@
156 result = []155 result = []
157 for partial in partial_paths:156 for partial in partial_paths:
158 # ensure that the illegal chars are not there157 # ensure that the illegal chars are not there
159 for illegal_char, replacement in WINDOWS_ILLEGAL_CHARS_MAP.iteritems():158 for illegal_char, replacement in chars.iteritems():
160 partial = partial.replace(illegal_char, replacement)159 partial = partial.replace(illegal_char, replacement)
161 result.append(partial)160 result.append(partial)
162 result = os.path.join(*result)161 result = os.path.join(*result)
@@ -166,6 +165,27 @@
166 result = LONG_PATH_PREFIX + result165 result = LONG_PATH_PREFIX + result
167 return unicode(result)166 return unicode(result)
168167
168
169def is_illegal_path(path):
170 """Return if the path is illegal in the system."""
171 return _contains_chars(path, WINDOWS_ILLEGAL_CHARS_MAP)
172
173
174def remove_windows_illegal_chars(path):
175 """Remove illegal chars and return a valid windows path."""
176 return _modify_chars(path, is_illegal_path, WINDOWS_ILLEGAL_CHARS_MAP)
177
178
179def is_modified_path(path):
180 """Return if the path contains the special utf8 chars."""
181 return _contains_chars(path, LINUX_ILLEGAL_CHARS_MAP)
182
183
184def remove_utf8_chars(path):
185 """Remove the special chars from the path and use the basic ones."""
186 return _modify_chars(path, is_modified_path, LINUX_ILLEGAL_CHARS_MAP)
187
188
169def _set_file_attributes(path, groups):189def _set_file_attributes(path, groups):
170 """Set file attributes using the wind32api."""190 """Set file attributes using the wind32api."""
171 security_descriptor = None191 security_descriptor = None
@@ -176,7 +196,7 @@
176 'for path %s', path, groups)196 'for path %s', path, groups)
177 #check if the issue was due to an illegal path197 #check if the issue was due to an illegal path
178 if getattr(e, "winerror", None) == 123:198 if getattr(e, "winerror", None) == 123:
179 path = _remove_windows_illegal_chars(path)199 path = remove_windows_illegal_chars(path)
180 return _set_file_attributes(path, groups)200 return _set_file_attributes(path, groups)
181 else:201 else:
182 raise202 raise
@@ -358,8 +378,8 @@
358@longpath(paths_indexes=[0])378@longpath(paths_indexes=[0])
359def path_exists(path):379def path_exists(path):
360 """Return if the path exists."""380 """Return if the path exists."""
361 if _is_illegal_path(path):381 if is_illegal_path(path):
362 path = _remove_windows_illegal_chars(path)382 path = remove_windows_illegal_chars(path)
363 return os.path.exists(path)383 return os.path.exists(path)
364384
365385
@@ -409,8 +429,8 @@
409 '%s to %s', path_from, path_to)429 '%s to %s', path_from, path_to)
410 #check if the issue was due to an illegal path430 #check if the issue was due to an illegal path
411 if getattr(e, "winerror", None) == 123:431 if getattr(e, "winerror", None) == 123:
412 path_from = _remove_windows_illegal_chars(path_from)432 path_from = remove_windows_illegal_chars(path_from)
413 path_to = _remove_windows_illegal_chars(path_to)433 path_to = remove_windows_illegal_chars(path_to)
414 try:434 try:
415 MoveFileExW(435 MoveFileExW(
416 path_from, path_to,436 path_from, path_to,
@@ -489,8 +509,8 @@
489 # since that will only occur in the case where the user created the path509 # since that will only occur in the case where the user created the path
490 # for a file to be readable it has to be readable either by the user or510 # for a file to be readable it has to be readable either by the user or
491 # by the everyone group511 # by the everyone group
492 if _is_illegal_path(path):512 if is_illegal_path(path):
493 path = _remove_windows_illegal_chars(path)513 path = remove_windows_illegal_chars(path)
494 if not os.path.exists(path):514 if not os.path.exists(path):
495 return False515 return False
496 security_descriptor = GetFileSecurity(path, DACL_SECURITY_INFORMATION)516 security_descriptor = GetFileSecurity(path, DACL_SECURITY_INFORMATION)
@@ -588,7 +608,7 @@
588 """Return an abspath."""608 """Return an abspath."""
589 # TODO: do we really need to do this, are the tests broken?609 # TODO: do we really need to do this, are the tests broken?
590 path = path.replace('/', '\\')610 path = path.replace('/', '\\')
591 abs = os.path.abspath(_remove_windows_illegal_chars(path))611 abs = os.path.abspath(remove_windows_illegal_chars(path))
592 if not LONG_PATH_PREFIX in abs:612 if not LONG_PATH_PREFIX in abs:
593 abs = LONG_PATH_PREFIX + abs613 abs = LONG_PATH_PREFIX + abs
594 return abs614 return abs
595\ No newline at end of file615\ No newline at end of file

Subscribers

People subscribed via source and target branches