def write_source_vc_info(run_source_dir, output=None, popen=None): """Write version control information of sources used in run time. run_source_dir -- The source directory we are interested in. output -- An open file handle or a string containing a writable path. If not specified, use sys.stdout. popen -- A metomi.rose.popen.RosePopener instance for running vc commands. If not specified, use a new local instance. """ if popen is None: popen = RosePopener() if output is None: handle = sys.stdout elif hasattr(output, "write"): handle = output else: handle = open(output, "wb") msg = "%s\n" % run_source_dir write_safely(msg, handle) environ = dict(os.environ) environ["LANG"] = "C" for vcs, args_list in [ ( "svn", [ ["info", "--non-interactive"], ["status", "--non-interactive"], ["diff", "--internal-diff", "--non-interactive"], ], ), ("git", [["describe"], ["status"], ["diff"]]), ]: if not popen.which(vcs): continue cwd = os.getcwd() os.chdir(run_source_dir) try: for args in args_list: cmd = [vcs] + args ret_code, out, _ = popen.run(*cmd, env=environ) if out: write_safely(("#" * 80 + "\n"), handle) write_safely(("# %s\n" % popen.list_to_shell_str(cmd)), handle) write_safely(("#" * 80 + "\n"), handle) write_safely(out, handle) if ret_code: # If cmd fails once, it will likely fail again break finally: os.chdir(cwd)
def test_try_StringIO_and_bytes(): with io.StringIO() as handle: write_safely(TESTBYTES, handle) assert isinstance(handle, io.StringIO) assert handle.getvalue() == TESTBYTES.decode()
def test_try_StringIO_and_str(): with io.StringIO() as handle: write_safely(TESTSTR, handle) assert isinstance(handle, io.StringIO) assert handle.getvalue() == TESTSTR
def test_try_BufferedWriter_and_bytes(tmpdir): with open(tmpdir.join("test.txt"), "wb") as handle: assert isinstance(handle, io.BufferedWriter) write_safely(TESTBYTES, handle) with open(tmpdir.join("test.txt"), "rb") as handle: assert handle.read() == TESTBYTES