Skip to content

Instantly share code, notes, and snippets.

@mdboom
Created May 14, 2015 13:19
Show Gist options
  • Save mdboom/89a8e9f2f38922d723a4 to your computer and use it in GitHub Desktop.
Save mdboom/89a8e9f2f38922d723a4 to your computer and use it in GitHub Desktop.
Hackish scripts to make installing and testing astropy-like packages from emacs easier
#!/usr/bin/env python
"""
Run from anywhere in a projects directory tree, this will compile and install
"""
import sys
import os
current_path = '.'
# Don't import things from the current directory -- that causes this
# script not to work.
sys.path[:] = [
path for path in sys.path
if os.path.abspath(path) != os.path.abspath(current_path)
]
import argparse
p = argparse.ArgumentParser()
p.add_argument('--clean', '-c', action='store_true')
p.add_argument('--three', '-3', action='store_true')
p.add_argument('--test', '-t', action='store_true')
p.add_argument('--test-file', '-f', nargs=1)
p = p.parse_args()
if p.three:
python_exec = 'python3'
else:
python_exec = 'python'
if p.test or p.test_file:
command = 'test'
else:
command = 'install'
types = [
(('setup.py',),
'%s setup.py %s' % (python_exec, command),
'rm -rf build; %s setup.py %s' % (python_exec, command),
False),
(('waf',),
'./waf build',
'./waf clean build',
False),
(('make.sh',),
'./make.sh',
'./make.sh',
False),
(('Makefile', 'GNUmakefile'),
'make',
'make clean; make',
True),
]
def find_directory_start(path):
while len(path) > 1:
for filenames, cmd, cmd_clean, recursive in types:
for name in filenames:
filepath = os.path.join(path, name)
if os.path.exists(filepath):
return path, name, cmd, cmd_clean, recursive
path, _ = os.path.split(path)
return None, None, None, None, None
def find_directory(path):
path, name, cmd, cmd_clean, recursive = find_directory_start(path)
if path:
if recursive:
last_path = path
while len(path) > 1:
filepath = os.path.join(path, name)
if not os.path.exists(filepath):
break
last_path = path
path, _ = os.path.split(path)
return last_path, cmd, cmd_clean
else:
return path, cmd, cmd_clean
else:
return None, None, None
if __name__ == '__main__':
pwd = os.path.abspath('.')
path, cmd, cmd_clean = find_directory(pwd)
if path:
if p.clean:
cmd = cmd_clean
if p.test_file:
cmd += ' -t ' + os.path.relpath(p.test_file[0], path) + ' --pdb '
print "%s: %s" % (path, cmd)
os.chdir(path)
try:
result = os.system(cmd)
finally:
os.chdir(pwd)
sys.exit(result)
else:
print "Error: Unable to find compilation script"
sys.exit(1)
(defun smart-compile ()
(interactive)
(compile "smart_compile"))
(defun compile-and-test ()
(interactive)
(compile "smart_compile -t"))
(defun compile-and-test-py3 ()
(interactive)
(compile "smart_compile -t -3"))
(defun compile-py3 ()
(interactive)
(compile "smart_compile -3"))
(defun compile-and-test-single-file ()
(interactive)
(pdb (concat "smart_compile -f " buffer-file-name)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment