예제 #1
0
파일: food.py 프로젝트: martinb3/fastfood
def create_new_cookbook(cookbook_name, cookbooks_home):
    """Create a new cookbook.

    :param cookbook_name: Name of the new cookbook.
    :param cookbooks_home: Target dir for new cookbook.
    """
    cookbooks_home = utils.normalize_path(cookbooks_home)

    if not os.path.exists(cookbooks_home):
        raise ValueError("Target cookbook dir %s does not exist."
                         % os.path.relpath(cookbooks_home))

    target_dir = os.path.join(cookbooks_home, cookbook_name)
    LOG.debug("Creating dir -> %s", target_dir)
    try:
        os.makedirs(target_dir)
    except OSError as err:
        if err.errno != errno.EEXIST:
            raise
        else:
            LOG.info("Skipping existing directory %s", target_dir)

    cookbook_path = os.path.join(cookbooks_home, cookbook_name)
    cookbook = book.CookBook(cookbook_path)

    return cookbook
예제 #2
0
 def __init__(self, path):
     """Initialize CookBook wrapper at 'path'."""
     self.path = utils.normalize_path(path)
     self._metadata = None
     if not os.path.isdir(path):
         raise ValueError("Cookbook dir %s does not exist."
                          % self.path)
     self._berksfile = None
예제 #3
0
 def __init__(self, path):
     self.path = utils.normalize_path(path)
     self._metadata = None
     if not os.path.isdir(path):
         raise ValueError("Cookbook dir %s does not exist." % self.path)
     self.metadata_path = os.path.join(self.path, 'metadata.rb')
     if not os.path.isfile(self.metadata_path):
         raise ValueError("Cookbook needs metadata.rb, %s" %
                          self.metadata_path)
     self._berksfile = None
     self.berks_path = os.path.join(self.path, 'Berksfile')
예제 #4
0
파일: pack.py 프로젝트: erulabs/fastfood
 def __init__(self, path):
     """Initialize, asserting templatepack path and manifest."""
     self._manifest = None
     self._stencil_sets = {}
     self.path = utils.normalize_path(path)
     if not os.path.isdir(self.path):
         raise ValueError("Templatepack dir %s does not exist." % self.path)
     self.manifest_path = os.path.join(self.path, 'manifest.json')
     if not os.path.isfile(self.manifest_path):
         raise ValueError("Templatepack needs manifest file, %s" %
                          os.path.relpath(self.manifest_path))
     self._validate('api', cls=int)
     self._validate('base', cls=dict)
예제 #5
0
파일: stencil.py 프로젝트: erulabs/fastfood
    def __init__(self, path):
        # assign these attrs early
        self._manifest = None
        self._stencils = {}

        self.path = utils.normalize_path(path)
        if not os.path.isdir(path):
            raise ValueError("Stencil Set dir %s does not exist." % path)
        self.manifest_path = os.path.join(self.path, 'manifest.json')
        if not os.path.isfile(self.manifest_path):
            raise ValueError("Stencil Set needs manifest file, %s"
                             % self.manifest_path)
        self._validate('api', cls=int)
        self._validate('default_stencil', cls=basestring)
예제 #6
0
파일: pack.py 프로젝트: martinb3/fastfood
 def __init__(self, path):
     """Initialize, asserting templatepack path and manifest."""
     self._manifest = None
     # for caching Stencil instances
     self._stencil_sets = {}
     self.path = utils.normalize_path(path)
     if not os.path.isdir(self.path):
         raise ValueError("Templatepack dir %s does not exist."
                          % self.path)
     self.manifest_path = os.path.join(self.path, 'manifest.json')
     if not os.path.isfile(self.manifest_path):
         raise ValueError("Templatepack needs manifest file, %s"
                          % os.path.relpath(self.manifest_path))
     self._validate('api', cls=int)
     self._validate('stencil_sets', cls=dict)
예제 #7
0
    def __init__(self, path):
        # assign these attrs early
        self._manifest = None
        self._stencils = {}

        self.path = utils.normalize_path(path)
        if not os.path.isdir(path):
            raise exc.FastfoodStencilSetInvalidPath(
                "Stencil Set dir %s does not exist." % path)
        self.manifest_path = os.path.join(self.path, 'manifest.json')
        if not os.path.isfile(self.manifest_path):
            raise exc.FastfoodStencilSetMissingManifest(
                "Stencil Set needs manifest file, %s"
                % self.manifest_path)
        self._validate('api', cls=int)
        self._validate('default_stencil', cls=basestring)
예제 #8
0
파일: food.py 프로젝트: erulabs/fastfood
def create_new_cookbook(cookbook_name,
                        templatepack,
                        cookbooks_home,
                        force=False):
    """Create a new cookbook.

    :param cookbook_name: Name of the new cookbook.
    :param templatepack: Path to templatepack.
    :param cookbooks_home: Target dir for new cookbook.

    TODO:
        return something, like files added or path to new cookbook or both
    """
    cookbooks_home = utils.normalize_path(cookbooks_home)

    tmppack = pack.TemplatePack(templatepack)
    tpmanifest = tmppack.manifest

    files, directories = [], []
    for option, data in tpmanifest['base'].iteritems():
        if option == 'files':
            if not isinstance(data, list):
                raise TypeError("Base files should be a list of files.")
            files = data
        elif option == 'directories':
            if not isinstance(data, list):
                raise TypeError("Base directories should be a list of "
                                "directory names.")
            directories = data
        else:
            raise ValueError("Unknown 'base' option '%s'" % option)

    if files:
        if not os.path.exists(cookbooks_home):
            raise ValueError("Target cookbook dir %s does not exist." %
                             os.path.relpath(cookbooks_home))

    template_map = {
        'cookbook': {
            'name': cookbook_name,
            'year': datetime.datetime.now().year,
        }
    }

    template_files = [os.path.join(templatepack, 'base', f) for f in files]
    path_map = dict(zip(template_files, files))
    filetable = templating.render_templates(*template_files, **template_map)

    for path in directories:
        target_dir = os.path.join(cookbooks_home, cookbook_name, path)
        LOG.debug("Creating dir -> %s", target_dir)
        try:
            os.makedirs(target_dir)
        except OSError as err:
            if err.errno != errno.EEXIST:
                raise
            else:
                LOG.info("Skipping existing directory %s", target_dir)

    written_files = []
    for orig_path, content in filetable:
        target_path = os.path.join(cookbooks_home, cookbook_name,
                                   path_map[orig_path])
        needdir = os.path.dirname(target_path)
        assert needdir, "Target should have valid parent dir"
        try:
            os.makedirs(needdir)
        except OSError as err:
            if err.errno != errno.EEXIST:
                raise

        if os.path.isfile(target_path):
            if force:
                LOG.warning("Forcing overwrite of existing file %s.",
                            target_path)
            else:
                LOG.info("Skipping existing file %s", target_path)
                continue
        with open(target_path, 'w') as newfile:
            LOG.info("Writing rendered file %s", target_path)
            written_files.append(target_path)
            newfile.write(content)

    return written_files