Ejemplo n.º 1
0
 def test_tree_items_dir_sort(self):
     x = Tree()
     for name, item in _TREE_ITEMS.iteritems():
         x[name] = item
     self.assertEquals(_SORTED_TREE_ITEMS, x.items())
Ejemplo n.º 2
0
class Directory(object):
    """Wrapper for Tree objects to make updating easier."""
    def __init__(self, id=None):
        """Create a new Directory object to mutate the specified tree.

		Args:
		  id: ID of the tree to mutate.
		"""
        if id is not None:
            self.tree = gitrepo.get_object(id)
            self.dirty = False
        else:
            self.tree = Tree()
            self.dirty = True

        self.subdirs = {}

    def __get_subdir(self, name):
        if name in self.subdirs:
            return self.subdirs[name]

        # Lazily create subdirectory objects as they are needed.
        _, sha = self.tree[name]
        subdir = Directory(sha)
        self.subdirs[name] = subdir

        return subdir

    def __contains__(self, path):
        components = path.split("/", 1)

        if len(components) == 2:
            dirname = components[0]
            if dirname not in self.tree:
                return False
            mode, _ = self.tree[dirname]
            if (mode & 040000) == 0:
                return False
            subdir = self.__get_subdir(dirname)
            return components[1] in subdir
        else:
            return components[0] in self.tree

    def __getitem__(self, path):
        components = path.split("/", 1)

        if len(components) == 2:
            subdir = self.__get_subdir(components[0])
            return subdir[components[1]]
        else:
            return self.tree[path]

    def __setitem__(self, path, value):
        components = path.split("/", 1)

        if len(components) == 2:
            subdir = self.__get_subdir(components[0])
            subdir[components[1]] = value
        else:
            self.tree[path] = value

        self.dirty = True

    def __delitem__(self, path):
        components = path.split("/", 1)

        if len(components) == 2:
            subdir = self.__get_subdir(components[0])
            del subdir[components[1]]
        else:
            filename = components[0]
            if filename in self.subdirs:
                del self.subdirs[filename]
            if filename not in self.tree:
                print "Doh..."
                for i in self.tree.items():
                    print i
            del self.tree[filename]

        self.dirty = True

    def mkdir(self, path):
        components = path.split("/", 1)

        if len(components) == 2:
            subdir = self.__get_subdir(components[0])
            subdir.mkdir(components[1])
        else:
            self.subdirs[components[0]] = Directory()
            self.tree[components[0]] = (040000, '')

        self.dirty = True

    def save(self):
        # Commit all dirty subdirectories:

        for filename, subdir in self.subdirs.items():
            if subdir.dirty:
                subdir_id = subdir.save()
                mode, _ = self.tree[filename]
                self.tree[filename] = (mode, subdir_id)

        self.dirty = False
        gitrepo.object_store.add_object(self.tree)

        return self.tree.id
Ejemplo n.º 3
0
 def test_tree_items_dir_sort(self):
     x = Tree()
     for name, item in _TREE_ITEMS.iteritems():
         x[name] = item
     self.assertEqual(_SORTED_TREE_ITEMS, x.items())
Ejemplo n.º 4
0
class Directory(object):
	"""Wrapper for Tree objects to make updating easier."""

	def __init__(self, id=None):
		"""Create a new Directory object to mutate the specified tree.

		Args:
		  id: ID of the tree to mutate.
		"""
		if id is not None:
			self.tree = gitrepo.get_object(id)
			self.dirty = False
		else:
			self.tree = Tree()
			self.dirty = True

		self.subdirs = {}

	def __get_subdir(self, name):
		if name in self.subdirs:
			return self.subdirs[name]

		# Lazily create subdirectory objects as they are needed.
		_, sha = self.tree[name]
		subdir = Directory(sha)
		self.subdirs[name] = subdir

		return subdir

	def __contains__(self, path):
		components = path.split("/", 1)

		if len(components) == 2:
			dirname = components[0]
			if dirname not in self.tree:
				return False
			mode, _ = self.tree[dirname]
			if (mode & 040000) == 0:
				return False
			subdir = self.__get_subdir(dirname)
			return components[1] in subdir
		else:
			return components[0] in self.tree

	def __getitem__(self, path):
		components = path.split("/", 1)

		if len(components) == 2:
			subdir = self.__get_subdir(components[0])
			return subdir[components[1]]
		else:
			return self.tree[path]

	def __setitem__(self, path, value):
		components = path.split("/", 1)

		if len(components) == 2:
			subdir = self.__get_subdir(components[0])
			subdir[components[1]] = value
		else:
			self.tree[path] = value

		self.dirty = True

	def __delitem__(self, path):
		components = path.split("/", 1)

		if len(components) == 2:
			subdir = self.__get_subdir(components[0])
			del subdir[components[1]]
		else:
			filename = components[0]
			if filename in self.subdirs:
				del self.subdirs[filename]
			if filename not in self.tree:
				print "Doh..."
				for i in self.tree.items():
					print i
			del self.tree[filename]

		self.dirty = True

	def mkdir(self, path):
		components = path.split("/", 1)

		if len(components) == 2:
			subdir = self.__get_subdir(components[0])
			subdir.mkdir(components[1])
		else:
			self.subdirs[components[0]] = Directory()
			self.tree[components[0]] = (040000, '')

		self.dirty = True

	def save(self):
		# Commit all dirty subdirectories:

		for filename, subdir in self.subdirs.items():
			if subdir.dirty:
				subdir_id = subdir.save()
				mode, _ = self.tree[filename]
				self.tree[filename] = (mode, subdir_id)

		self.dirty = False
		gitrepo.object_store.add_object(self.tree)

		return self.tree.id