Merge lp:~brian.curtin/ubuntuone-control-panel/autostart-clean into lp:ubuntuone-control-panel

Proposed by Brian Curtin on 2012-03-23
Status: Merged
Approved by: Roberto Alsina on 2012-03-26
Approved revision: 306
Merged at revision: 298
Proposed branch: lp:~brian.curtin/ubuntuone-control-panel/autostart-clean
Merge into: lp:ubuntuone-control-panel
Diff against target: 216 lines (+128/-0)
5 files modified
ubuntuone/controlpanel/gui/qt/gui.py (+2/-0)
ubuntuone/controlpanel/gui/qt/tests/test_gui.py (+16/-0)
ubuntuone/controlpanel/utils/__init__.py (+2/-0)
ubuntuone/controlpanel/utils/tests/test_windows.py (+88/-0)
ubuntuone/controlpanel/utils/windows.py (+20/-0)
To merge this branch: bzr merge lp:~brian.curtin/ubuntuone-control-panel/autostart-clean
Reviewer Review Type Date Requested Status
Roberto Alsina (community) 2012-03-23 Approve on 2012-03-26
Natalia Bidart Approve on 2012-03-26
Manuel de la Peña (community) Approve on 2012-03-23
Review via email: mp+99089@code.launchpad.net

Commit Message

- Add Ubuntu One to the Windows auto-start registry key

Description of the Change

Introduce add_to_autostart (from ubuntuone-windows-installer) and add it to the install wizard for new users as well as ensure it gets called for users who already have credentials.

To post a comment you must log in.
Natalia Bidart (nataliabidart) wrote :

If we're a already calling add_to_autostart from ubuntuone/controlpanel/gui/qt/gui.py, why would you also add it in the wizard code?

Unless I'm missing something (please let me know), there is no need to do that. Also, semantically, the wizard is a 're usable' widget, and we would not like to leak thing like adding to autostart when using it.

review: Needs Fixing
Manuel de la Peña (mandel) wrote :

Adding tho the comments of natalia, I would call the parents setup before I do any patching or operations. Also I would not add the setup of we only have one test, but im lazy :)

review: Needs Fixing
301. By Brian Curtin on 2012-03-23

Removed an add_to_autostart call which is no longer needed.

Brian Curtin (brian.curtin) wrote :

Rev 301 removes the call Natalia was talking about.

Manuel: I need that ordering in the setUp because I need add_to_autostart patched before anything else up the chain runs. The add_to_autostart is called when MainWindow is created, and it's created within BaseTestCase, so I have to get patched in before then.

302. By Brian Curtin on 2012-03-23

Add docstrings to two test classes

Natalia Bidart (nataliabidart) wrote :

ubuntuone/controlpanel/gui/qt/tests/test_gui.py:
    130: [C0111, AutoStartTestCase.test_add_to_autostart] Missing docstring

ubuntuone/controlpanel/utils/tests/test_windows.py:
    162: [W0622, AutostartTestCase.test_add_syncdaemon_to_autostart] Redefining built-in 'dir'

ubuntuone/controlpanel/utils/windows.py:
    22: [F0401] Unable to import '_winreg'

review: Needs Fixing
303. By Brian Curtin on 2012-03-23

fix lint errors

Brian Curtin (brian.curtin) wrote :

Rev 303 fixes the remaining lint issues.

Natalia Bidart (nataliabidart) wrote :

Lookis at the logic inside add_to_autostart, is there any reason to have the "else: return" in the middle of the code? Why not just:

def add_to_autostart():
    """Add syncdaemon to the session's autostart."""
    if getattr(sys, "frozen", False):
        sd_path = '"%s"' % os.path.join(os.path.dirname(
            os.path.abspath(sys.executable)),
            "ubuntuone-syncdaemon.exe")
        u1cp_path = '"%s"' % os.path.join(os.path.dirname(
            os.path.abspath(sys.executable)),
            "ubuntuone-control-panel-qt.exe")

        with _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, AUTORUN_KEY,
                             0, _winreg.KEY_ALL_ACCESS) as key:
            # pylint: disable=E0602
            _winreg.SetValueEx(key, "Ubuntu One", 0, _winreg.REG_SZ, sd_path)
            _winreg.SetValueEx(key, "Ubuntu One Icon", 0, _winreg.REG_SZ,
                               u1cp_path + " --minimized --with-icon")

Brian Curtin (brian.curtin) wrote :

That was an artifact of the old implementation from ubuntuone-windows-installer. Removed.

304. By Brian Curtin on 2012-03-23

Remove unnecessary else/return block

Manuel de la Peña (mandel) wrote :

> Rev 301 removes the call Natalia was talking about.
>
> Manuel: I need that ordering in the setUp because I need add_to_autostart
> patched before anything else up the chain runs. The add_to_autostart is called
> when MainWindow is created, and it's created within BaseTestCase, so I have to
> get patched in before then.

Very well then.. I would have added a comment to state so to avoid stupid reviewers :)

Few things:

8 +from ubuntuone.controlpanel.utils import add_to_autostart

I know we do take a lot of effort in doing alphabetical ordered imports, can you move the import a few lines? I'm the first one to ignore this rule, but is a style we try to keep.

Besides that every thing looks good so I'll approve assuming you will order the imports!

PS: I had no idea _winreg could be a context manager!

review: Approve
305. By Brian Curtin on 2012-03-26

Alphabetical import ordering

306. By Brian Curtin on 2012-03-26

Add comment explaining patching order

Natalia Bidart (nataliabidart) wrote :

Looks good!

review: Approve
Roberto Alsina (ralsina) wrote :

+1

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntuone/controlpanel/gui/qt/gui.py'
2--- ubuntuone/controlpanel/gui/qt/gui.py 2012-03-21 19:17:21 +0000
3+++ ubuntuone/controlpanel/gui/qt/gui.py 2012-03-26 13:36:23 +0000
4@@ -20,6 +20,7 @@
5
6 from ubuntuone.controlpanel.gui.qt.systray import TrayIcon
7 from ubuntuone.controlpanel.gui.qt.ui import mainwindow_ui
8+from ubuntuone.controlpanel.utils import add_to_autostart
9
10 # pylint: disable=E0611
11 try:
12@@ -49,6 +50,7 @@
13 self.installer = installer
14 if installer:
15 self.ui.control_panel.start_from_license()
16+ add_to_autostart()
17 if USE_LIBUNITY:
18 self.entry = Unity.LauncherEntry.get_for_desktop_id(U1_DOTDESKTOP)
19 else:
20
21=== modified file 'ubuntuone/controlpanel/gui/qt/tests/test_gui.py'
22--- ubuntuone/controlpanel/gui/qt/tests/test_gui.py 2012-03-09 01:05:49 +0000
23+++ ubuntuone/controlpanel/gui/qt/tests/test_gui.py 2012-03-26 13:36:23 +0000
24@@ -18,6 +18,8 @@
25
26 """Tests for the Qt UI."""
27
28+from twisted.internet import defer
29+
30 from ubuntuone.controlpanel.gui.qt import gui
31 from ubuntuone.controlpanel.gui.qt.tests import BaseTestCase
32
33@@ -115,3 +117,17 @@
34 self.patch(self.ui, "entry", entry)
35 self.ui.set_urgent("foo")
36 self.assertEqual(entry.called, (('urgent', "foo"), {}))
37+
38+
39+class AutoStartTestCase(MainWindowTestCase):
40+ """Test the add_to_autostart call."""
41+
42+ @defer.inlineCallbacks
43+ def setUp(self):
44+ # Be sure to patch add_to_autostart *before* class_ui creation occurs.
45+ self.patch(gui, "add_to_autostart", self._set_called)
46+ yield super(AutoStartTestCase, self).setUp()
47+
48+ def test_add_to_autostart(self):
49+ """Test that the add_to_autostart function is called when CP opens."""
50+ self.assertTrue(self._called)
51
52=== modified file 'ubuntuone/controlpanel/utils/__init__.py'
53--- ubuntuone/controlpanel/utils/__init__.py 2012-03-16 16:40:27 +0000
54+++ ubuntuone/controlpanel/utils/__init__.py 2012-03-26 13:36:23 +0000
55@@ -38,11 +38,13 @@
56 are_updates_present = windows.are_updates_present
57 default_folders = windows.default_folders
58 perform_update = windows.perform_update
59+ add_to_autostart = windows.add_to_autostart
60 else:
61 from ubuntuone.controlpanel.utils import linux
62 are_updates_present = lambda *args, **kwargs: False
63 default_folders = linux.default_folders
64 perform_update = lambda *args, **kwargs: None
65+ add_to_autostart = lambda *args, **kwargs: None
66
67 # pylint: enable=C0103
68
69
70=== modified file 'ubuntuone/controlpanel/utils/tests/test_windows.py'
71--- ubuntuone/controlpanel/utils/tests/test_windows.py 2012-03-16 19:55:22 +0000
72+++ ubuntuone/controlpanel/utils/tests/test_windows.py 2012-03-26 13:36:23 +0000
73@@ -18,6 +18,7 @@
74 """Test the windows utils functions."""
75
76 import os
77+import sys
78
79 from twisted.internet import defer
80
81@@ -87,6 +88,93 @@
82 self.assertEqual(0, self._called[0][5])
83
84
85+class FakeOpenKey(object):
86+
87+ """A Fake OpenKey class.
88+
89+ This class becomes a method-like callable on FakeRegistry, allowing
90+ FakeRegistry.OpenKey to operate as a context manager."""
91+
92+ def __init__(self):
93+ self.openkey_args = None
94+ super(FakeOpenKey, self).__init__()
95+
96+ def __call__(self, *args, **kwargs):
97+ self.openkey_args = (args, kwargs)
98+ return self
99+
100+ def __enter__(self, *args, **kwargs):
101+ #self.openkey_args = (args, kwargs)
102+ return self
103+
104+ def __exit__(self, *args):
105+ pass
106+
107+
108+class FakeRegistry(object):
109+
110+ """A fake registry."""
111+
112+ # pylint: disable=C0103
113+ HKEY_CURRENT_USER = 2
114+ KEY_ALL_ACCESS = 4
115+ REG_SZ = 1
116+
117+ def __init__(self):
118+ self.HKEY_CURRENT_USER = 2
119+ self.KEY_ALL_ACCESS = 4
120+ self.openkey_args = None
121+ self.query_args = None
122+ self.set_args = []
123+
124+ self.OpenKey = FakeOpenKey()
125+
126+ def QueryValueEx(self, *args, **kwargs):
127+ """Fake QueryValueEx."""
128+ self.query_args = (args, kwargs)
129+
130+ def SetValueEx(self, *args, **kwargs):
131+ """Fake SetValueEx."""
132+ self.set_args.append((args, kwargs))
133+
134+
135+class AutostartTestCase(TestCase):
136+
137+ """Test add_syncdaemon_to_autostart."""
138+
139+ @defer.inlineCallbacks
140+ def setUp(self):
141+ """Initialize this test instance."""
142+ self.registry = FakeRegistry()
143+ self.patch(utils.windows, "_winreg", self.registry)
144+ yield super(AutostartTestCase, self).setUp()
145+
146+ def test_add_syncdaemon_to_autostart(self):
147+ """Check that the registry is updated correctly."""
148+ # I can't patch sys because frozen is not there by default
149+ sys.frozen = True
150+ self.addCleanup(delattr, sys, 'frozen')
151+ utils.windows.add_to_autostart()
152+ self.assertEqual(self.registry.OpenKey.openkey_args,
153+ ((self.registry.HKEY_CURRENT_USER, utils.windows.AUTORUN_KEY, 0,
154+ self.registry.KEY_ALL_ACCESS), {}))
155+ self.assertEqual(self.registry.query_args, None)
156+ path = os.path.dirname(os.path.abspath(sys.executable))
157+ self.assertEqual(self.registry.set_args,
158+ [((self.registry.OpenKey, 'Ubuntu One', 0, 1,
159+ '"%s\\ubuntuone-syncdaemon.exe"' % path), {}),
160+ ((self.registry.OpenKey, 'Ubuntu One Icon', 0, 1,
161+ '"%s\\ubuntuone-control-panel-qt.exe" --minimized' % path),
162+ {})])
163+
164+ def test_not_added_if_not_frozen(self):
165+ """Not frozen binaries are not added to the registry."""
166+ utils.windows.add_to_autostart()
167+ self.assertEqual(self.registry.openkey_args, None)
168+ self.assertEqual(self.registry.query_args, None)
169+ self.assertEqual(self.registry.set_args, [])
170+
171+
172 class DefaultFoldersTestCase(TestCase):
173 """Test the default_folders method."""
174
175
176=== modified file 'ubuntuone/controlpanel/utils/windows.py'
177--- ubuntuone/controlpanel/utils/windows.py 2012-03-16 19:55:22 +0000
178+++ ubuntuone/controlpanel/utils/windows.py 2012-03-26 13:36:23 +0000
179@@ -23,6 +23,7 @@
180 # Avoid pylint error on Linux
181 # pylint: disable=F0401
182 import win32api
183+import _winreg
184
185 from win32com.shell import shell, shellcon
186 # pylint: enable=F0401
187@@ -33,6 +34,7 @@
188
189 logger = setup_logging('utils.windows')
190 AUTOUPDATE_EXE_NAME = 'autoupdate-windows.exe'
191+AUTORUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
192
193
194 def _get_update_path():
195@@ -96,3 +98,21 @@
196 win32api.ShellExecute(None, 'runas',
197 update_path,
198 '--unattendedmodeui none', '', 0)
199+
200+
201+def add_to_autostart():
202+ """Add syncdaemon to the session's autostart."""
203+ if getattr(sys, "frozen", False):
204+ sd_path = '"%s"' % os.path.join(os.path.dirname(
205+ os.path.abspath(sys.executable)),
206+ "ubuntuone-syncdaemon.exe")
207+ u1cp_path = '"%s"' % os.path.join(os.path.dirname(
208+ os.path.abspath(sys.executable)),
209+ "ubuntuone-control-panel-qt.exe")
210+
211+ with _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, AUTORUN_KEY,
212+ 0, _winreg.KEY_ALL_ACCESS) as key:
213+ # pylint: disable=E0602
214+ _winreg.SetValueEx(key, "Ubuntu One", 0, _winreg.REG_SZ, sd_path)
215+ _winreg.SetValueEx(key, "Ubuntu One Icon", 0, _winreg.REG_SZ,
216+ u1cp_path + " --minimized --with-icon")

Subscribers

People subscribed via source and target branches