Skip to content

Instantly share code, notes, and snippets.

@rtluckie
Created January 3, 2016 20:27
Show Gist options
  • Save rtluckie/d48219916c79730cf418 to your computer and use it in GitHub Desktop.
Save rtluckie/d48219916c79730cf418 to your computer and use it in GitHub Desktop.
Initial attempt to check installed deps vs deps listed in requirements.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: sw=4:ts=4:expandtab
"""
Initial attempt to check installed deps vs deps listed in requirements.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import with_statement
import json
import os
import re
from plumbum import local
from twitter.common.dirutil.fileset import Fileset
def get_installed_dependencies(include_transitive=False):
ptree = local["pipdeptree"]
data = json.loads(ptree(['-j']))
if include_transitive:
return data
return [[key, value] for i in data for key, value in i.iteritems() if key == 'package']
def get_dependencies_from_requirements(globspecs=('*.pip', '*.txt'), root=None):
if not root:
root = os.path.join(os.getcwd(), 'requirements')
req_files = Fileset.rglobs(*globspecs, root=root)
reqs = []
ptrn_str = r"""(?P<package_name>^[^#|\s|\d|>|<|=]+)(?P<operator>[==|<|>|>=|<=]+)?(?P<required_version>[0-9.]+)?"""
ptrn = re.compile(ptrn_str)
for f in req_files:
data = {'fpath': f, 'dependencies': []}
fpath = os.path.join(root, f)
with open(fpath, 'r') as fin:
for l in fin.readlines():
m = ptrn.match(l)
if ptrn.match(l):
data['dependencies'].append(m.groupdict())
if data['dependencies']:
reqs.append(data)
return reqs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment