Return a mapping of arguments passed to a setup.py setup() function. Also include not parsable identifiers values such as variable name and attribute references if ``include_not_parsable`` is True
(location, include_not_parsable=False)
| 2413 | |
| 2414 | |
| 2415 | def get_setup_py_args_legacy(location, include_not_parsable=False): |
| 2416 | """ |
| 2417 | Return a mapping of arguments passed to a setup.py setup() function. Also |
| 2418 | include not parsable identifiers values such as variable name and attribute |
| 2419 | references if ``include_not_parsable`` is True |
| 2420 | """ |
| 2421 | with open(location) as inp: |
| 2422 | setup_text = inp.read() |
| 2423 | |
| 2424 | setup_args = {} |
| 2425 | |
| 2426 | # Parse setup.py file and traverse the AST |
| 2427 | tree = ast.parse(setup_text) |
| 2428 | for statement in tree.body: |
| 2429 | # We only care about function calls or assignments to functions named |
| 2430 | # `setup` or `main` |
| 2431 | |
| 2432 | # TODO: also collect top level variables assigned later as arguments values |
| 2433 | if not is_setup_call(statement): |
| 2434 | continue |
| 2435 | |
| 2436 | # Process the arguments to the setup function |
| 2437 | for kw in getattr(statement.value, 'keywords', []): |
| 2438 | arg_name = kw.arg |
| 2439 | arg_value = kw.value |
| 2440 | |
| 2441 | # FIXME: use a recursive function to extract structured data |
| 2442 | |
| 2443 | if isinstance(arg_value, (ast.List, ast.Tuple, ast.Set,)): |
| 2444 | # We collect the elements of a list if the element |
| 2445 | # and tag function calls |
| 2446 | val = [ |
| 2447 | elt.s for elt in arg_value.elts |
| 2448 | if not isinstance(elt, ast.Call) |
| 2449 | ] |
| 2450 | setup_args[arg_name] = val |
| 2451 | |
| 2452 | elif isinstance(arg_value, ast.Dict): |
| 2453 | # we only collect simple name/value and name/[values] constructs |
| 2454 | keys = [elt.value for elt in arg_value.keys] |
| 2455 | values = [] |
| 2456 | for val in arg_value.values: |
| 2457 | |
| 2458 | if isinstance(val, (ast.List, ast.Tuple, ast.Set,)): |
| 2459 | val = [ |
| 2460 | elt.s for elt in val.elts |
| 2461 | if not isinstance(elt, ast.Call) |
| 2462 | ] |
| 2463 | values.append(val) |
| 2464 | |
| 2465 | elif isinstance(val, ast.Constant): |
| 2466 | values.append(val.value) |
| 2467 | |
| 2468 | else: |
| 2469 | if include_not_parsable: |
| 2470 | if isinstance(val, ast.Attribute): |
| 2471 | values.append(val.attr) |
| 2472 |
nothing calls this directly
no test coverage detected