Set up your devel env to work on U1 on Windows

by mandel on September 29th, 2010

In one of my previous posts about the port of U1 to Windows I mentioned that setting up the enviroment to build the solution and work on it was a PITA. To solve that I have created a hideous batch script that will set up your devel environment to work on the U1 windows port.

Take a look at the monster:

@ECHO off 
:: Tell the user what the batch does.
ECHO. 
ECHO This batch will set up your enviroment do that you can build the Windows Ubuntu One port.
ECHO.
ECHO The following actions will be taken:
ECHO    1. Download and install python 2.6.
ECHO    2. Install easy_install for windows.
ECHO    3. Install python required libraries.
ECHO    4. Install py2exe.
ECHO    5. Install bazaar.
ECHO    6. Install Ubuntu One Protocol
ECHO.
ECHO Quering OS architecture
:: Get the first 3 chars of process identifier
SET ARCH=%processor_identifier:~0,3%
IF NOT %ARCH% == x86 GOTO :X64
:: set the paths for the x86 packages
ECHO Setting Install Congi for arch %ARCH%
SET PYWIN32INSTALLER=pywin32-214.win32-py2.6.exe
SET PYTHONDOWNLOAD=http://www.python.org/ftp/python/2.6.6/python-2.6.6.msi
SET EASYINSTALLDOWNLOAD=http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11.win32-py2.6.exe#md5=1509752c3c2e64b5d0f9589aafe053dc
SET PY2EXEDOWNLOAD=http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/py2exe-0.6.9.win32-py2.6.exe/download
SET PROTOCDOWNLOAD=http://protobuf.googlecode.com/files/protoc-2.3.0-win32.zip
SET PROTOBUFDOWNLOAD=http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
SET PYOPENSSLDOWNLOAD=http://pypi.python.org/packages/2.6/p/pyOpenSSL/pyOpenSSL-0.10.winxp32-py2.6.msi#md5=90920217fb35d76524cab66c8c135cc8
SET TWISTEDDOWNLOAD=http://tmrc.mit.edu/mirror/twisted/Twisted/10.1/Twisted-10.1.0.winxp32-py2.6.msi
SET BZRDOWNLOAD=http://launchpad.net/bzr/2.2/2.2.0/+download/bzr-2.2.0.win32-py2.6.exe
GOTO :STARTINSTALLATION
:X64
SET PYWIN32INSTALLER=pywin32-214.win-amd64-py2.6.exe
SET PYTHONDOWNLOAD=http://www.python.org/ftp/python/2.6.6/python-2.6.6.amd64.msi
SET EASYINSTALLDOWNLOAD=http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11.win32-py2.6.exe#md5=1509752c3c2e64b5d0f9589aafe053dc
SET PY2EXEDOWNLOAD=http://sourceforge.net/projects/py2exe/files/py2exe/0.6.9/py2exe-0.6.9.win64-py2.6.amd64.exe/download
SET PROTOCDOWNLOAD=http://protobuf.googlecode.com/files/protoc-2.3.0-win32.zip
SET PROTOBUFDOWNLOAD=http://protobuf.googlecode.com/files/protobuf-2.3.0.zip
SET PYOPENSSLDOWNLOAD=http://pypi.python.org/packages/2.6/p/pyOpenSSL/pyOpenSSL-0.10.winxp32-py2.6.msi#md5=90920217fb35d76524cab66c8c135cc8
SET TWISTEDDOWNLOAD=http://tmrc.mit.edu/mirror/twisted/Twisted/10.1/Twisted-10.1.0.winxp32-py2.6.msi
SET BZRDOWNLOAD=http://launchpad.net/bzr/2.2/2.2.0/+download/bzr-2.2.0.win32-py2.6.exe
ECHO 64
:STARTINSTALLATION
ECHO Starting installation

:: ============================================================================
:: Set up temp directory 
:: ============================================================================
ECHO.
SET TEMPFILE=%TEMP%ENV_SET_UP
ECHO The file that will be used to store the downloaded data is:
ECHO %TEMPFILE%
:: If the dir does not exist we have not problem and continue
:: otherwhise delete the dir and create it so that we do not
:: have old data present
IF NOT EXIST %TEMPFILE% GOTO :NOTEMPDIR
:: Ask user if he wants to delete the dir, he might not want to
CHOICE /C YN /M "The dir is already present. Do you want to delete it"
IF ERRORLEVEL 1 GOTO :DELETEDIR
:: User does not want to delete, we are not that smart!! bye!
ECHO Please delete the dir manually
ECHO Leaving installation
EXIT
:DELETEDIR
RD /s /Q %TEMPFILE%
:NOTEMPDIR
MD %TEMPFILE%

:: ============================================================================
:: Set up python
:: ============================================================================
 
SET PYTHONPATH=""
ECHO Checking if python2.6 is in the system
:GETPYTHONPATH
:: This is very anoying, FOR /F will work differently depending on the output
:: of reg which is not consistent between os (xp, 7) we have to choose the tokens 
:: according to the os
SET PYTHONPATHTOKENS=3
VER | FIND "XP" > nul
IF %ERRORLEVEL% == 0 SET PYTHONPATHTOKENS=4
FOR /F "tokens=%PYTHONPATHTOKENS%" %%A IN ('REG QUERY HKLMSoftwarePythonPythonCore2.6InstallPath /ve') DO @SET PYTHONPATH=%%A
IF NOT %PYTHONPATH% == "" GOTO :PYTHONPRESENT
:: donload python and install it
ECHO Download python 2.6
wget.exe -v --output-document=%TEMPFILE%python.msi %PYTHONDOWNLOAD%
ECHO Installing python...
START /wait msiexec.exe /i %TEMPFILE%python.msi
:: Set the location of python
GOTO :GETPYTHONPATH
:PYTHONPRESENT
:: Let user know we did find python 2.6
ECHO Python is present!
ECHO Python dir is %PYTHONPATH%

:: ============================================================================
:: Setup pywin32 extensions
:: ============================================================================

:: This should be downloaded, but I have issues with wget and sourceforge
ECHO Installing pywin32 python extensions
START /wait %PYWIN32INSTALLER%

:: ============================================================================
:: Set up easy_install
:: ============================================================================
 
ECHO Checking if easy_install is in the system
IF EXIST %PYTHONPATH%Scriptseasy_install.exe GOTO :EASYINSTALLPRESENT
ECHO Download easy_install for Windows
wget.exe -v --output-document=%TEMPFILE%easy_install.exe 
ECHO Installing easy_install...
START /wait %TEMPFILE%easy_install.exe %EASYINSTALLDOWNLOAD%
:EASYINSTALLPRESENT
:: set the easy_install path, this is not superb since if the user changed
:: the path of python, we have problems
SET EASYINSTALLPATH=%PYTHONPATH%Scriptseasy_install.exe
ECHO easy_install is present!
ECHO Python dir is %EASYINSTALLPATH%

:: ============================================================================
:: Set up dependencies
:: ============================================================================
 
 
ECHO The following dependencies will be installed using easy_install
ECHO    1. zope.interface
ECHO    2. oauth
ECHO    4. boto
ECHO    5. lazr.authentication
ECHO    6. lazr.restfulclient
ECHO    7. lazr.uri
ECHO Installing dependencies
%EASYINSTALLPATH% -Z -U zope.interface oauth boto lazr.authentication lazr.restfulclient lazr.uri 
ECHO.
ECHO Python dependencies have been installed
ECHO.

:: ============================================================================
:: Set up depedencies that cannot be install with easy_install
::=============================================================================

:: Install py2exe using an msi, the easy_install pacakage fails in systems such
:: as XP
ECHO Downloading  py2exe for Windows
wget.exe -v --output-document=%TEMPFILE%py2exe.exe "%PY2EXEDOWNLOAD%"
ECHO Installing py2exe
%TEMPFILE%py2exe.exe

:: It is of extreme importance to install protoc before, otherwhise the protobuf
:: module for python will not be correctly generated
ECHO Downloading protobuf compiler for python
wget.exe -v --output-document=%TEMPFILE%protoc_compiler.zip %PROTOCDOWNLOAD%
ECHO Extracting protobuf compiler
IF NOT EXIST "%ProgramFiles%Protoc" MD "%ProgramFiles%Protoc"
unzip -o %TEMPFILE%protoc_compiler.zip -d "%ProgramFiles%Protoc"

 
:: distutils does not work with the pacakage correctly and we have to unxip ourselves
ECHO Downloading Protobuf for Windows
wget.exe -v --output-document=%TEMPFILE%protoc.zip %PROTOBUFDOWNLOAD%
unzip -o %TEMPFILE%protoc.zip -d %TEMPFILE%Protoc
:: distutils.spawn.find_executable is used to find protoc but it does not do a
:: a very good job on windows, although we have installer protoc in the 
:: %ProgramFiles% we are going to copy it to the current location so that 
:: python can find it... lame!
COPY /B "%ProgramFiles%Protocprotoc.exe" %TEMPFILE%Protocprotobuf-2.3.0python
ECHO Installing Protobuf for Windows
START /B "%PYTHONPATH%pycdthon.exe" /D%TEMPFILE%Protocprotobuf-2.3.0python setup.py install
:: The setup.py from google creates an egg, but py2exe does notlike that, lets extract it
unzip -o %PYTHONPATH%Libsite-packagesprotobuf-2.3.0-py2.6.egg -d %PYTHONPATH%Libsite-packages

:: The msi does not add an entry in the reg therefore we always install :(
ECHO Downloading  pyOpenSSl for Windows
wget.exe -v --output-document=%TEMPFILE%pyOpenSSL.msi %PYOPENSSLDOWNLOAD%
ECHO Installing pyOpenSSL
START /wait msiexec.exe /i %TEMPFILE%pyOpenSSL.msi

:: Twisted cannot be installer with easy_install on windows
ECHO Downloading twisted for Windows
wget.exe -v --output-document=%TEMPFILE%twisted.msi %TWISTEDDOWNLOAD%
ECHO Installing twisted
START /wait msiexec.exe /i %TEMPFILE%twisted.msi

:: Install xdg.BaseDirectory for Windows
ECHO Installing xdg.BaseDirectory
IF NOT EXIST %PYTHONPATH%Libsite-packagesxdg MD %PYTHONPATH%Libsite-packagesxdg
COPY BaseDirectory.py %PYTHONPATH%Libsite-packagesxdg
ECHO. 2>%PYTHONPATH%Libsite-packagesxdg__init__.py

:: ============================================================================
:: Set up bazaar
:: ============================================================================
 
ECHO Checking if bzr is in the system
:GETBZRPATH
SET BZRPATH = ""
:: This is tricky since the Program Files dir has a space and does provide 
:: a problem because the delimeters are not correctly set, well is batch :(
FOR /F "tokens=3-6" %%A IN ('REG QUERY HKLMSoftwareBazaar /v InstallPath') DO @SET BZRPATH=%%A %%B
IF NOT BZRPATH == "" GOTO :BZRPRESENT
ECHO Downloading bazaar
wget.exe -v --output-document=%TEMPFILE%bazaar.exe %BZRDOWNLOAD%
ECHO Installing bazaar
%TEMPFILE%bazaar.exe
:: get the path from reg 
GOTO :GETBZRPATH
:BZRPRESENT
ECHO bzr is present!
ECHO bzr path is %BZRPATH%

 
:: ============================================================================
:: Set up ubuntuone-storage-protocol
:: ============================================================================
 
ECHO Branching ubuntuone-storage-protocol
"%BZRPATH%bzr.exe" branch lp:ubuntuone-storage-protocol  %TEMPFILE%ubuntuone-storage-protocol
:: distutils.spawn.find_executable is used to find protoc but it does not do a
:: a very good job on windows, although we have installer protoc in the 
:: %ProgramFiles% we are going to copy it to the current location so that 
:: python can find it... lame!
COPY /B "%ProgramFiles%Protocprotoc.exe" %TEMPFILE%ubuntuone-storage-protocol
ECHO Installing ubuntuone-storage-protocol from source
:: Use start, otherwhise we will not be able to set the execution dir and 
:: setup.py will complain about not being able to find the correct dirs
:: to install
START /B "%PYTHONPATH%python.exe" /D%TEMPFILE%ubuntuone-storage-protocol setup.py install
:: ubuntuone.logger is in not pacakage, we just copy it from the utils dir
COPY logger.py %PYTHONPATH%Libsite-packagesubuntuone

:: ============================================================================
:: Clean up the downloaded data
:: ============================================================================
 
ECHO Cleaning Temp files
RD /s /Q %TEMPFILE%
ECHO Installation completed!!

I most confess I felt dirty when I finished, but at least it does the job. I hope that know more people are tempted to compile the solution and give it a go. You can find this script in lp:~mandel/ubuntuone-windows-installer/add_env_set_up

Remember that we do not have yet an official .msi from Canonical, so please is you do find a U1 Windows before the official release, DO NOT TRUST IT.

From Canonical