Skip to content

Instantly share code, notes, and snippets.

@tych0
Created March 28, 2012 01:45
Show Gist options
  • Save tych0/2222790 to your computer and use it in GitHub Desktop.
Save tych0/2222790 to your computer and use it in GitHub Desktop.
Generating pkg config files inside of disutils' setup.py
### XXX: The following is a hack. py2cairo looks for xpyb.pc when configuring.
### However (as near as I can find) there is no way to get it to install with
### distutils, so we fake it here.
# make xpyb.pc file
class PCOpt(object):
def __init__(self, replace_str, val):
self.replace_str = replace_str
self.val = val
def gen_pc():
# --root is provided when doing maintainer type things, so set the prefix
# to local in that case, or /usr/local when doing normal things.
rootarg = filter(lambda a: a.startswith('--root'), sys.argv)
try:
rootarg = rootarg[0].split('=')[1]
except IndexError:
rootarg = ''
if rootarg:
prefix = '/usr'
else:
prefix = '/usr/local'
pc_opts = {
'--prefix': PCOpt('@prefix@', prefix),
'--exec-prefix': PCOpt('@exec_prefix@', '${prefix}'),
'--install-lib': PCOpt('@libdir@', '${exec_prefix}/lib'),
'--install-headers': PCOpt('@includedir@', '${prefix}/include/python2.7/xpyb'),
# please don't actually use this :-)
'--dont-set-the-xcb-version': PCOpt('@PACKAGE_VERSION@', VERSION),
}
def override_arg(arg):
for (k, v) in pc_opts.items():
if arg.startswith(k):
try:
v.val = arg.split('=')[1]
except IndexError:
pass
for arg in sys.argv:
override_arg(arg)
with open('xpyb.pc.in') as in_:
pc = in_.read()
with open('xpyb.pc', 'w') as out:
for opt in pc_opts.values():
pc = pc.replace(opt.replace_str, opt.val)
out.write(pc)
# if we're not installing, don't install the .pc
if 'install' not in sys.argv:
return
def resolve(path):
""" Resolve a path through pkgconfig variables. """
for opt in pc_opts.values():
name = '${' + opt.replace_str[1:-1] + '}' # strip off @s
if name in path:
path = path.replace(name, opt.val)
path = resolve(path)
return path
# XXX: moar haxxx: here we strip off the leading slash to keep join() happy
install_path = resolve(pc_opts['--install-lib'].val).strip('/')
pkgconfig = os.path.join(rootarg, install_path, 'pkgconfig')
try:
os.makedirs(pkgconfig)
except OSError as e:
if e.errno == errno.EEXIST:
pass
else:
raise
copyfile('xpyb.pc', os.path.join(pkgconfig, 'xpyb.pc'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment