def copy_helper(path, directory): """ copy helper used to generate an app""" templatedir = find_template_dir(directory) if templatedir: if directory == "vendor": path = os.path.join(path, directory) try: os.makedirs(path) except: pass for root, dirs, files in os.walk(templatedir): rel = relpath(root, templatedir) if rel == ".": rel = "" target_path = os.path.join(path, rel) for d in dirs: try: os.makedirs(os.path.join(target_path, d)) except: continue for f in files: shutil.copy2(os.path.join(root, f), os.path.join(target_path, f)) else: raise AppError("Can't create a CouchApp in %s: default template not found." % ( path))
def replace_url(mo): """ make sure urls are relative to css path """ css_url = mo.group(0)[4:].strip(")").replace("'", "").replace('"','') css_path = os.path.join(os.path.dirname(src_fpath), css_url) rel_path = relpath(css_path, fname_dir) return "url(%s)" % rel_path
def _process_attachments(self, path, vendor=None): """ the function processing directory to yeld attachments. """ if os.path.isdir(path): for root, dirs, files in os.walk(path): for dirname in dirs: if dirname.startswith('.'): dirs.remove(dirname) elif self.check_ignore(dirname): dirs.remove(dirname) if files: for filename in files: if filename.startswith('.'): continue elif self.check_ignore(filename): continue else: filepath = os.path.join(root, filename) name = relpath(filepath, path) if vendor is not None: name = os.path.join('vendor', vendor, name) name = _replace_backslash(name) yield (name, filepath)
def dir_to_fields(self, current_dir='', depth=0, manifest=[]): """ process a directory and get all members """ fields={} if not current_dir: current_dir = self.docdir for name in os.listdir(current_dir): current_path = os.path.join(current_dir, name) rel_path = _replace_backslash(relpath(current_path, self.docdir)) if name.startswith("."): continue elif self.check_ignore(name): continue elif depth == 0 and name.startswith('_'): # files starting with "_" are always "special" continue elif name == '_attachments': continue elif depth == 0 and (name == 'couchapp' or name == 'couchapp.json'): # we are in app_meta if name == "couchapp": manifest.append('%s/' % rel_path) content = self.dir_to_fields(current_path, depth=depth+1, manifest=manifest) else: manifest.append(rel_path) content = self.ui.read_json(current_path) if not isinstance(content, dict): content = { "meta": content } if 'signatures' in content: del content['signatures'] if 'manifest' in content: del content['manifest'] if 'objects' in content: del content['objects'] if 'length' in content: del content['length'] if 'couchapp' in fields: fields['couchapp'].update(content) else: fields['couchapp'] = content elif os.path.isdir(current_path): manifest.append('%s/' % rel_path) fields[name] = self.dir_to_fields(current_path, depth=depth+1, manifest=manifest) else: if self.ui.verbose >= 2: self.ui.logger.info("push %s" % rel_path) content = '' if name.endswith('.json'): try: content = self.ui.read_json(current_path) except ValueError: if self.ui.verbose >= 2: self.ui.logger.error("Json invalid in %s" % current_path) else: try: content = self.ui.read(current_path) except UnicodeDecodeError, e: self.ui.logger.error("%s isn't encoded in utf8" % current_path) content = self.ui.read(current_path, utf8=False) try: content.encode('utf-8') except UnicodeError, e: self.ui.logger.error("plan B didn't work, %s is a binary" % current_path) self.ui.logger.error("use plan C: encode to base64") content = "base64-encoded;%s" % base64.b64encode(content) # remove extension name, ext = os.path.splitext(name) if name in fields and ext in ('.txt'): if self.ui.verbose >= 2: self.ui.logger.error("%(name)s is already in properties. Can't add (%(name)s%(ext)s)" % { "name": name, "ext": ext }) else: manifest.append(rel_path) fields[name] = content