-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.py
More file actions
64 lines (50 loc) · 2 KB
/
setup.py
File metadata and controls
64 lines (50 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
import setuptools
import sys
# We can't rely on the PyYAML package being installed, so we include a hard-coded version
try:
if sys.version_info >= (3, 0):
import python_packages.yaml_python3 as yaml
else:
import python_packages.yaml_python2 as yaml
except ImportError:
print('ERROR: Pre-commit hooks template: Failed to import the "yaml" python package.')
raise
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
# We assume that the name of the directory that contains this file is the name of our project
PROJECT_NAME = os.path.basename(SCRIPT_DIR)
PROJECT_VERSION = '1.0.0'
# The package name is the name of the directory where we will store our scripts
PACKAGE_NAME = 'pre_commit_hooks'
HOOKS_DEFINITIONS_FILENAME = '.pre-commit-hooks.yaml'
def read_hooks_config():
config_file_path = os.path.join(SCRIPT_DIR, HOOKS_DEFINITIONS_FILENAME)
raw_config = open(config_file_path, 'rb').read()
hooks_config = yaml.safe_load(raw_config)
return hooks_config
def get_python_hook_script_names(hooks_config):
return [hook_data['entry'] for hook_data in hooks_config if hook_data['language'].strip().lower() == 'python']
def main():
hooks_config = read_hooks_config()
script_names = get_python_hook_script_names(hooks_config)
console_scripts = []
for script_name in script_names:
script_name_without_suffix = script_name[:-3] if script_name.endswith('.py') else script_name
console_scripts.append(
'{script_name} = {package_name}.{script_name_without_suffix}:main'.format(
script_name_without_suffix=script_name_without_suffix,
script_name=script_name,
package_name=PACKAGE_NAME
)
)
setuptools.setup(
name=PROJECT_NAME,
description=PROJECT_NAME,
version=PROJECT_VERSION,
packages=[PACKAGE_NAME],
entry_points={
'console_scripts': console_scripts,
},
)
if __name__ == '__main__':
main()