Skip to content

Instantly share code, notes, and snippets.

@eban5
Created March 12, 2019 15:41
Show Gist options
  • Save eban5/2a61b28c27285d44dc58a797224199c4 to your computer and use it in GitHub Desktop.
Save eban5/2a61b28c27285d44dc58a797224199c4 to your computer and use it in GitHub Desktop.
Python script to parse a Requirements.txt file and get a list of Package Name, Version, and Dependency Source URL
import os
import re
import subprocess
packages = {
'package_name': re.compile(r'Name: (?P<package_name>.*)\n'),
'version': re.compile(r'Version: (?P<version>.*)\n'),
'url': re.compile(r'Home-page: (?P<url>.*)\n'),
}
def _parse_line(line):
for key, val in packages.items():
match = val.search(line)
if match:
return key, match
# no matches
return None, None
def parse_file(filepath):
with open(filepath, 'r') as file:
lines = file.readlines()
for line in lines:
key, match = _parse_line(line)
if key == 'package_name':
package_name = match.group('package_name')
print(package_name)
if key == 'version':
version = match.group('version')
print(version)
if key == 'url':
url = match.group('url')
print(url)
def generate_package_info(filepath):
with open(filepath, 'r') as file:
lines = file.readlines()
for line in lines:
end = line.find('==')
match = line[0:end]
cmd = ['pip', 'show', match]
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
o, e = proc.communicate()
print(o.decode('ascii'))
if __name__ == '__main__':
parse_file('./pip_info.txt')
"""
Attribution: Regex and Line Matching inspired by https://www.vipinajayakumar.com/parsing-text-with-python/
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment