diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2015-06-23 09:28:34 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2015-06-23 09:28:34 -0400 | 
| commit | 3a8198bfb603a6aaf3cc795f753d741530bdcb51 (patch) | |
| tree | fae26ec465ce19c3e92470411b94eaaccc736912 | |
| parent | defbf2d0d1ff6520bb3f7aab9904c0f239de784e (diff) | |
| download | cbc-3a8198bfb603a6aaf3cc795f753d741530bdcb51.tar.gz | |
Refactored and split apart exceptions
| -rwxr-xr-x | cbc/environment.py | 16 | ||||
| -rwxr-xr-x[-rw-r--r--] | cbc/meta.py | 186 | ||||
| -rwxr-xr-x[-rw-r--r--] | tests/data/test.ini | 106 | ||||
| -rwxr-xr-x[-rw-r--r--] | tests/test.py | 136 | 
4 files changed, 228 insertions, 216 deletions
| diff --git a/cbc/environment.py b/cbc/environment.py index 78b64cc..34d77b2 100755 --- a/cbc/environment.py +++ b/cbc/environment.py @@ -1,7 +1,8 @@  import os
 +from .exceptions import IncompleteEnv
 +from tempfile import TemporaryDirectory
 +import time
 -class IncompleteEnv(Exception):
 -    pass
  class Environment(object):
      def __init__(self, *args, **kwargs):
 @@ -19,10 +20,17 @@ class Environment(object):          if not os.path.exists(self.cbchome):
              os.makedirs(self.cbchome)
 -            
 +        
 +        temp_prefix = os.path.basename(os.path.splitext(__name__)[0])
 +        tempdir = TemporaryDirectory(prefix=temp_prefix, dir=self.cbchome)
 +        self.working_dir = tempdir.name 
 +        time.sleep(10)
          self.config['meta'] = self.join('meta.yaml')
          self.config['build'] = self.join('build.sh')
          self.config['build_windows'] = self.join('bld.bat')
 +        print(self.working_dir)
      def join(self, path):
 -        return os.path.join(self.cbchome, path)
\ No newline at end of file +        return os.path.join(self.cbchome, path)
 +            
 +        
\ No newline at end of file diff --git a/cbc/meta.py b/cbc/meta.py index da6ce3b..0670304 100644..100755 --- a/cbc/meta.py +++ b/cbc/meta.py @@ -1,93 +1,95 @@ -'''I refuse to write the same thing over and over again in meta.yaml. -And yeah, conda supports Jinja2, but ugh... No. -''' - -import os -import conda_build.metadata -import yaml -from configparser import SafeConfigParser, ExtendedInterpolation -from collections import OrderedDict -from .environment import Environment - -class SpecError(Exception): -    pass - -class Spec(object): -    def __init__(self, filename, env): -         -        if not os.path.exists(filename): -            raise OSError('"{0}" does not exist.'.format(filename)); -         -        self.filename = filename -         -        if not isinstance(env, Environment): -            raise SpecError('Expecting instance of cbc.environment.Environment, got: "{0}"'.format(type(env))) -         -        self.env = env -        self.keywords = ['build_ext', 'cgi'] -         -         -        self.fields = self.convert_conda_fields(conda_build.metadata.FIELDS) -        self.config = SafeConfigParser(interpolation=ExtendedInterpolation(), allow_no_value=True) -        # Include built-in Conda metadata fields -        self.config.read_dict(self.fields) -        # Include user-defined build fields -        self.config.read(self.filename) -        # Convert ConfigParser -> dict -        self.spec = self.as_dict(self.config) -         -        self.spec_metadata = {} -        for keyword in self.keywords: -            if self.spec[keyword]: -                self.spec_metadata[keyword] = self.spec[keyword]   - -        # Convert dict to YAML-compatible dict -        self.conda_metadata = self.scrub(self.spec, self.keywords) - -    def conda_write_meta(self): -        with open(os.path.join(self.env.config['meta']), 'w+') as metafile: -            yaml.safe_dump(self.conda_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 - +'''I refuse to write the same thing over and over again in meta.yaml.
 +And yeah, conda supports Jinja2, but ugh... No.
 +'''
 +
 +import os
 +import conda_build.metadata
 +import yaml
 +from configparser import SafeConfigParser, ExtendedInterpolation
 +from collections import OrderedDict
 +from .environment import Environment
 +from .exceptions import MetaDataError
 +
 +
 +class MetaData(object):
 +    def __init__(self, filename, env):
 +        
 +        if not os.path.exists(filename):
 +            raise OSError('"{0}" does not exist.'.format(filename));
 +        
 +        self.filename = filename
 +        
 +        if not isinstance(env, Environment):
 +            raise MetaDataError('Expecting instance of cbc.environment.Environment, got: "{0}"'.format(type(env)))
 +        
 +        self.env = env
 +        self.keywords = ['cbc_build', 'cbc_cgi']
 +        
 +        
 +        self.fields = self.convert_conda_fields(conda_build.metadata.FIELDS)
 +        self.config = SafeConfigParser(interpolation=ExtendedInterpolation(), allow_no_value=True)
 +        # Include built-in Conda metadata fields
 +        self.config.read_dict(self.fields)
 +        # Include user-defined build fields
 +        self.config.read(self.filename)
 +        # Convert ConfigParser -> dict
 +        self.local = self.as_dict(self.config)
 +        
 +        self.local_metadata = {}
 +        for keyword in self.keywords:
 +            if keyword in self.local:
 +                self.local_metadata[keyword] = self.local[keyword]  
 +
 +        # Convert dict to YAML-compatible dict
 +        self.conda_metadata = self.scrub(self.local, self.keywords)
 +        
 +    def run(self):
 +        self.conda_write_meta()
 +
 +    def conda_write_meta(self):
 +        with open(os.path.join(self.env.config['meta']), 'w+') as metafile:
 +            yaml.safe_dump(self.conda_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
 +
      
\ No newline at end of file diff --git a/tests/data/test.ini b/tests/data/test.ini index bf8d904..b5f6cca 100644..100755 --- a/tests/data/test.ini +++ b/tests/data/test.ini @@ -1,53 +1,53 @@ -[cgi] -local_server: true -local_port: 8888 -local_sources: /srv/conda/sources -protocol: http -url: ${cgi:protocol}://localhost:${cgi:local_port} - -[package] -name: test -version: 1.0.0 -release: 1 - -[about] -home: http://example.com/${package:name} -license: GPL -summary: ${package:name} is a test package -readme: README.md - -[source] -fn: ${package:name}-${package:version}.tar.gz -url: ${cgi:url}/${fn} - -[build] -number: 1 - - -[build_ext] -prefix: /usr/local -win_prefix: c:\anaconda3\ - - -linux: -	#find . -type f -name "*.py" -exec "sed -i 's|beep|bloop|g'" {} \; -	for x in *.py -	do -		echo "$$x" -	done -	(./configure --prefix=${prefix} ; \ -	make ; \ -	make install ) - -darwin: -	find . -type f -name "*.py" -exec "sed -i 's|blat|splat|g'" {} \; -	./configure --prefix=${prefix} -	make -	make install - -windows: -	rem oh no, windows! -	echo "ow it hurts" -	nmake /D_PREFIX=${win_prefix} -	 - +[cbc_cgi]
 +local_server: true
 +local_port: 8888
 +local_sources: /srv/conda/sources
 +protocol: http
 +url: ${cbc_cgi:protocol}://localhost:${cbc_cgi:local_port}
 +
 +[package]
 +name: test
 +version: 1.0.0
 +release: 1
 +
 +[about]
 +home: http://example.com/${package:name}
 +license: GPL
 +summary: ${package:name} is a test package
 +readme: README.md
 +
 +[source]
 +fn: ${package:name}-${package:version}.tar.gz
 +url: ${cbc_cgi:url}/${fn}
 +
 +[build]
 +number: 1
 +
 +
 +[cbc_build]
 +prefix: /usr/local
 +win_prefix: c:\anaconda3\
 +
 +
 +linux:
 +	#find . -type f -name "*.py" -exec "sed -i 's|beep|bloop|g'" {} \;
 +	for x in *.py
 +	do
 +		echo "$$x"
 +	done
 +	(./configure --prefix=${prefix} ; \
 +	make ; \
 +	make install )
 +
 +darwin:
 +	find . -type f -name "*.py" -exec "sed -i 's|blat|splat|g'" {} \;
 +	./configure --prefix=${prefix}
 +	make
 +	make install
 +
 +windows:
 +	rem oh no, windows!
 +	echo "ow it hurts"
 +	nmake /D_PREFIX=${win_prefix}
 +	
 +
 diff --git a/tests/test.py b/tests/test.py index 9041024..a6dc3aa 100644..100755 --- a/tests/test.py +++ b/tests/test.py @@ -1,68 +1,70 @@ -import nose -import nose.tools -import os -import cbc -from cbc.environment import IncompleteEnv -from cbc.meta import SpecError -import sys - - -class TestCBC(object): -    def setUp(self): -        lookup = os.path.join(os.path.dirname(__file__), 'data') -        output = os.path.join(lookup, 'output') -        os.makedirs(output, exist_ok=True) -        os.environ['CBC_HOME'] = output -        self.env = cbc.environment.Environment() -        self.ini = os.path.join(lookup, 'test.ini') -          -     -    def tearDown(self): -        pass -     -    @nose.tools.raises(OSError) -    def test_spec_does_not_exist(self): -        '''Issue non-existent INI and see what happens. -        '''  -        spec = cbc.meta.Spec('deadbeefcafe.ini', self.env) -     -    @nose.tools.raises(IncompleteEnv) -    def test_spec_incomplete_environment(self): -        '''Screw up the environment on purpose -        ''' -        del os.environ['CBC_HOME']  -        env = cbc.environment.Environment() -     -    @nose.tools.raises(SpecError) -    def test_spec_environment_instance(self): -        '''Issue the incorrect class instance as the environment -        ''' -        env = '' -        spec = cbc.meta.Spec(self.ini, env) -         -    def test_spec_standalone_build_data(self): -        spec = cbc.meta.Spec(self.ini, self.env) -        nose.tools.assert_in('build_ext', spec.spec_metadata) -         -    def test_spec_standalone_cgi_server_data(self): -        spec = cbc.meta.Spec(self.ini, self.env) -        nose.tools.assert_in('cgi', spec.spec_metadata) -     -    def test_spec_no_ini_and_yaml_crosstalk(self): -        spec = cbc.meta.Spec(self.ini, self.env) -        nose.tools.assert_not_in('build_ext', spec.conda_metadata) -        nose.tools.assert_not_in('cgi', spec.conda_metadata) - -    def test_spec_outputs_valid_conda_metadata(self): -        spec = cbc.meta.Spec(self.ini, self.env) -        spec.conda_write_meta() -        import conda_build.metadata -        meta = conda_build.metadata.MetaData(self.env.cbchome) -        nose.tools.assert_is_instance(meta, conda_build.metadata.MetaData) -        nose.tools.assert_equal(meta.dist(), 'test-1.0.0-1') -         -         -         -if __name__ == '__main__': -    sys.argv.append('--verbosity=3') +import nose
 +import nose.tools
 +import os
 +import cbc
 +from cbc.environment import IncompleteEnv
 +from cbc.meta import MetaDataError
 +import sys
 +
 +
 +class TestCBC(object):
 +    def setUp(self):
 +        lookup = os.path.join(os.path.dirname(__file__), 'data')
 +        output = os.path.join(lookup, 'output')
 +        os.makedirs(output, exist_ok=True)
 +        os.environ['CBC_HOME'] = output
 +        self.env = cbc.environment.Environment()
 +        self.ini = os.path.join(lookup, 'test.ini')
 +         
 +    
 +    def tearDown(self):
 +        pass
 +    
 +    @nose.tools.raises(OSError)
 +    def test_spec_does_not_exist(self):
 +        '''Issue non-existent INI and see what happens.
 +        ''' 
 +        spec = cbc.meta.MetaData('deadbeefcafe.ini', self.env)
 +    
 +    @nose.tools.raises(IncompleteEnv)
 +    def test_spec_incomplete_environment(self):
 +        '''Screw up the environment on purpose
 +        '''
 +        del os.environ['CBC_HOME'] 
 +        env = cbc.environment.Environment()
 +    
 +    @nose.tools.raises(MetaDataError)
 +    def test_spec_environment_instance(self):
 +        '''Issue the incorrect class instance as the environment
 +        '''
 +        env = ''
 +        cbc_meta = cbc.meta.MetaData(self.ini, env)
 +        
 +    def test_spec_standalone_build_data(self):
 +        cbc_meta = cbc.meta.MetaData(self.ini, self.env)
 +        nose.tools.assert_in('cbc_build', cbc_meta.local_metadata)
 +        
 +    def test_spec_standalone_cgi_server_data(self):
 +        cbc_meta = cbc.meta.MetaData(self.ini, self.env)
 +        nose.tools.assert_in('cbc_cgi', cbc_meta.local_metadata)
 +    
 +    def test_spec_no_ini_and_yaml_crosstalk(self):
 +        cbc_meta = cbc.meta.MetaData(self.ini, self.env)
 +        nose.tools.assert_not_in('cbc_build', cbc_meta.conda_metadata)
 +        nose.tools.assert_not_in('cbc_cgi', cbc_meta.conda_metadata)
 +
 +    def test_spec_outputs_valid_conda_metadata(self):
 +        import conda_build.metadata
 +        cbc_meta = cbc.meta.MetaData(self.ini, self.env)
 +        cbc_meta.conda_write_meta()
 +        
 +        # Test against conda's build system
 +        conda_meta = conda_build.metadata.MetaData(self.env.cbchome)
 +        nose.tools.assert_is_instance(conda_meta, conda_build.metadata.MetaData)
 +        nose.tools.assert_equal(conda_meta.dist(), 'test-1.0.0-1')
 +        
 +        
 +        
 +if __name__ == '__main__':
 +    sys.argv.append('--verbosity=3')
      nose.main(argv=sys.argv)
\ No newline at end of file | 
