def test_escape_greater_than(self): """shellutils - escaping strings containing the greater-than sign""" self.assertEqual(r"'10 > 5'", escape_shell_arg(r'10 > 5'))
def test_escape_less_than(self): """shellutils - escaping strings containing the less-than sign""" self.assertEqual(r"'5 < 10'", escape_shell_arg(r'5 < 10'))
def test_escape_number_sign(self): """shellutils - escaping strings containing the number sign""" self.assertEqual(r"'Python comments start with #.'", escape_shell_arg(r'Python comments start with #.'))
def test_escape_ampersand(self): """shellutils - escaping strings containing ampersand""" self.assertEqual(r"'Today the weather is hot & sunny'", escape_shell_arg(r'Today the weather is hot & sunny'))
def test_escape_windows_style_path(self): """shellutils - escaping strings containing windows-style file paths""" self.assertEqual(r"'C:\Users\Test User\My Documents" \ "\funny file name (for testing).pdf'", escape_shell_arg(r'C:\Users\Test User\My Documents' \ '\funny file name (for testing).pdf'))
def test_escape_unix_style_path(self): """shellutils - escaping strings containing unix-style file paths""" self.assertEqual(r"'/tmp/z_temp.txt'", escape_shell_arg(r'/tmp/z_temp.txt'))
def test_escape_double_quoted(self): """shellutils - escaping strings containing double-quotes""" self.assertEqual("""'"hello world"'""", escape_shell_arg('"hello world"'))
def test_escape_complex_quoted(self): """shellutils - escaping strings containing complex quoting""" self.assertEqual(r"""'"Who is this `Eve'\'', Bob?", asked Alice.'""", escape_shell_arg(r""""Who is this `Eve', Bob?", asked Alice."""))
def test_escape_quoted(self): """shellutils - escaping strings containing single quotes""" self.assertEqual("'hello'\\''world'", escape_shell_arg("hello'world"))
def test_escape_backtick(self): """shellutils - escaping strings containing backticks""" self.assertEqual(r"'hello `world`'", escape_shell_arg(r'hello `world`'))
def test_escape_simple(self): """shellutils - escaping simple strings""" self.assertEqual("'hello'", escape_shell_arg("hello"))
def dump_database(dump_path, host=CFG_DATABASE_HOST, port=CFG_DATABASE_PORT, \ user=CFG_DATABASE_USER, passw=CFG_DATABASE_PASS, \ name=CFG_DATABASE_NAME, params=None, compress=False, \ ignore_tables=None): """ Dump Invenio database into SQL file located at DUMP_PATH. Will perform the command to mysqldump with the given host configuration and user credentials. Optional mysqldump parameters can also be passed. Otherwise, a default set of parameters will be used. @param dump_path: path on the filesystem to save the dump to. @type dump_path: string @param host: hostname of mysql database node to connect to. @type host: string @param port: port of mysql database node to connect to @type port: string @param user: username to connect with @type user: string @param passw: password to connect to with @type passw: string @param name: name of mysql database node to dump @type name: string @param params: command line parameters to pass to mysqldump. Optional. @type params: string @param compress: should the dump be compressed through gzip? @type compress: bool @param ignore_tables: list of tables to ignore in the dump @type ignore: list of string """ write_message("... writing %s" % (dump_path,)) partial_dump_path = dump_path + ".part" # Is mysqldump installed or in the right path? cmd_prefix = CFG_PATH_MYSQL + 'dump' if not os.path.exists(cmd_prefix): raise StandardError("%s is not installed." % (cmd_prefix)) if not params: # No parameters set, lets use the default ones. params = " --skip-opt --add-drop-table --add-locks --create-options" \ " --quick --extended-insert --set-charset --disable-keys" \ " --lock-tables=false --max_allowed_packet=2G " if ignore_tables: params += " ".join([escape_shell_arg("--ignore-table=%s.%s" % (CFG_DATABASE_NAME, table)) for table in ignore_tables]) dump_cmd = "%s %s " \ " --host=%s --port=%s --user=%s --password=%s %s" % \ (cmd_prefix, \ params, \ escape_shell_arg(host), \ escape_shell_arg(str(port)), \ escape_shell_arg(user), \ escape_shell_arg(passw), \ escape_shell_arg(name)) if compress: dump_cmd = "%s | %s -cf; exit ${PIPESTATUS[0]}" % \ (dump_cmd, \ CFG_PATH_GZIP) dump_cmd = "bash -c %s" % (escape_shell_arg(dump_cmd),) write_message(dump_cmd, verbose=2) exit_code, stdout, stderr = run_shell_command(dump_cmd, None, partial_dump_path) if exit_code: raise StandardError("ERROR: mysqldump exit code is %s. stderr: %s stdout: %s" % \ (repr(exit_code), \ repr(stderr), \ repr(stdout))) else: os.rename(partial_dump_path, dump_path) write_message("... completed writing %s" % (dump_path,))
def dump_database(dump_path, host=CFG_DATABASE_HOST, port=CFG_DATABASE_PORT, \ user=CFG_DATABASE_USER, passw=CFG_DATABASE_PASS, \ name=CFG_DATABASE_NAME, params=None, compress=False, \ ignore_tables=None): """ Dump Invenio database into SQL file located at DUMP_PATH. Will perform the command to mysqldump with the given host configuration and user credentials. Optional mysqldump parameters can also be passed. Otherwise, a default set of parameters will be used. @param dump_path: path on the filesystem to save the dump to. @type dump_path: string @param host: hostname of mysql database node to connect to. @type host: string @param port: port of mysql database node to connect to @type port: string @param user: username to connect with @type user: string @param passw: password to connect to with @type passw: string @param name: name of mysql database node to dump @type name: string @param params: command line parameters to pass to mysqldump. Optional. @type params: string @param compress: should the dump be compressed through gzip? @type compress: bool @param ignore_tables: list of tables to ignore in the dump @type ignore: list of string """ write_message("... writing %s" % (dump_path, )) partial_dump_path = dump_path + ".part" # Is mysqldump installed or in the right path? cmd_prefix = CFG_PATH_MYSQL + 'dump' if not os.path.exists(cmd_prefix): raise StandardError("%s is not installed." % (cmd_prefix)) if not params: # No parameters set, lets use the default ones. params = " --skip-opt --add-drop-table --add-locks --create-options" \ " --quick --extended-insert --set-charset --disable-keys" \ " --lock-tables=false --max_allowed_packet=2G " if ignore_tables: params += " ".join([ escape_shell_arg("--ignore-table=%s.%s" % (CFG_DATABASE_NAME, table)) for table in ignore_tables ]) dump_cmd = "%s %s " \ " --host=%s --port=%s --user=%s --password=%s %s" % \ (cmd_prefix, \ params, \ escape_shell_arg(host), \ escape_shell_arg(str(port)), \ escape_shell_arg(user), \ escape_shell_arg(passw), \ escape_shell_arg(name)) if compress: dump_cmd = "%s | %s -cf; exit ${PIPESTATUS[0]}" % \ (dump_cmd, \ CFG_PATH_GZIP) dump_cmd = "bash -c %s" % (escape_shell_arg(dump_cmd), ) write_message(dump_cmd, verbose=2) exit_code, stdout, stderr = run_shell_command(dump_cmd, None, partial_dump_path) if exit_code: raise StandardError("ERROR: mysqldump exit code is %s. stderr: %s stdout: %s" % \ (repr(exit_code), \ repr(stderr), \ repr(stdout))) else: os.rename(partial_dump_path, dump_path) write_message("... completed writing %s" % (dump_path, ))