Repository: conda/conda-build

Issue:
UnboundLocalError with --skip-existing and --no-locking flags
Hit this today on conda-build 2.1.2. Also tried with the tip of master and I get the same result.  For reproduciblity, this is the output for trying to build the conda.recipe folder inside of conda-build itself:

```
$ conda build conda.recipe --no-locking --skip-existing                 master :: 1h :: ⬢
Cloning into '/home/edill/miniconda/conda-bld/conda.recipe_1485803296268/work'...
done.
checkout: 'HEAD'
Your branch is up-to-date with 'origin/_conda_cache_origin_head'.
==> git log -n1 <==

commit 6922ec3ed1afc287a4cd7f3872572f2bef89d892
Merge: 837fbc8 c82ea9b
Author: Mike Sarahan <msarahan@gmail.com>
Date:   Mon Jan 30 11:38:01 2017 -0600

    Merge pull request #1704 from jerowe/feature/fix-perl-build

    adding some fixes to cpan skeleton


==> git describe --tags --dirty <==

2.1.2-20-g6922ec3


==> git status <==

On branch _conda_cache_origin_head
Your branch is up-to-date with 'origin/_conda_cache_origin_head'.

nothing to commit, working directory clean


updating index in: /home/edill/miniconda/conda-bld/linux-64
Traceback (most recent call last):
  File "/home/edill/miniconda/bin/conda-build", line 11, in <module>
    load_entry_point('conda-build', 'console_scripts', 'conda-build')()
  File "/home/edill/dev/conda/conda-build/conda_build/cli/main_build.py", line 322, in main
    execute(sys.argv[1:])
  File "/home/edill/dev/conda/conda-build/conda_build/cli/main_build.py", line 313, in execute
    noverify=args.no_verify)
  File "/home/edill/dev/conda/conda-build/conda_build/api.py", line 97, in build
    need_source_download=need_source_download, config=config)
  File "/home/edill/dev/conda/conda-build/conda_build/build.py", line 1478, in build_tree
    config=config)
  File "/home/edill/dev/conda/conda-build/conda_build/build.py", line 928, in build
    package_exists = is_package_built(m, config)
  File "/home/edill/dev/conda/conda-build/conda_build/build.py", line 1633, in is_package_built
    update_index(d, config, could_be_mirror=False)
  File "/home/edill/dev/conda/conda-build/conda_build/index.py", line 83, in update_index
    with try_acquire_locks(locks, config.timeout):
UnboundLocalError: local variable 'locks' referenced before assignment
```

And some debug info

```
$ conda info 
Current conda install:

               platform : linux-64
          conda version : 4.2.13
       conda is private : False
      conda-env version : 4.2.13
    conda-build version : 2.1.2+20.g6922ec3
         python version : 3.5.3.final.0
       requests version : 2.13.0
       root environment : /home/edill/miniconda  (writable)
    default environment : /home/edill/miniconda
       envs directories : /home/edill/miniconda/envs
          package cache : /home/edill/miniconda/pkgs
           channel URLs : ...
            config file : /home/edill/.condarc
           offline mode : False
```



Discussion:
Thanks. Definitely a bug. Can I ask why you're using no-locking though?  Are the locks getting in your way somehow?
After doing a bunch of digging, there was a bad package cached locally that was causing locking issues.  Shortened log output was this:

```
12:53:16 Exceeded max retries, giving upWARNING conda_build.build:create_env(725): failed to create env, retrying.  exception was: Lock error: 
12:53:16 LOCKERROR: It looks like conda is already doing something.
12:53:16 The lock ['/var/jenkins/workspace/maxforge-recipes-nightly/miniconda/pkgs/boost-cpp-1.63.0-1.tar.bz2.pid20778.conda_lock'] was found. Wait for it to finish before continuing.
12:53:16 If you are sure that conda is not running, remove it and try again.
12:53:16 You can also use: $ conda clean --lock
```

After removing boost-cpp-1.63.0 the locking issue went away. 
Trying a full rebuild of our internal conda recipes now without the --no-locking flag
OK.  Sadly, conda-build locking is different from conda locking.  The error you saw was a conda locking error.  ``conda clean --locks`` should have taken care of things.
Ah. So the `--no-locking` flag wouldn't have even helped?
Nope, totally separate set of locks.  Glad you found the bug, though!
Thanks for the help @msarahan . Learned something new about conda, conda-build and locking :smile: 

Resolving patch:
diff --git a/conda_build/index.py b/conda_build/index.py
--- a/conda_build/index.py
+++ b/conda_build/index.py
@@ -77,8 +77,9 @@ def update_index(dir_path, config, force=False, check_md5=False, remove=True, lo
     if not lock:
         lock = get_lock(dir_path)
 
+    locks = []
     if config.locking:
-        locks = [lock]
+        locks.append(lock)
 
     with try_acquire_locks(locks, config.timeout):
         if force:


Test patch: