Merge lp:~facundo/magicicada-client/new-trash into lp:magicicada-client

Proposed by Facundo Batista
Status: Merged
Approved by: Natalia Bidart
Approved revision: 1442
Merged at revision: 1441
Proposed branch: lp:~facundo/magicicada-client/new-trash
Merge into: lp:magicicada-client
Diff against target: 293 lines (+55/-114)
7 files modified
Makefile (+6/-3)
dependencies-devel.txt (+0/-1)
requirements-devel.txt (+1/-0)
requirements.txt (+1/-0)
run-tests (+1/-9)
ubuntuone/platform/os_helper/linux.py (+17/-29)
ubuntuone/platform/tests/os_helper/test_linux.py (+29/-72)
To merge this branch: bzr merge lp:~facundo/magicicada-client/new-trash
Reviewer Review Type Date Requested Status
Natalia Bidart Approve
Review via email: mp+341764@code.launchpad.net

Commit message

Start using a new way to send to trash, going away from Gnome.

Description of the change

Start using a new way to send to trash, going away from Gnome.

Now we start using a package installed in a virtualenv, so I normalized some stuff around it:

- create it at bootstrap time, not everytime on every make test

- install packages listed in requirements-{devel}.txt, not hardcoded

- pinpointed flake8 in the requirements, and removed it as a system dependency

Also cleaned up the `run-tests` script:

- no more building when running tests, only at bootstrap

- run u1trial from inside the venv

- removed quotes around $MODULE, so we can do now

      ./run-tests -t TestClass.test_foo tests/test_bleh.py
Start using a new way to send to trash, going away from Gnome.

Now we start using a package installed in a virtualenv, so I normalized some stuff around it:

- create it at bootstrap time, not everytime on every make test

- install packages listed in requirements-{devel}.txt, not hardcoded

- pinpointed flake8 in the requirements, and removed it as a system dependency

Also cleaned up the `run-tests` script:

- no more building when running tests, only at bootstrap

- run u1trial from inside the venv

- removed quotes around $MODULE, so we can do now

      ./run-tests -t TestClass.test_foo tests/test_bleh.py

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

Looks great! Thanks

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Makefile'
2--- Makefile 2018-03-14 21:55:27 +0000
3+++ Makefile 2018-03-20 19:57:57 +0000
4@@ -44,16 +44,19 @@
5 update-protocol:
6 cd $(PROTOCOL_DIR) && bzr pull && python setup.py build
7
8-bootstrap: deps $(PROTOCOL_DIR) $(PROTOCOL_LINK) update-protocol
9+bootstrap: deps $(PROTOCOL_DIR) $(PROTOCOL_LINK) update-protocol venv
10+ $(ENV)/bin/python setup.py build
11
12 docker-bootstrap: clean
13 cat dependencies.txt | xargs apt-get install -y --no-install-recommends
14 cat dependencies-devel.txt | xargs apt-get install -y --no-install-recommends
15 $(MAKE) $(PROTOCOL_DIR) $(PROTOCOL_LINK) update-protocol
16
17+venv:
18+ virtualenv --system-site-packages $(ENV)
19+ $(ENV)/bin/pip install -r requirements.txt -r requirements-devel.txt
20+
21 lint:
22- virtualenv $(ENV)
23- $(ENV)/bin/pip install flake8
24 $(ENV)/bin/flake8 --filename='*.py' --exclude='u1fsfsm.py,test_run_hello.py' ubuntuone
25
26 test: lint
27
28=== modified file 'dependencies-devel.txt'
29--- dependencies-devel.txt 2016-08-14 19:52:30 +0000
30+++ dependencies-devel.txt 2018-03-20 19:57:57 +0000
31@@ -1,6 +1,5 @@
32 bzr
33 make
34-python-flake8
35 python-mocker
36 ubuntuone-dev-tools
37 virtualenv
38
39=== added file 'requirements-devel.txt'
40--- requirements-devel.txt 1970-01-01 00:00:00 +0000
41+++ requirements-devel.txt 2018-03-20 19:57:57 +0000
42@@ -0,0 +1,1 @@
43+flake8==3.5.0
44
45=== added file 'requirements.txt'
46--- requirements.txt 1970-01-01 00:00:00 +0000
47+++ requirements.txt 2018-03-20 19:57:57 +0000
48@@ -0,0 +1,1 @@
49+Send2Trash==1.5.0
50
51=== modified file 'run-tests'
52--- run-tests 2018-03-14 21:01:56 +0000
53+++ run-tests 2018-03-20 19:57:57 +0000
54@@ -28,10 +28,6 @@
55 # version. If you delete this exception statement from all source
56 # files in the program, then also delete it here.
57
58-# Allow alternative python executable via environment variable. This is
59-# useful for virtualenv testing.
60-PYTHON=${PYTHON:-'python'}
61-
62 set -e
63 if [ $# -ne 0 ]; then
64 # run specific module given by the caller
65@@ -42,7 +38,6 @@
66 fi
67
68 SYSNAME=`uname -s`
69-
70 if [ "$SYSNAME" == "Darwin" ]; then
71 IGNORE_FILES="test_linux.py,test_windows.py"
72 IGNORE_PATHS="ubuntuone/platform/tests/linux"
73@@ -54,8 +49,5 @@
74
75 echo "*** Running test suite for ""$MODULE"" ***"
76 export SSL_CERTIFICATES_DIR=/etc/ssl/certs
77-$PYTHON ./setup.py build
78-u1trial -i "$IGNORE_FILES" -p "$IGNORE_PATHS" "$MODULE"
79-$PYTHON ./setup.py clean
80+.env/bin/python /usr/bin/u1trial -i "$IGNORE_FILES" -p "$IGNORE_PATHS" $MODULE
81 rm -rf _trial_temp
82-rm -rf build
83
84=== modified file 'ubuntuone/platform/os_helper/linux.py'
85--- ubuntuone/platform/os_helper/linux.py 2013-02-12 23:15:48 +0000
86+++ ubuntuone/platform/os_helper/linux.py 2018-03-20 19:57:57 +0000
87@@ -31,17 +31,17 @@
88 to support the linux platform.
89 """
90
91-import errno
92 import logging
93 import os
94 import shutil
95
96 try:
97- from gi.repository import Gio, GLib
98+ from gi.repository import GObject
99 has_gi = True
100 except ImportError:
101- import gio
102+ import gobject
103 has_gi = False
104+from send2trash import send2trash
105
106 from ubuntuone.platform.os_helper import unix
107
108@@ -50,44 +50,32 @@
109 logger = logging.getLogger('ubuntuone.SyncDaemon')
110
111
112+def _remove_path(path):
113+ """Remove the path, no matter if file or dir structure."""
114+ if os.path.isdir(path):
115+ shutil.rmtree(path)
116+ else:
117+ os.remove(path)
118+
119+
120 def move_to_trash(path):
121 """Move the file or dir to trash.
122
123 If had any error, or the system can't do it, just remove it.
124 """
125- if has_gi:
126- not_supported = Gio.IOErrorEnum.NOT_SUPPORTED
127- try:
128- return_code = Gio.File.new_for_path(path).trash(None)
129- except GLib.GError:
130- exc = OSError()
131- exc.errno = errno.ENOENT
132- raise exc
133- else:
134- not_supported = gio.ERROR_NOT_SUPPORTED
135- try:
136- return_code = gio.File(path).trash()
137- except gio.Error:
138- exc = OSError()
139- exc.errno = errno.ENOENT
140- raise exc
141-
142- if not return_code or return_code == not_supported:
143- logger.warning("Problems moving to trash! (%s) Removing anyway: %r",
144- return_code, path)
145- if os.path.isdir(path):
146- shutil.rmtree(path)
147- else:
148- os.remove(path)
149+ try:
150+ send2trash(path)
151+ except OSError as exc:
152+ logger.warning(
153+ "Problems moving to trash! (%s) Removing anyway: %r", exc, path)
154+ _remove_path(path)
155
156
157 def set_application_name(app_name):
158 """Set the name of the application."""
159 if has_gi:
160- from gi.repository import GObject
161 GObject.set_application_name(app_name)
162 else:
163- import gobject
164 gobject.set_application_name(app_name)
165
166
167
168=== modified file 'ubuntuone/platform/tests/os_helper/test_linux.py'
169--- ubuntuone/platform/tests/os_helper/test_linux.py 2016-05-29 19:15:01 +0000
170+++ ubuntuone/platform/tests/os_helper/test_linux.py 2018-03-20 19:57:57 +0000
171@@ -29,17 +29,11 @@
172 import logging
173 import os
174
175-try:
176- from gi.repository import Gio as gio
177- GIO_NOT_SUPPORTED = gio.IOErrorEnum.NOT_SUPPORTED
178-except ImportError:
179- import gio
180- GIO_NOT_SUPPORTED = gio.ERROR_NOT_SUPPORTED
181-
182 from twisted.internet import defer
183 from ubuntuone.devtools.handlers import MementoHandler
184
185 from ubuntuone.platform.tests.os_helper import test_os_helper
186+from ubuntuone.platform.os_helper import linux
187 from ubuntuone.platform import (
188 move_to_trash,
189 open_file,
190@@ -47,24 +41,6 @@
191 )
192
193
194-class FakeGIOFile(object):
195- """Fake File for gio."""
196-
197- _bad_trash_call = None
198-
199- def __init__(self, path):
200- pass
201-
202- @classmethod
203- def new_for_path(klass, path):
204- """Fake new_for_path for GI."""
205- return klass(path)
206-
207- def trash(self, *args):
208- """Fake trash call."""
209- return self._bad_trash_call
210-
211-
212 class OSWrapperTests(test_os_helper.OSWrapperTests):
213 """Tests for os wrapper functions."""
214
215@@ -90,50 +66,31 @@
216 self.assertNotEqual(os.stat(link).st_ino, stat_path(link).st_ino)
217 self.assertEqual(os.lstat(link).st_ino, stat_path(link).st_ino)
218
219- def test_movetotrash_file_bad(self):
220- """Something bad happen when moving to trash, removed anyway."""
221- FakeGIOFile._bad_trash_call = False # error
222- self.patch(gio, "File", FakeGIOFile)
223- path = os.path.join(self.basedir, 'foo')
224- open_file(path, 'w').close()
225- move_to_trash(path)
226- self.assertFalse(os.path.exists(path))
227- self.assertTrue(self.handler.check_warning("Problems moving to trash!",
228- "Removing anyway", "foo"))
229-
230- def test_movetotrash_dir_bad(self):
231- """Something bad happen when moving to trash, removed anyway."""
232- FakeGIOFile._bad_trash_call = False # error
233- self.patch(gio, "File", FakeGIOFile)
234- path = os.path.join(self.basedir, 'foo')
235- os.mkdir(path)
236- open_file(os.path.join(path, 'file inside directory'), 'w').close()
237- move_to_trash(path)
238- self.assertFalse(os.path.exists(path))
239- self.assertTrue(self.handler.check_warning("Problems moving to trash!",
240- "Removing anyway", "foo"))
241-
242- def test_movetotrash_file_systemnotcapable(self):
243- """The system is not capable of moving into trash."""
244- FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
245- self.patch(gio, "File", FakeGIOFile)
246- path = os.path.join(self.basedir, 'foo')
247- open_file(path, 'w').close()
248- move_to_trash(path)
249- self.assertFalse(os.path.exists(path))
250- self.assertTrue(self.handler.check_warning("Problems moving to trash!",
251- "Removing anyway", "foo",
252- "ERROR_NOT_SUPPORTED"))
253-
254- def test_movetotrash_dir_systemnotcapable(self):
255- """The system is not capable of moving into trash."""
256- FakeGIOFile._bad_trash_call = GIO_NOT_SUPPORTED
257- self.patch(gio, "File", FakeGIOFile)
258- path = os.path.join(self.basedir, 'foo')
259- os.mkdir(path)
260- open_file(os.path.join(path, 'file inside directory'), 'w').close()
261- move_to_trash(path)
262- self.assertFalse(os.path.exists(path))
263- self.assertTrue(self.handler.check_warning("Problems moving to trash!",
264- "Removing anyway", "foo",
265- "ERROR_NOT_SUPPORTED"))
266+ def test_movetotrash_bad(self):
267+ """Something bad happen when moving to trash, removed anyway.
268+
269+ Simulating this as giving a non-existant path to the function, which
270+ will make it fail with OSError, which is the exception the send2trash
271+ library raises on any problem.
272+ """
273+ called = []
274+ self.patch(linux, '_remove_path', lambda p: called.append(p))
275+
276+ path = os.path.join(self.basedir, 'non-existant')
277+ move_to_trash(path)
278+ self.assertEqual(called[0], path)
279+ self.assertTrue(self.handler.check_warning(
280+ "Problems moving to trash!", "Removing anyway", path))
281+
282+ def test_remove_path_file(self):
283+ path = os.path.join(self.basedir, 'foo')
284+ open_file(path, 'w').close()
285+ linux._remove_path(path)
286+ self.assertFalse(os.path.exists(path))
287+
288+ def test_remove_path_dir(self):
289+ path = os.path.join(self.basedir, 'foo')
290+ os.mkdir(path)
291+ open_file(os.path.join(path, 'file inside directory'), 'w').close()
292+ linux._remove_path(path)
293+ self.assertFalse(os.path.exists(path))

Subscribers

People subscribed via source and target branches

to all changes: