def _save(self, path=None): try: data = request.form['data'] # Newline fix (remove cr) data = data.replace('\r', '').rstrip() fullpath = git_absolute_path(path) os.makedirs(os.path.dirname(fullpath), exist_ok=True) with open(fullpath, 'w') as f: f.write(data) f.write('\n') return jsonify({'path': normalize_path(path)}) except Exception as ex: logging.error(ex) if hasattr(ex, 'strerror'): message = ex.strerror elif hasattr(ex, 'message'): message = ex.message else: message = str(ex) return jsonify({ 'path': normalize_path(path), 'error': { 'message': 'Error saving {path}: {message}'.format(path=path, message=message) }, })
def _save(self, path=None): try: data = request.form["data"] # Newline fix (remove cr) data = data.replace("\r", "").rstrip() fullpath = git_absolute_path(path) os.makedirs(os.path.dirname(fullpath), exist_ok=True) with open(fullpath, "w") as f: f.write(data) f.write("\n") return prepare_api_response(path=normalize_path(path)) except Exception as ex: logging.error(ex) return prepare_api_response( path=normalize_path(path), error_message="Error saving {path}: {message}".format( path=path, message=error_message(ex)), )
def get_tree(path: Optional[str] = None) -> List[Dict[str, Any]]: " Get tree nodes " if not path: root = None path_argv = None else: splitted_path = path.split('/', 1) root = splitted_path.pop(0) path_argv = normalize_path( splitted_path.pop(0) if splitted_path else None) if root in TREE_NODES: return TREE_NODES[root](path_argv) else: return []
def get_tree(path: Path = None, args: Args = {}) -> TreeOutput: "Get tree nodes at the given path" if not path: root = None path_argv = None else: splitted_path = path.split('/', 1) root = splitted_path.pop(0) path_argv = normalize_path( splitted_path.pop(0) if splitted_path else None) if root not in TREE_NODES: return [] # Check condition if not TREE_NODES[root].condition(): return [] # Execute node function return TREE_NODES[root].get_children(path_argv, args)
def _editor(self, session=None, path=None): path = normalize_path(path) try: code = None # Display import error for ie in session.query(ImportError).all(): if ie.filename == path: flash( 'Broken DAG: [{ie.filename}] {ie.stacktrace}'.format( ie=ie), 'dag_import_error') # Load or Save DAG if 'code' in request.form: code = self._save(path) else: code = self._load(path) finally: return self._render('editor', code=code, path=path, back=ROUTE, root=request.args.get('root'))
def _load(self, path): " Send the contents of a file to the client " try: path = normalize_path(path) if path.startswith('~git/'): # Download git blob - path = '~git/<hash>/<name>' _, path, filename = path.split('/', 3) response = execute_git_command(["cat-file", "-p", path]) response.headers['Content-Disposition'] = ( 'attachment; filename="%s"' % filename) try: content_type = mimetypes.guess_type(filename)[0] if content_type: response.headers['Content-Type'] = content_type except Exception: pass return response else: # Download file fullpath = git_absolute_path(path) return send_file(fullpath, as_attachment=True) except Exception as ex: logging.error(ex) abort(HTTP_404_NOT_FOUND)
def test_normalize_path(self): self.assertEqual(normalize_path(None), '') self.assertEqual(normalize_path('/'), '') self.assertEqual(normalize_path('/../'), '') self.assertEqual(normalize_path('../'), '') self.assertEqual(normalize_path('../../'), '') self.assertEqual(normalize_path('../..'), '') self.assertEqual(normalize_path('/..'), '') self.assertEqual(normalize_path('//'), '') self.assertEqual(normalize_path('////../'), '') self.assertEqual(normalize_path('..///'), '') self.assertEqual(normalize_path('..///../'), '') self.assertEqual(normalize_path('..///..'), '') self.assertEqual(normalize_path('//..'), '') self.assertEqual(normalize_path('/aaa'), 'aaa') self.assertEqual(normalize_path('/../aaa'), 'aaa') self.assertEqual(normalize_path('../aaa'), 'aaa') self.assertEqual(normalize_path('../../aaa'), 'aaa') self.assertEqual(normalize_path('../../aaa'), 'aaa') self.assertEqual(normalize_path('/../aaa'), 'aaa') self.assertEqual(normalize_path('/aaa'), 'aaa') self.assertEqual(normalize_path('aaa'), 'aaa')