-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildsrcdist.py
executable file
·61 lines (43 loc) · 1.45 KB
/
buildsrcdist.py
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
#!/usr/bin/env python
import subprocess
import platform
import tempfile
import tarfile
import shutil
import sys
import os
def getRCommand():
if platform.system() == "Windows": # Windows
curDrive = os.getcwd()[:2]
for n in os.listdir(curDrive + "\\Program Files\\R\\"):
print n
if n.startswith("R-3"):
fn = curDrive + "\\Program Files\\R\\" + n + "\\bin\\R.exe"
if os.path.exists(fn):
return fn
raise Exception("Unable to find R executable")
return "R"
def createSourcePackage(Rcmd = "R"):
tmpDir = tempfile.mkdtemp()
print "Created dir", tmpDir
curDir = os.getcwd()
print "Current dir", curDir
tarBall = subprocess.check_output([ "git", "archive", "--format", "tar", "--prefix=archive/", "HEAD"])
try:
os.chdir(tmpDir)
with open("archive.tar", "wb") as f:
f.write(tarBall)
tar = tarfile.open("archive.tar", "r")
tar.extractall()
tar.close()
subprocess.check_call([ Rcmd, "CMD", "build", os.path.join("archive","pkg")])
sourcePackage = [ n for n in os.listdir(".") if n.endswith(".tar.gz")][0]
finally:
os.chdir(curDir)
return (tmpDir, sourcePackage)
if __name__ == "__main__":
d,p = createSourcePackage(getRCommand())
if os.path.exists(p):
os.unlink(p)
shutil.move(os.path.join(d,p), ".")
shutil.rmtree(d)