So yet again I have been confronted with broken tests in Ubuntu One. As I have already mentioned before I have spent a significant amount of time ensuring that the tests of Ubuntu One (which use twisted a lot) are deterministic and we do not leave a dirty reactor in the way. In order to do that a few week a go I wrote the following code that will help the rest of the team write such tests:

importosimportshutilimporttempfilefrom twisted.internetimport defer, endpoints, protocol
from twisted.spreadimport pb
from ubuntuone.devtools.testcasesimport BaseTestCase
# no init method + twisted common warnings# pylint: disable=W0232, C0103, E1101def server_protocol_factory(cls):
"""Factory to create tidy protocols."""if cls isNone:
cls = protocol.Protocolclass ServerTidyProtocol(cls):
"""A tidy protocol."""def connectionLost(self, *args):
"""Lost the connection."""
cls.connectionLost(self, *args)# lets tell everyone# pylint: disable=W0212if(self.factory._disconnecting
andself.factory.testserver_on_connection_lostisnotNoneandnotself.factory.testserver_on_connection_lost.called):
self.factory.testserver_on_connection_lost.callback(self)# pylint: enable=W0212return ServerTidyProtocol
def client_protocol_factory(cls):
"""Factory to create tidy protocols."""if cls isNone:
cls = protocol.Protocolclass ClientTidyProtocol(cls):
"""A tidy protocol."""def connectionLost(self, *a):
"""Connection list."""# pylint: disable=W0212if(self.factory._disconnecting
andself.factory.testserver_on_connection_lostisnotNoneandnotself.factory.testserver_on_connection_lost.called):
self.factory.testserver_on_connection_lost.callback(self)# pylint: enable=W0212
cls.connectionLost(self, *a)return ClientTidyProtocol
class TidySocketServer(object):
"""Ensure that twisted servers are correctly managed in tests.
Closing a twisted server is a complicated matter. In order to do so you
have to ensure that three different deferreds are fired:
1. The server must stop listening.
2. The client connection must disconnect.
3. The server connection must disconnect.
This class allows to create a server and a client that will ensure that
the reactor is left clean by following the pattern described at
http://mumak.net/stuff/twisted-disconnect.html
"""def__init__(self):
"""Create a new instance."""self.listener=Noneself.server_factory=Noneself.connector=Noneself.client_factory=Nonedef get_server_endpoint(self):
"""Return the server endpoint description."""raiseNotImplementedError('To be implemented by child classes.')def get_client_endpoint(self):
"""Return the client endpoint description."""raiseNotImplementedError('To be implemented by child classes.')@defer.inlineCallbacksdef listen_server(self, server_class, *args, **kwargs):
"""Start a server in a random port."""from twisted.internetimport reactor
self.server_factory= server_class(*args, **kwargs)self.server_factory._disconnecting =Falseself.server_factory.testserver_on_connection_lost= defer.Deferred()self.server_factory.protocol= server_protocol_factory(self.server_factory.protocol)
endpoint = endpoints.serverFromString(reactor,self.get_server_endpoint())self.listener=yield endpoint.listen(self.server_factory)
defer.returnValue(self.server_factory)@defer.inlineCallbacksdef connect_client(self, client_class, *args, **kwargs):
"""Conect a client to a given server."""from twisted.internetimport reactor
ifself.server_factoryisNone:
raiseValueError('Server Factory was not provided.')ifself.listenerisNone:
raiseValueError('%s has not started listening.',self.server_factory)self.client_factory= client_class(*args, **kwargs)self.client_factory._disconnecting =Falseself.client_factory.protocol= client_protocol_factory(self.client_factory.protocol)self.client_factory.testserver_on_connection_lost= defer.Deferred()
endpoint = endpoints.clientFromString(reactor,self.get_client_endpoint())self.connector=yield endpoint.connect(self.client_factory)
defer.returnValue(self.client_factory)def clean_up(self):
"""Action to be performed for clean up."""ifself.server_factoryisNoneorself.listenerisNone:
# nothing to cleanreturn defer.succeed(None)ifself.listenerandself.connector:
# clean client and serverself.server_factory._disconnecting =Trueself.client_factory._disconnecting =Trueself.connector.transport.loseConnection()
d = defer.maybeDeferred(self.listener.stopListening)return defer.gatherResults([d,self.client_factory.testserver_on_connection_lost,self.server_factory.testserver_on_connection_lost])ifself.listener:
# just clean the server since there is no clientself.server_factory._disconnecting =Truereturn defer.maybeDeferred(self.listener.stopListening)class TidyTCPServer(TidySocketServer):
"""A tidy tcp domain sockets server."""
client_endpoint_pattern ='tcp:host=127.0.0.1:port=%s'
server_endpoint_pattern ='tcp:0:interface=127.0.0.1'def get_server_endpoint(self):
"""Return the server endpoint description."""returnself.server_endpoint_patterndef get_client_endpoint(self):
"""Return the client endpoint description."""ifself.server_factoryisNone:
raiseValueError('Server Factory was not provided.')ifself.listenerisNone:
raiseValueError('%s has not started listening.',self.server_factory)returnself.client_endpoint_pattern % self.listener.getHost().portclass TidyUnixServer(TidySocketServer):
"""A tidy unix domain sockets server."""
client_endpoint_pattern ='unix:path=%s'
server_endpoint_pattern ='unix:%s'def__init__(self):
"""Create a new instance."""super(TidyUnixServer,self).__init__()self.temp_dir=tempfile.mkdtemp()self.path=os.path.join(self.temp_dir,'tidy_unix_server')def get_server_endpoint(self):
"""Return the server endpoint description."""returnself.server_endpoint_pattern % self.pathdef get_client_endpoint(self):
"""Return the client endpoint description."""returnself.client_endpoint_pattern % self.pathdef clean_up(self):
"""Action to be performed for clean up."""
result =super(TidyUnixServer,self).clean_up()# remove the dir once we are disconnected
result.addCallback(lambda _: shutil.rmtree(self.temp_dir))return result
class ServerTestCase(BaseTestCase):
"""Base test case for tidy servers."""@defer.inlineCallbacksdef setUp(self):
"""Set the diff tests."""yieldsuper(ServerTestCase,self).setUp()try:
self.server_runner=self.get_server()exceptNotImplementedError:
self.server_runner=Noneself.server_factory=Noneself.client_factory=Noneself.server_disconnected=Noneself.client_connected=Noneself.client_disconnected=Noneself.listener=Noneself.connector=Noneself.addCleanup(self.tear_down_server_client)def get_server(self):
"""Return the server to be used to run the tests."""raiseNotImplementedError('To be implemented by child classes.')@defer.inlineCallbacksdef listen_server(self, server_class, *args, **kwargs):
"""Listen a server.
The method takes the server class and the arguments that should be
passed to the server constructor.
"""self.server_factory=yieldself.server_runner.listen_server(
server_class, *args, **kwargs)self.server_disconnected=self.server_factory.testserver_on_connection_lostself.listener=self.server_runner.listener@defer.inlineCallbacksdef connect_client(self, client_class, *args, **kwargs):
"""Connect the client.
The method takes the client factory class and the arguments that
should be passed to the client constructor.
"""self.client_factory=yieldself.server_runner.connect_client(
client_class, *args, **kwargs)self.client_disconnected=self.client_factory.testserver_on_connection_lostself.connector=self.server_runner.connectordef tear_down_server_client(self):
"""Clean the server and client."""ifself.server_runner:
returnself.server_runner.clean_up()class TCPServerTestCase(ServerTestCase):
"""Test that uses a single twisted server."""def get_server(self):
"""Return the server to be used to run the tests."""return TidyTCPServer()class UnixServerTestCase(ServerTestCase):
"""Test that uses a single twisted server."""def get_server(self):
"""Return the server to be used to run the tests."""return TidyUnixServer()class PbServerTestCase(ServerTestCase):
"""Test a pb server."""def get_server(self):
"""Return the server to be used to run the tests."""raiseNotImplementedError('To be implemented by child classes.')@defer.inlineCallbacksdef listen_server(self, *args, **kwargs):
"""Listen a pb server."""yieldsuper(PbServerTestCase,self).listen_server(pb.PBServerFactory,
*args, **kwargs)@defer.inlineCallbacksdef connect_client(self, *args, **kwargs):
"""Connect a pb client."""yieldsuper(PbServerTestCase,self).connect_client(pb.PBClientFactory,
*args, **kwargs)class TCPPbServerTestCase(PbServerTestCase):
"""Test a pb server over TCP."""def get_server(self):
"""Return the server to be used to run the tests."""return TidyTCPServer()class UnixPbServerTestCase(PbServerTestCase):
"""Test a pb server over Unix domain sockets."""def get_server(self):
"""Return the server to be used to run the tests."""return TidyUnixServer()

The idea of the code is that developers do not need to worry about how to stop listening ports in their tests and just write tests like the following:

As you can see those tests do not give a rats ass about ensuring that the clients lose connection or we stop listening ports… Or so I though because the following code made such approach break in Mac OS X (although I suspect it was broken on Linux and Windows but we never experienced it):

While in all the other platforms the tests passed with no problems on Mac OS X the tests would block in the clean_up method from the server because the deferred that was called in the connectionLost from the ServerTidyProtocol was never fired… Interesting.. After digging in the code I realized that the main issue with the approach of the clean_up code was wrong. The problem relies on the way in which the NullProtocol works. As you can see in the code the protocol loses its connections as soon as it made. This results in to possible things:

The server does know that we have a client connected and calls buildProtocol.

The connection is lost so fast that the buildProtocol on the ServerFactory does not get call.

When running the tests on Windows and Linux we were always facing the first scenario, buildProtocol was called which meant that connectionLost in the server protocol would be called. On the other hand, on Mac OS X, 1 out of 10 runs of the tests would block in the clean up because we would be in the second scenario, that is, no protocol would be build in the ServerFactory which results in the connectionLost never being called because it was no needed. The work around this issue is quite simple once you understand what is going on. The ServerFactory has to be modified to set the deferred when buildProtocol is called and not before ensuring that when we cleanup we check if the deferred is None and if it is not we wait for it to be fired. The fixed version of the helper code is the following:

The last few days I have taken a considerable time to remove all the errors from the Ubuntu One tests on Windows. The tests were leaving tcp connections in the twisted reactor that on some Windows systems will result on a DirtyReactorError which is a PITA. Due to a refactor the pattern I described here was remove (ouch!) and I had to re-add it. In this case, instead of doing the pattern everywhere is needed I created some helper classes that will ensure that the clients and servers are correctly cleaned up after you use them.

When using this webserver you have to be careful because we do not longer pay attention to the client protocols, if you do not use twisted to access it you have no problems (libsoup, qtnetwork etc..) but if, for example, you use the HTTPClientFactory you have to do something similar to this in your TestCase setUp:

Due to a small project I’m developing at the moment I had to access the Mac OS X keyring over python. There is some code of seen around but was not very ‘clean’. The following is my attempt to access the Mac OS X Keyring via python using ctypes, I hope it helps someone out there:

I have not added the code of the decorator because they are just noise, the only thing they do is to check that the keyring was indeed opened (self.keyring != None) and that the parameters with the given index are not None (I’m lazy and I prefer to use decorators for this mundane tasks that are done everywhere.

On Windows Ubuntu One uses the twisted reactor to run the Qt UI. The main reason for this is that the IPC protocol that is used was written in twisted. This has been giving us a number of head aches like the one we experienced today.

The following code simply shows a dialog that will as the user for his proxy credentials and will store them in the key-ring of which ever platform is being used:

And to give even more details, the following is what is used to spawn a thread to store the credentials on windows:

def set_credentials(self, app_name, cred):
"""Set the credentials of the Ubuntu SSO item."""# the windows keyring can only store a pair username-password# so we store the data using ubuntu_sso as the user name. Then# the cred will be stored as the string representation of the dict.return deferToThread(self.keyring.set_password, app_name, USERNAME,
dumps(cred))

A priori there is nothing wrong with the code, or is it? Doing an IRL test you will see that the credentials are never stored, what’s even more none of the deferreds are called. But why? In theory the qt reactor should be taking care of everything which includes the deferreds, the deferToThread and the execution of the ui.. well, it is not. When we look a little closer we can see that we use the exec_ method from the QDialog and this is the root of the bug. Lets put an example, the following is possible in Qt:

As you can see we are launching the dialog but we did not execute the application, but why is that? The main reason is found in the implementation of exec in the QDialog class (this time in C++, ouch!):

As you can see the implementation uses a QEventLoop, if you read the documentation you will noticed that using exec of the event loops and the default flag all the events will me processed by this event loop, which is not the main event loop, but a child one (small brain fuck here). This means that, due to the implementation of the qtreactor, when using a dialog exec_ (or better say a QEventLoop.exec method) the reactor main loop is not processing the events which means that all your deferreds, deferToThread etc.. will not be processed

Nevertheless there is a way to work around this bug in the qtreactor implementation (because is not the qt fault and it is well documented) which is doing the following workaround:

I have been writing some integration tests lately between Ubuntu One and proxies which use SSL certificates. The idea behind this tests was to be able to test that we deal correctly with those certificates that are not correct (notify the user, remember exceptions, etc..) For that I wrote this small function that I used to generate the certificates.

Recently in Ubuntu One we have been working or using PyQt for the UI of our application so that we could keep a smaller code base. While doing the move I noticed that we needed to have a widget similar to GtkArrow and to my surprise there is not one.

The following is a small implementation fo such a widget that might help other people that are in need of it. Even maybe someone that cares enough will write it in C++ and will propose it to Qt, sorry but I don’t have the time.

Yesterday I was working on a small UI that uses a WebKit.WebView to show certain information to the user. My idea yesterday was to try and show a native context menu on the view that does not have the default menu items that the WebView has, the only way I’ve found to this is a as follows:

from gi.repositoryimport GObject, Gdk, Gtk, WebKit
from utils import get_data_file
class CustomView(Gtk.EventBox):
"""Main window used to show the ui and generate signals."""def__init__(self):
"""Create a new instance."""super(CustomView, self).__init__()self.set_events(Gdk.EventMask.BUTTON_PRESS_MASK)self._create_context_menu()self.scroll = Gtk.ScrolledWindow()self.scroll.show()self.view = WebKit.WebView()self.view.show()self.scroll.add(self.view)# load the html used for the main window, from that point# on we will let the html and js do the work and raise# signals via the titleself.view.open(get_data_file(MAIN_WINDOW_HMTL))# lets no use the webkit context menu
settings = self.view.get_settings()
settings.set_property('enable-default-context-menu',
False)self.view.set_settings(settings)self.add(self.scroll)# connect the on button pressed event of the event box# so that we can add a context menuself.connect('button_press_event', self._on_button_press_event)def _create_context_menu(self):
"""Create the context menu."""self.menu = Gtk.Menu()
delete_menu = Gtk.MenuItem("Delete Task")self.menu.append(delete_menu)def _on_button_press_event(self, widget, event):
"""Deal with the button press event."""if event.button == 3:
self.menu.popup(None, None, None, None, event.button, event.time)self.menu.show_all()

Does any one out there know a better way to do this. I know that there is a “populate-popup” signal I can listen to but I cannot get my head around on how to do exactly what I want, which is remove all defaul menu items and add my own. And, does it make sense to have to do that for every popup signal?

This is not utterly complicated but I’m notice that there are not many examples out there, so there you go. There is no code there that can be considered hard but I’d like to point out that if you use the get_value method from the Settings object you will have to call the appropriate get_* method from the returned GVariant, that is:

As Tim-Erwin said in his comments, the above code was not being blocked in the WaitForSingleObject call but in the ReadDirecotryChangesW one. This is clearly my fault because in the blog post I did not give a very important detail that is needed to get the code working async. In order for the above to work correctly it is imperative that you pass to the CreateFile function the FILE_FLAG_OVERLAPPED flag. That means, that in order to fix the code and be blocked in WaitForSingleObject we have to modify the CreateFile call in the following way:

In the past days I have been working on implementing a python TestCase that can be used to perform integration tests to the future implementation of proxy support that will be landing in Ubuntu One. The idea of the TestCase is the following:

Start a proxy so that connections go throw it. The proxy has to be listening to two different ports, one in which auth is not required and a second one in which auth is required. At the moment the only supported proxy is Squid using base auth.

The test case should provide a way to access to the proxy details for subclasses to use.

The test case should integrate with the ubuntuone-dev-tools.

Initially, one of the major problems I had was to start squid in two different ports so that:

Port A accepts non-auth requests.

Port A rejects auth requests.

Port B accepts auth requests.

The idea is simple, if you use port A you should never auth while you must in port B, and example configuration of the ACLs and ports is the following:

Once the above was achieved the code of the test case was quite simple for Ubuntu O, unfortunatly, it was not that issues in Ubuntu P because there we have squid3 which supports http 1.1 and keeps the proxy keeps the connection alive. The fact that the connection is kept alive means that the reactor has a selectable running because the proxy keep it there. In order to solve the issue I wrote the code so that the server could say that the connection timedout. Here is the code that does it:

class SaveHTTPChannel(http.HTTPChannel):
"""A save protocol to be used in tests."""
protocolInstance = Nonedef connectionMade(self):
"""Keep track of the given protocol."""
SaveHTTPChannel.protocolInstance = self
http.HTTPChannel.connectionMade(self)class SaveSite(server.Site):
"""A site that let us know when it closed."""
protocol = SaveHTTPChannel
def__init__(self, *args, **kwargs):
"""Create a new instance."""
server.Site.__init__(self, *args, **kwargs)# we disable the timeout in the tests, we will deal with it manually.self.timeOut = None

The above defines a protocol that will know the instance that it was used so that we can trigger the time out in a clean up function.

The following is some code in which I have been working (and stupidly wasting time in a small error) that allows to get a page using a methos similar to twisted.web.client.getPage through a proxy that uses base auth.

Recently a very interesting bug has been reported agains Ubuntu One on Windows. Apparently we try to sync a number of system folders that are present on Windows 7 to be backward compatible.

The problem

The actual problem in the code is that we are using os.listdir. While lisdir on python does return system folders (at the end of the day, they are there) os.walk does not, for example, lets imaging hat we have the following scenario:

Having a DirtyReactorException in your tests is a PITA and bug 885342 was that type on annoying bug. Since I use this bug not only to tell others what I’m doing but as a log for myself here it is the way to clean the resources nicely when you are testing your PB clients and servers (I mention PB because we use that, a similar approach can be used with any protocol) inspired by this way more interesting post.

Two really good developers, Alecu and Diego, have discovered a very interestning bug in the os.path.expanduser function in Python. If you have a user in your Windows machine with a name hat uses Japanese characters like “??????” you will have the following in your system:

The Windows Shell will show the path correctly, that is: “C:\Users\??????”

cmd.exe will show: “C:\Users\??????”

All the env variables will be wrong, which means they will be similar to the info shown in cmd.exe

The above is clearly a problem, specially when the implementation of os.path.expanduser on Winodws is:

The above code ensure that we only use SHGetFolderPathW when SHGetKnownFolderPathW is not available in the system. The reasoning for that is that SHGetFolderPathW is deprecated and new applications are encourage to use SHGetKnownFolderPathW.

A much better solution is to patch ntpath.py so that is something like what I propose for Ubuntu One. Does anyone know if this is fixed in Python 3? Shall I propose a fix?

On Ubuntu One we use BtiRock to create the packages for Windows. One of the new features I’m working on is to check if there are updates every so often so that the user gets notified. This code on Ubuntu is not needed because the Update Manger takes care of that, but when you work in an inferior OS…

Generate the auto-update.exe

In order to check for updates we use the generated auto-update.exe wizard from BitRock. Generating the wizard is very straight forward first, as with most of the BitRock stuff, we generate the XML to configure the generated .exe.

There is just a single thing that is worth mentioning about the above XML. The requireInstallationByRootUser is true because we use the generated .exe to check if there are updates present and we do not what the user to have to be root for that, it does not make sense. Once you have the above or similar XML you can execute:

The new Ubuntu One Windows client is very close to be released (we have already been sending the new code to our beta testers) and in order to make life easier to new user we wanted to provide a migration script that will allow the user migrate his data to the new client and uninstall the old one. In order to be able to know if the old msi is present in the system we had to use the Windows Installer SDK to query the installed applications and find if the old Ubuntu One client is present.

The following code is a small script that contains the functions to query the installed software in the system which is very similar to the script found in WiLstPrd.vbs but using python instead of VB and ctypes.

# This scripts allows to get a list of all installed products in a windows# machine. The code uses ctypes becuase there were a number of issues when# trying to achieve the same win win32com.clientfromcollectionsimport namedtuple
from ctypes import byref, create_unicode_buffer, windll
from ctypes.wintypesimport DWORD
fromitertoolsimport count
# defined at http://msdn.microsoft.com/en-us/library/aa370101(v=VS.85).aspx
UID_BUFFER_SIZE = 39
PROPERTY_BUFFER_SIZE = 256
ERROR_MORE_DATA = 234
ERROR_INVALID_PARAMETER = 87
ERROR_SUCCESS = 0
ERROR_NO_MORE_ITEMS = 259
ERROR_UNKNOWN_PRODUCT = 1605# diff propoerties of a product, not all products have all properties
PRODUCT_PROPERTIES = [u'Language',
u'ProductName',
u'PackageCode',
u'Transforms',
u'AssignmentType',
u'PackageName',
u'InstalledProductName',
u'VersionString',
u'RegCompany',
u'RegOwner',
u'ProductID',
u'ProductIcon',
u'InstallLocation',
u'InstallSource',
u'InstallDate',
u'Publisher',
u'LocalPackage',
u'HelpLink',
u'HelpTelephone',
u'URLInfoAbout',
u'URLUpdateInfo',]# class to be used for python users :)
Product = namedtuple('Product', PRODUCT_PROPERTIES)def get_property_for_product(product, property, buf_size=PROPERTY_BUFFER_SIZE):
"""Retruns the value of a fiven property from a product."""
property_buffer = create_unicode_buffer(buf_size)
size = DWORD(buf_size)
result = windll.msi.MsiGetProductInfoW(product, property, property_buffer,
byref(size))if result == ERROR_MORE_DATA:
return get_property_for_product(product, property,
2* buf_size)elif result == ERROR_SUCCESS:
return property_buffer.valueelse:
returnNonedef populate_product(uid):
"""Return a Product with the different present data."""
properties = []forpropertyin PRODUCT_PROPERTIES:
properties.append(get_property_for_product(uid, property))return Product(*properties)def get_installed_products_uids():
"""Returns a list with all the different uid of the installed apps."""# enum will return an error code according to the result of the app
products = []for i in count(0):
uid_buffer = create_unicode_buffer(UID_BUFFER_SIZE)
result = windll.msi.MsiEnumProductsW(i, uid_buffer)if result == ERROR_NO_MORE_ITEMS:
# done interating over the collectionbreak
products.append(uid_buffer.value)return products
def get_installed_products():
"""Returns a collection of products that are installed in the system."""
products = []for puid in get_installed_products_uids():
products.append(populate_product(puid))return products
def is_product_installed_uid(uid):
"""Return if a product with the given id is installed.
uid Most be a unicode object with the uid of the product using
the following format {uid}
"""# we try to get the VersisonString for the uid, if we get an error it means# that the product is not installed in the system.
buf_size = 256
uid_buffer = create_unicode_buffer(uid)property = u'VersionString'
property_buffer = create_unicode_buffer(buf_size)
size = DWORD(buf_size)
result = windll.msi.MsiGetProductInfoW(uid_buffer, property, property_buffer,
byref(size))if result == ERROR_UNKNOWN_PRODUCT:
returnFalseelse:
returnTrue

The above code will allow a python developer to check which products are installed on Windows as well as to know if a product with the given UID is indeed installed.

In a few days (well, if I find some kind person to take care of Iron) I will be attending the Ubuntu One Developer evening in which we should be able to hear Stuart will be talking about the bunch of crazy ideas he has for developers to use or infrastructure to do cool stuff. I’ll be there for two reason:

Hear what Stuart has been planning. I’ve got to recognized I should know a lot more of the Ubuntu One developer program but unfortunately I have been in the working in the Windows port too much and I have ignored the rests of the teams… mea culpa .

Learn a few more things of the APIs so that I can find my little Chrome extension that uses Ubuntu One (no, it is not bookmark sync, I cannot be less interested in that!).

See a bunch of developers and learn about there ideas and what they are doing.

Drinks, drinks, drinks! I’m a University of Manchester alumni and a bloody miss Manchester, I don’t care what people say, it is a great city.

If you are going to be in Manchester you should join us, the event is FREE and trust me Stuart is a great guy to go out for drinks with (I’m not bad either, but I always get in trouble ).