Skip to content

Commit

Permalink
Add support to compile from local location rather than just download
Browse files Browse the repository at this point in the history
  • Loading branch information
benwah committed Oct 11, 2019
1 parent c66dc8e commit 56d735d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 41 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ venv/
ENV/
env.bak/
venv.bak/
dist/
dist/

dwm_src
119 changes: 79 additions & 40 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,50 @@
import tempfile
import hashlib
import tarfile
import shutil

from setuptools.command.build_ext import build_ext
from setuptools import Extension
from setuptools import setup
from setuptools.command.install import install
from setuptools import (Extension, setup)

try:
from urllib.request import urlopen
except ImportError:
from urllib2 import urlopen


DWM_VERSION = '6.1'
DWM_SRC_ROOT_DIR = 'dwm_src'
DWM_SRC_DIR = os.path.join(DWM_SRC_ROOT_DIR, 'dwm-{version}').format(
version=DWM_VERSION
)
DWM_SOURCE = (
DWM_VERSION = '6.2'
DWM_SRC_ROOT_DIR= os.path.join('dwm_src')
DWM_REMOTE_SOURCE = (
'https://dl.suckless.org/dwm/dwm-{version}.tar.gz'.format(
version=DWM_VERSION
)
)
DWM_MD5 = 'f0b6b1093b7207f89c2a90b848c008ec'
DWM_MD5 = '9929845ccdec4d2cc191f16210dd7f3d'


dwm = Extension(
'dwm',
libraries=['X11', 'Xinerama', 'fontconfig', 'Xft'],
library_dirs=['/usr/X11R6/lib'],
include_dirs=[
'/usr/X11R6/include',
'/usr/include/freetype2',
DWM_SRC_DIR,
],
define_macros=[
class DwmExtension(Extension):
def __init__(self, *args, **kwargs):
super(DwmExtension, self).__init__(*args, **kwargs)

def set_dwm_options(self, dwm_version):
self.define_macros=[
('_DEFAULT_SOURCE',),
('_BSD_SOURCE',),
('_POSIX_C_SOURCE', 2),
('VERSION', '"{version}"'.format(version=DWM_VERSION)),
('VERSION', '"{version}"'.format(version=dwm_version)),
('XINERAMA',),
],
sources=[
os.path.join(DWM_SRC_DIR, 'drw.c'),
os.path.join(DWM_SRC_DIR, 'dwm.c'),
os.path.join(DWM_SRC_DIR, 'util.c'),
],
]


def relative_path(*parts):
return os.path.join(os.path.dirname(__file__), *parts)


dwm = DwmExtension(
'dwm',
libraries=['X11', 'Xinerama', 'fontconfig', 'Xft'],
library_dirs=['/usr/X11R6/lib'],
extra_compile_args=[
'-c',
'-fPIC',
Expand All @@ -59,22 +58,37 @@
],
extra_link_args=[
'-fPIC',
]
],
include_dirs=[
'/usr/X11R6/include',
'/usr/include/freetype2',
relative_path(DWM_SRC_ROOT_DIR),
],
sources=[
relative_path(DWM_SRC_ROOT_DIR, 'drw.c'),
relative_path(DWM_SRC_ROOT_DIR, 'dwm.c'),
relative_path(DWM_SRC_ROOT_DIR, 'util.c'),
],
)


class BuildDwm(build_ext, object):
def relative_path(self, *dirs):
return os.path.join(os.path.dirname(__file__), *dirs)
user_options = build_ext.user_options + [
('dwm-source=', None, 'Path to DWM source')
]

def initialize_options(self):
self.dwm_source = None
super(BuildDwm, self).initialize_options()

def download_dwm(self):
if not os.path.exists(self.relative_path(DWM_SRC_DIR)):
if not os.path.exists(relative_path(DWM_SRC_ROOT_DIR)):
logger = logging.getLogger()
logger.warn('Downloading {file}...'.format(file=DWM_SRC_DIR))
response = urlopen(DWM_SOURCE)
logger.warn('Downloading {file}...'.format(file=DWM_SRC_ROOT_DIR))
response = urlopen(DWM_REMOTE_SOURCE)
data = response.read()

os.mkdir(self.relative_path(DWM_SRC_ROOT_DIR))
os.mkdir(relative_path(DWM_SRC_ROOT_DIR))

logger.warn('Validating MD5...')
assert(hashlib.md5(data).hexdigest() == DWM_MD5)
Expand All @@ -89,13 +103,23 @@ def download_dwm(self):
) as archive:
archive.extractall(DWM_SRC_ROOT_DIR)
destination_file.close()
unpacked_dest = 'dwm-{version}'.format(version=DWM_VERSION)
unpacked_dest = relative_path(
DWM_SRC_ROOT_DIR,
unpacked_dest
)
for file in os.listdir(unpacked_dest):
shutil.move(
relative_path(unpacked_dest, file),
relative_path(DWM_SRC_ROOT_DIR),
)

def copy_default_config(self):
dest_file_path = self.relative_path(DWM_SRC_DIR, 'config.h')
dest_file_path = relative_path(DWM_SRC_ROOT_DIR, 'config.h')
if not os.path.exists(dest_file_path):
source_file = open(
self.relative_path(
DWM_SRC_DIR, 'config.def.h'
relative_path(
DWM_SRC_ROOT_DIR, 'config.def.h'
),
'r'
)
Expand All @@ -104,10 +128,21 @@ def copy_default_config(self):
source_file.close()
dest_file.close()

def copy_dwm_source(self):
if not os.path.exists(relative_path(DWM_SRC_ROOT_DIR)):
shutil.copytree(self.dwm_source, DWM_SRC_ROOT_DIR)

def build_extension(self, ext):
self.download_dwm()
self.copy_default_config()
self.compiler = cc.new_compiler()
if ext.name == 'dwm':
self.compiler = cc.new_compiler()

if self.dwm_source is None:
self.download_dwm()
self.copy_default_config()
else:
self.copy_dwm_source()

del(self.dwm_source)
return super(BuildDwm, self).build_extension(ext)

def get_export_symbols(self, ext):
Expand All @@ -116,12 +151,16 @@ def get_export_symbols(self, ext):
def get_ext_filename(self, ext_name):
return ext_name + '.so'

@property
def dwm_version(self):
return 'custom' if self.dwm_source else VERSION

setup(
name='pydwm',
url='https://github.com/benwah/pydwm',
author='Benoit C. Sirois',
author_email='[email protected]',
version='0.1.3',
version='0.1.4',
description='A simple python wrapper around DWM.',
long_description=(
'This is a very simple python wrapper around DWM. It downloads DWM, '
Expand Down

0 comments on commit 56d735d

Please sign in to comment.