-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
185 lines (149 loc) · 5.84 KB
/
index.ts
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
import axios from "axios";
import * as fs from 'fs';
import { glob } from 'glob';
import DesciExtractor from "./lib/desciExtractor";
interface RootComponentPayload {
path: string,
cid: string,
externalUrl?: string,
keywords?: string[],
description?: string,
url?: string,
title?: string,
ontologyPurl?: string,
cedarLink?: string,
controlledVocabTerms?: string[],
}
interface RootComponent {
id: string,
name: string,
type: string,
payload: RootComponentPayload,
starred: boolean,
}
class pyExtractor {
pid: number;
constructor(PID: number) {
this.pid = PID;
}
async getRoot() {
const response = await axios.get(`https://beta.dpid.org/${this.pid.toString()}?raw`, {
headers: {
"Content-Type": "application/json"
}
})
const components: RootComponent[] = response.data.components;
for (let x = 0; x < components.length; x++) {
if (components[x].name === "Code Repository") {
await this.getPyFiles(components[x].payload.path)
}
}
await this.getParentFunctions();
await this.upload();
}
async getPyFiles(path: string) {
const response = await axios.get(`https://beta.dpid.org/${this.pid.toString()}/${path}?raw`)
const codeLinks = response.data.Links;
// If no links/files
if (!codeLinks) return []
let pyFiles: any[] = []
for (let x = 0; x < codeLinks.length; x++) {
// If python file is found
if (codeLinks[x].Name.includes(".") && codeLinks[x].Name.includes(".py")) {
console.log("Python file found - " + codeLinks[x].Name)
let file = {
...codeLinks[x],
path: `${path}/${codeLinks[x].Name}`
}
await this.writePyFile(file.Hash["/"], file.Name)
pyFiles.push(file)
continue;
} else if (codeLinks[x].Name.includes(".")) {
// Filetype other than PY
console.log("Non-python file found - " + codeLinks[x].Name)
continue;
} else {
// Directory
console.log("Directory found - " + codeLinks[x].Name)
const sub: any = await this.getPyFiles(`${path}/${codeLinks[x].Name}`)
pyFiles = [...pyFiles, ...sub]
}
}
return pyFiles
}
async writePyFile(hash: string, name: string): Promise<void> {
if (!fs.existsSync('./desci')) fs.mkdirSync('./desci')
const response = await axios.get(`https://ipfs.desci.com/ipfs/${hash}`)
fs.writeFileSync(`desci/${name}`, response.data, 'utf-8')
}
async getParentFunctions(): Promise<void> {
const parentFiles = await glob('./desci/*.py', {
ignore: './inputs/example.py'
})
parentFiles.forEach((dir: string) => {
const extract = new DesciExtractor({
directory: dir
})
extract.pullFunctions()
})
}
async upload(): Promise<void> {
const files = await glob('./desci/**/*')
let manifest: any = {}
// Add parent files to dictionary
for (let x = 0; x < files.length; x++) {
if (files[x].split("/").length === 2 && files[x].includes(".py")) {
let data = new FormData();
let fileBuffer = await fs.readFileSync(`./${files[x]}`)
let fileBlob = new Blob([fileBuffer], { type: 'application/octet-stream' })
data.append('file', fileBlob, files[x].split("/")[1]);
const response = await axios("http://127.0.0.1:5001/api/v0/add?hash=sha2-256&pin=true&to-files=/&raw-leaves=true", {
method: "post",
data
})
manifest = {
...manifest,
[`${files[x].split("/")[1]}`]: {
"cid": response.data.Hash,
"dependencies": []
}
};
}
}
// Add dependencies to dictionary
for (let x = 0; x < Object.keys(manifest).length; x++) {
// Check if parent has any dependencies
if (fs.existsSync(`./desci/${Object.keys(manifest)[x].split('.py')[0]}`)) {
const deps = await glob(`./desci/${Object.keys(manifest)[x].split('.py')[0]}/*`)
for (let y = 0; y < deps.length; y++) {
let data = new FormData();
let fileBuffer = await fs.readFileSync(`./${deps[y]}`)
let fileBlob = new Blob([fileBuffer], { type: 'application/octet-stream' })
data.append('file', fileBlob, deps[y].split("/")[2]);
const response = await axios("http://127.0.0.1:5001/api/v0/add?hash=sha2-256&pin=true&to-files=/&raw-leaves=true", {
method: "post",
data
})
manifest[Object.keys(manifest)[x]].dependencies = [
...manifest[Object.keys(manifest)[x]].dependencies,
{
"name": deps[y].split("/")[2],
"cid": response.data.Hash
}
]
}
} else {
continue
}
}
// Write manfiest
fs.writeFileSync('desci/manifest.json', JSON.stringify(manifest, null, 4), 'utf-8')
console.log("[!] Wrote Manifest to desci folder")
}
}
async function main() {
// Replace PID with node of interest
let tmp = new pyExtractor(76)
const traversal = await tmp.getRoot();
}
main();