Example #1
0
def copy_helper(src, dest):
    '''
    copy helper similar to ``shutil.copytree``

    But we do not require ``dest`` non-exist

    :param str src: source dir
    :param str dest: destination dir

    e.g::

        foo/
            bar.txt

        baz/
            *empty dir*

    ``copy_helper('foo', 'bar')`` will copy ``bar.txt`` as ``baz/bar.txt``.

    ..versionchanged: 1.1
    '''
    if not os.path.isdir(src):
        raise OSError('source "{0}" is not a directory'.format(src))

    setup_dir(dest, require_empty=False)

    for p in os.listdir(src):
        _src = os.path.join(src, p)
        _dest = os.path.join(dest, p)

        if os.path.isdir(_src):
            copytree(_src, _dest)
        else:
            copy2(_src, _dest)
Example #2
0
def init_basic(path):
    '''
    Generate a basic CouchApp which contain following files::

        /path/
            .couchapprc
            .couchappignore
            _attachments/
            lists/
            shows/
            updates/
            views/
    '''
    setup_dir(path, require_empty=True)

    for n in DEFAULT_APP_TREE:
        tp = os.path.join(path, n)
        os.makedirs(tp)

    fid = os.path.join(path, '_id')
    if not os.path.isfile(fid):
        with open(fid, 'wb') as f:
            f.write('_design/{0}'.format(os.path.split(path)[1]))

    localdoc.document(path, create=True)
Example #3
0
def init_basic(path):
    '''
    Generate a basic CouchApp which contain following files::

        /path/
            .couchapprc
            .couchappignore
            _attachments/
            lists/
            shows/
            updates/
            views/
    '''
    setup_dir(path, require_empty=True)

    for n in DEFAULT_APP_TREE:
        tp = os.path.join(path, n)
        os.makedirs(tp)

    fid = os.path.join(path, '_id')
    if not os.path.isfile(fid):
        with open(fid, 'wb') as f:
            f.write('_design/{0}'.format(os.path.split(path)[1]))

    localdoc.document(path, create=True)
Example #4
0
    def create(self):
        util.setup_dir(self.docdir, require_empty=False)

        rcfile = os.path.join(self.docdir, ".couchapprc")
        ignfile = os.path.join(self.docdir, ".couchappignore")
        if not os.path.isfile(rcfile):
            util.write_json(rcfile, {})
            util.write(ignfile, DEFAULT_IGNORE)
        else:
            logger.info("CouchApp already initialized in %s.", self.docdir)
Example #5
0
    def create(self):
        util.setup_dir(self.docdir, require_empty=False)

        rcfile = os.path.join(self.docdir, '.couchapprc')
        ignfile = os.path.join(self.docdir, '.couchappignore')
        if not os.path.isfile(rcfile):
            util.write_json(rcfile, {})
            util.write(ignfile, DEFAULT_IGNORE)
        else:
            logger.info("CouchApp already initialized in %s." % self.docdir)
Example #6
0
    def create(self):
        '''
        Init a dir as a couchapp.

        Only create ``.couchapprc`` and ``.couchappignore``.
        Do nothing if ``.couchapprc`` exists.
        '''
        util.setup_dir(self.docdir, require_empty=False)

        rcfile = os.path.join(self.docdir, '.couchapprc')
        ignfile = os.path.join(self.docdir, '.couchappignore')

        if not os.path.isfile(rcfile):
            util.write_json(rcfile, {})
            util.write(ignfile, DEFAULT_IGNORE)
        else:
            logger.info("CouchApp already initialized in %s.", self.docdir)
Example #7
0
    def create(self):
        '''
        Init a dir as a couchapp.

        Only create ``.couchapprc`` and ``.couchappignore``.
        Do nothing if ``.couchapprc`` exists.
        '''
        util.setup_dir(self.docdir, require_empty=False)

        rcfile = os.path.join(self.docdir, '.couchapprc')
        ignfile = os.path.join(self.docdir, '.couchappignore')

        if not os.path.isfile(rcfile):
            util.write_json(rcfile, {})
            util.write(ignfile, DEFAULT_IGNORE)
        else:
            logger.info("CouchApp already initialized in %s.", self.docdir)
Example #8
0
def generate_vendor(path, name, template='default'):
    '''
    Generate vendor from template set

    :param path: the app dir
    :param name: the vendor name
    :param template: the template set
    '''
    tmpl_name = os.path.join(*template.split('/'))
    tmpl_dir = find_template_dir(tmpl_name, tmpl_type='vendor',
                                 raise_error=True)

    vendor_dir = os.path.join(path, 'vendor')
    setup_dir(vendor_dir)

    copy_helper(tmpl_dir, vendor_dir)

    logger.debug('vendor dir "%s"', tmpl_dir)
    logger.info('vendor "%s" generated successfully', name)
Example #9
0
def init_basic(path):
    '''
    Generate a basic CouchApp which contain following files::

        /path/
            .couchapprc
            .couchappignore
            _attachments/
            lists/
            shows/
            updates/
            views/

    .. versionadded:: 1.1
    '''
    setup_dir(path, require_empty=True)
    setup_dirs(os.path.join(path, n) for n in DEFAULT_APP_TREE)

    save_id(path, '_design/{0}'.format(os.path.split(path)[-1]))
    localdoc.document(path, create=True)
Example #10
0
def init_template(path, template=None):
    '''
    Generates a CouchApp via template
    '''
    TEMPLATES = ['app']
    prefix = os.path.join(*template.split('/')) if template is not None else ''

    setup_dir(path, require_empty=True)

    for n in DEFAULT_APP_TREE:
        tp = os.path.join(path, n)
        os.makedirs(tp)

    for t in TEMPLATES:
        appdir = path
        if prefix:
            # we do the job twice for now to make sure an app or vendor
            # template exist in user template location
            # fast on linux since there is only one user dir location
            # but could be a little slower on windows
            for user_location in user_path():
                location = os.path.join(user_location, 'templates', prefix, t)
                if os.path.exists(location):
                    t = os.path.join(prefix, t)
                    break

        copy_helper(appdir, t)

    # add vendor
    vendor_dir = os.path.join(appdir, 'vendor')
    os.makedirs(vendor_dir)
    copy_helper(vendor_dir, '', tname="vendor")

    fid = os.path.join(appdir, '_id')
    if not os.path.isfile(fid):
        with open(fid, 'wb') as f:
            f.write('_design/{0}'.format(os.path.split(appdir)[1]))

    localdoc.document(path, create=True)
Example #11
0
def init_template(path, template=None):
    '''
    Generates a CouchApp via template
    '''
    TEMPLATES = ['app']
    prefix = os.path.join(*template.split('/')) if template is not None else ''

    setup_dir(path, require_empty=True)

    for n in DEFAULT_APP_TREE:
        tp = os.path.join(path, n)
        os.makedirs(tp)

    for t in TEMPLATES:
        appdir = path
        if prefix:
            # we do the job twice for now to make sure an app or vendor
            # template exist in user template location
            # fast on linux since there is only one user dir location
            # but could be a little slower on windows
            for user_location in user_path():
                location = os.path.join(user_location, 'templates', prefix, t)
                if os.path.exists(location):
                    t = os.path.join(prefix, t)
                    break

        copy_helper(appdir, t)

    # add vendor
    vendor_dir = os.path.join(appdir, 'vendor')
    os.makedirs(vendor_dir)
    copy_helper(vendor_dir, '', tname="vendor")

    fid = os.path.join(appdir, '_id')
    if not os.path.isfile(fid):
        with open(fid, 'wb') as f:
            f.write('_design/{0}'.format(os.path.split(appdir)[1]))

    localdoc.document(path, create=True)
Example #12
0
 def main(self):
     setup_dir(self.tmpfile.name)
Example #13
0
 def test(self, mkdir):
     setup_dir(self.tmpdir, require_empty=False)
     assert not mkdir.called
Example #14
0
 def main(self):
     setup_dir(self.tmpdir, require_empty=True)
Example #15
0
def test_setup_dir(mkdir):
    setup_dir('/mock')
    assert mkdir.called
Example #16
0
def generate_function(path, func_type, name, template='default'):
    '''
    Generate function from template

    :param path: the app dir
    :param func_type: function type. e.g. ``view``, ``show``.
    :param name: the function name
    :param template: the template set

    The big picture of template dir is discribed
    in :py:func:`~couchapp.generate.init_template`.

    Here we show the detail structure of ``functions`` dir::

        functions/
            filter.js
            list.js
            map.js
            reduce.js
            show.js
            spatial.js
            update.js
            validate_doc_update.js
            ...
            myfunc.js
    '''
    tmpl_name = os.path.join(*template.split('/'))
    tmpl_dir = find_template_dir(tmpl_name, tmpl_type='functions',
                                 raise_error=True)

    file_list = []  # [(src, dest), ...]
    empty_dir = False
    if func_type == 'view':
        dir_ = os.path.join(path, 'views', name)
        empty_dir = True
        file_list.append(('map.js', 'map.js'))
        file_list.append(('reduce.js', 'reduce.js'))

    elif func_type in ('filter', 'list', 'show', 'update'):
        dir_ = os.path.join(path, '{0}s'.format(func_type))
        file_list.append(('{0}.js'.format(func_type),
                          '{0}.js'.format(name)))

    elif func_type == 'function':  # user defined function
        dir_ = path
        file_list.append(('{0}.js'.format(name),
                          '{0}.js'.format(name)))

    elif func_type == 'spatial':
        dir_ = os.path.join(path, 'spatial')
        file_list.append(('spatial.js', '{0}.js'.format(name)))

    elif func_type == 'validate_doc_update':
        dir_ = path
        file_list.append(('validate_doc_update.js', 'validate_doc_update.js'))

    else:
        raise AppError('unrecognized function type "{0}"'.format(func_type))

    setup_dir(dir_, require_empty=empty_dir)

    for src, dest in file_list:
        full_src = os.path.join(tmpl_dir, src)
        full_dest = os.path.join(dir_, dest)

        try:
            copy2(full_src, full_dest)
        except Error:
            logger.warning('function "%s" not found in "%s"', src, tmpl_dir)
        else:
            logger.debug('function "%s" generated successfully', dest)

    logger.info('enjoy the %s function, "%s"!', func_type, name)