Skip to content

Commit 34de91a

Browse files
committed
Prevent destruction of .ebcli-virtual-env if it is not created by the script
1 parent 0a774bc commit 34de91a

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

scripts/ebcli_installer.py

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
input = raw_input
3434

3535

36+
EBCLI_INSTALLER_STAMP = '.ebcli_installer_stamp'
37+
38+
3639
EXECUTABLE_WRAPPERS = {
3740
'bat': '\n'.join(
3841
[
@@ -378,6 +381,15 @@ def _create_virtualenv(
378381
of execution of this script and now
379382
- this script was executed with a Python executable not in PATH
380383
384+
Prior to creation of the virtualenv, this function checks whether
385+
one by name `.ebcli-virtual-env` already exists. If this directory
386+
was not created by this installer, installation halts and the user
387+
is asked to either delete the directory or to specify an alternate
388+
location using the `--location` argument of this script.
389+
390+
In all other cases, `.ebcli-virtual-env` is (re)created and a file
391+
to denote that the installer created `.ebcli-virtual-env` is added.
392+
381393
:param virtualenv_executable: the name of the virtualenv executable
382394
:param virtualenv_location: the relative or absolute path to the location
383395
where the virtualenv, ".ebcli-virtual-env", must
@@ -392,10 +404,29 @@ def _create_virtualenv(
392404
virtualenv, ".ebcli-virtual-env", was created.
393405
"""
394406
virtualenv_location = virtualenv_location or _user_local_directory()
407+
virtualenv_directory = os.path.join(virtualenv_location, VIRTUALENV_DIR_NAME)
408+
409+
if (
410+
os.path.exists(virtualenv_directory)
411+
and not _directory_was_created_by_installer(virtualenv_directory)
412+
):
413+
_error(
414+
'Installation cannot proceed because "{virtualenv_location}" already exists '
415+
'but was not created by this EBCLI installer.'
416+
'\n'
417+
'\n'
418+
'You can either:\n'
419+
'\n'
420+
'1. Delete "{virtualenv_location}" after verifying you don\'t need it; OR\n'
421+
'2. Specify an alternate location to install the EBCLI and its artifacts in '
422+
'using the `--location` argument of this script .\n'.format(
423+
virtualenv_location=virtualenv_directory
424+
)
425+
)
395426

396427
virtualenv_args = [
397428
virtualenv_executable,
398-
os.path.join(virtualenv_location, VIRTUALENV_DIR_NAME)
429+
virtualenv_directory
399430
]
400431

401432
python_installation and virtualenv_args.extend(
@@ -405,6 +436,8 @@ def _create_virtualenv(
405436
if _exec_cmd(virtualenv_args, quiet) != 0:
406437
exit(1)
407438

439+
_add_ebcli_stamp(virtualenv_directory)
440+
408441
return virtualenv_location
409442

410443

@@ -502,6 +535,26 @@ def _install_ebcli(quiet, version, ebcli_source):
502535
_exec_cmd(install_args, quiet)
503536

504537

538+
def _add_ebcli_stamp(virtualenv_directory):
539+
"""
540+
Function adds a stamp in the form of a file, `EBCLI_INSTALLER_STAMP`
541+
to recognize during future executions of this script that it created
542+
it.
543+
544+
:param virtualenv_directory: The directory where the EBCLI and its artifacts
545+
will be installed
546+
:return: None
547+
"""
548+
with open(
549+
os.path.join(
550+
virtualenv_directory,
551+
EBCLI_INSTALLER_STAMP
552+
),
553+
'w'
554+
) as file:
555+
file.write('\n')
556+
557+
505558
def _bat_script_body(virtualenv_location):
506559
"""
507560
Function returns a CMD Prompt (bat) script which essentially will
@@ -517,6 +570,24 @@ def _bat_script_body(virtualenv_location):
517570
)
518571

519572

573+
def _directory_was_created_by_installer(virtualenv_directory):
574+
"""
575+
Function checks whether `virtualenv_directory` was previously created
576+
by this script.
577+
578+
:param virtualenv_directory: The directory where the EBCLI and its
579+
artifacts will be installed.
580+
:return: Boolean indicating whether `virtualenv_directory` was created
581+
by this script or not.
582+
"""
583+
return os.path.exists(
584+
os.path.join(
585+
virtualenv_directory,
586+
EBCLI_INSTALLER_STAMP
587+
)
588+
)
589+
590+
520591
def _eb_wrapper_location(virtualenv_location):
521592
"""
522593
Function returns the location of the directory within the virtualenv,

0 commit comments

Comments
 (0)