3636import subprocess
3737
3838
39- os .chdir (os .pardir ) #go to etc
40- os .chdir (os .pardir ) # go to scancode-toolkit
41- cwd = os .getcwd ()
42- print ("Current working directory:" , cwd )
43- fp = open ("requirements.txt" , "wb" )
44- fp .close ()
45-
46-
4739def is_package_exist_with_hashes (package_name ,hash_of_file ):
4840 """ Check if any line in the file contains package with same hashes """
4941 # Open the file in read only mode
50- line_with_hash = package_name + " \\ " + " \n " + " --hash=sha256:" + hash_of_file
51- with open (" requirements.txt" , 'r' ) as read_obj :
42+ line_with_hash = "{} \\ \n --hash=sha256:{}" . format ( package_name , hash_of_file )
43+ with open (' requirements.txt' ) as file :
5244 # Read all lines in the file one by one
53- for line in read_obj :
45+ for line in file :
5446 # For each line, check if line contains the package with same hashes
5547 if line_with_hash in line :
5648 return True
@@ -59,33 +51,33 @@ def is_package_exist_with_hashes(package_name,hash_of_file):
5951def is_package_exist_without_hashes (package_name ):
6052 """ Check if any line in the file contains package without same hash """
6153 # Open the file in read only mode
62- line_with_hash = package_name + " \\ " + " \n "
63- with open (" requirements.txt" , 'r' ) as read_obj :
54+ line_with_hash = "{} \\ \n ". format ( package_name )
55+ with open (' requirements.txt' ) as file :
6456 # Read all lines in the file one by one
65- for line in read_obj :
57+ for line in file :
6658 # For each line, check if line contains the string
6759 if line_with_hash in line :
6860 return True
6961 return False
7062
7163
7264def add_package (package_name ,hash_of_file ):
73- with open (" requirements.txt" , "a" ) as file_object :
65+ with open (' requirements.txt' , 'a' ) as file :
7466 # Append package with hashes at the end of file
75- line = package_name + " \\ " + " \n " + " --hash=sha256:" + hash_of_file + " \n "
76- file_object .write (line )
67+ line = "{} \\ \n --hash=sha256:{} \n ". format ( package_name , hash_of_file )
68+ file .write (line )
7769
7870def append_requirement_file (package_name ,hash_of_file ):
7971 if is_package_exist_with_hashes (package_name ,hash_of_file ):
8072 return
8173 else :
82- inputfile = open (" requirements.txt" , 'r ' ).readlines ()
83- write_file = open (" requirements.txt" ,'w' )
74+ inputfile = open (' requirements.txt' ).readlines ()
75+ write_file = open (' requirements.txt' ,'w' )
8476 for line in inputfile :
8577 write_file .write (line )
86- lion = package_name + " \\ " + "\n "
87- if lion in line :
88- new_line = " --hash=sha256:" + hash_of_file + " \\ "
78+ current_line = package_name + " \\ " + "\n "
79+ if current_line in line :
80+ new_line = " --hash=sha256:{} \\ " . format ( hash_of_file )
8981 write_file .write (new_line + "\n " )
9082 write_file .close ()
9183
@@ -108,10 +100,14 @@ def hash_of_file(path):
108100 return hash .hexdigest ()
109101
110102def main ():
103+ os .chdir (os .pardir ) #go to etc
104+ os .chdir (os .pardir ) # go to scancode-toolkit
105+ cwd = os .getcwd ()
106+ print ("Current working directory:" , cwd )
107+ fp = open ('requirements.txt' ,'w' ) #Create empty file because it must exits before opening the file in read mode.
111108 for subdir , dirs , files in os .walk (cwd + "/thirdparty" ):
112109 for filename in files :
113110 filepath = subdir + os .sep + filename
114- print (filename )
115111 if filepath .endswith (".whl" ) and (fnmatch .fnmatchcase (filename , "*py3*" ) or fnmatch .fnmatchcase (filename , "*cp36*" )):
116112 name = filename .split ('-' )[0 ]
117113 version = filename .split ('-' )[1 ]
@@ -133,6 +129,7 @@ def main():
133129 if is_package_exist_without_hashes (package_name ):
134130 append_requirement_file (package_name ,hs )
135131 else : add_package (package_name ,hs )
132+ fp .close ()
136133
137134if __name__ == '__main__' :
138135 main ()
0 commit comments