Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with open(target_path) as fh:
created_file_contents = fh.read()
first_line, second_line, third_line, remainder = created_file_contents.split('\n', 3)
if on_win:
win_conda_exe = join(conda_prefix, 'Scripts', 'conda.exe')
assert first_line == 'set -gx CONDA_EXE (cygpath "%s")' % win_conda_exe
assert second_line == 'set _CONDA_ROOT (cygpath "%s")' % conda_prefix
assert third_line == 'set _CONDA_EXE (cygpath "%s")' % win_conda_exe
else:
assert first_line == 'set -gx CONDA_EXE "%s"' % join(conda_prefix, 'bin', 'conda')
assert second_line == 'set _CONDA_ROOT "%s"' % conda_prefix
assert third_line == 'set _CONDA_EXE "%s"' % join(conda_prefix, 'bin', 'conda')
with open(join(CONDA_PACKAGE_ROOT, 'shell', 'etc', 'fish', 'conf.d', 'conda.fish')) as fh:
original_contents = fh.read()
assert remainder == original_contents
result = install_conda_fish(target_path, conda_prefix)
assert result == Result.NO_CHANGE
def test_install_conda_fish(self):
with tempdir() as conda_temp_prefix:
conda_prefix = abspath(sys.prefix)
target_path = join(conda_temp_prefix, 'etc', 'fish', 'conf.d', 'conda.fish')
result = install_conda_fish(target_path, conda_prefix)
assert result == Result.MODIFIED
with open(target_path) as fh:
created_file_contents = fh.read()
first_line, second_line, third_line, remainder = created_file_contents.split('\n', 3)
if on_win:
win_conda_exe = join(conda_prefix, 'Scripts', 'conda.exe')
assert first_line == 'set -gx CONDA_EXE (cygpath "%s")' % win_conda_exe
assert second_line == 'set _CONDA_ROOT (cygpath "%s")' % conda_prefix
assert third_line == 'set _CONDA_EXE (cygpath "%s")' % win_conda_exe
else:
assert first_line == 'set -gx CONDA_EXE "%s"' % join(conda_prefix, 'bin', 'conda')
assert second_line == 'set _CONDA_ROOT "%s"' % conda_prefix
assert third_line == 'set _CONDA_EXE "%s"' % join(conda_prefix, 'bin', 'conda')
with open(join(CONDA_PACKAGE_ROOT, 'shell', 'etc', 'fish', 'conf.d', 'conda.fish')) as fh:
original_contents = fh.read()
assert remainder == original_contents
def parse(self):
"""
parse the history file and return a list of
tuples(datetime strings, set of distributions/diffs, comments)
"""
res = []
if not isfile(self.path):
return res
sep_pat = re.compile(r'==>\s*(.+?)\s*<==')
with open(self.path) as f:
lines = f.read().splitlines()
for line in lines:
line = line.strip()
if not line:
continue
m = sep_pat.match(line)
if m:
res.append((m.group(1), set(), []))
elif line.startswith('#'):
res[-1][2].append(line)
elif len(res) > 0:
res[-1][1].add(line)
return res
# bat file redirect
if not isfile(dst + '.bat'):
with open(dst + '.bat', 'w') as f:
f.write('@echo off\ncall "%s" %%*\n' % src)
# TODO: probably need one here for powershell at some point
# This one is for bash/cygwin/msys
# set default shell to bash.exe when not provided, as that's most common
if not shell:
shell = "bash.exe"
# technically these are "links" - but islink doesn't work on win
if not isfile(dst):
with open(dst, "w") as f:
f.write("#!/usr/bin/env bash \n")
if src.endswith("conda"):
f.write('%s "$@"' % shells[shell]['path_to'](src+".exe"))
else:
f.write('source %s "$@"' % shells[shell]['path_to'](src))
# Make the new file executable
# http://stackoverflow.com/a/30463972/1170370
mode = stat(dst).st_mode
mode |= (mode & 292) >> 2 # copy R bits to X
chmod(dst, mode)
def install_deactivate(target_path, conda_prefix):
# target_path: join(conda_prefix, get_bin_directory_short_path(), 'deactivate')
src_path = join(CONDA_PACKAGE_ROOT, 'shell', 'bin', 'deactivate')
file_content = (
"#!/bin/sh\n"
"_CONDA_ROOT=\"%s\"\n"
) % conda_prefix
with open(src_path) as fsrc:
file_content += fsrc.read()
return _install_file(target_path, file_content)
rc_content,
flags=re.MULTILINE,
)
# TODO: maybe remove all but last of replace_str, if there's more than one occurrence
rc_content = rc_content.replace(replace_str, conda_initialize_content)
if "# >>> conda initialize >>>" not in rc_content:
rc_content += '\n%s\n' % conda_initialize_content
if rc_content != rc_original_content:
if context.verbosity:
print('\n')
print(target_path)
print(make_diff(rc_original_content, rc_content))
if not context.dry_run:
with open(user_rc_path, 'w') as fh:
fh.write(rc_content)
return Result.MODIFIED
else:
return Result.NO_CHANGE
def read_index_json(extracted_package_directory):
with open(join(extracted_package_directory, 'info', 'index.json')) as fi:
record = IndexJsonRecord(**json.load(fi))
return record
def init_powershell_user(target_path, conda_prefix, reverse):
# target_path: $PROFILE
profile_path = target_path
# NB: the user may not have created a profile. We need to check
# if the file exists first.
if os.path.exists(profile_path):
with open(profile_path) as fp:
profile_content = fp.read()
else:
profile_content = ""
profile_original_content = profile_content
# TODO: comment out old ipmos and Import-Modules.
if reverse:
profile_content = re.sub(CONDA_INITIALIZE_PS_RE_BLOCK,
"",
profile_content,
count=1,
flags=re.DOTALL | re.MULTILINE
)
else:
def make_entry_point(target_path, conda_prefix, module, func):
# target_path: join(conda_prefix, 'bin', 'conda')
conda_ep_path = target_path
if isfile(conda_ep_path):
with open(conda_ep_path) as fh:
original_ep_content = fh.read()
else:
original_ep_content = ""
if on_win:
# no shebang needed on windows
new_ep_content = ""
else:
new_ep_content = "#!%s\n" % join(conda_prefix, get_python_short_path())
conda_extra = dals("""
# Before any more imports, leave cwd out of sys.path for internal 'conda shell.*' commands.
# see https://github.com/conda/conda/issues/6549
if len(sys.argv) > 1 and sys.argv[1].startswith('shell.') and sys.path and sys.path[0] == '':
# The standard first entry in sys.path is an empty string,
# and os.path.abspath('') expands to os.getcwd().
def _digest_path(algo, path):
if not isfile(path):
raise PathNotFoundError(path)
hasher = hashlib.new(algo)
with open(path, "rb") as fh:
for chunk in iter(partial(fh.read, 8192), b''):
hasher.update(chunk)
return hasher.hexdigest()