~fabricematrat/charmworld/redirect-charm

« back to all changes in this revision

Viewing changes to charmworld/models.py

  • Committer: Brad Crittenden
  • Date: 2014-04-21 14:40:48 UTC
  • mto: This revision was merged to the branch mainline in revision 503.
  • Revision ID: bac@canonical.com-20140421144048-hk7xt13j1lcv2cir
Unrefactored a function because YAGNI.  Added an explicit test for a newly refactored function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
__metaclass__ = type
6
6
 
7
7
from calendar import timegm
8
 
from copy import deepcopy
9
8
import datetime
10
9
import hashlib
11
10
import itertools
1891
1890
    Constraints are space-separated entities, not comma-separated.  As
1892
1891
    bundle_data is a dictionary, the changes occur in place.
1893
1892
    """
1894
 
    original_bundle_data = deepcopy(bundle_data)
1895
1893
    services = bundle_data.get('services')
1896
1894
    if services:
1897
1895
        for service, service_data in services.items():
1901
1899
                pairs = ['{}={}'.format(k, v)
1902
1900
                         for k, v in sorted(parsed.items())]
1903
1901
                service_data['constraints'] = ' '.join(pairs)
1904
 
    return original_bundle_data != bundle_data
1905
1902
 
1906
1903
 
1907
1904
def make_constraints_space_separated(basket):
1910
1907
    :input: basket - The YAML string representing the bundles.yaml file.
1911
1908
    :returns: Manipulated YAML string.
1912
1909
    """
1913
 
    # Convert constraints to space separated, whether it needs it or not.
 
1910
    # Convert constraints to space-separated, whether it needs it or not.
1914
1911
    data = yaml.load(basket)
1915
 
    for _, bundle_data in data.items():
 
1912
    for bundle_data in data.values():
1916
1913
        convert_bundle_constraints(bundle_data)
1917
1914
    return yaml.dump(data, default_flow_style=False)
1918
1915
 
1919
1916
 
1920
1917
def save_file(fs, bytes_, content_type=None):
 
1918
    """Save the bytes to the file system."""
1921
1919
    if content_type is None:
1922
1920
        content_type = magic.from_buffer(bytes_, mime=True)
1923
1921
 
1960
1958
        elif path == 'bundles.yaml':
1961
1959
            bytes_ = make_constraints_space_separated(bytes_)
1962
1960
 
1963
 
        hashes[path] = save_file(fs, bytes_, content_type)
 
1961
        _id = hashlib.sha256(bytes_).hexdigest()
 
1962
        if not fs.exists(_id):
 
1963
            fs.put(bytes_, contentType=content_type, _id=_id)
 
1964
        hashes[path] = _id
1964
1965
    return hashes
1965
1966
 
1966
1967