/
Fetch.d
369 lines (288 loc) · 9.16 KB
/
Fetch.d
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* Copyright: Copyright (c) 2010-2011 Jacob Carlborg. All rights reserved.
* Authors: Jacob Carlborg
* Version: Initial created: Nov 8, 2010
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*/
module dvm.commands.Fetch;
import std.algorithm : splitter, startsWith;
import std.exception : assumeUnique;
import std.format : format;
import std.range : empty, front;
import std.regex;
import std.stdio : writeln;
import std.string : split;
import tango.core.Exception;
import tango.io.device.File;
import tango.io.model.IConduit;
import Path = tango.io.Path;
import tango.net.InternetAddress;
import tango.net.device.Socket;
import tango.net.http.HttpGet;
import tango.net.http.HttpConst;
import dvm.commands.Command;
import dvm.util._;
import dvm.dvm._;
class Fetch : Command
{
this (string name, string summary = "")
{
super(name, summary);
}
this ()
{
super("fetch", "Fetch a D compiler but don't install it.");
}
override void execute ()
{
if (args.any && args.first == "dmc")
fetchDMC;
else
{
auto version_ = getDMDVersion();
string filename = buildFilename(version_);
string url = buildUrl(version_, filename);
fetch(url, Path.join(".", filename).assumeUnique);
}
}
protected:
enum userAgent = buildUserAgent();
enum dmcArchiveName = "dm852c.zip";
void fetchDMC (string destinationPath=".")
{
auto url = "http://ftp.digitalmars.com/Digital_Mars_C++/Patch/" ~ dmcArchiveName;
fetch(url, Path.join(destinationPath, dmcArchiveName).assumeUnique);
}
void fetch (string source, string destination)
{
if (Path.exists(destination))
return;
if (options.verbose)
{
writeln("Fetching:");
writeln(options.indentation, "source: ", source);
writeln(options.indentation, "destination: ", destination, '\n');
}
else
writeln("Fetching: ", source);
createPath(Path.parse(destination).folder);
writeFile(downloadFile(source), destination);
}
void[] downloadFile (string url)
{
static void print (A...)(A args)
{
import tango.io.Stdout;
static enum string fmt = "{}{}{}{}{}{}{}{}" ~
"{}{}{}{}{}{}{}{}" ~
"{}{}{}{}{}{}{}{}";
static assert (A.length <= fmt.length / 2, "mambo.io.print :: too many arguments");
Stdout.format(fmt[0 .. args.length * 2], args).flush;
}
auto page = new HttpGet(url);
page.setTimeout(30f);
page.getRequestHeaders.add(HttpHeader.UserAgent, userAgent);
auto buffer = page.open;
scope(exit)
page.close;
checkPageStatus(page, url);
// load in chunks in order to display progress
int contentLength = page.getResponseHeaders.getInt(HttpHeader.ContentLength);
enum width = 40;
int num = width;
version (Posix)
{
enum clearLine = "\033[1K"; // clear backwards
enum saveCursor = "\0337";
enum restoreCursor = "\0338";
}
else
{
enum clearLine = "\r";
// Leaving these empty string causes a linker error:
// http://d.puremagic.com/issues/show_bug.cgi?id=4315
enum saveCursor = "\0";
enum restoreCursor = "\0";
}
print(saveCursor);
int bytesLeft = contentLength;
int chunkSize = bytesLeft / num;
while (bytesLeft > 0)
{
buffer.load(chunkSize > bytesLeft ? bytesLeft : chunkSize);
bytesLeft -= chunkSize;
int i = 0;
print(clearLine ~ restoreCursor ~ saveCursor);
print("[");
for ( ; i < (width - num); i++)
print("=");
print('>');
for ( ; i < width; i++)
print(" ");
print("]");
print(" ", (contentLength - bytesLeft) / 1024, "/", contentLength / 1024, " KB");
num--;
}
writeln(restoreCursor);
writeln();
return buffer.slice;
}
void writeFile (void[] data, string filename)
{
auto file = new File(filename, File.WriteCreate);
scope(exit) file.close();
file.write(data);
}
string buildFilename (string ver="")
{
if (ver == "")
ver = getDMDVersion;
auto filename = format("dmd.%s.%s.zip", ver, platformForArchive);
auto url = buildUrl(ver, filename);
return urlExists(url) ? filename : format("dmd.%s.zip", ver);
}
string buildUrl (string version_, string filename)
{
auto url = dlangUrl(version_, filename);
if (urlExists(url))
return url;
return digitalMarsUrl(filename);
}
string digitalMarsUrl (string filename)
{
return "http://ftp.digitalmars.com/" ~ filename;
}
string dlangUrl (string version_, string filename)
{
enum baseUrl = "http://downloads.dlang.org/";
string releases = "releases";
if (isPreRelease(version_))
{
releases = "pre-releases";
version_ = version_.split("-").front;
}
return format(baseUrl ~ "%s/%s.x/%s/%s", releases, version_.front, version_, filename);
}
void createPath (string path)
{
if (!Path.exists(path))
Path.createPath(path);
}
void checkPageStatus (HttpGet page, string url)
{
if (page.getStatus == 404)
throw new IOException(format(`The resource with URL "%s" could not be found.`, url));
else if (!page.isResponseOK)
throw new IOException(format(`An unexpected error occurred. The resource "%s" responded with the message "%s" and the status code %s.`, url, page.getResponse.getReason, page.getResponse.getStatus));
}
bool urlExists (string url, float timeout = 5f)
{
scope page = new HttpGet(url);
page.setTimeout(timeout);
page.enableRedirect();
page.open();
return page.isResponseOK;
}
string getDMDVersion ()
{
if (options.latest)
{
auto vers = getDVersion;
return args.first = getLatestDMDVersion(vers);
}
else
{
validateArguments();
return args.first;
}
}
string getDVersion ()
{
return args.empty() ? "2" : args.first;
}
string getLatestDMDVersion (string dVersion)
{
enum mirrors = [
"http://downloads.dlang.org/releases/LATEST",
"http://ftp.digitalmars.com/LATEST"
];
static string fetchFromMirror(string url)
{
scope page = new HttpGet(url);
page.setTimeout(5f);
page.enableRedirect();
page.open();
return page.isResponseOK ? cast(string) page.read : null;
}
foreach (mirror ; mirrors)
{
if (auto content = fetchFromMirror(mirror))
return content;
}
throw new DvmException("Failed to get the latest DMD version.", __FILE__, __LINE__);
}
void validateArguments (string errorMessage = null)
{
if (errorMessage.empty)
errorMessage = "Cannot fetch a compiler without specifying a version";
if (args.empty)
throw new DvmException(errorMessage, __FILE__, __LINE__);
}
bool isPreRelease (string version_)
{
immutable parts = version_.split("-");
if (parts.length != 2)
return false;
immutable suffix = parts[1];
return suffix.front == 'b' || suffix.startsWith("rc");
}
string platformForArchive()
{
auto platform = options.platform;
version (FreeBSD)
platform ~= options.is64bit ? "-64" : "-32";
return platform;
}
}
string buildUserAgent ()
{
import std.system;
import std.string;
version (X86) auto architecture = "i386";
else version (X86_64) auto architecture = "x86_64";
else static assert("Unhandled platform");
return format("dvm/%s (%s-%s)", dvm.dvm.Version.Version, architecture, os);
}
template FetchImpl ()
{
void execute ()
{
auto filename = buildFilename;
auto url = buildUrl(filename);
fetch(url, join(".", filename));
}
protected void fetch (string source, string destination)
{
writeFile(downloadFile(source), destination);
}
private void[] downloadFile (string url)
{
auto page = new HttpGet(url);
if (!page.isResponseOK())
throw new IOException(format("%s", page.getResponse.getStatus));
return page.read;
}
private void writeFile (void[] data, string filename)
{
auto file = new File(filename, File.WriteCreate);
file.write(data);
}
private string buildFilename ()
{
return "dmd." ~ args.first ~ ".zip";
}
private string buildUrl (string filename)
{
return "http://ftp.digitalmars.com/" ~ filename;
}
}