aboutsummaryrefslogtreecommitdiff
path: root/cbc
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2015-06-22 00:35:10 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2015-06-22 00:35:10 -0400
commit3bdc5103ae2b845e9c83a789b5781ee4ee979d7c (patch)
treeff6a7123da24223f666132ad11d967c97423a0c3 /cbc
parent348e41e7ee22bcb23d549d7d816f4a946df54eac (diff)
downloadcbc-3bdc5103ae2b845e9c83a789b5781ee4ee979d7c.tar.gz
Refactored and added Environment module
Diffstat (limited to 'cbc')
-rw-r--r--cbc/__init__.py2
-rwxr-xr-xcbc/environment.py28
-rw-r--r--cbc/meta.py95
3 files changed, 94 insertions, 31 deletions
diff --git a/cbc/__init__.py b/cbc/__init__.py
index e69de29..1e05628 100644
--- a/cbc/__init__.py
+++ b/cbc/__init__.py
@@ -0,0 +1,2 @@
+from . import environment
+from cbc import meta \ No newline at end of file
diff --git a/cbc/environment.py b/cbc/environment.py
new file mode 100755
index 0000000..78b64cc
--- /dev/null
+++ b/cbc/environment.py
@@ -0,0 +1,28 @@
+import os
+
+class IncompleteEnv(Exception):
+ pass
+
+class Environment(object):
+ def __init__(self, *args, **kwargs):
+ self.environ = os.environ.copy()
+ self.config = {}
+ self.cbchome = None
+
+ if 'CBC_HOME' in kwargs:
+ self.cbchome = kwargs['CBC_HOME']
+ if 'CBC_HOME' in self.environ:
+ self.cbchome = self.environ['CBC_HOME']
+
+ if self.cbchome is None:
+ raise IncompleteEnv('Environment.cbchome is undefined')
+
+ if not os.path.exists(self.cbchome):
+ os.makedirs(self.cbchome)
+
+ self.config['meta'] = self.join('meta.yaml')
+ self.config['build'] = self.join('build.sh')
+ self.config['build_windows'] = self.join('bld.bat')
+
+ def join(self, path):
+ return os.path.join(self.cbchome, path) \ No newline at end of file
diff --git a/cbc/meta.py b/cbc/meta.py
index f38532f..fb36c57 100644
--- a/cbc/meta.py
+++ b/cbc/meta.py
@@ -3,46 +3,79 @@ And yeah, conda supports Jinja2, but ugh... No.
'''
import os
-import conda_build
import conda_build.metadata
-
-import configparser
-from configparser import ConfigParser, ExtendedInterpolation
-from pprint import pprint
import yaml
-
-def convert_conda_fields(fields):
- temp = {}
- for fkey, fval in fields.items():
- temp[fkey] = { x: '' for x in fval}
- #fields[field] = dict(value)
- return temp
+from configparser import SafeConfigParser, ExtendedInterpolation
+from collections import OrderedDict
-class Specfile(object):
- def __init__(self, filename):
+class Spec(object):
+ def __init__(self, filename, env):
if not os.path.exists(filename):
print('"{0}" does not exist.'.format(filename));
return
-
- self.filename = filename
- fields = convert_conda_fields(conda_build.metadata.FIELDS)
-
- config = ConfigParser(interpolation=ExtendedInterpolation(), allow_no_value=True)
- config.read_dict(fields)
- config.read(self.filename)
- with open('../../test.ini.out', 'w+') as testfile:
- config.write(testfile)
+ self.keywords = ['build_ext', 'cgi']
+ self.filename = filename
+ self.fields = self.convert_conda_fields(conda_build.metadata.FIELDS)
- y = yaml.load(fields)
- md = conda_build.metadata.MetaData()
+ config = SafeConfigParser(interpolation=ExtendedInterpolation(), allow_no_value=True)
- #for section in config.sections():
- # for sub in config[section]:
- # print('{0}:{1}:{2}'.format(section, sub, config[section][sub]))
+ # Include built-in Conda metadata fields
+ config.read_dict(self.fields)
+
+ # Include user-defined build fields
+ config.read(self.filename)
+
+ # Convert ConfigParser -> dict
+ spec = self.as_dict(config)
+
+ # Convert dict to YAML-compatible dict
+ metadata = self.scrub(spec, self.keywords)
+
+ with open(os.path.join(env.config['meta']), 'w+') as metafile:
+ yaml.safe_dump(metadata, metafile, default_flow_style=False, line_break=True, indent=4)
+
+ def convert_conda_fields(self, fields):
+ temp = OrderedDict()
+ for fkey, fval in fields.items():
+ temp[fkey] = { x: '' for x in fval}
+
+ return temp
+
+ def scrub(self, obj, force_remove=[]):
+ obj_c = obj.copy()
+ if isinstance(obj_c, dict):
+ for key,val in obj_c.items():
+ for reserved in force_remove:
+ if reserved in key:
+ del obj[reserved]
+ continue
+ if isinstance(val, dict):
+ val = self.scrub(val)
+ if val is None or val == {} or not val:
+ del obj[key]
+
+ return obj
+
+
+ def as_dict(self, config):
+ """
+ Converts a ConfigParser object into a dictionary.
+
+ The resulting dictionary has sections as keys which point to a dict of the
+ sections options as key => value pairs.
+ """
+ the_dict = {}
+ for section in config.sections():
+ the_dict[section] = {}
+ for key, val in config.items(section):
+ for cast in (int, float, bool, str):
+ try:
+ the_dict[section][key] = cast(val)
+ except ValueError:
+ pass
+
+ return the_dict
-if __name__ == '__main__':
- spec = Specfile('../../test.spec')
- #pprint()
\ No newline at end of file