View Single Post
Old 11-17-2014, 04:27 AM   #1
ShellShock
Wizard
ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.ShellShock ought to be getting tired of karma fortunes by now.
 
ShellShock's Avatar
 
Posts: 1,178
Karma: 2431850
Join Date: Sep 2008
Device: IPad Mini 2 Retina
Thumbs up A free Calibre Windows development environment using Visual Studio

In this post I give a brief overview of how to use Visual Studio to create a freeware Windows development and debugging environment for Calibre.
  1. First set up the Calibre Windows development environment as normal: http://manual.calibre-ebook.com/develop.html.
  2. Install the freeware Microsoft Visual Studio Community 2013 edition: http://www.visualstudio.com/products...oper-offers-vs. Click the "Visual Studio" Download button to install.
  3. Install the Python Tools for Visual Studio: http://pytools.codeplex.com/. Pay attention to the installation instructions: http://pytools.codeplex.com/wikipage...20Installation. Under step 2, install the CPython interpreter by installing the Python 2.7.8 32 bit Windows Installer.
  4. Run Visual Studio 2013.
  5. On the menu, chose File, New, Project.... Drill down into Installed, Templates, Other Languages, Python. Select the "From Existing Python code" template. Enter the Name as Calibre, and the Location as your GitHub root, e.g., "C:\Users\user\Documents\GitHub". Solution should be set to "Create new solution", and Solution name to "Calibre". Tick "Create directory for solution". Click OK.
  6. In the "Create New Project from Existing Python Code" wizard, set the folder containing your Python code to your Calibre source directory, e.g., C:\Users\user\Documents\GitHub\calibre\src. This must be the same location as your CALIBRE_DEVELOP_FROM environment variable. Click Next.
  7. Set the Python interpreter to use as 2.7. Click Next.
  8. Click Finish.

You will now have a project hierarchy that contains all the Calibre source code, making it easy to navigate the various folders and python files, edit the code etc.

Visual Studio 2013 has GitHub integration for source control. Yay! It is explained in detail here http://msdn.microsoft.com/en-us/library/hh850445.aspx, especially the section "Put an existing solution under local Git version control".

You cannot build the Calibre project you have just created, but that is not necessary because you can install the latest Calibre release, and run that from source, which will use the Python files in your Calibre project, as defined by the CALIBRE_DEVELOP_FROM environment variable.

So to debug Calibre:
  1. Make sure the CALIBRE_DEVELOP_FROM environment variable is set correctly.
  2. Run Calibre as normal.
  3. With the Calibre project open in Visual Studio, click the DEBUG menu, Attach to process, and select the calibre.exe. Make sure "Attach to" is set to "Python code" (use the Select button to change if necessary). Click Attach.
  4. Visual Studio will attach to the calibre process, using the Python debugger.
  5. You can now set breakpoints, step through code, inspect/watch variables, etc.

An optional extra: a simpler alternative way to attach the debugger to Calibre. Normally we use F5 in Visual Studio to debug. This builds the solution and launches it in the debugger. This is not possible with the Calibre project we created...unless:
  1. Download the attached CalibreDebug.zip, and unzip to your GitHub root, to create a CalibreDebug sub-directory.
  2. Add the CalibreDebug project to your existing Calibre solution: in Solution Explorer, right click "Solution Calibre", choose Add, Existing project, and select the CalibreDebug.csproj.
  3. In Solution Explorer, Right click on CalibreDebug, and click "Set as StartUp Project".
  4. In Solution Explorer, Right click on CalibreDebug, and select Properties.
  5. Select the Debug tab. Set the Command line arguments to

    Code:
    "C:\Program Files\Calibre\calibre.exe" "CALIBRE_DEVELOP_FROM=C:\Users\user\Documents\GitHub\calibre\src"
    Change the paths to the correct values for your environment.

Now when you hit F5, the CalibreDebug project builds and launches in the debugger. CalibreDebug is a small C# project that does two things:
  1. Launches the calibre.exe you specified in the command line arguments, with the correct CALIBRE_DEVELOP_FROM environment variable.
  2. Attaches the Python debugger in the current Visual Studio session to the calibre process.

In theory it should be possible to mix Python debugging with C/C++, so we could step from Python code into the C/C++ libraries that Calibre uses, but I have not got this working yet.

I hope you find this post useful.
Attached Files
File Type: zip CalibreDebug.zip (4.4 KB, 1289 views)

Last edited by ShellShock; 01-01-2015 at 06:05 AM.
ShellShock is offline   Reply With Quote