Skip to content

Commit

Permalink
Support write_mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerhot committed Dec 22, 2021
1 parent cd4f642 commit d218580
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions gitfx/run_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
"""The main program to run functions."""

import os
import sys
import subprocess

from gitfx import parse_funcs
from gitfx import lang_version


ROOT_DIR = os.getenv('GITHUB_WORKSPACE', os.getcwd())
WRITE_MODE = os.getenv('GITFX_WRITE_MODE', 'overwrite')

SUPPORTED_LANGS = ['ruby',
'python',
Expand Down Expand Up @@ -87,16 +89,19 @@ def run_fun(func_path, func):
# decide language version
version = lang_version.get_version(func_path, func_lang) or 'latest'

if func_lang == 'bash':
cmd = ["bash", os.path.join(func_path, func_file_name)]
else:
cmd = ['docker', 'run', '--rm', '--workdir', '/github/workspace',
'-v', ROOT_DIR + ':/github/workspace',
docker_image(func_lang) + ':' + version, 'sh', '-c',
"cd " + os.path.relpath(func_path, ROOT_DIR) + " && " + # noqa
deps_install_cmd(func_lang, func_path) + " && " + # noqa
run_before_script + " && " + # noqa
RUN_CMDS[func_lang] + " " + func_file_name]
try:
if func_lang == 'bash':
cmd = ["bash", os.path.join(func_path, func_file_name)]
else:
cmd = ['docker', 'run', '--rm', '--workdir', '/github/workspace',
'-v', ROOT_DIR + ':/github/workspace',
docker_image(func_lang) + ':' + version, 'sh', '-c',
"cd " + os.path.relpath(func_path, ROOT_DIR) + " && " + # noqa
deps_install_cmd(func_lang, func_path) + " && " + # noqa
run_before_script + " && " + # noqa
RUN_CMDS[func_lang] + " " + func_file_name]
except:
sys.exit(1)

output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
return output.decode("utf8")
Expand All @@ -111,7 +116,10 @@ def write_to_route(result, func_route):
os.makedirs(dst_dir)

file_path = os.path.join(ROOT_DIR, func_route)
f = open(file_path, 'w')
if os.path.isfile(file_path) and WRITE_MODE == 'append':
f = open(file_path, 'a')
else:
f = open(file_path, 'w')
f.write(result)
f.close()

Expand Down

0 comments on commit d218580

Please sign in to comment.