Skip to content

Commit

Permalink
Add flake8 config and fix styles
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerhot committed May 11, 2021
1 parent 5d0b768 commit 08b93dd
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 73 deletions.
10 changes: 10 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[flake8]
ignore = D203,E501
exclude =
.git,
__pycache__,
docs/source/conf.py,
old,
build,
dist
max-complexity = 10
13 changes: 6 additions & 7 deletions gitfx/lang_version.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os


LANG_VERSION_FILES = {
'python': '.python-version', # pyenv
'ruby': '.ruby-version', # rvm/rbenv
'perl': '.perl-version', # plenv
'node': '.nvmrc', # nvm
'php': '.phpenv-version', # phpenv
}
LANG_VERSION_FILES = {'python': '.python-version', # pyenv
'ruby': '.ruby-version', # rvm/rbenv
'perl': '.perl-version', # plenv
'node': '.nvmrc', # nvm
'php': '.phpenv-version'} # phpenv


def read_first_line(ver_file):
Expand All @@ -20,6 +18,7 @@ def read_first_line(ver_file):
line = line.strip()
return line


def get_version(lang):
if lang not in LANG_VERSION_FILES:
return ''
Expand Down
68 changes: 33 additions & 35 deletions gitfx/parse_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,27 @@
from glob import glob


EXT_LANG = {
'.py': 'python',
'.rb': 'ruby',
'.pl': 'perl',
'.go': 'golang',
'.js': 'node',
'.hs': 'haskell',
'.exs': 'elixir',
'.php': 'php',
'.sh': 'bash',
'.rs': 'rust',
}

COMMENT_PREFIX = {
'python': '#',
'ruby': '#',
'perl': '#',
'golang': '//',
'node': '//',
'haskell': '--',
'elixir': '#',
'php': '//',
'bash': '#',
'rust': '//',
}
EXT_LANG = {'.py': 'python',
'.rb': 'ruby',
'.pl': 'perl',
'.go': 'golang',
'.js': 'node',
'.hs': 'haskell',
'.exs': 'elixir',
'.php': 'php',
'.sh': 'bash',
'.rs': 'rust'}

COMMENT_PREFIX = {'python': '#',
'ruby': '#',
'perl': '#',
'golang': '//',
'node': '//',
'haskell': '--',
'elixir': '#',
'php': '//',
'bash': '#',
'rust': '//'}


def get_language(extension):
Expand Down Expand Up @@ -58,16 +54,18 @@ def parse(path):
os.chdir(path)

result = []
files = glob('*.rb') + \
glob('*.py') + \
glob('*.go') + \
glob('*.js') + \
glob('*.pl') + \
glob('*.hs') + \
glob('*.exs') + \
glob('*.php') + \
glob('*.sh') + \
glob('*.rs')
types = ('*.rb', # Ruby
'*.py', # Python
'*.go', # Golang
'*.js', # Node.js
'*.pl', # Perl
'*.hs', # haskell
'*.exs', # Elixir
'*.php', # PHP
'*sh', # Bash
'*.rs') # Rust

files = [f for fs in [glob(t) for t in types] for f in fs]

for f in files:
ext_name = os.path.splitext(f)[1]
Expand Down
58 changes: 28 additions & 30 deletions gitfx/run_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,36 @@

ROOT_DIR = os.getenv('GITHUB_WORKSPACE', os.getcwd())

SUPPORTED_LANGS = [
'ruby',
'python',
'perl',
'node',
'golang',
'elixir',
'haskell',
'php',
'bash',
'rust',
]

RUN_CMDS = {
'ruby': 'ruby',
'python': 'python',
'node': 'node',
'perl': 'perl',
'golang': 'go run',
'elixir': 'elixir',
'haskell': 'runhaskell',
'php': 'php',
'rust': 'perl -e \'($n = $ARGV[0]) =~ s/\.rs$//; system "rustc $ARGV[0] && ./$n && rm $n"\'',
}

DOCKER_IMAGES = {
}
SUPPORTED_LANGS = ['ruby',
'python',
'perl',
'node',
'golang',
'elixir',
'haskell',
'php',
'bash',
'rust']

RUN_CMDS = {'ruby': 'ruby',
'python': 'python',
'node': 'node',
'perl': 'perl',
'golang': 'go run',
'elixir': 'elixir',
'haskell': 'runhaskell',
'rust': 'perl -e \'($n = $ARGV[0]) =~ s/\.rs$//; system "rustc $ARGV[0] && ./$n && rm $n"\'', # noqa
'php': 'php'}

DOCKER_IMAGES = {}


def docker_image(lang):
"""you can specify a docker image name for a language,
otherwise the language name will be returned as the image name"""
return DOCKER_IMAGES.get(lang, lang)


def run_fun(func_path, func):
func_lang = func['language']
if func_lang not in SUPPORTED_LANGS:
Expand Down Expand Up @@ -74,14 +70,15 @@ def run_fun(func_path, func):
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) + ";" +
deps_install.get(func_lang, ':') + ";" +
run_before_script + ";" +
"cd " + os.path.relpath(func_path, ROOT_DIR) + ";" + # noqa
deps_install.get(func_lang, ':') + ";" + # noqa
run_before_script + ";" + # noqa
RUN_CMDS[func_lang] + " " + func_file_name]

output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
return output.decode("utf8")


def write_to_route(result, func_route):
if func_route.strip() == '':
return
Expand All @@ -97,6 +94,7 @@ def write_to_route(result, func_route):

print_github_raw_url(func_route)


def print_github_raw_url(file_path):
repo_name = os.getenv('GITHUB_REPOSITORY')
branch = os.getenv('GITHUB_REF')
Expand Down
2 changes: 1 addition & 1 deletion test/func_examples/function.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
# import but not used, just for testing package installation
import openpyxl
import openpyxl # noqa

# GET /test/result/python.json

Expand Down

0 comments on commit 08b93dd

Please sign in to comment.