def find_template_dir(name, directory=''): paths = ['%s' % name, '../%s' % name] if hasattr(sys, 'frozen'): # py2exe modpath = sys.executable else: modpath = __file__ default_locations = [os.path.join(os.path.dirname(modpath), p, directory) for p in paths] if directory: user_locations = [] for user_location in user_path(): user_locations.append(os.path.join(user_location, name, directory)) default_locations = user_locations + default_locations found = False for location in default_locations: template_dir = os.path.normpath(location) if os.path.isdir(template_dir): found = True break if found: return template_dir return False
def find_template_dir(name, directory=''): paths = ['%s' % name, os.path.join('..', name)] if hasattr(sys, 'frozen'): # py2exe modpath = sys.executable elif sys.platform == "win32" or os.name == "nt": modpath = os.path.join(sys.prefix, "Lib", "site-packages", "couchapp", "templates") else: modpath = __file__ default_locations = [os.path.join(os.path.dirname(modpath), p, directory) for p in paths] if directory: user_locations = [] for user_location in user_path(): user_locations.append(os.path.join(user_location, name, directory)) default_locations = user_locations + default_locations found = False for location in default_locations: template_dir = os.path.normpath(location) if os.path.isdir(template_dir): found = True break if found: return template_dir return False
def generate_app(path, template=None, create=False): """ Generates a CouchApp in app_dir :attr verbose: boolean, default False :return: boolean, dict. { 'ok': True } if ok, { 'ok': False, 'error': message } if something was wrong. """ TEMPLATES = ["app"] prefix = "" if template is not None: prefix = os.path.join(*template.split("/")) try: os.makedirs(path) except OSError as e: errno, message = e raise AppError("Can't create a CouchApp in %s: %s" % (path, message)) 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/%s" % os.path.split(appdir)[1]) if create: localdoc.document(path, create=True) logger.info("%s generated." % path)
def find_template_dir(name, directory=''): paths = ['%s' % name, os.path.join('..', name)] if hasattr(sys, 'frozen'): # py2exe modpath = sys.executable elif sys.platform == "win32" or os.name == "nt": modpath = os.path.join(sys.prefix, "Lib", "site-packages", "couchapp", "templates") else: modpath = __file__ if sys.platform != "win32" and os.name != "nt": default_locations = [ "/usr/share/couchapp/templates/%s" % directory, "/usr/local/share/couchapp/templates/%s" % directory, "/opt/couchapp/templates/%s" % directory ] else: default_locations = [] default_locations.extend( [os.path.join(os.path.dirname(modpath), p, directory) for p in paths]) if sys.platform == "darwin": home = os.path.expanduser('~'), data_path = "%s/Library/Application Support/Couchapp" % home default_locations.extend(["%s/%s/%s" % (data_path, p, directory) \ for p in paths]) if directory: for user_location in user_path(): default_locations.append( os.path.join(user_location, name, directory)) found = False for location in default_locations: template_dir = os.path.normpath(location) if os.path.isdir(template_dir): found = True break if found: return template_dir return False
def find_template_dir(name, directory=''): paths = ['%s' % name, os.path.join('..', name)] if hasattr(sys, 'frozen'): # py2exe modpath = sys.executable elif sys.platform == "win32" or os.name == "nt": modpath = os.path.join(sys.prefix, "Lib", "site-packages", "couchapp", "templates") else: modpath = __file__ if sys.platform != "win32" and os.name != "nt": default_locations = [ "/usr/share/couchapp/templates/%s" % directory, "/usr/local/share/couchapp/templates/%s" % directory, "/opt/couchapp/templates/%s" % directory] else: default_locations = [] default_locations.extend([os.path.join(os.path.dirname(modpath), p, directory) for p in paths]) if sys.platform == "darwin": home = os.path.expanduser('~'), data_path = "%s/Library/Application Support/Couchapp" % home default_locations.extend(["%s/%s/%s" % (data_path, p, directory) for p in paths]) if directory: for user_location in user_path(): default_locations.append(os.path.join(user_location, name, directory)) found = False for location in default_locations: template_dir = os.path.normpath(location) if os.path.isdir(template_dir): found = True break if found: return template_dir return False
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)
except OSError, e: errno, message = e raise AppError("Can't create a CouchApp in %s: %s" % (path, message)) 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', 'couchapp') 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:
except OSError, e: errno, message = e raise AppError("Can't create a CouchApp in %s: %s" % (path, message)) 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:
def find_template_dir(tmpl_name='default', tmpl_type='', raise_error=False): ''' Find template dir for different platform :param tmpl_name: The template name under ``templates``. It can be empty string. If it is set to ``default``, we will use consider the tmpl_name as empty. e.g. ``mytmpl`` mentioned in the docstring of :py:func:`~couchapp.generate.init_template` :param tmpl_type: the type of template. e.g. 'app', 'functions', 'vendor' :param bool raise_error: raise ``AppError`` if not found :return: the absolute path or ``None`` if not found We will check the ``<search path>/templates/<tmpl_name>/<tmpl_type>`` is dir or not. The first matched win. For posix platform, the search locations are following: - ~/.couchapp/ - <module dir path>/ - <module dir path>/../ - /usr/share/couchapp/ - /usr/local/share/couchapp/ - /opt/couchapp/ For darwin (OSX) platform, we have some extra search locations: - ${HOME}/Library/Application Support/Couchapp/ For windows with standlone binary (py2exe): - <executable dir path>/ - <executable dir path>/../ For windows with python interpreter: - ${USERPROFILE}/.couchapp/ - <module dir path>/ - <module dir path>/../ - <python prefix>/Lib/site-packages/couchapp/ ..versionchanged:: 1.1 ''' if tmpl_type and tmpl_type not in TEMPLATE_TYPES: raise AppError('invalid template type "{0}"'.format(tmpl_type)) if tmpl_name == 'default': tmpl_name = '' modpath = os.path.dirname(__file__) search_paths = user_path() + [ modpath, os.path.join(modpath, '..'), ] if os.name == 'posix': search_paths.extend([ '/usr/share/couchapp', '/usr/local/share/couchapp', '/opt/couchapp', ]) elif is_py2exe(): search_paths.append(os.path.dirname(sys.executable)) elif is_windows(): search_paths.append( os.path.join(sys.prefix, 'Lib', 'site-packages', 'couchapp') ) # extra path for darwin if sys.platform.startswith('darwin'): search_paths.append( os.path.expanduser('~/Library/Application Support/Couchapp') ) # the first win! for path in search_paths: path = os.path.normpath(path) path = os.path.join(path, 'templates', tmpl_name, tmpl_type) if os.path.isdir(path): logger.debug('template path match: "{0}"'.format(path)) return path logger.debug('template search path: "{0}" not found'.format(path)) if raise_error: logger.info('please use "-d" to checkout search paths.') raise AppError('template "{0}/{1}" not found.'.format( tmpl_name, tmpl_type)) return None