diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-02 12:47:29 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-10-02 12:47:29 +0200 |
commit | b4795a28dcc8f82823c2cae3f4413835a5e35044 (patch) | |
tree | 4f8f2e1f63a951b05352079442643f30b80fe892 | |
parent | ecd0c61d668d57c237265ee44c20a2e10500191c (diff) | |
download | psutil-sync-test-primitives.tar.gz |
refactor create_temp_executable_filesync-test-primitives
-rw-r--r-- | psutil/tests/__init__.py | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/psutil/tests/__init__.py b/psutil/tests/__init__.py index 0d646650..8da8bd37 100644 --- a/psutil/tests/__init__.py +++ b/psutil/tests/__init__.py @@ -480,13 +480,16 @@ def chdir(dirname): def create_temp_executable_file(suffix, c_code=None): - tmpdir = None - if TRAVIS and OSX: - tmpdir = "/private/tmp" - fd, path = tempfile.mkstemp( - prefix=TESTFILE_PREFIX, suffix=suffix, dir=tmpdir) - os.close(fd) + def create_temp_file(suffix=None): + tmpdir = None + if TRAVIS and OSX: + tmpdir = "/private/tmp" + fd, path = tempfile.mkstemp( + prefix=TESTFILE_PREFIX, suffix=suffix, dir=tmpdir) + os.close(fd) + return path + exe_file = create_temp_file(suffix=suffix) if which("gcc"): if c_code is None: c_code = textwrap.dedent( @@ -496,20 +499,18 @@ def create_temp_executable_file(suffix, c_code=None): pause(); } """) - fd, c_file = tempfile.mkstemp( - prefix=TESTFILE_PREFIX, suffix='.c', dir=tmpdir) - os.close(fd) + c_file = create_temp_file(suffix=".c") with open(c_file, "w") as f: f.write(c_code) - subprocess.check_call(["gcc", c_file, "-o", path]) + subprocess.check_call(["gcc", c_file, "-o", exe_file]) safe_rmpath(c_file) else: # fallback - use python's executable - shutil.copyfile(sys.executable, path) + shutil.copyfile(sys.executable, exe_file) if POSIX: - st = os.stat(path) - os.chmod(path, st.st_mode | stat.S_IEXEC) - return path + st = os.stat(exe_file) + os.chmod(exe_file, st.st_mode | stat.S_IEXEC) + return exe_file # =================================================================== |