Guest User

Untitled

a guest
May 2nd, 2023
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | Cybersecurity | 0 0
  1. import os
  2. import subprocess
  3.  
  4. def get_installed_version(package_name):
  5.     output = subprocess.check_output(['pip', 'show', package_name]).decode('utf-8')
  6.     lines = output.split('\n')
  7.     for line in lines:
  8.         if line.startswith('Version:'):
  9.             return line.split(':')[1].strip()
  10.     return None
  11.  
  12. def update_requirements_file(file_path):
  13.     with open(file_path, 'r') as f:
  14.         lines = f.readlines()
  15.  
  16.     updated_lines = []
  17.  
  18.     for line in lines:
  19.         package_name = line.strip().split('==')[0]
  20.         installed_version = get_installed_version(package_name)
  21.  
  22.         if installed_version is not None:
  23.             updated_line = f"{package_name}=={installed_version}\n"
  24.         else:
  25.             updated_line = line
  26.  
  27.         updated_lines.append(updated_line)
  28.  
  29.     with open(file_path, 'w') as f:
  30.         f.writelines(updated_lines)
  31.  
  32. if __name__ == "__main__":
  33.     requirements_file = "requirements.txt"  # Replace this with the path to your requirements.txt file
  34.     update_requirements_file(requirements_file)
  35.  
Advertisement
Add Comment
Please, Sign In to add comment