-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatherAndPrint.groovy
189 lines (168 loc) · 5.08 KB
/
gatherAndPrint.groovy
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
import java.nio.channels.Channels
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import org.kohsuke.github.GHAsset
import org.kohsuke.github.GHOrganization
import org.kohsuke.github.GHRelease
import org.kohsuke.github.GHRepository
import org.kohsuke.github.GitHub;
import org.kohsuke.github.PagedIterable
import com.neuronrobotics.bowlerstudio.scripting.PasswordManager
import com.neuronrobotics.bowlerstudio.scripting.ScriptingEngine
import com.neuronrobotics.bowlerstudio.vitamins.Vitamins
import eu.mihosoft.vrl.v3d.CSG
import net.lingala.zip4j.ZipFile
import net.lingala.zip4j.exception.ZipException
@groovy.transform.Field HttpURLConnection connection
GitHub getGithub() throws IOException {
File workspace = new File(System.getProperty("user.home") + "/bowler-workspace/");
if (!workspace.exists()) {
workspace.mkdir();
}
try {
PasswordManager.loadLoginData(workspace);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//PasswordManager.login();
GitHub gh = PasswordManager.getGithub();
return gh;
}
void extract(String URL, File directory, String zipfileName) {
File zipfile = new File(directory.getAbsolutePath() + "/" + zipfileName);
if (!zipfile.exists()) {
//String curl = "curl -sL --header \"Accept: application/octet-stream\" --header \"Authorization: token "+PasswordManager.getPassword()+"\" "+URL+" -o "+zipfile.getAbsolutePath()
//println curl
try {
ProcessBuilder pb = new ProcessBuilder(
"curl",
"-LkJ",
"--http2",
"--header",
"\"Accept: application/octet-stream\"",
"--header",
"\"Authorization: token "+PasswordManager.getPassword()+"\"",
URL,
"-o",
zipfile.getAbsolutePath())
String scriptContents = String.join(" ",pb.command().toArray(new String[0]))
File script = new File(directory.getAbsolutePath() + "/run.sh" )
BufferedWriter writer = new BufferedWriter(new FileWriter(script));
writer.write("#!/bin/bash\n"+scriptContents);
writer.close();
pb = new ProcessBuilder(
"bash",
script.getAbsolutePath()
)
Process process = pb.start();
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
BufferedReader errorReader = new BufferedReader(
new InputStreamReader(process.getErrorStream()));
String line;
while ((line = reader.readLine()) != null ||((line = errorReader.readLine()) != null)) {
System.out.println(line);
}
int exitValue = process.waitFor();
if (exitValue != 0) {
System.out.println("Abnormal process termination "+exitValue);
}
reader.close();
errorReader.close();
} catch (IOException e) {
e.printStackTrace();
}
if(zipfile.exists()) {
println "Unzip the downloaded release "+zipfile.getAbsolutePath();
String source = zipfile.getAbsolutePath();
String destination =directory.getAbsolutePath();
try {
ZipFile zipFile = new ZipFile(source);
zipFile.extractAll(destination);
} catch (ZipException e) {
e.printStackTrace();
}
}
}else {
println" Zip Extracted already"
}
}
void loadAllFiles(ArrayList<File> files, File directory) {
File[] filesInDir=directory.listFiles()
boolean hasPrintBed = false;
for(File f:filesInDir) {
if(!f.isDirectory()){
if(f.getName().contains("Print-Bed")) {
hasPrintBed=true;
files.add(f)
}
}else {
loadAllFiles(files,f)
}
}
if(!hasPrintBed) {
for(File f:filesInDir) {
if(!f.isDirectory()){
if(f.getName().contains(".stl")&& !f.getName().contains("Part")) {
files.add(f)
}
}
}
}
}
GitHub github = getGithub();
String projectDestBaseName = "BancroftKineticSystemsClass"
GHOrganization dest = github.getMyOrganizations().get(projectDestBaseName);
if (dest == null) {
System.out.println("FAIL, you do not have access to " + projectDestBaseName);
return;
}
PagedIterable<GHRepository> repos = dest.listRepositories();
ArrayList<File> stls = []
ArrayList<File> toLoad =[]
for (GHRepository R : repos) {
//println R.getHtmlUrl()
GHRelease release= R.getLatestRelease()
if(release!=null) {
String rGetFullName = R.getFullName()
println "Files for "+rGetFullName
if(rGetFullName.contains("Group09"))
continue;
if(rGetFullName.contains("Group03"))
continue;
if(rGetFullName.contains("Group08"))
continue;
String tmpDirsLocation = System.getProperty("java.io.tmpdir");
File tmp = new File(tmpDirsLocation+"/"+rGetFullName)
tmp.mkdirs( )
List<GHAsset> assets= release.getAssets()
for(GHAsset asset :assets) {
//println tmp.getAbsolutePath()
//println "\t"+ asset.getApiRoute()
extract("https://api.github.com"+asset.getApiRoute(),tmp,asset.getName())
loadAllFiles(toLoad, tmp)
}
}
}
ArrayList<CSG> parts = []
int count = 0
for(File f:toLoad) {
println f.getAbsolutePath()
String name = f.getAbsolutePath()
CSG get = Vitamins.get(f)
.toZMin()
//.rotz(count>toLoad.size()/2?90:0)
get.setName(name)
count++;
if(count%4==0)
get.setPrintBedNumber(0)
if((count-1)%4==0)
get.setPrintBedNumber(1)
if((count-2)%4==0)
get.setPrintBedNumber(2)
if((count-3)%4==0)
get.setPrintBedNumber(3)
parts.add(get)
}
return parts