Пример #1
0
def build(package=None, **options):
    """Build the package and create a kecpkg file."""
    echo_info('Locating package ``'.format(package))
    package_dir = get_package_dir(package_name=package)
    package_name = os.path.basename(package_dir)
    echo_info('Package `{}` has been selected'.format(package_name))
    settings = load_settings(
        package_dir=package_dir,
        settings_filename=options.get('settings_filename'))

    # ensure build directory is there
    build_dir = settings.get('build_dir', 'dist')
    build_path = os.path.join(package_dir, build_dir)

    if options.get('update_package_info'):
        render_package_info(settings, package_dir=package_dir, backup=True)

    if options.get('clean_first'):
        remove_path(build_path)
    ensure_dir_exists(build_path)

    # do package building
    build_package(package_dir,
                  build_path,
                  settings,
                  options=options,
                  verbose=options.get('verbose'))

    echo_success('Complete')
Пример #2
0
def get_gpg():
    # type: () -> gnupg.GPG
    """Return the GPG objects instantiated with custom KECPKG keyring in custom KECPKG GNUPG home."""
    global __gpg
    if not __gpg:
        if six.PY2:
            echo_failure(
                'Package signing capability is not available in python 2.7. Please use python 3 or greater.'
            )
            sys.exit(1)

        import gnupg
        logging.basicConfig(level=LOGLEVEL)
        logging.getLogger('gnupg')
        gpg_bin = 'gpg'
        if ON_LINUX:
            gpg_bin = subprocess.getoutput('which gpg')
        if ON_WINDOWS:
            bin_path_guesses = [
                "C:\\Program Files (x86)\\GnuPG\\bin\\gpg.exe",
                "C:\\Program Files\\GnuPG\\gpg.exe",
                "C:\\Program Files (x86)\\GnuPG\\gpg.exe",
                "C:\\Program Files\\GnuPG\\bin\\gpg.exe"
            ]
            gpg_bins = [p for p in bin_path_guesses if os.path.exists(p)]
            if gpg_bins is not None:
                gpg_bin = gpg_bins[0]
            else:
                gpg_bin = bin_path_guesses[0]
        elif ON_MACOS:
            gpg_bin = '/usr/local/bin/gpg'
        if not os.path.exists(gpg_bin):
            echo_failure(
                "Unable to detect installed GnuPG executable. Ensure you have it installed. "
                "We checked: '{}'".format(gpg_bin))
            echo_failure(
                "- For Linux please install GnuPG using your package manager. In Ubuntu/Debian this can be "
                "achieved with `sudo apt install gnupg`.")
            echo_failure(
                "- For Mac OSX please install GnuPG using `brew install gpg`.")
            echo_failure(
                "- For Windows please install GnuPG using the downloads via: https://gnupg.org/download/"
            )
            sys.exit(1)

        if not os.path.exists(GNUPG_KECPKG_HOME):
            # create the GNUPG_KECPKG_HOME when not exist, otherwise the GPG will fail
            ensure_dir_exists(GNUPG_KECPKG_HOME)

        __gpg = gnupg.GPG(gpgbinary=gpg_bin, gnupghome=GNUPG_KECPKG_HOME)

    return __gpg
Пример #3
0
def save_settings(settings, package_dir=None, settings_filename=None):
    """
    Save settings in path (in the package).

    :param settings: settings to save
    :param settings_filename: (optional) pathname of the file where the settings are stored
    :param package_dir: (optional) package_dir to save to
    :return: None
    """
    if settings.get('package_name') and not package_dir:
        package_dir = get_package_dir(settings.get('package_name'))
    settings_filepath = get_settings_filepath(package_dir, settings_filename)

    ensure_dir_exists(os.path.dirname(settings_filepath))
    with atomic_write(settings_filepath, overwrite=True) as f:
        f.write(json.dumps(settings, indent=4))
Пример #4
0
def create_package(package_dir, settings):
    """
    Create the package directory.

    package_name  (or package_dir)
    +-- README.md
    +-- requirements.txt
    +-- package_info.json
    +-- main.py  (settable with settings['entrypoint_script']

    :param package_dir: the full path to the package dir
    :param settings: settings dict
    """
    ensure_dir_exists(package_dir)
    render_to_file('README.md', content=settings, target_dir=package_dir)
    render_to_file('requirements.txt',
                   content=settings,
                   target_dir=package_dir)
    render_to_file('package_info.json',
                   content=dict(
                       requirements_txt='requirements.txt',
                       entrypoint_script=settings.get('entrypoint_script'),
                       entrypoint_func=settings.get('entrypoint_func')),
                   target_dir=package_dir)
    render_to_file('.gitignore', content=dict(), target_dir=package_dir)
    render_to_file('.env', content=dict(), target_dir=package_dir)

    # runconfigurations
    run_configurations_path = os.path.join(package_dir, '.idea',
                                           'runConfigurations')
    ensure_dir_exists(run_configurations_path)
    render_to_file('Upload_the_kecpkg.xml',
                   content=dict(),
                   target_dir=run_configurations_path)
    render_to_file('Build_the_kecpkg.xml',
                   content=dict(),
                   target_dir=run_configurations_path)

    script_filename = '{}.py'.format(settings.get('entrypoint_script'))

    render_to_file(script_filename,
                   content=settings,
                   template='script.py.template',
                   target_dir=package_dir)
Пример #5
0
    def test_build_with_extra_ignores(self):
        pkgname = 'new_pkg'

        with temp_chdir() as d:
            runner = CliRunner()
            result = runner.invoke(kecpkg, ['new', pkgname, '--no-venv'])
            package_dir = get_package_dir(pkgname)
            self.assertTrue(os.path.exists(package_dir))
            ensure_dir_exists(os.path.join(package_dir, 'data'))

            # add additional files (to exclude for building later)
            touch_file(os.path.join(package_dir, 'data', 'somefile.txt'))
            touch_file(
                os.path.join(package_dir, 'local_extra_file.someext.txt'))

            os.chdir(package_dir)

            # check if those files exists
            package_dir_contents = os.listdir(os.path.join(package_dir))
            self.assertTrue(
                'local_extra_file.someext.txt' in package_dir_contents)
            self.assertTrue('data' in package_dir_contents)

            # set exclude_paths in settings
            settings = copy_default_settings()
            settings["exclude_paths"] = ["data", "local_extra_file.*"]
            save_settings(settings, package_dir=package_dir)

            # run the builder
            result = runner.invoke(kecpkg, ['build', pkgname, '--verbose'])
            self.assertEqual(
                result.exit_code, 0,
                "Results of the run were: \n---\n{}\n---".format(
                    result.output))
            self.assertExists(os.path.join(package_dir, 'dist'))

            # check the zip such that the extra files are not packaged
            dist_list = os.listdir(os.path.join(package_dir, 'dist'))
            zipfile = ZipFile(os.path.join(package_dir, 'dist', dist_list[0]),
                              'r')
            contents = zipfile.namelist()

            self.assertFalse('local_extra_file.someext.txt' in contents)
            self.assertFalse('data' in contents)