-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg-build
executable file
·274 lines (192 loc) · 6.43 KB
/
pkg-build
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#!/usr/bin/python
# urllib2 makes the https request
import urllib2, datetime, sys, os, shutil
import subprocess
# python <= 2.5 requires the inclusion of "simplejson" since the json module
# is not standard on old versions of python
import simplejson as json
from optparse import OptionParser
from tempfile import mkdtemp
# log
class log:
# Constructor (open the file handle)
def __init__ (self, file):
self.file = file
self.handle = open(file, "a")
# Deconstructor (close the file handle)
def __del__ (self):
self.pad()
self.handle.close()
# Write a message. This is the low level print function
def write (self, msg): print >> self.handle, msg
# Just write a message with a stamp
def info (self, msg):
timestamp = "[%d/%d/%d %s:%s:%s] " % self.time()
self.write(timestamp + msg)
# Write an error to the log
def error (self, msg): self.info("ERROR: " + msg)
# Write a *BIG ERROR* to the log
def bigerror (self, msg): self.info("***ERROR*** " + msg)
# Write a warning to the log
def warning (self, msg): self.info("Warning: " + msg)
#
def time (self):
now = datetime.datetime.now()
now = (now.month, now.day, now.year, now.hour, now.minute, now.second)
return now
# ..
def pad (self, lines=2, string = "\n"): self.write(string * lines)
#
#
#
def is_r_package (path):
if not os.path.isdir(path): return False
R = os.path.join(path, "R")
man = os.path.join(path, "man")
DESCRIPTION = os.path.join(path, "DESCRIPTION")
NAMESPACE = os.path.join(path, "NAMESPACE")
goodfile = lambda x: os.path.isfile(x) and not os.path.islink(x)
if not goodfile(DESCRIPTION): return False
if not goodfile(NAMESPACE): return False
if not os.path.isdir(R): return False
if not os.path.isdir(man): return False
return True
# Craete Option parser
parser = OptionParser()
# Create a menu
parser.add_option("-d", "--dest", dest="dest", metavar = "REPOSITORY",
help = "Specify the destination directory of repository"
)
parser.add_option("-l", "--log", dest="log", default="pkg-build.log",
metavar = "LOG_FILE",
help = "Create a log file"
)
parser.add_option("-S", "--skip-check", dest="skip_check",
action = "store_true", default=False,
help = "Do not check package for errors/warnings. This uses `R CMD check`."
)
parser.add_option(
"-R", "--repo", dest="repo",
default = "https://api.github.com/users/zeligdev/repos",
help = "Specify the URI of the repository"
)
# Parse the commandline
(options, args) = parser.parse_args()
# Parse options
clone_dir = mkdtemp(prefix = "clone")
check_dir = mkdtemp(prefix = "check")
build_dir = mkdtemp(prefix = "build")
url = os.path.expanduser(options.repo)
# Ensure that -d or --dest are set
if options.dest is None:
print "Error: \"--dest\" is not specified!"
sys.exit()
# Continue parse options
destination = os.path.expanduser(options.dest)
# directory variables
original_wd = os.getcwd()
# Display info blurb
print "Zelig Build v0.9"
print "----------------"
print " Destination Repository ...", options.dest
print " Clone Directory ..........", clone_dir
print " Rcheck Directory .........", check_dir
print " Build Directory ..........", build_dir
print " Log File .................", options.log
print " Skip Check? ..............", not options.skip_check
# Logger
zelig_build = log(options.log)
zelig_build.info("Staring up")
#
if not os.path.exists(destination):
zelig_build.info(
"The destination repository directory (" + destination + ") must exist"
)
# Get a response from the github api
# logger.info("Getting repository listing from zeligdev")
request = urllib2.Request(url)
response_stream = urllib2.urlopen(request)
response = response_stream.read()
# Program start
zelig_build.info("Getting list of repositories from " + url)
# Parses the repsonse into an actual data structure. Awesome!
repos = json.loads(response)
# Change working directory
os.chdir(clone_dir)
# If command
if len(args):
pkg_list = []
for r in repos:
pkg = r['name']
if pkg in args: pkg_list.append(r)
pass
repos = pkg_list
# Clone
for r in repos:
pkg = r['name']
url = r['git_url']
pkg_dest = os.path.join(clone_dir, pkg)
zelig_build.info("Cloning %s (%s)" % (url, pkg))
result = subprocess.call("git clone %s %s" % (url, pkg), shell=True)
if result == 0:
zelig_build.info("Clone: Success")
else:
zelig_build.info("Clone: Fail")
# Change working directory
os.chdir(os.path.join(original_wd, check_dir))
# Check
if not options.skip_check:
for r in repos:
pkg = r['name']
pkg = os.path.join("..", clone_dir, pkg)
if not is_r_package(pkg):
zelig_build.info(pkg + " is not an R package. Skipping")
continue
zelig_build.info(pkg + " is an R package")
zelig_build.info("Checking %s")
# Run from the system's shell
result = subprocess.call("/usr/bin/R CMD check %s" % pkg, shell=True)
if result == 0:
zelig_build.info("Check: Success")
else:
zelig_build.info("Check: Fail")
# Change working directory
os.chdir(os.path.join(original_wd, build_dir))
# Build
for r in repos:
pkg = r['name']
pkg = os.path.join("..", clone_dir, pkg)
zelig_build.info("Building %s" % pkg)
# Run from the system's shell
result = subprocess.call("/usr/bin/R CMD build %s" % pkg, shell=True)
# Move into the "REPOS" file
# print tarball, "->", pkg_repos_dest
# os.rename(tarball, pkg_repos_dest)
if result == 0:
zelig_build.info("Build: Success")
else:
zelig_build.info("Build: Fail")
# Ensure we're in the parent directory
os.chdir(original_wd)
# Copy the recently created tarballs over to the destination directory
for src_file in os.listdir(build_dir):
# Source file
src_file = os.path.join(build_dir, src_file)
# Destination file
dest_file = os.path.join(destination, os.path.basename(src_file))
zelig_build.info('Copying "%s" -> "%s"' % (src_file, dest_file))
# Copy (recursively) files into the destination
shutil.copy(src_file, dest_file)
# Move into the code repository. This is to facilitate the making of the
# PACKAGES files
os.chdir(destination)
#
zelig_build.info("Creating 'PACKAGES' and 'PACKAGES.gz' Files in " + os.getcwd())
# Create PACKAGES and PACKAGES.gz file
r_script = '"tools:::write_PACKAGES()"'
cmd = "echo %s | /usr/bin/R --vanilla --slave" % r_script
subprocess.call(cmd, shell=True)
# Clean up temp directories
shutil.rmtree(clone_dir)
shutil.rmtree(check_dir)
shutil.rmtree(build_dir)