1
- import json
2
1
import logging
3
2
from base64 import b16encode
4
3
from enum import Enum
5
4
from hashlib import md5
6
5
from uuid import uuid4
7
6
8
- from django .conf import settings
9
7
from django .utils import timezone
10
- from shared .utils .ReportEncoder import ReportEncoder
11
8
12
9
from services .storage import StorageService
13
10
from utils .config import get_config
17
14
18
15
class MinioEndpoints (Enum ):
19
16
chunks = "{version}/repos/{repo_hash}/commits/{commitid}/chunks.txt"
20
- json_data = "{version}/repos/{repo_hash}/commits/{commitid}/json_data/{table}/{field}/{external_id}.json"
21
- json_data_no_commit = (
22
- "{version}/repos/{repo_hash}/json_data/{table}/{field}/{external_id}.json"
23
- )
24
- raw = "v4/raw/{date}/{repo_hash}/{commit_sha}/{reportid}.txt"
17
+
25
18
raw_with_upload_id = (
26
19
"v4/raw/{date}/{repo_hash}/{commit_sha}/{reportid}/{uploadid}.txt"
27
20
)
@@ -31,7 +24,6 @@ class MinioEndpoints(Enum):
31
24
static_analysis_single_file = (
32
25
"{version}/repos/{repo_hash}/static_analysis/files/{location}"
33
26
)
34
-
35
27
test_results = "test_results/v1/raw/{date}/{repo_hash}/{commit_sha}/{uploadid}.txt"
36
28
37
29
def get_path (self , ** kwaargs ):
@@ -71,21 +63,6 @@ def __init__(self, repository, ttl=None):
71
63
self .storage = StorageService ()
72
64
self .storage_hash = self .get_archive_hash (repository )
73
65
74
- """
75
- Accessor for underlying StorageService. You typically shouldn't need
76
- this for anything.
77
- """
78
-
79
- def storage_client (self ):
80
- return self .storage
81
-
82
- """
83
- Getter. Returns true if the current configuration is enterprise.
84
- """
85
-
86
- def is_enterprise (self ):
87
- return settings .IS_ENTERPRISE
88
-
89
66
"""
90
67
Generates a hash key from repo specific information.
91
68
Provides slight obfuscation of data in minio storage
@@ -109,133 +86,23 @@ def get_archive_hash(cls, repository):
109
86
_hash .update (val )
110
87
return b16encode (_hash .digest ()).decode ()
111
88
112
- def write_json_data_to_storage (
113
- self ,
114
- commit_id ,
115
- table : str ,
116
- field : str ,
117
- external_id : str ,
118
- data : dict ,
119
- * ,
120
- encoder = ReportEncoder ,
121
- ):
122
- if commit_id is None :
123
- # Some classes don't have a commit associated with them
124
- # For example Pull belongs to multiple commits.
125
- path = MinioEndpoints .json_data_no_commit .get_path (
126
- version = "v4" ,
127
- repo_hash = self .storage_hash ,
128
- table = table ,
129
- field = field ,
130
- external_id = external_id ,
131
- )
132
- else :
133
- path = MinioEndpoints .json_data .get_path (
134
- version = "v4" ,
135
- repo_hash = self .storage_hash ,
136
- commitid = commit_id ,
137
- table = table ,
138
- field = field ,
139
- external_id = external_id ,
140
- )
141
- stringified_data = json .dumps (data , cls = encoder )
142
- self .write_file (path , stringified_data )
143
- return path
144
-
145
- """
146
- Writes a generic file to the archive -- it's typically recommended to
147
- not use this in lieu of the convenience methods write_raw_upload and
148
- write_chunks
149
- """
150
-
151
- def write_file (self , path , data , reduced_redundancy = False , gzipped = False ):
152
- self .storage .write_file (
153
- self .root ,
154
- path ,
155
- data ,
156
- reduced_redundancy = reduced_redundancy ,
157
- gzipped = gzipped ,
158
- )
159
-
160
- """
161
- Convenience write method, writes a raw upload to a destination.
162
- Returns the path it writes.
163
- """
164
-
165
- def write_raw_upload (self , commit_sha , report_id , data , gzipped = False ):
166
- # create a custom report path for a raw upload.
167
- # write the file.
168
- path = "/" .join (
169
- (
170
- "v4/raw" ,
171
- timezone .now ().strftime ("%Y-%m-%d" ),
172
- self .storage_hash ,
173
- commit_sha ,
174
- "%s.txt" % report_id ,
175
- )
176
- )
177
-
178
- self .write_file (path , data , gzipped = gzipped )
179
-
180
- return path
181
-
182
- """
183
- Convenience method to write a chunks.txt file to storage.
184
- """
185
-
186
- def write_chunks (self , commit_sha , data ):
187
- path = MinioEndpoints .chunks .get_path (
188
- version = "v4" , repo_hash = self .storage_hash , commitid = commit_sha
189
- )
190
-
191
- self .write_file (path , data )
192
- return path
193
-
194
- """
195
- Generic method to read a file from the archive
196
- """
197
-
198
89
def read_file (self , path ):
90
+ """
91
+ Generic method to read a file from the archive
92
+ """
199
93
contents = self .storage .read_file (self .root , path )
200
94
return contents .decode ()
201
95
202
- """
203
- Generic method to delete a file from the archive.
204
- """
205
-
206
- def delete_file (self , path ):
207
- self .storage .delete_file (self .root , path )
208
-
209
- """
210
- Deletes an entire repository's contents
211
- """
212
-
213
- def delete_repo_files (self ):
214
- path = "v4/repos/{}" .format (self .storage_hash )
215
- objects = self .storage .list_folder_contents (self .root , path )
216
- for obj in objects :
217
- self .storage .delete_file (self .root , obj .object_name )
218
-
219
- """
220
- Convenience method to read a chunks file from the archive.
221
- """
222
-
223
96
def read_chunks (self , commit_sha ):
97
+ """
98
+ Convenience method to read a chunks file from the archive.
99
+ """
224
100
path = MinioEndpoints .chunks .get_path (
225
101
version = "v4" , repo_hash = self .storage_hash , commitid = commit_sha
226
102
)
227
103
log .info ("Downloading chunks from path %s for commit %s" , path , commit_sha )
228
104
return self .read_file (path )
229
105
230
- """
231
- Delete a chunk file from the archive
232
- """
233
-
234
- def delete_chunk_from_archive (self , commit_sha ):
235
- path = "v4/repos/{}/commits/{}/chunks.txt" .format (self .storage_hash , commit_sha )
236
-
237
- self .delete_file (path )
238
-
239
106
def create_presigned_put (self , path ):
240
107
return self .storage .create_presigned_put (self .root , path , self .ttl )
241
108
0 commit comments