2020-05-05 19:34:34 +02:00
#!/usr/bin/env python3
2019-01-01 20:22:32 +01:00
2020-05-05 19:34:34 +02:00
import sys
2019-01-01 20:22:32 +01:00
2019-01-20 17:51:21 +01:00
def get_pkg_hash_from_Packages ( Packages_file , package , version , hash = " SHA256 " ) :
2019-01-01 20:22:32 +01:00
with open ( Packages_file , ' r ' ) as Packages :
package_list = Packages . read ( ) . split ( ' \n \n ' )
for pkg in package_list :
if pkg . split ( ' \n ' ) [ 0 ] == " Package: " + package :
for line in pkg . split ( ' \n ' ) :
2019-02-16 18:49:13 +01:00
# Assuming Filename: comes before Version:
if line . startswith ( ' Filename: ' ) :
print ( line . split ( " " ) [ 1 ] + " " )
elif line . startswith ( ' Version: ' ) :
2019-01-20 17:51:21 +01:00
if line != ' Version: ' + version :
# Seems the repo contains the wrong version, or several versions
# We can't use this one so continue looking
break
elif line . startswith ( hash ) :
2019-01-01 20:22:32 +01:00
print ( line . split ( " " ) [ 1 ] )
break
2019-02-16 18:49:13 +01:00
def get_Packages_hash_from_Release ( Release_file , arch , component , hash = " SHA256 " ) :
string_to_find = component + ' /binary- ' + arch + ' /Packages '
with open ( Release_file , ' r ' ) as Release :
hash_list = Release . readlines ( )
2019-01-01 20:22:32 +01:00
for i in range ( len ( hash_list ) ) :
if hash_list [ i ] . startswith ( hash + ' : ' ) :
break
for j in range ( i , len ( hash_list ) ) :
2019-02-10 12:26:32 +01:00
if string_to_find in hash_list [ j ] . strip ( ' ' ) :
2020-06-23 14:21:29 +02:00
hash_entry = list ( filter ( lambda s : s != ' ' , hash_list [ j ] . strip ( ' ' ) . split ( ' ' ) ) )
if hash_entry [ 2 ] . startswith ( " .work_ " ) :
continue
print ( hash_entry [ 0 ] )
2019-01-01 20:22:32 +01:00
break
if __name__ == ' __main__ ' :
if len ( sys . argv ) < 2 :
2019-02-10 12:26:32 +01:00
sys . exit ( ' Too few arguments, I need the path to a Packages file, a package name and a version, or an InRelease file, an architecture and a component name. Exiting ' )
2019-01-01 20:22:32 +01:00
if sys . argv [ 1 ] . endswith ( ' Packages ' ) :
2019-01-20 17:51:21 +01:00
g et_pkg_hash_from_Packages ( sys . argv [ 1 ] , sys . argv [ 2 ] , sys . argv [ 3 ] )
2019-02-16 18:49:13 +01:00
elif sys . argv [ 1 ] . endswith ( ( ' InRelease ' , ' Release ' ) ) :
get_Packages_hash_from_Release ( sys . argv [ 1 ] , sys . argv [ 2 ] , sys . argv [ 3 ] )
2019-01-01 20:22:32 +01:00
else :
2019-02-16 18:49:13 +01:00
sys . exit ( sys . argv [ 1 ] + ' does not seem to be a path to a Packages or InRelease/Release file ' )