def test_click_invocation(mocker):
    prompt = mocker.patch("click.prompt")
    prompt.return_value = "sekrit"

    assert read_repo_password("Password") == "sekrit"

    prompt.assert_called_once_with("Password", hide_input=True)
def test_click_invocation(mocker):
    prompt = mocker.patch('click.prompt')
    prompt.return_value = 'sekrit'

    assert read_repo_password('Password') == 'sekrit'

    prompt.assert_called_once_with('Password', hide_input=True)
def test_click_invocation(mocker):
    prompt = mocker.patch('click.prompt')
    prompt.return_value = 'sekrit'

    assert read_repo_password('Password') == 'sekrit'

    prompt.assert_called_once_with('Password', hide_input=True)
def test_click_invocation(mocker):
    """Test click function called correctly by cookiecutter.

    Test for password (hidden input) type invocation.
    """
    prompt = mocker.patch('click.prompt')
    prompt.return_value = 'sekrit'

    assert read_repo_password('Password') == 'sekrit'

    prompt.assert_called_once_with('Password', hide_input=True)
Example #5
0
def unzip(zip_uri,
          is_url,
          clone_to_dir='.',
          no_input=False,
          refresh=False,
          password=None):
    """Download and unpack a zipfile at a given URI.

    This will download the zipfile to the cookiecutter repository,
    and unpack into a temporary directory.

    :param zip_uri: The URI for the zipfile.
    :param is_url: Is the zip URI a URL or a file?
    :param clone_to_dir: The cookiecutter repository directory
        to put the archive into.
    :param no_input: Suppress any prompts
    :param refresh: If true, overwrites cached cookiecutter without prompting user
    :param password: The password to use when unpacking the repository.
    """
    # Ensure that clone_to_dir exists
    clone_to_dir = os.path.expanduser(clone_to_dir)
    make_sure_path_exists(clone_to_dir)

    if is_url:
        # Build the name of the cached zipfile,
        # and prompt to delete if it already exists.
        identifier = zip_uri.rsplit('/', 1)[1]
        zip_path = os.path.join(clone_to_dir, identifier)

        if os.path.exists(zip_path):
            download = prompt_and_delete(zip_path,
                                         no_input=no_input,
                                         refresh=refresh)
        else:
            download = True

        if download:
            # (Re) download the zipfile
            r = requests.get(zip_uri, stream=True)
            with open(zip_path, 'wb') as f:
                for chunk in r.iter_content(chunk_size=1024):
                    if chunk:  # filter out keep-alive new chunks
                        f.write(chunk)
    else:
        # Just use the local zipfile as-is.
        zip_path = os.path.abspath(zip_uri)

    # Now unpack the repository. The zipfile will be unpacked
    # into a temporary directory
    try:
        zip_file = ZipFile(zip_path)

        if len(zip_file.namelist()) == 0:
            raise InvalidZipRepository(
                'Zip repository {} is empty'.format(zip_uri))

        # The first record in the zipfile should be the directory entry for
        # the archive. If it isn't a directory, there's a problem.
        first_filename = zip_file.namelist()[0]
        if not first_filename.endswith('/'):
            raise InvalidZipRepository('Zip repository {} does not include '
                                       'a top-level directory'.format(zip_uri))

        # Construct the final target directory
        project_name = first_filename[:-1]
        unzip_base = tempfile.mkdtemp()
        unzip_path = os.path.join(unzip_base, project_name)

        # Extract the zip file into the temporary directory
        try:
            zip_file.extractall(path=unzip_base)
        except RuntimeError:
            # File is password protected; try to get a password from the
            # environment; if that doesn't work, ask the user.
            if password is not None:
                try:
                    zip_file.extractall(path=unzip_base,
                                        pwd=password.encode('utf-8'))
                except RuntimeError:
                    raise InvalidZipRepository(
                        'Invalid password provided for protected repository')
            elif no_input:
                raise InvalidZipRepository(
                    'Unable to unlock password protected repository')
            else:
                retry = 0
                while retry is not None:
                    try:
                        password = read_repo_password('Repo password')
                        zip_file.extractall(path=unzip_base,
                                            pwd=password.encode('utf-8'))
                        retry = None
                    except RuntimeError:
                        retry += 1
                        if retry == 3:
                            raise InvalidZipRepository(
                                'Invalid password provided for protected repository'
                            )

    except BadZipFile:
        raise InvalidZipRepository(
            'Zip repository {} is not a valid zip archive:'.format(zip_uri))

    return unzip_path
Example #6
0
if __name__ == '__main__':

    from cookiecutter.prompt import read_user_variable, read_user_choice, read_repo_password
    from cookiecutter.main   import cookiecutter

    context = {}

    context['project_name']                = read_user_variable('project_name', 'my_project_name')
    context['django_template']             = read_user_choice('django_template', ['django'])
    context['db_backend']                  = read_user_choice('db_backend', ['mysql', 'postgresql'])
    context['db_password']                 = read_repo_password('db_password')
    context['docker_compose_file_version'] = read_user_variable('docker_compose_file_version', '3.8')

    if context['db_backend'] == 'mysql':

        context['db_user'] = '******'

    elif context['db_backend'] == 'postgresql':

        context['db_user'] = '******'

    repo       = 'https://github.com/otto-torino/webapp-boilerplate/'
    template   = 'templates/base'
    output_dir = '.'

    cookiecutter(repo, no_input=True, extra_context=context, directory=template, output_dir=output_dir)

    template   = f'templates/{context['django_template']}'
    output_dir = f'{output_dir}/{context['project_name']}'

    cookiecutter(repo, no_input=True, extra_context=context, directory=template, output_dir=output_dir)