Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make every setup.py only require the base check #1796

Merged
merged 1 commit into from
Jun 26, 2018
Merged

make every setup.py only require the base check #1796

merged 1 commit into from
Jun 26, 2018

Conversation

ofek
Copy link
Contributor

@ofek ofek commented Jun 25, 2018

Motivation

We need to install dependencies separately and need an easy way to parse the base check dep string for upgrading during releases.

Additional Notes

Script (Python 3 only)

import os
import re

CORE_DIR = r'C:\Users\Ofek\Desktop\code\integrations-core'
IGNORED_CHECKS = {'datadog_checks_tests_helper'}
JMX_CHECKS = {'activemq', 'cassandra', 'jmx', 'kafka', 'solr', 'tomcat'}
IGNORED_CHECKS.update(JMX_CHECKS)
CHECKS_BASE_VAR = 'CHECKS_BASE_REQ'
REQUIRES_PATTERN = re.compile(r'install_requires *=.+?(\)|\]) *,', re.S)
SETUP_CALL_PATTERN = re.compile(r'^setup\(', re.M)
TOX_BASE_DEP_PATTERN = re.compile(r'^ *\.\.(/|\\)datadog_checks_base', re.M)


def get_setup_file(check_name):
    f = os.path.join(CORE_DIR, check_name, 'setup.py')
    if os.path.isfile(f):
        return f


def get_tox_file(check_name):
    f = os.path.join(CORE_DIR, check_name, 'tox.ini')
    if os.path.isfile(f):
        return f


def add_dep(match):
    match = match.group(0)
    indent = ' ' * (len(match) - len(match.lstrip()))
    return '{}\n{}{}'.format(match, indent, '-r../datadog_checks_base/requirements.in')


def main():
    for check_name in os.listdir(CORE_DIR):
        setup_file = get_setup_file(check_name)
        if setup_file and check_name not in IGNORED_CHECKS:
            with open(setup_file, 'r', encoding='utf-8') as f:
                setup_content = f.read()

            if CHECKS_BASE_VAR not in setup_content:
                if check_name == 'datadog_checks_base':
                    setup_content = REQUIRES_PATTERN.sub('install_requires=[],', setup_content)
                else:
                    setup_content = SETUP_CALL_PATTERN.sub(
                        "{} = 'datadog_checks_base'\n\nsetup(".format(CHECKS_BASE_VAR),
                        setup_content
                    )
                    setup_content = REQUIRES_PATTERN.sub(
                        'install_requires=[{}],'.format(CHECKS_BASE_VAR),
                        setup_content
                    )

                with open(setup_file, 'w', encoding='utf-8') as f:
                    f.write(setup_content)

                tox_file = get_tox_file(check_name)
                if tox_file:
                    with open(tox_file, 'r', encoding='utf-8') as f:
                        tox_content = f.read()

                    tox_content = TOX_BASE_DEP_PATTERN.sub(
                        add_dep,
                        tox_content
                    )

                    with open(tox_file, 'w', encoding='utf-8') as f:
                        f.write(tox_content)

if __name__ == '__main__':
    main()

@ofek ofek added this to the 6.4 milestone Jun 25, 2018
@ofek ofek requested a review from a team as a code owner June 25, 2018 01:15
@ofek ofek force-pushed the ofek/reqs branch 4 times, most recently from aa56908 to a72cdb7 Compare June 25, 2018 02:39
@masci masci removed this from the 6.4 milestone Jun 25, 2018
Copy link
Contributor

@masci masci left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

@masci masci merged commit f276c4e into master Jun 26, 2018
@masci masci deleted the ofek/reqs branch June 26, 2018 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants