-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy patharchive-binary-dependencies.ts
More file actions
194 lines (167 loc) · 5.58 KB
/
archive-binary-dependencies.ts
File metadata and controls
194 lines (167 loc) · 5.58 KB
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
/*
* archive-binary-dependencies.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { join } from "../../../src/deno_ral/path.ts";
import { info } from "../../../src/deno_ral/log.ts";
import { logPandoc } from "../../../src/core/log.ts";
import { execProcess } from "../../../src/core/process.ts";
import { Configuration, withWorkingDir } from "./config.ts";
import {
ArchitectureDependency,
Dependency,
kDependencies,
PlatformDependency,
} from "./dependencies/dependencies.ts";
const kBucket = "s3://rstudio-buildtools/quarto";
const kBucketBaseUrl = "https://s3.amazonaws.com/rstudio-buildtools/quarto";
// Provides a URL in the archive for a dependency
export function archiveUrl(
dependency: Dependency,
platformDependency: PlatformDependency,
) {
// TODO: Deal w/archive bin deps for this
if (dependency.bucket === "deno") {
return platformDependency.url + platformDependency.filename;
} else {
return `${kBucketBaseUrl}/${dependency.bucket}/${dependency.version}/${platformDependency.filename}`;
}
}
// Archives dependencies (if they are not present in the archive already)
export async function archiveBinaryDependencies(_config: Configuration) {
await withWorkingDir(async (workingDir) => {
await logPandoc(`## Updating binary dependencies`);
for (const dependency of kDependencies) {
await archiveBinaryDependency(dependency, workingDir);
}
});
}
// Archives dependencies (if they are not present in the archive already)
export async function checkBinaryDependencies(_config: Configuration) {
await withWorkingDir(async (workingDir) => {
await logPandoc(`## Checking binary dependencies`);
for (const dependency of kDependencies) {
await checkBinaryDependency(dependency, workingDir);
}
});
}
export async function checkBinaryDependency(
dependency: Dependency,
workingDir: string,
) {
info(`** ${dependency.name} ${dependency.version} **`);
const dependencyBucketPath = `${dependency.bucket}/${dependency.version}`;
info("Checking archive status...\n");
const archive = async (
architectureDependency: ArchitectureDependency,
) => {
const platformDeps = [
architectureDependency.darwin,
architectureDependency.linux,
architectureDependency.windows,
];
for (const platformDep of platformDeps) {
if (platformDep) {
// This dependency doesn't exist, archive it
info(
`Checking ${dependencyBucketPath} - ${platformDep.filename}`,
);
// Download the file
await download(
workingDir,
platformDep,
);
}
}
};
for (const arch of Object.keys(dependency.architectureDependencies)) {
info(`Checking ${dependency.name}`);
const archDep = dependency.architectureDependencies[arch];
await archive(archDep);
}
info("");
}
export async function archiveBinaryDependency(
dependency: Dependency,
workingDir: string,
) {
await logPandoc(`## ${dependency.name} ${dependency.version}\n\nChecking archive status...`, "markdown");
const dependencyBucketPath = `${dependency.bucket}/${dependency.version}`;
const archive = async (
architectureDependency: ArchitectureDependency,
) => {
const platformDeps = [
architectureDependency.darwin,
architectureDependency.linux,
architectureDependency.windows,
];
for (const platformDep of platformDeps) {
if (platformDep) {
const dependencyAwsPath =
`${kBucket}/${dependencyBucketPath}/${platformDep.filename}`;
await logPandoc(`Checking \`${dependencyAwsPath}\``, "markdown");
const response = await s3cmd("ls", [dependencyAwsPath]);
if (response?.includes('Unable to locate credentials')) {
throw new Error("Unable to locate S3 credentials, please try again.");
}
if (!response) {
// This dependency doesn't exist, archive it
await logPandoc(
`Archiving \`${dependencyBucketPath}\` - ${platformDep.filename}`,
);
// Download the file
const localPath = await download(
workingDir,
platformDep,
);
// Sync to S3
info(`Copying to ${dependencyAwsPath}\n`);
const result = await s3cmd("cp", [
localPath,
dependencyAwsPath,
"--acl",
"public-read",
]);
info(` (Response): ${result}`);
} else {
info(` ${dependencyAwsPath.split("/").slice(-1)[0]} already archived.\n`);
}
}
}
};
for (const arch of Object.keys(dependency.architectureDependencies)) {
const archDep = dependency.architectureDependencies[arch];
await archive(archDep);
}
}
async function s3cmd(cmd: string, args: string[]) {
const s3Command = ["aws", "s3", cmd, ...args];
const p = await execProcess({
cmd: s3Command,
stdout: "piped",
});
return p.stdout || p.stderr;
}
async function download(
workingDir: string,
dependency: PlatformDependency,
) {
info("Downloading " + dependency.url);
const response = await fetch(dependency.url);
if (response.status === 200) {
const blob = await response.blob();
const bytes = await blob.arrayBuffer();
const data = new Uint8Array(bytes);
const targetPath = join(workingDir, dependency.filename);
Deno.writeFileSync(
targetPath,
data,
);
return targetPath;
} else {
throw new Error(
`Failed to fetch dependency ${dependency.filename}.\nThe url ${dependency.url} returned a non 200 status code:\n${response.status} - ${response.statusText}`,
);
}
}