[cairo] [PATCH] py2cairo: Add a setup.py file back

Simon Sapin simon.sapin at exyr.org
Sat Dec 8 02:21:39 PST 2012


Hi,

py2cairo used to be installable with distutils but the setup.py file was 
just removed just before the 1.10.0 release, for reasons unknown to me. 
The same file was more recently added back to pycairo for Python 3, but 
that change is still unreleased.

The patch below add the setup.py file back to py2cairo. Please consider 
merging it, making new releases for both py2cairo and pycairo, and 
registering py2cairo in the Python Package Index. (Just linking to 
http://cairographics.org/pycairo is enough so that setuptools/pip can 
manage to find the release tarballs.)

Installing with distutils really helps to integrate with pip,
virtualenv, and many other tools in the Python ecosystem. Right now 
users have to do crazy hacks to use pycairo with virtualenv’s 
--no-site-packages isolation:

http://stackoverflow.com/questions/11491268/install-pycairo-in-virtualenv/13279739

Hopefully in the near future, users can just run "pip install py2cairo" 
in a virtualenv; projects like WeasyPrint and CairoSVG can add 'pycairo' 
to the install_requires line of their own setup.py file: everything will 
Just Work®.


Many thanks to the makers of cairo and pycairo for their work.
Cheers,
--
Simon Sapin


 From 18392a26cc13c354d12231437222212faa23e92c Mon Sep 17 00:00:00 2001
From: Simon Sapin <simon.sapin at exyr.org>
Date: Sat, 8 Dec 2012 09:36:10 +0100
Subject: [PATCH] Revert "Remove setup.py"

This reverts commit 6e4502773cd810504b4ecedaec49f13752cace10
and adjusts version numbers.

Installing with distutils really helps to integrate with pip,
virtualenv, and many other tools in the Python ecosystem.
---
  INSTALL  |   5 +++
  README   |   2 +-
  setup.py | 116 
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 122 insertions(+), 1 deletion(-)
  create mode 100755 setup.py

diff --git a/INSTALL b/INSTALL
index a11bc04..cce134a 100644
--- a/INSTALL
+++ b/INSTALL
@@ -33,6 +33,11 @@ Python will not be able to find the cairo module 
until you add
  $prefix/lib/pythonX.Y/site-packages to the PYTHONPATH variable.


+Python distutils
+----------------
+$ python setup.py install
+
+
  Testing
  -------
  See test/README
diff --git a/README b/README
index 68153cf..a8a4787 100644
--- a/README
+++ b/README
@@ -3,7 +3,7 @@ http://www.cairographics.org/pycairo

  Dependencies
  ------------
-    cairo   >= 1.10.0
+    cairo   >= 1.10.2
      Python  >= 2.6

  Compiling
diff --git a/setup.py b/setup.py
new file mode 100755
index 0000000..3c0c440
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,116 @@
+#!/usr/bin/env python
+
+import distutils.core      as dic
+import distutils.dir_util  as dut
+import distutils.file_util as fut
+import distutils.sysconfig as dsy
+import io
+import os
+import subprocess
+import sys
+
+pycairo_version        = '1.10.1'
+cairo_version_required = '1.10.2'
+pkgconfig_file = 'pycairo.pc'
+config_file    = 'src/config.h'
+
+
+def call(command):
+  pipe = subprocess.Popen(command, shell=True,
+                          stdout=subprocess.PIPE,
+                          stderr=subprocess.PIPE)
+  pipe.wait()
+  return pipe
+
+def pkg_config_version_check(pkg, version):
+  pipe = call('pkg-config --print-errors --exists "%s >= %s"' %
+              (pkg, version))
+  if pipe.returncode == 0:
+    print '%s >= %s detected' % (pkg, version)
+  else:
+    print pipe.stderr.read()
+    raise SystemExit('Error: %s >= %s not found' % (pkg, version))
+
+def pkg_config_parse(opt, pkg):
+  pipe = call("pkg-config %s %s" % (opt, pkg))
+  output = pipe.stdout.read()
+  opt = opt[-2:]
+  return [x.lstrip(opt) for x in output.split()]
+
+
+def createPcFile(PcFile):
+  print 'creating %s' % PcFile
+  with open(PcFile, 'w') as fo:
+    fo.write ("""\
+prefix=%s
+
+Name: Pycairo
+Description: Python bindings for cairo
+Version: %s
+Requires: cairo
+Cflags: -I${prefix}/include/pycairo
+Libs:
+""" % (sys.prefix, pycairo_version)
+            )
+
+def createConfigFile(ConfigFile):
+  print 'creating %s' % ConfigFile
+  v = pycairo_version.split('.')
+
+  with open(ConfigFile, 'w') as fo:
+    fo.write ("""\
+// Configuration header created by setup.py - do not edit
+#ifndef _CONFIG_H
+#define _CONFIG_H 1
+
+#define PYCAIRO_VERSION_MAJOR %s
+#define PYCAIRO_VERSION_MINOR %s
+#define PYCAIRO_VERSION_MICRO %s
+#define VERSION "%s"
+
+#endif // _CONFIG_H
+""" % (v[0], v[1], v[2], pycairo_version)
+            )
+
+
+if sys.version_info < (2,6):
+  raise SystemExit('Error: Python >= 2.6 is required')
+
+pkg_config_version_check ('cairo', cairo_version_required)
+if sys.platform == 'win32':
+  runtime_library_dirs = []
+else:
+  runtime_library_dirs = pkg_config_parse('--libs-only-L', 'cairo')
+
+createPcFile(pkgconfig_file)
+createConfigFile(config_file)
+
+
+cairo = dic.Extension(
+  name = 'cairo._cairo',
+  sources = ['src/cairomodule.c',
+             'src/context.c',
+             'src/font.c',
+             'src/matrix.c',
+             'src/path.c',
+             'src/pattern.c',
+             'src/surface.c',
+             ],
+  include_dirs = pkg_config_parse('--cflags-only-I', 'cairo'),
+  library_dirs = pkg_config_parse('--libs-only-L', 'cairo'),
+  libraries    = pkg_config_parse('--libs-only-l', 'cairo'),
+  runtime_library_dirs = runtime_library_dirs,
+  )
+
+dic.setup(
+  name = "pycairo",
+  version = pycairo_version,
+  description = "python interface for cairo",
+  ext_modules = [cairo],
+  data_files = [
+    ('include/pycairo', ['src/pycairo.h']),
+    ('lib/pkgconfig', [pkgconfig_file]),
+    (os.path.join(dsy.get_python_lib(), 'cairo'),
+     ['src/__init__.py']),
+    ],
+  )
-- 
1.8.0.1


More information about the cairo mailing list