Exemplo n.º 1
0
def validate_with_codestyle(report_dir):
    """Run the codestyle command directly to do the validation"""

    src_dir = os.environ.get('PACKAGE_DIR', '')
    if not src_dir:
        src_dir = os.environ.get('PACKAGE_DIRECTORY', '')

    parent_interpreter = interpreter_parent(sys.executable)
    interpreter = os.environ.get('BASE_PYTHON', parent_interpreter)
    bin_dir = os.path.dirname(interpreter)

    pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')
    if not os.path.exists(pycodestyle_command):  # pragma: no cover
        bin_dir = os.path.dirname(sys.executable)
        pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')
        if not os.path.exists(pycodestyle_command):
            bin_dir = os.path.dirname(parent_interpreter)
            pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')

    package_name = PackageMetadata().metadata['name']

    # Generate the command line from the environment settings
    command = [pycodestyle_command]

    # Add extra arguments
    extra_args = os.environ.get('CODESTYLE_ARGS', '')
    if extra_args:
        command += extra_args.split()

    # Add targets
    if src_dir not in ['', '.'] and src_dir != package_name:
        command.append(os.path.join(src_dir, package_name.replace('.', '/')))
    else:
        command.append(package_name.replace('.', '/'))

    print('-' * 90 + '\nRunning:',
          ' '.join(command) + '\n' + '-' * 90,
          flush=True)
    rc = 0
    try:
        output = subprocess.check_output(command,
                                         stderr=subprocess.STDOUT)  # nosec
    except subprocess.CalledProcessError as error:  # pragma: no cover
        rc = error.returncode
        output = error.output

    os.makedirs(report_dir, exist_ok=True)

    text_report = os.path.join(report_dir, 'codestyle.txt')

    with open(text_report, 'wb') as fh:
        for line in output.split(b'\n'):
            textline = line.decode(errors='ignore').strip()
            fh.write(line)
            if 'error:' in textline:  # pragma: no cover
                print(colored(textline, 'red'), flush=True)
            else:
                print(textline, flush=True)

    return rc
def validate_with_safety():
    """Run the safety command in a virtualenv to validate the package dependencies"""

    artifacts_dir = os.environ.get('SD_ARTIFACTS_DIR', '')
    report_dir = os.path.join(artifacts_dir, 'reports/dependency_validation')
    create_artifact_directory(report_dir)
    
    interpreter = interpreter_parent(sys.executable)
    interpreter = os.environ.get('BASE_PYTHON', interpreter)

    # Generate a full text report
    full_report_filename = os.path.join(report_dir, 'safetydb.full')
    rc = install_and_run(package='safety,.', command=f'safety check --full-report -o "{full_report_filename}"', interpreter=interpreter, upgrade_setuptools=True, upgrade_pip=True)

    if rc == 0:
        update_job_status(status='SUCCESS', message='Dependency check passed')
        print(colored('Safetydb check passed', color='green'))
        return rc

    # Generate the report in json format
    json_report_filename = os.path.join(report_dir, 'safetydb.json')
    rc = install_and_run(package='safety,.', command=f'safety check --json -o "{json_report_filename}"', interpreter=interpreter, upgrade_setuptools=True, upgrade_pip=True)

    bad_packages = -1
    if os.path.exists(json_report_filename):
        with open(json_report_filename) as fh:
            results = json.load(fh)
            bad_packages = len(results)

    update_job_status(status='FAILURE', message=f'Dependency check failed {bad_packages} bad dependencies found')

    return rc
def interpreter_bin_command(command: str = 'python',
                            fallback_path: bool = True) -> str:
    """
    Return the full path to a command in the current interpreter's bin directory.

    This command handles finding the bin directory, even if the interpreter is running within a virtualenv

    Parameters
    ----------
    command: str, optional
        The command to find in the interpreter bin directory, default=python

    Returns
    -------
    str:
        Full path to the command.  If the command is not present returns command if fallback_path is True or an empty
        string otherwise.
    """
    bin_dir = os.path.dirname(sys.executable)
    new_command = os.path.join(bin_dir, command)
    if os.path.exists(new_command):
        return new_command
    bin_dir = os.path.dirname(interpreter_parent(sys.executable))
    new_command = os.path.join(bin_dir, command)
    if os.path.exists(new_command):
        return new_command
    if fallback_path:
        return command
    return ''
Exemplo n.º 4
0
 def setUp(self):
     self._cwd = os.getcwd()
     super().setUp()
     self.original_environ = os.environ
     self.tempdir = tempfile.TemporaryDirectory()
     os.chdir(self.tempdir.name)
     with open(CONFIG_FILE, 'w') as config_handle:
         config_handle.write(TEST_CONFIG)
     self.venv_dir = os.path.join(self.tempdir.name, 'venv')
     self.venv_bin_dir = os.path.join(self.venv_dir, 'bin')
     interpreter = interpreter_parent(sys.executable)
     subprocess.check_call([interpreter, '-m', 'venv', self.venv_dir])
Exemplo n.º 5
0
def validate_with_codestyle(report_dir):
    """Run the codestyle command directly to do the validation"""

    package_metadata = PackageMetadata()
    package_name = package_metadata.metadata['name']
    src_dir = package_srcdir()

    parent_interpreter = interpreter_parent(sys.executable)
    interpreter = os.environ.get('BASE_PYTHON', parent_interpreter)
    bin_dir = os.path.dirname(interpreter)

    pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')
    if not os.path.exists(pycodestyle_command):  # pragma: no cover
        bin_dir = os.path.dirname(sys.executable)
        pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')
        if not os.path.exists(pycodestyle_command):
            bin_dir = os.path.dirname(parent_interpreter)
            pycodestyle_command = os.path.join(bin_dir, 'pycodestyle')
            if not os.path.exists(pycodestyle_command):
                pycodestyle_command = 'pycodestyle'

    # Generate the command line from the environment settings
    command = [pycodestyle_command]

    # Add extra arguments
    extra_args = os.environ.get('CODESTYLE_ARGS', '')
    if extra_args:
        command += extra_args.split()

    if src_dir and src_dir not in ['.']:
        target = src_dir
    else:
        # Add targets
        target = ''
        src_dir = '.'
        if hasattr(package_metadata, 'packages'):
            for package in package_metadata.packages:
                package_path = os.path.join(src_dir, package)
                if os.path.exists(package_path):
                    target = package_path
                    break

        if not target:
            if src_dir not in ['', '.'] and src_dir != package_name:
                target = os.path.join(src_dir, package_name.replace('.', '/'))
            else:
                target = package_name.replace('.', '/')

        print(f'target: {target}')
        target = ins_filename(target)
        if not target:
            print_error(f'ERROR: Unable to find package directory for package {package_name!r}, target directory {target!r} does not exist')
            return 1
    command.append(target)

    print('-' * 90 + '\nRunning:', ' '.join(command) + '\n' + '-' * 90, flush=True)
    rc = 0
    try:
        output = subprocess.check_output(command, stderr=subprocess.STDOUT)  # nosec
    except subprocess.CalledProcessError as error:  # pragma: no cover
        rc = error.returncode
        output = error.output

    os.makedirs(report_dir, exist_ok=True)

    text_report = os.path.join(report_dir, 'codestyle.txt')

    with open(text_report, 'wb') as fh:
        for line in output.split(b'\n'):
            textline = line.decode(errors='ignore').strip()
            fh.write(line)
            if 'error:' in textline:  # pragma: no cover
                print(colored(textline, 'red'), flush=True)
            else:
                print(textline, flush=True)

    return rc
Exemplo n.º 6
0
 def test__install_and_run(self):
     rc = cli.install_and_run(package='.',
                              command='pypirun_true',
                              interpreter=cli.interpreter_parent(
                                  sys.executable))
     self.assertEqual(rc, 0)