-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_and_distribute
executable file
·124 lines (89 loc) · 3.94 KB
/
build_and_distribute
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -*- mode: python -*-
import os
import configobj
import re
import shutil
import subprocess
def distribute(projects_dir, distribution_dir, yasnippet_dir):
" Main function. Installs the compiled code "
print "> Building elisp"
projects = build_projects(projects_dir, distribution_dir)
snippets = find_snippets(projects_dir, yasnippet_dir)
party = build_third_party(projects_dir, distribution_dir)
paths = find_paths(projects + party)
dot_emacs = [(os.path.join(projects_dir, 'dot-emacs.el'), '/home/shm/.emacs')]
do_dist(projects, snippets, party, dot_emacs)
def do_dist(projects, snippets, party, dot_emacs):
" Copy identified files"
print "Copying files"
for src, dst in projects:
if os.path.exists(dst):
shutil.rmtree(dst)
shutil.copytree(src, dst)
for src, dst in snippets + party + dot_emacs:
if os.path.exists(dst):
os.remove(dst)
if not os.path.exists(os.path.dirname(dst)):
os.makedirs(os.path.dirname(dst))
shutil.copy(src, dst)
def build_projects(projects_dir, distribution_dir):
" Build projects "
projects = []
for project in filter(lambda x: os.path.isdir(x) and x != '3rdParty' and x != 'yasnippets', os.listdir(projects_dir)):
build_ini = os.path.join(projects_dir, project, 'build.ini')
if os.path.exists(build_ini):
config = configobj.ConfigObj(build_ini)
if not 'distribute' in config or config['distribute'].lower() != "false":
projects.append(build_project(os.path.join(projects_dir, project), config))
return map(lambda x: (x, os.path.join(distribution_dir, os.path.basename(x)[:-7])), projects)
def find_snippets(projects_dir, yasnippet_dir):
" Find snippets "
snippets = []
snippets_dir = os.path.join(projects_dir, 'yasnippets')
for root, dirs, files in os.walk(snippets_dir):
for f in files:
snippets.append((os.path.join(root, f), os.path.join(yasnippet_dir, root[len(snippets_dir) + 1:], f)))
return snippets
def build_third_party(projects_dir, distribution_dir):
""
el_files = []
party_dir = os.path.join(projects_dir, '3rdParty')
for f in filter(lambda x: x.endswith('.el'), os.listdir(party_dir)):
el_files.append(os.path.join(party_dir, f))
cmd = "emacs -batch -f batch-byte-compile %s" % " ".join(el_files)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout, stderr = proc.communicate()
if proc.returncode != 0:
raise RuntimeError("Error caught during byte-compiling: %s" % stdout + stderr)
return map(lambda x: (x, os.path.join(distribution_dir, '3rdParty', os.path.basename(x))), map(lambda x: x + 'c', el_files))
def find_paths(paths):
targets = set()
for src, dst in paths:
if dst.endswith('.elc'):
targets.add(os.path.dirname(dst))
else:
targets.add(dst)
return targets
def build_project(build_dir, config):
" Build project at build_dir "
print "Building %s" % config['name']
cwd = os.getcwd()
os.chdir(build_dir)
proc = subprocess.Popen("ebuild", shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
(stdout, stderr) = proc.communicate()
if proc.returncode == 1:
raise RuntimeError("caught error during build: %s" % stdout+stderr)
os.chdir(cwd)
dist_dir = os.path.join(build_dir, 'dist')
for f in os.listdir(dist_dir):
if re.match("^%s-.+-binary$" % config['name'], f):
target = f
break
if not target:
raise RuntimeError("Could not find expected distribution package for %s" % config['name'])
return os.path.join(dist_dir, target)
YASNIPPET_DIR = "/home/shm/.emacs.d/elpa/yasnippet-0.8.0"
DISTRIBUTION_DIR = "/home/shm/.emacs.d/my-code"
distribute('/home/shm/repos/git/elisp-devel', DISTRIBUTION_DIR, YASNIPPET_DIR)