def copyproj(self, team, src, dest): # Create a temporary directory tmpdir = tempfile.mkdtemp() #open the branch and sprout a new copy, in the temp dir b = open_branch(team, src) self.createproj(dest, team) nb = open_branch(team, dest) b.push(nb) return dict(status=0)
def filesrc(self, team, file=None, revision=None): """ Returns the contents of the file. """ file_path = file #save for later project, file = self.get_project_path(file_path) curtime = time.time() b = open_branch(int(team), project) #TODO: Need to security check here! No ../../ or /etc/passwd nautiness trac#208 autosaved_code = self.autosave.getfilesrc(team, file_path, 1) if revision == None or revision == "HEAD": revno, revid = b.last_revision_info() else: revno = int(revision) revid = b.get_rev_id(revno) if file != None and file != "": #TODO BZRPORT: URL checking #Load file from bzr # TODO BZRPORT: mime checking. Bzr doesn't have a mime property so the file will need to be checked with python try: branch_tree = b.repository.revision_tree(revid) file_id = branch_tree.path2id(file) b.lock_read() code = branch_tree.get_file_text(file_id) file_revid = self.get_file_revision( branch_tree, file_id) # get revision the file was last modified file_revno = b.revision_id_to_revno(file_revid) except: code = "Error loading file '%s' at revision %s." % (file, revision) file_revno = 0 # always unlock: finally: b.unlock() else: code = "Error loading file: No filename was supplied by the IDE. Contact an SR admin!" revision = 0 return dict(curtime=curtime, code=code, autosaved_code=autosaved_code, file_rev=str(file_revno), revision=revno, path=file_path, name=os.path.basename(file))
def pollchanges(self, team, project, rev, date=0): """ Used to determine if certain facets need updating. Currently this is only for the filelist, to remove the need to send the entire filelist just to see if its changed """ b = open_branch(int(team), project) head_rev_id = self.get_rev_id(team, project, 'HEAD') target_rev_id = self.get_rev_id(team, project, rev) filelist = True if head_rev_id == target_rev_id: filelist = False return dict(filelist=filelist)
def calendar(self, mnth, yr, file, team): #returns data for calendar function if file == '/': #no project selected return dict(path=file, history=[]) month = int(mnth) + 1 year = int(yr) b = open_branch(team, file) try: log = b.repository.get_revisions(b.revision_history()) except: logging.debug("Log failed for %s" % file) print "failed to retrieve log" return dict(path=file, history=[]) if len(log) == 0: #if there's nothing there return dict(path=file, history=[]) #get a list of users based on log authors start = datetime.datetime(year, month, 1, 0, 0, 0) if (month >= 12): end = datetime.datetime(year + 1, 1, 1, 0, 0, 0) #watchout for rollover else: end = datetime.datetime(year, month + 1, 1, 0, 0, 0) result = [] for y in log: now = datetime.datetime(2000, 1, 1) #create a dummy datetime now = now.fromtimestamp(y.timestamp) if (start <= now < end): result.append(y) result.reverse() return dict( path=file,\ history=[{"author":x.get_apparent_author(), \ "date":time.strftime("%Y/%m/%d/%H/%M/%S", \ time.localtime(x.timestamp)), \ "message":x.message, "rev":b.revision_id_to_revno(x.revision_id)} \ for x in result])
def get_rev_object(self, team, rev_id=""): """ Get a revision object for a given bzr revision id. If no revision id is provided, get latest revision. inputs: rev_id - str in bzr rev_id format, ie returned by get_rev_id() returns: revision object for given revision id.""" b = open_branch(int(team)) if rev_id == "": rev_id = b.last_revision() try: rev = b.repository.get_revision(rev_id) except: #TODO BZRPORT: implement exception properly, revision number failure etc print "Get revision failed, returned latest revision object" rev = b.repository.get_revision(b.last_revision()) return rev
def get_rev_id(self, team, project, revno=-1): """ Get revision ID string from revision number. inputs: revno - revision number convertable with int(). if revno is -1 or not supplied, get latest revision id. returns: revision id string """ b = open_branch(int(team), project) try: if revno == -1 or revno == "-1" or revno == "HEAD": #TODO BZRPORT: stop anything calling "HEAD" string rev_id = b.last_revision() else: rev_id = b.get_rev_id(int(revno)) except (TypeError): # TODO BZRPORT: add bzr exception print "Getting ID for revno: %s failed, returning latest revision id." % revno rev_id = b.last_revision() return rev_id
def filelist(self, team, project, rootpath="/", rev=-1, date=0): """ Returns a directory tree of the current repository. inputs: project - the bzr branch rootpath - to return file from a particular directory within the branch (recursive) returns: A tree as a list of files/directory objects: { tree : [{path : filepath kind : FOLDER or FILE children : [list as above] name : name of file}, ...]} """ b = open_branch(int(team), project) target_rev_id = self.get_rev_id(team, project, rev) self.user.set_setting('project.last', project) try: rev_tree = b.repository.revision_tree(target_rev_id) except: return {"error": "Error getting revision tree"} # Get id of root folder from which to list files. if it is not found it will return None rootid = rev_tree.path2id(rootpath) try: rev_tree.lock_read() # Get generator object containing file information from base rootid. If rootid=None, will return from root. files = rev_tree.inventory.iter_entries(rootid) except: # TODO BZRPORT: Proper error handling return {"error": "Error getting file list"} # Always unlock tree: finally: rev_tree.unlock() #grab the autosave listings autosave_data = self.autosave.getfilesrc(team, '/' + project + rootpath) def branch_recurse(project, path, entry, files, given_parent_id): """ Travels recursively through a generator object provided by revision_tree.inventory.iter_items. Iter_items returns child items immediately after their parents, so by checking the parent_id field of the item with the actual id of the directory item that called it, we can check if we are still within that directory and therefore need to add the item as a child. This function will return a list of all children of a particular branch, along with the next items for analysis. Whenever it encounters a directory it will call itself to find the children. inputs: path - path of item to be analysed first entry - InventoryEntry-derived object of item to be analysed first files - generator object created by iter_items given_parent_id - id (string) of calling directory returns: entry_list - list of children. if given_parent_id does not match entry.parent_id, this will be an empty list. path - path of item that has not yet been added to the tree entry - the entry object that has not yet been added to the tree. if given_parent_id did not match entry.parent_id, then path and entry returned will be the same as path and entry called. """ entry_list = [] while entry.parent_id == given_parent_id: # is a child of parent if entry.kind == "directory": try: next_path, next_entry = files.next() children_list, next_path, next_entry = branch_recurse( project, next_path, next_entry, files, entry.file_id) except StopIteration: # No more files to iterate through after this one next_entry = None # break after adding this entry children_list = [ ] # no more items, so there can't be any children entry_list.append({ "name": entry.name, "path": project + path, "kind": "FOLDER", "autosave": 0, # No autosave data for directories "rev": "-1", #TODO BZRPORT: what's this show/for? yes, i know revision, i mean, current, or when it was created? "children": children_list }) if next_entry is None: break # there are no more iterations so break else: path = next_path entry = next_entry # now we'll use the returned entry else: if project + path in autosave_data: autosave_info = autosave_data[project + path] else: autosave_info = 0 entry_list.append({ "name": entry.name, "path": project + path, "kind": "FILE", "autosave": autosave_info, "rev": "-1", #TODO BZRPORT: what's this show/for? yes, i know revision, i mean, current, or when it was created? "children": [] }) try: path, entry = files.next() # grab next entry except StopIteration: # No more files to iterate through break return entry_list, path, entry # Determine tree_root string to pass to recursing function as a parent id if rootid == None: tree_root = "TREE_ROOT" else: tree_root = rootid try: first_path, first_entry = files.next() # grab next entry except StopIteration: # StopIteration caught on first pass: project tree must be empty return dict(tree=[]) tree, last_path, last_entry = branch_recurse('/' + project + '/', first_path, first_entry, files, tree_root) return dict(tree=tree)
def gethistory(self, team, file, user=None, offset=0): """ This function retrieves the bzr history for the given file(s) to restrict logs to particular user, supply a user parameter a maximum of 10 results are sent to the browser, if there are more than 10 results available, overflow > 0. supply an offset to view older results: 0<offset < overflow; offset = 0 is the most recent logs """ if file[:9] == 'New File ': return dict(path=file, history=[]) file_path = file #save for later project, file = self.get_project_path(file_path) b = open_branch(int(team), project) revisions = [ b.repository.get_revision(r) for r in b.revision_history() ] #Get a list of authors authors = list(set([r.committer for r in revisions])) #If a user is passed, only show revisions committed by that user if user != None: revisions = [r for r in revisions if r.committer == user] #Only show revisions where the delta touches file fileid = b.basis_tree().path2id(file) if fileid == None: #File not found return dict() def revisionTouchesFile(revision): """ Return true if the revision changed a the file referred to in fileid. """ delta = b.get_revision_delta( b.revision_id_to_revno(revision.revision_id)) return delta.touches_file_id(fileid) revisions = filter(revisionTouchesFile, revisions) #Calculate offsets for paging try: offset = int(offset) except ValueError: #Someone passed a string return dict() start = offset * 10 end = start + 10 maxval = len(revisions) if maxval % 10 > 0: overflow = maxval / 10 + 1 else: overflow = maxval / 10 revisions = revisions[start:end] revisions.reverse() return dict(path=file_path, overflow=overflow, offset=offset, authors=authors, history=[{ "author": r.committer, "date": time.strftime("%H:%M:%S %d/%m/%Y", time.localtime(r.timestamp)), "message": r.message, "rev": b.revision_id_to_revno(r.revision_id) } for r in revisions])
def checkout(self, team, project, simulator=False): """ This function grabs a set of files and makes a zip available. Should be linked to directly. inputs: team & project - code to retrieve simulator - true if code is being delivered to a simulator. returns: A zip file as a downloadable file with appropriate HTTP headers sent. """ b = open_branch(int(team), project) rev_tree = b.basis_tree() # get latest revision tree for branch #Avoid using /tmp by writing into a memory based file zipData = StringIO.StringIO() zip = zipfile.ZipFile(zipData, "w", zipfile.ZIP_DEFLATED) #Need to lock_read before reading any file contents rev_tree.lock_read() try: #Get a list of files in the tree files = [ f for f in rev_tree.iter_entries_by_dir() if f[1].kind == "file" ] for filename, file in files: #Set external_attr on a ZipInfo to make sure the files are #created with the right permissions info = zipfile.ZipInfo(filename.encode("ascii")) info.external_attr = 0666 << 16L #Read the file contents and add to zip zip.writestr(info, rev_tree.get_file(file.file_id).read()) #Need a __init__ in the root of all code exports if not "__init__.py" in [f[0].encode("ascii") for f in files]: info = zipfile.ZipInfo("__init__.py") info.external_attr = 0666 << 16L zip.writestr(info, "") except: return "Error exporting project" finally: #Always unlock or get GC related errors rev_tree.unlock() zip.close() #Seek back to start of file so read() works later on zipData.seek(0) if not simulator: """ The zipfile delivered to the robot is the contents of the repository as a zip inside another zip that contains firmware. """ #Get a copy of the firmware zip, drop the code zip (in zipData) #in it and then put the resulting zip back into zipData sysZipData = open(config.get("robot.packagezip")).read() sysZipBuffer = StringIO.StringIO(sysZipData) sysZip = zipfile.ZipFile(sysZipBuffer, "a") info = zipfile.ZipInfo(ZIPNAME) info.external_attr = 0666 << 16L sysZip.writestr(info, zipData.read()) sysZip.close() sysZipBuffer.seek(0) zipData = StringIO.StringIO(sysZipBuffer.read()) #Set up headers for correctly serving a zipfile cherrypy.response.headers['Content-Type'] = \ "application/x-download" cherrypy.response.headers['Content-Disposition'] = \ 'attachment; filename="' + ZIPNAME + '"' #Return the data return zipData.read()
def diff(self, team, file, rev, code=None): """ This function returns the patch applied by a particular revision to a file. """ if file[:9] == 'New File ': return dict(path=file, history=[]) project, file = self.get_project_path(file) b = open_branch(int(team), project) if code == None: #the patch from a commit rev_id = b.revision_history()[int(rev) - 1] rev = b.repository.get_revision(rev_id) from cStringIO import StringIO from bzrlib import diff if len(rev.parent_ids) == 0: ancestor_id = bzrlib.revision.NULL_REVISION else: ancestor_id = rev.parent_ids[0] tree_1 = b.repository.revision_tree(ancestor_id) tree_2 = b.repository.revision_tree(rev_id) s = StringIO() diff.show_diff_trees(tree_1, tree_2, s, old_label='', new_label='') filediff = s.getvalue() else: #the current difference path, file_name = os.path.split(file) ancestor_id = b.last_revision() # Check out the code wt = WorkingTree(int(team), project) # Directory we're working in td = wt.tmpdir print td + os.path.sep + file tmpfile = open(td + os.path.sep + file, 'w') tmpfile.write(str(code)) tmpfile.close() print 'temp_dir: ' + td + "\nfile: " + file # Run pychecker p = subprocess.Popen(['bzr', 'diff'], cwd=td, stdout=subprocess.PIPE, stderr=subprocess.PIPE) output = p.communicate() rval = p.wait() wt.destroy() if rval == 0: return dict() else: filediff = output[0] return dict(diff=filediff, oldrev=b.revision_id_to_revno(ancestor_id))