class GitTessera(object): SORTING = { "date": lambda t1, t2: t1.mtime < t2.mtime, "status": cmp_status, "title": lambda t1, t2: cmp( t1.get_attribute("title").lower(), t2.get_attribute("title").lower()), } def __init__(self, config): self.gitdir = "." self.git = MyGit(self.gitdir) if self.gitdir != ".": self.tesserae = os.path.join(self.gitdir, ".tesserae") else: self.tesserae = ".tesserae" self._config = config def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: raise ArgumentError( "Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys()))) except KeyError: raise ArgumentError("No sort algorithm for '%s' available" % args[idx + 1]) try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: raise ArgumentError("Please specify minimum one tag.") contents = [ self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode) ] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path, self._config) if t.get_attribute("updated") == 0: tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") author, author_time = self.git.read_author(tessera_path) import re r = re.compile("^([^\<]+) \<([^\>]+)\>$") m = r.search(author) if m: fout.write("author: %s\n" % m.group(1)) fout.write("email: %s\n" % m.group(2)) fout.write("updated: %d\n" % author_time) fout.close() te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae def get(self, key): for i in os.listdir(self.tesserae): tessera_path = os.path.join(self.tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise TesseraError("git tessera %s not found" % key) return Tessera(tessera_path, self._config) def create(self, title="tessera title goes here"): """ create a new tessera with title {title}. @returns Tessera object of the new Tessera """ uuid = uuid1() tessera_path = os.path.join(Tessera._tesserae, str(uuid)) tessera_file = "%s/tessera" % tessera_path os.mkdir(tessera_path) fin = open(os.path.join(Tessera._tesserae, "template"), "r") fout = open(tessera_file, "w") for line in fin.readlines(): if line == "@title@\n": line = "# %s\n" % title fout.write(line) fin.close() fout.close() tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") c = StackedConfig(StackedConfig.default_backends()) fout.write("author: %s\n" % c.get("user", "name")) fout.write("email: %s\n" % c.get("user", "email")) fout.write("updated: %d\n" % int(time())) fout.close() return Tessera(tessera_path, self._config) def commit(self, t): """ commits a Tessera created by the create() method to the repository. """ files = [ os.path.join(t.tessera_path, "tessera"), os.path.join(t.tessera_path, "info") ] self.git.add(files, "tessera created: %s" % t.get_attribute("title"))
class GitCommands(object): def __init__(self): git_directory = "." self.git = MyGit(git_directory) Tessera._tesserae = os.path.relpath(os.path.join(git_directory, ".tesserae")) self._config = TesseraConfig(os.path.join(Tessera._tesserae, "config")) def cmd_init(self, args): if len(args) != 0: raise ArgumentError("git tessera init takes no arguments") if os.path.exists(Tessera._tesserae): raise TesseraError("git tesserae directory already exists: %s" % Tessera._tesserae) os.mkdir(Tessera._tesserae) files = [] for source in [ "template", "status", "types", "config" ]: files.append(_install(Tessera._tesserae, source)) self.git.add(files, "tessera: initialized") return True def cmd_ls(self, args): gt = GitTessera(self._config) tesserae = gt.ls(args) for t in tesserae: print t.summary() return True def cmd_show(self, args): if len(args) != 1: raise ArgumentError("git tessera show takes identifier as argument") gt = GitTessera(self._config) t = gt.get(args[0]) if not t: return False short = t.summary() length = len(short) print "=" * length print short print "=" * length print t.content return True def cmd_edit(self, args): if len(args) < 1: raise ArgumentError("git tessera edit takes one or more identifier as argument") tessera_paths = [] for key in args: tessera_path = None found = False for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: found = True break if not found: raise TesseraError("git tessera %s not found" % key) tessera_paths.append(tessera_path) tessera_files = ["%s/tessera" % x for x in tessera_paths] _edit(tessera_files, self._config) #if self.git.is_dirty(): for tessera_path in tessera_paths: t = Tessera(tessera_path, self._config) self.git.add("%s/tessera" % tessera_path, "tessera updated: %s" % t.title) return True def cmd_create(self, args): #if self.git.is_dirty(): # stderr.write("repo is dirty\n") # return False gt = GitTessera(self._config) t = gt.create(" ".join(args)) if args else gt.create() _edit(t.filename, self._config) gt.commit(t) return True def cmd_remove(self, args): if len(args) != 1: raise ArgumentError("git tessera remove takes identifier as argument") key = args[0] tessera_file = None tessera_path = None for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: tessera_file = "%s/tessera" % tessera_path break if not tessera_file: raise TesseraError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) stdout.write("remove tessera %s: %s ? [Y/n] " % (key, t.get_attribute("title"))) try: answer = stdin.readline().strip() except KeyboardInterrupt: return False if not answer or answer.lower() == "y": files = ["%s/%s" % (tessera_path, x) for x in os.listdir(tessera_path)] self.git.rm(files, "tessera removed: %s" % t.get_attribute("title")) from shutil import rmtree rmtree(tessera_path) def cmd_serve(self, args): from tesseraweb import TesseraWeb web = TesseraWeb() web.serve() def cmd_tag(self, args): if len(args) != 2: raise ArgumentError("git tessera show takes identifier as argument and new tag") key = args[0] for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise ArgumentError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) t.add_tag(args[1]) self.git.add(t.filename, "tessera updated: add tag %s to %s" % (args[1], t.get_attribute("title"))) return True def cmd_config(self, args): if len(args) < 1: raise ArgumentError("specify minimum one argument to read the config's value") setting = args[0].split(".") if len(setting) < 2: raise ArgumentError("to set a config value you have to use the schema: section.option") if len(args) > 1: self._config.set(setting[0], setting[1], args[1]) self._config.store() return True option = self._config.get(setting[0], ".".join(setting[1:])) print("%s has value %s" % (args[0], option)) return True
class GitTessera(object): SORTING = { "date": lambda t1, t2: t1.mtime < t2.mtime, "status": cmp_status, "title": lambda t1, t2: cmp(t1.get_attribute("title").lower(), t2.get_attribute("title").lower()), } def __init__(self, config): self.gitdir = "." self.git = MyGit(self.gitdir) if self.gitdir != ".": self.tesserae = os.path.join(self.gitdir, ".tesserae") else: self.tesserae = ".tesserae" self._config = config def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: raise ArgumentError("Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys()))) except KeyError: raise ArgumentError("No sort algorithm for '%s' available" % args[idx + 1]) try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: raise ArgumentError("Please specify minimum one tag.") contents = [self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode)] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path, self._config) if t.get_attribute("updated") == 0: tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") author, author_time = self.git.read_author(tessera_path) import re r = re.compile("^([^\<]+) \<([^\>]+)\>$") m = r.search( author ) if m: fout.write("author: %s\n" % m.group(1)) fout.write("email: %s\n" % m.group(2)) fout.write("updated: %d\n"%author_time) fout.close() te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae def get(self, key): for i in os.listdir(self.tesserae): tessera_path = os.path.join(self.tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise TesseraError("git tessera %s not found" % key) return Tessera(tessera_path, self._config) def create(self, title="tessera title goes here"): """ create a new tessera with title {title}. @returns Tessera object of the new Tessera """ uuid = uuid1() tessera_path = os.path.join(Tessera._tesserae, str(uuid)) tessera_file = "%s/tessera" % tessera_path os.mkdir(tessera_path) fin = open(os.path.join(Tessera._tesserae, "template"), "r") fout = open(tessera_file, "w") for line in fin.readlines(): if line == "@title@\n": line = "# %s\n" % title fout.write(line) fin.close() fout.close() tessera_info = "%s/info" % tessera_path fout = open(tessera_info, "w") c = StackedConfig(StackedConfig.default_backends()) fout.write("author: %s\n"%c.get("user", "name")) fout.write("email: %s\n"%c.get("user", "email")) fout.write("updated: %d\n"%int(time())) fout.close() return Tessera(tessera_path, self._config) def commit(self, t): """ commits a Tessera created by the create() method to the repository. """ files = [ os.path.join(t.tessera_path, "tessera"), os.path.join(t.tessera_path, "info") ] self.git.add(files, "tessera created: %s" % t.get_attribute("title"))
class GitCommands(object): def __init__(self): git_directory = "." self.git = MyGit(git_directory) Tessera._tesserae = os.path.relpath( os.path.join(git_directory, ".tesserae")) self._config = TesseraConfig(os.path.join(Tessera._tesserae, "config")) def cmd_init(self, args): if len(args) != 0: raise ArgumentError("git tessera init takes no arguments") if os.path.exists(Tessera._tesserae): raise TesseraError("git tesserae directory already exists: %s" % Tessera._tesserae) os.mkdir(Tessera._tesserae) files = [] for source in ["template", "config"]: files.append(_install(Tessera._tesserae, source)) self.git.add(files, "tessera: initialized") return True def cmd_ls(self, args): gt = GitTessera(self._config) tesserae = gt.ls(args) for t in tesserae: print t.summary() return True def cmd_show(self, args): if len(args) != 1: raise ArgumentError( "git tessera show takes identifier as argument") gt = GitTessera(self._config) t = gt.get(args[0]) if not t: return False short = t.summary() length = len(short) print "=" * length print short print "=" * length print t.content return True def cmd_edit(self, args): if len(args) < 1: raise ArgumentError( "git tessera edit takes one or more identifier as argument") tessera_paths = [] for key in args: tessera_path = None found = False for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: found = True break if not found: raise TesseraError("git tessera %s not found" % key) tessera_paths.append(tessera_path) while True: tessera_files = ["%s/tessera" % x for x in tessera_paths] _edit(tessera_files, self._config) # if self.git.is_dirty(): failed = [] while tessera_paths: tessera_path = tessera_paths.pop() t = Tessera(tessera_path, self._config) if not t.error: t._write_info() files = [ "%s/tessera" % tessera_path, "%s/info" % tessera_path ] self.git.add( files, "tessera updated: %s" % t.get_attribute("title")) continue # failed parsing failed.append(tessera_path) if failed: stdout.write("Abort ? [y/N] ") try: answer = stdin.readline().strip() except KeyboardInterrupt: break if answer and answer.lower() == "y": break tessera_paths = failed else: break return True def cmd_create(self, args): #if self.git.is_dirty(): # stderr.write("repo is dirty\n") # return False gt = GitTessera(self._config) t = gt.create(" ".join(args)) if args else gt.create() while True: _edit(t.filename, self._config) if t.update(): gt.commit(t) return True stdout.write("Abort ? [y/N] ") try: answer = stdin.readline().strip() except KeyboardInterrupt: break if answer and answer.lower() == "y": break return False def cmd_remove(self, args): if len(args) != 1: raise ArgumentError( "git tessera remove takes identifier as argument") key = args[0] tessera_file = None tessera_path = None for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: tessera_file = "%s/tessera" % tessera_path break if not tessera_file: raise TesseraError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) stdout.write("remove tessera %s: %s ? [Y/n] " % (key, t.get_attribute("title"))) try: answer = stdin.readline().strip() except KeyboardInterrupt: return False if not answer or answer.lower() == "y": files = [ "%s/%s" % (tessera_path, x) for x in os.listdir(tessera_path) ] self.git.rm(files, "tessera removed: %s" % t.get_attribute("title")) from shutil import rmtree rmtree(tessera_path) def cmd_serve(self, args): from tesseraweb import TesseraWeb web = TesseraWeb() web.serve() def cmd_tag(self, args): if len(args) != 2: raise ArgumentError( "git tessera show takes identifier as argument and new tag") key = args[0] for i in os.listdir(Tessera._tesserae): tessera_path = "%s/%s" % (Tessera._tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split('-')[0] == key or i == key: break if not tessera_path: raise ArgumentError("git tessera %s not found" % key) t = Tessera(tessera_path, self._config) t.add_tag(args[1]) files = [ os.path.join(t.tessera_path, "tessera"), os.path.join(t.tessera_path, "info") ] self.git.add( files, "tessera updated: add tag %s to %s" % (args[1], t.get_attribute("title"))) return True def cmd_config(self, args): if len(args) < 1: raise ArgumentError( "specify minimum one argument to read the config's value") setting = args[0].split(".") if len(setting) < 2: raise ArgumentError( "to set a config value you have to use the schema: section.option" ) if len(args) > 1: self._config.set(setting[0], setting[1], args[1]) self._config.store() return True option = self._config.get(setting[0], ".".join(setting[1:])) print("%s has value %s" % (args[0], option)) return True
class GitTessera(object): SORTING = { "date": lambda t1, t2: t1.mtime < t2.mtime, "status": lambda t1, t2: cmp(t1.status_id, t2.status_id), "title": lambda t1, t2: cmp(t1.get_attribute("title").lower(), t2.get_attribute("title").lower()), "hash": lambda t1, t2: cmp(t1.tessera_hash.lower(), t2.tessera_hash.lower()), } def __init__(self, config): self.gitdir = "." self.git = MyGit(self.gitdir) self.tesserae = os.path.join(self.gitdir, ".tesserae") self._config = config def ls(self, args=[]): if not os.path.exists(self.tesserae): return False try: idx = args.index("--sort") sortfunc = GitTessera.SORTING[args[idx + 1]] except ValueError: sortfunc = GitTessera.SORTING["status"] except IndexError: raise ArgumentError( "Please specify a sort algorithm. Available: %s" % (", ".join(GitTessera.SORTING.keys())) ) except KeyError: raise ArgumentError("No sort algorithm for '%s' available" % args[idx + 1]) try: idx = args.index("--tags") tags = args[idx + 1].split(",") except ValueError: tags = None except IndexError: raise ArgumentError("Please specify minimum one tag.") contents = [ self.tesserae + "/" + x for x in os.listdir(self.tesserae) if stat.S_ISDIR(os.lstat(self.tesserae + "/" + x).st_mode) ] tesserae = [] for tessera_path in contents: t = Tessera(tessera_path, self._config) te_tags = t.get_attribute("tags") if not tags or any(x in te_tags for x in tags): tesserae.append(t) tesserae = sorted(tesserae, cmp=sortfunc) return tesserae def get(self, key): for i in os.listdir(self.tesserae): tessera_path = os.path.join(self.tesserae, i) if not stat.S_ISDIR(os.lstat(tessera_path).st_mode): continue if i.split("-")[0] == key or i == key: break if not tessera_path: raise TesseraError("git tessera %s not found" % key) return Tessera(tessera_path, self._config) def create(self, title="tessera title goes here"): """ create a new tessera with title {title}. @returns Tessera object of the new Tessera """ uuid = uuid1() tessera_path = os.path.join(Tessera._tesserae, str(uuid)) tessera_file = "%s/tessera" % tessera_path os.mkdir(tessera_path) fin = open(os.path.join(Tessera._tesserae, "template"), "r") fout = open(tessera_file, "w") for line in fin.readlines(): if line == "@title@\n": line = "# %s\n" % title fout.write(line) fin.close() fout.close() return Tessera(tessera_path, self._config) def commit(self, t): """ commits a Tessera created by the create() method to the repository. """ t.update() self.git.add(t.filename, "tessera created: %s" % t.get_attribute("title"))