def add_install(dependency): """Add a dependency to install_requires. This will add an entry to the install_requires list. It will not abide by any formatting standards, it simply adds a new item at the end. Usage: psh add-install setup.py mynewdependency If a specific version is required, use the same syntax as with pip, e.g. psh add-install setup.py foo>=0.4.5 If the dependency exists in any form, no update is made. :param filename: The filename to update, if -n not specified. :param dependency: The dependency to add. """ setupfile, encoding = _common.load_file(config.filename) try: tree = _common.parse_string(setupfile, python_grammar) except ParseError as pe: print(pe.context) else: try: _mutators.add_arg_to_install(tree, dependency) except _mutators.AlreadyExistsError as e: print("{}, not modifying".format(str(e))) else: write_output(tree, config.filename, encoding)
def test_find_install_requires(): """Install requires is correctly located""" tree = _common.parse_string(TRAILING_COMMA, python_grammar) setup_args = _searching.find_setup_args(tree) ir = _searching.find_install_requires(setup_args) assert ir.children[0].type == token.NAME assert ir.children[0].value == _searching.ARG_INSTALL_REQUIRES
def test_trailing_comma(): """A trailing comma is properly detected""" tree = _common.parse_string(TRAILING_COMMA, python_grammar) target = None for a in _searching.iterate_kinds_all(tree, (python_symbols.atom, )): if _searching.is_argument_atom(a, _searching.ARG_INSTALL_REQUIRES): target = a target = _searching.find_first_of_type( target, (token.STRING, python_symbols.listmaker)) assert _searching.trailing_comma(target)
def version(new_version): """Get or modify the version""" setupfile, encoding = _common.load_file(config.filename) try: tree = _common.parse_string(setupfile, python_grammar) except ParseError as pe: print(pe.context) else: try: version_node = _searching.get_version(tree) except _common.NodeNotFoundError: if new_version: version_node = _mutators.add_version(tree) else: current_version = "There doesn't seem to be a version specification" else: current_version = _common.UNQUOTED_STRING.match( version_node.value).groups()[0] if new_version: version_node.value = _common.quote_string(new_version) write_output(tree, config.filename, encoding) else: print(current_version)
def test_add_install_exists(): """Arguments can be added to an existing list of > 1""" tree = _common.parse_string(IR_PRE, python_grammar) with pytest.raises(_mutators.AlreadyExistsError): _mutators.add_arg_to_install(tree, "buzz==0.3.4") assert IR_PRE == str(tree)
def test_add_install_to_many(): """Arguments can be added to an existing list of > 1""" tree = _common.parse_string(IR_PRE, python_grammar) _mutators.add_arg_to_install(tree, "foo") assert IR_POST == str(tree)
def test_add_install_one_exists(): """Arguments are successfully added to an install_requires list of one""" tree = _common.parse_string(ONE_IR_PRE, python_grammar) _mutators.add_arg_to_install(tree, "barg==0.7.8") assert ONE_IR_POST == str(tree), print(str(tree))
def test_add_to_empty(): """Adding to and empty install requires inserts an entry""" tree = _common.parse_string(EMPTY_IR_PRE, python_grammar) _mutators.add_arg_to_install(tree, "rugz") assert str(tree) == EMPTY_IR_POST
def test_add_install_nothing(): """If no install requires argument exists, one is added.""" tree = _common.parse_string(NO_IR_PRE, python_grammar) _mutators.add_arg_to_install(tree, "baz") assert str(tree) == NO_IR_POST