Skip to content

Commit a5a7ba0

Browse files
Implement initial debian purl2meta implementation
Reference: #245 Signed-off-by: Ayan Sinha Mahapatra <ayansmahapatra@gmail.com>
1 parent b901304 commit a5a7ba0

File tree

5 files changed

+398
-47
lines changed

5 files changed

+398
-47
lines changed

minecode/utils.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,44 @@ def get_http_response(uri, timeout=10):
238238
return response
239239

240240

241+
def get_package_sha1(package):
242+
"""
243+
Return the sha1 value for `package` by checking if the sha1 file exists for
244+
`package` on maven and returning the contents if it does.
245+
246+
If the sha1 is invalid, we download the package's JAR and calculate the sha1
247+
from that.
248+
"""
249+
download_url = package.repository_download_url
250+
sha1_download_url = f'{download_url}.sha1'
251+
response = requests.get(sha1_download_url)
252+
if response.ok:
253+
sha1_contents = response.text.strip().split()
254+
sha1 = sha1_contents[0]
255+
sha1 = validate_sha1(sha1)
256+
if not sha1:
257+
# Download JAR and calculate sha1 if we cannot get it from the repo
258+
response = requests.get(download_url)
259+
if response:
260+
sha1_hash = hashlib.new('sha1', response.content)
261+
sha1 = sha1_hash.hexdigest()
262+
return sha1
263+
264+
265+
def validate_sha1(sha1):
266+
"""
267+
Validate a `sha1` string.
268+
269+
Return `sha1` if it is valid, None otherwise.
270+
"""
271+
if sha1 and len(sha1) != 40:
272+
logger.warning(
273+
f'Invalid SHA1 length ({len(sha1)}): "{sha1}": SHA1 ignored!'
274+
)
275+
sha1 = None
276+
return sha1
277+
278+
241279
def system_temp_dir(temp_dir=os.getenv('MINECODE_TMP')):
242280
"""
243281
Return the global temp directory..

0 commit comments

Comments
 (0)