Created
August 28, 2024 12:18
-
-
Save eugen-hoppe/7a0dbb6857904a992a604e6d23752b5d to your computer and use it in GitHub Desktop.
Create for each record in requirements.txt a record with a version depending on environment.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pkg_resources | |
| import os | |
| INPUT_FILE = "requirements.txt" | |
| OUTPUT_FILE = "requirements_.txt" | |
| # OUTPUT_FILE = "requirements.txt" | |
| def get_installed_version(package_name): | |
| try: | |
| return pkg_resources.get_distribution(package_name).version | |
| except pkg_resources.DistributionNotFound: | |
| return None | |
| def process_requirements(input_file=INPUT_FILE, output_file=OUTPUT_FILE): | |
| if not os.path.exists(input_file): | |
| print(f"Die Datei {input_file} existiert nicht.") | |
| return | |
| with open(input_file, 'r') as f: | |
| requirements = f.readlines() | |
| with open(output_file, 'w') as f: | |
| for requirement in requirements: | |
| requirement = requirement.strip() | |
| if '[' in requirement: | |
| package_name, extras = requirement.split('[') | |
| extras = '[' + extras | |
| else: | |
| package_name = requirement | |
| extras = '' | |
| version = get_installed_version(package_name) | |
| if version: | |
| f.write(f"{package_name}{extras}=={version}\n") | |
| else: | |
| f.write(f"{requirement}\n") | |
| if __name__ == "__main__": | |
| process_requirements() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.