Repository: conda/conda-build

Issue:
KeyError: 'build' in File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/post.py", line 394, in get_build_metadata
Conda has started to fail to build my openocd package at https://github.com/timvideos/conda-hdmi2usb-packages/tree/master/openocd

You can see the complete logs at https://travis-ci.org/timvideos/conda-hdmi2usb-packages/jobs/135197247

```
+ ./src/openocd --version
+ head -1
+ sed -es/-/_/g '-es/.* 0\./0./' '-es/ .*//'
+ ./src/openocd --version
+ sed '-es/[^(]*(//' '-es/)//' -es/://g -es/-/_/g
+ head -1
+ TZ=UTC
+ date +%Y%m%d%H%M%S
Deprecation warning: support for __conda_version__ will be removed in Conda build 2.0.Try Jinja templates instead: http://conda.pydata.org/docs/building/environment-vars.html#git-environment-variables
Setting version from __conda_version__.txt: 0.10.0_dev_00248_g772a165
Deprecation warning: support for __conda_buildnum__ will be removed in Conda build 2.0.Try Jinja templates instead: http://conda.pydata.org/docs/building/environment-vars.html#git-environment-variables
Setting build number from __conda_buildnum__.txt: 20160604040737
An unexpected error has occurred, please consider sending the
..
    https://github.com/conda/conda-build/issues
.
Include the output of the command 'conda info' in your report.
..
Traceback (most recent call last):
  File "/tmp/.../conda/bin/conda-build", line 5, in <module>
    sys.exit(main())
  File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/main_build.py", line 134, in main
    args_func(args, p)
  File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/main_build.py", line 373, in args_func
    args.func(args, p)
  File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/main_build.py", line 320, in execute
    need_source_download=need_source_download)
  File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/build.py", line 523, in build
    get_build_metadata(m)
  File "/tmp/.../conda/lib/python3.5/site-packages/conda_build/post.py", line 394, in get_build_metadata
    m.meta['build']['number'] = build_number
KeyError: 'build'
```



Discussion:
I think the problem appears to be the lack of build section in my meta.yaml file?

Yes, in order to use the post-build versioning, you must have the build section in your meta.yaml file.  We can fix that by adding the 'build' key if it doesn't exist.

As you've mentioned in #8, post-build versioning is deprecated.  Post-build versioning makes it impossible to reason about the filename that will be output by a build, which in turn breaks other parts of conda-build, such as pre-rendering templates, and outputting built package filenames without actually doing a build.  I could not come up with good reasons why versions can't be known before a build.  I'm open to discussion if you have ideas.  I think your versioning needs will be well-met with jinja2 templating, though, and I'm happy to answer questions you may have on that.


Resolving patch:
diff --git a/conda_build/post.py b/conda_build/post.py
--- a/conda_build/post.py
+++ b/conda_build/post.py
@@ -375,6 +375,8 @@ def check_symlinks(files):
 
 def get_build_metadata(m):
     src_dir = source.get_dir()
+    if "build" not in m.meta:
+        m.meta["build"] = {}
     if exists(join(src_dir, '__conda_version__.txt')):
         print("Deprecation warning: support for __conda_version__ will be removed in Conda build 2.0."  # noqa
               "Try Jinja templates instead: "


Test patch: