def fancylistDirs(self, dir, key, listfiles, all=False): dir = urllib.unquote(dir) realdir = realpath(self.config, dir) if not realdir: raise NameError('Could not load directory %s: Permission denied' % dir) fileList = [] dirList = [] i = 0 ldir = sorted(os.listdir(realdir), key=str.lower) for f in self._filterPathList(ldir): if f.startswith('.') and not all: # do not display this file/folder continue ff = os.path.join(dir, f) realfile = os.path.join(realdir, f) identity = "%s%s" % (key, i) if os.path.isdir(realfile): dirList.append({"title": f, "key": identity, "folder":True, "lazy":True, "path": ff}) elif listfiles: regex = re.compile("(^.*)\.(.*)", re.VERBOSE) ext = regex.sub(r'\2', f) if ext == f: ext = "" fileList.append({"title": f, "key": identity, "extraClasses": "ext_"+ext, "path": ff}) i+=1 return (dirList + fileList)
def getProjectDiff(): path = realpath(app.config, request.form['project']) if path: return jsonify(code=1, result=getDiff(path)) else: return jsonify(code=0, result="Error: No such file or directory. PERMISSION DENIED!")
def listDirs(self, dir, all=False): """List elements of directory 'dir' taken""" html = 'var gsdirs = [], gsfiles = [];' dir = urllib.unquote(dir) # XXX-Marco 'dir' and 'all' should not shadow builtin names realdir = realpath(self.config, dir) if not realdir: raise NameError('Could not load directory %s: Permission denied' % dir) ldir = sorted(os.listdir(realdir), key=str.lower) for f in self._filterPathList(ldir): if f.startswith('.') and not all: # do not display this file/folder continue ff = os.path.join(dir, f) realfile = os.path.join(realdir, f) mdate = datetime.datetime.fromtimestamp(os.path.getmtime(realfile) ).strftime("%Y-%d-%m %I:%M") md5sum = md5.md5(realfile).hexdigest() if not os.path.isdir(realfile): size = os.path.getsize(realfile) regex = re.compile("(^.*)\.(.*)", re.VERBOSE) ext = regex.sub(r'\2', f) if ext == f: ext = "unknow" else: ext = str.lower(ext) html += 'gsfiles.push(new gsItem("1", "%s", "%s", "%s", "%s", "%s", "%s"));' % (f, ff, size, md5sum, ext, mdate) else: html += 'gsdirs.push(new gsItem("2", "%s", "%s", "0", "%s", "dir", "%s"));' % (f, ff, md5sum, mdate) return html
def copyItem(self, dir, files, del_source=False): """Copy a list of files or directory to dir""" realdir = self._realdir(dir) lfiles = urllib.unquote(files).split(',,,') try: # XXX-Marco do not shadow 'file' for file in lfiles: realfile = realpath(self.config, file) if not realfile: raise NameError('Could not load file or directory %s: Permission denied' % file) #prepare destination file details = realfile.split('/') dest = os.path.join(realdir, details[-1]) if os.path.exists(dest): raise NameError('NOT ALLOWED OPERATION : File or directory already exists') if os.path.isdir(realfile): shutil.copytree(realfile, dest) if del_source: shutil.rmtree(realfile) else: shutil.copy(realfile, dest) if del_source: os.unlink(realfile) except Exception as e: return str(e) return "{result: '1'}"
def saveFileContent(): file_path = realpath(app.config, request.form['file'], False) if file_path: with open(file_path, 'w') as f: f.write(request.form['content'].encode("utf-8")) return jsonify(code=1, result="") else: return jsonify(code=0, result="Rejected!! Cannot access to this location.")
def getProjectStatus(): path = realpath(app.config, request.form['project']) if path: try: result, branch, isdirty = gitStatus(path) return jsonify(code=1, result=result, branch=branch, dirty=isdirty) except GitCommandError, e: return jsonify(code=0, result=safeResult(str(e)))
def cloneRepository(): path = realpath(app.config, request.form['name'], False) if not path: return jsonify(code=0, result="Can not access folder: Permission Denied") try: cloneRepo(request.form['repo'], path, request.form['user'], request.form['email']) return jsonify(code=1, result="") except GitCommandError, e: return jsonify(code=0, result=safeResult(str(e)))
def checkFileType(): path = realpath(app.config, request.form['path']) if not path: return jsonify(code=0, result="Can not open file: Permission Denied!") if isText(path): return jsonify(code=1, result="text") else: return jsonify(code=0, result="Can not open a binary file, please select a text file!")
def getmd5sum(): realfile = realpath(app.config, request.form['file']) if not realfile: return jsonify(code=0, result="Can not open file: Permission Denied!") md5 = md5sum(realfile) if md5: return jsonify(code=1, result=md5) else: return jsonify(code=0, result="Can not get md5sum for this file!")
def rename(self, dir, filename, newfilename): """Rename file or directory to dir/filename""" realdir = self._realdir(dir) realfile = realpath(self.config, urllib.unquote(filename)) if not realfile: raise NameError('Could not load directory %s: Permission denied' % filename) tofile = os.path.join(realdir, newfilename) if not os.path.exists(tofile): os.rename(realfile, tofile) return "{result: '1'}" raise NameError('NOT ALLOWED OPERATION : File or directory already exists')
def changeBranch(): path = realpath(app.config, request.form['project']) if path: try: if switchBranch(path, request.form['name']): return jsonify(code=1, result="") else: json = "This is already your active branch for this project" return jsonify(code=1, result=json) except GitCommandError, e: return jsonify(code=0, result=safeResult(str(e)))
def readFile(self, dir, filename, truncate=False): """Read file dir/filename and return content""" realfile = realpath(self.config, os.path.join(urllib.unquote(dir), urllib.unquote(filename))) if not realfile: raise NameError('Could not load directory %s: Permission denied' % dir) if not isText(realfile): return "FILE ERROR: Cannot display binary file, please open a text file only!" if not truncate: return open(realfile).read() else: return tail(open(realfile), 0)
def createFile(): path = realpath(app.config, request.form['file'], False) if not path: return jsonify(code=0, result="Error when creating your %s: Permission Denied" % request.form['type']) try: if request.form['type'] == "file": open(path, 'w') else: os.mkdir(path) return jsonify(code=1, result="") except Exception as e: return jsonify(code=0, result=str(e))
def getFileContent(): file_path = realpath(app.config, request.form['file']) if file_path: if not isText(file_path): return jsonify(code=0, result="Can not open a binary file, please select a text file!") if 'truncate' in request.form: content = tail(codecs.open(file_path, "r", "utf_8"), int(request.form['truncate'])) return jsonify(code=1, result=content) else: return jsonify(code=1, result=codecs.open(file_path, "r", "utf_8").read()) else: return jsonify(code=0, result="Error: No such file!")
def newBranch(): path = realpath(app.config, request.form['project']) if path: try: if request.form['create'] == '1': result = addBranch(path, request.form['name']) else: result = addBranch(path, request.form['name'], True) if result: return jsonify(code=1, result="") else: return jsonify(code=0, result="Failed to checkout to branch %s.") except GitCommandError, e: return jsonify(code=0, result=safeResult(str(e)))
def getPath(): files = request.form['file'].split('#') list = [] for p in files: path = realpath(app.config, p) if not path: list = [] break else: list.append(path) realfile = '#'.join(list) if not realfile: return jsonify(code=0, result="Can not access to this file: Permission Denied!") else: return jsonify(code=1, result=realfile)
def getFileLog(): logfile = request.form.get('filename', '').encode('utf-8') if logfile == "instance.log": file_path = app.config['instance_log'] elif logfile == "software.log": file_path = app.config['software_log'] else: file_path = realpath(app.config, logfile) try: if not os.path.exists(file_path): raise IOError if not isText(file_path): content = "Can not open binary file, please select a text file!" if 'truncate' in request.form: content = tail(open(file_path), int(request.form['truncate'])) else: with open(file_path) as f: content = f.read() return jsonify(code=1, result=html_escape(content)) except: return jsonify(code=0, result="Warning: Log file doesn't exist yet or empty log!!")
def _realdir(self, dir): realdir = realpath(self.config, urllib.unquote(dir)) if not realdir: raise NameError('Could not load directory %s: Permission denied' % dir) return realdir
def pullProjectFiles(): path = realpath(app.config, request.form['project']) if path: return gitPull(path) else: return jsonify(code=0, result="Can not read folder: Permission Denied")