# Buggy code
def __enter__(self):
    # Keep the string "LOCKERROR" in this string so that external
    # programs can look for it.
    lockstr = """\
    LOCKERROR: It looks like conda is already doing something.
    The lock %s was found. Wait for it to finish before continuing.
    If you are sure that conda is not running, remove it and try again.
    You can also use: $ conda clean --lock\n"""
    sleeptime = 1

    for _ in range(self.retries):
        if os.path.isdir(self.lock_path):
            stdoutlog.info(lockstr % self.lock_path)
            stdoutlog.info("Sleeping for %s seconds\n" % sleeptime)

            time.sleep(sleeptime)
            sleeptime *= 2
        else:
            os.makedirs(self.lock_path)
            return self

    stdoutlog.error("Exceeded max retries, giving up")
    raise LockError(lockstr % self.lock_path)


# Traceback (PermissionError)
Traceback (most recent call last):
File "/opt/anaconda/bin/conda-env", line 6, in <module>
sys.exit(conda_env.cli.main.main())
File "/opt/anaconda/lib/python3.5/site-packages/conda_env/cli/main.py", line 68, in main
return args_func(args, parser)
File "/opt/anaconda/lib/python3.5/site-packages/conda/cli/main.py", line 127, in args_func
args.func(args, p)
File "/opt/anaconda/lib/python3.5/site-packages/conda_env/cli/main_create.py", line 105, in execute
installer.install(prefix, pkg_specs, args, env)
File "/opt/anaconda/lib/python3.5/site-packages/conda_env/installers/conda.py", line 20, in install
plan.execute_actions(actions, index, verbose=not args.quiet)
File "/opt/anaconda/lib/python3.5/site-packages/conda/plan.py", line 589, in execute_actions
inst.execute_instructions(plan, index, verbose)
File "/opt/anaconda/lib/python3.5/site-packages/conda/instructions.py", line 137, in execute_instructions
cmd(state, arg)
File "/opt/anaconda/lib/python3.5/site-packages/conda/instructions.py", line 80, in LINK_CMD
link(state['prefix'], dist, lt, index=state['index'], shortcuts=shortcuts)
File "/opt/anaconda/lib/python3.5/site-packages/conda/install.py", line 1059, in link
with Locked(prefix), Locked(pkgs_dir):
File "/opt/anaconda/lib/python3.5/site-packages/conda/lock.py", line 60, in __enter__
os.makedirs(self.lock_path)
File "/opt/anaconda/lib/python3.5/os.py", line 241, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/opt/anaconda/pkgs/.conda_lock-5633'

# Fixed code
def __enter__(self):
    retries = 10
    # Keep the string "LOCKERROR" in this string so that external
    # programs can look for it.
    lockstr = """\
    LOCKERROR: It looks like conda is already doing something.
    The lock %s was found. Wait for it to finish before continuing.
    If you are sure that conda is not running, remove it and try again.
    You can also use: $ conda clean --lock\n"""
    sleeptime = 1
    files = None
    while retries:
        files = glob.glob(self.pattern)
        if files and not files[0].endswith(self.end):
            stdoutlog.info(lockstr % str(files))
            stdoutlog.info("Sleeping for %s seconds\n" % sleeptime)
            sleep(sleeptime)
            sleeptime *= 2
            retries -= 1
        else:
            break
    else:
        stdoutlog.error("Exceeded max retries, giving up")
        raise RuntimeError(lockstr % str(files))

    if not files:
        try:
            os.makedirs(self.lock_path)
        except OSError:
            pass
    else:  # PID lock already here --- someone else will remove it.
        self.remove = False