Skip to content

Instantly share code, notes, and snippets.

@chaobin
Created March 4, 2015 17:34
Show Gist options
  • Save chaobin/727faf9cbd0623d4b1bc to your computer and use it in GitHub Desktop.
Save chaobin/727faf9cbd0623d4b1bc to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:a4cd974f3f2077d598660fc78e967ba6252276a8cb4e74a77c8a6c4c6d2dfa5c"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Using ast to inspect the code without executing it\n",
"import ast"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def get_install_requires_from_setup_py(filename):\n",
" \"\"\"\n",
" Parse the setup.py and get the install_requires\n",
" from it.\n",
" \n",
" return\n",
" list of dependencies\n",
" \"\"\"\n",
" with open(filename) as _file:\n",
" module = ast.parse(_file.read())\n",
" \n",
" # iterate all the top level exprs and find 'setup()'\n",
" exprs = [n.value for n in module.body if isinstance(n, ast.Expr)]\n",
" calls = [e for e in exprs if isinstance(e, ast.Call)]\n",
" call_setup = None\n",
" for c in calls:\n",
" if getattr(c.func, 'id', None) == 'setup':\n",
" call_setup = c\n",
" break\n",
" assert call_setup, \"No setup() found\"\n",
" # iterate its keyword arguments and find 'install_requires'\n",
" install_requires = [k.value for k in call_setup.keywords if k.arg == 'install_requires']\n",
" assert len(install_requires) > 0, \"No keyword argument 'install_requires' in setup()\"\n",
" install_requires = install_requires[0]\n",
" return [_str.s for _str in install_requires.elts]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"get_install_requires_from_setup_py('setup.py')"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 40,
"text": [
"['Babel>=1.3,<1.4a0',\n",
" 'BabelDjango>=0.2,<0.3a0',\n",
" 'Django>=1.6',\n",
" 'django-images>=0.4,<0.5a0',\n",
" 'django-model-utils>=2.0.0,<2.1a0',\n",
" 'django-mptt>=0.5',\n",
" 'django-payments>=0.5.2,<0.6a0',\n",
" 'django-prices>=0.3.3,<0.4a0',\n",
" 'django-selectable==0.8.0',\n",
" 'fake-factory>=0.3.2',\n",
" 'google-measurement-protocol>=0.1.2,<0.2a0',\n",
" 'Markdown>=2.4',\n",
" 'prices>=0.5,<0.6a0',\n",
" 'requests>=1.2.0',\n",
" 'satchless>=1.1.2,<1.2a0',\n",
" 'unidecode']"
]
}
],
"prompt_number": 40
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment