From dd32ae7661be15fa8cdc73a005aabef490f715b5 Mon Sep 17 00:00:00 2001 From: Saturneic Date: Tue, 12 Mar 2019 02:28:36 +0800 Subject: [PATCH] Fixed and Added. --- .idea/giteasy.iml | 2 +- .idea/misc.xml | 2 +- .idea/workspace.xml | 154 ++++++++++++++--------- git.py | 294 +++++++++++++++++++++++++++----------------- keygen.py | 2 +- window.py | 144 ++++++++++++++++++---- 6 files changed, 397 insertions(+), 201 deletions(-) diff --git a/.idea/giteasy.iml b/.idea/giteasy.iml index 496be93..fa80a76 100644 --- a/.idea/giteasy.iml +++ b/.idea/giteasy.iml @@ -4,7 +4,7 @@ - + diff --git a/.idea/misc.xml b/.idea/misc.xml index 9e13014..8656114 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,5 +3,5 @@ - + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 32bad9b..b8414d8 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,10 +2,12 @@ - + + + + - - - + + @@ -176,13 +215,13 @@ - @@ -295,21 +333,21 @@ - + - + - - - + + + - + @@ -324,6 +362,9 @@ + + + + + + + + + - - + + + + + + + + + - + + - - + + - - + + - - - - - - - - - - - - - + diff --git a/git.py b/git.py index 91e3f2a..0615d98 100644 --- a/git.py +++ b/git.py @@ -7,11 +7,23 @@ class Git(ssh.SSH): projects = {} projects_list = [] remotes = {} + if_set_local = False + if_global_init = False + if_global_init_local = False + if_base_init = False + if_fix_project = False + if_fix_local = False + if_get_branches = False + if_get_remote = False + if_fetch_remote = False + if_get_project = False + if_connected = False def __init__(self, hostname, path, user, passwd): super().__init__(hostname, path, user, passwd) self.base_path = [False] self.connect(5) + self.if_connected = True self.local_path = None self.fix_cmd = None self.fix_name = None @@ -25,69 +37,93 @@ class Git(ssh.SSH): cmd += "git config --global user.name \"{0}\";".format(git_user) stdout, stderr = self.run(cmd) if self.check_error(stderr): - print("Global setting successful.") + print("Global Setting Successful.") + self.if_global_init = True else: print(stderr) - raise ValueError("Args Abnormal") + raise ValueError("Args Error") def base_init(self): - stdout, stderr = self.run("ls {}".format(self.path)) - if self.check_error(stderr): - if "giteasy\n" in stdout: - print("Directory already found.") - else: - stdout, stderr = self.run("mkdir {0}/{1}".format(self.path, "giteasy")) - if self.check_error(stderr): - print("Create directory.") + if self.if_connected: + stdout, stderr = self.run("ls {}".format(self.path)) + if self.check_error(stderr): + if "giteasy\n" in stdout: + print("Directory Already Found.") else: - raise ValueError(stderr) - self.base_path = [True, "{0}{1}".format(self.path, "giteasy")] + stdout, stderr = self.run("mkdir {0}/{1}".format(self.path, "giteasy")) + if self.check_error(stderr): + print("Create directory.") + else: + raise ValueError(stderr) + self.base_path = [True, "{0}{1}".format(self.path, "giteasy")] + self.if_base_init = True + else: + raise ValueError("Path Error") else: - raise ValueError("Home Path Abnormal") + raise AttributeError("Connect First.") def create_project(self, name): - if self.base_path[0]: - stdout, stderr = self.run("mkdir {0}/{1}".format(self.base_path[1], name)) - if self.check_error(stderr): - print("Succeed In Making Directory.") - self.projects[name] = {"path": "{0}/{1}".format(self.base_path[1], name), "branch": []} - cmd = "cd {0};".format(self.projects[name]["path"]) - cmd += "git init;" - cmd += "touch ReadME.md;" - cmd += "git add *;" - cmd += "git commit --message=init;" - stdout, stderr = self.run(cmd) - if self.check_error(stderr): - print("Succeed In Getting Repository.") - else: - print(stderr) - raise ValueError("Target Path Abnormal") + if self.if_base_init: + if os.path.exists("{0}/{1}.git".format(self.base_path[1], name)): + print("Server Project Already Exist.") else: - raise ValueError("Base Path Abnormal") + stdout, stderr = self.run("mkdir {0}/{1}.git".format(self.base_path[1], name)) + if self.check_error(stderr): + print("Succeed In Making Directory.") + self.projects[name] = {"path": "{0}/{1}".format(self.base_path[1], name), "branch": []} + cmd = "cd {0}.git;".format(self.projects[name]["path"]) + cmd += "git init --bare;" + stdout, stderr = self.run(cmd) + if self.check_error(stderr): + print("Succeed In Getting Repository.") + else: + print(stderr) + raise ValueError("Target Path Abnormal") + else: + raise ValueError("Path Error.") else: - raise UnboundLocalError("Init Base First") + raise AttributeError("Set Base Path First.") def clone_project(self): - os.chdir(self.local_path) - os.popen("git clone ssh://{0}@{1}:{2}/{3}/.git".format(self.user, self.hostname, self.base_path[1], - self.fix_name)) + if self.if_set_local and self.if_connected: + os.chdir(self.local_path) + os.popen("git clone ssh://{0}@{1}:{2}/{3}".format(self.user, self.hostname, self.base_path[1], + self.fix_name)) + else: + raise AttributeError("Connect & Set Local First.") + + def fix_project_local(self): + if self.if_set_local and self.if_fix_project: + os.chdir(os.path.join(self.local_path, self.fix_name)) + self.if_fix_local = True + else: + raise AttributeError("Set Local & Fix Project First.") def init_project_local(self, name): - os.chdir(self.local_path) - try: - os.mkdir(name) - except FileExistsError: + if self.if_set_local: + os.chdir(self.local_path) + try: + os.mkdir(name) + except FileExistsError: + os.chdir(os.path.join(self.local_path, name)) + print("Local Project Already Exist.") os.chdir(os.path.join(self.local_path, name)) - raise FileExistsError("Project Already Exist.") - os.chdir(os.path.join(self.local_path, name)) - cmd = "git init" - os.popen(cmd) + cmd = "git init" + readme = open(os.path.join(self.local_path, name,"ReadME.md"),"w") + readme.write("#Default Project Introduction.#\n") + readme.close() + cmd += "git add *" + cmd += "git commit --message=\"Initial Commit\"" + os.popen(cmd) + else: + raise AttributeError("Set Local First.") def global_init_local(self): cmd = "git config --global user.email \"{0}\"".format(self.git_email) print(os.popen(cmd).read()) cmd = "git config --global user.name \"{0}\"".format(self.git_user) print(os.popen(cmd).read()) + self.if_global_init = True def update_projects(self): if self.base_path[0]: @@ -95,10 +131,10 @@ class Git(ssh.SSH): if self.check_error(stderr): reform = re.compile("\w+") for project in stdout: - print("PROJECT", project) project_name = reform.match(project).string.strip('\n') self.projects[project_name] = {"path": "{0}/{1}".format(self.base_path[1], project_name), "branch": []} + self.if_get_project = True else: raise ValueError("Base Path Abnormal") @@ -108,104 +144,130 @@ class Git(ssh.SSH): raise UnboundLocalError("Init Base First") def list_projects(self): - for project in self.projects.keys(): - self.projects_list.append(project) - print(project) + if self.if_get_project: + for project in self.projects.keys(): + self.projects_list.append(project) + else: + raise AttributeError("Get Project First.") def set_local(self, path): - self.remotes = {} - self.local_path = path - os.chdir(self.local_path) - + if self.if_connected: + self.remotes = {} + self.local_path = path + self.if_set_local = True + os.chdir(self.local_path) + else: + raise AttributeError("Connect First.") def add_remote(self, name="origin"): - os.popen("git remote add {3} ssh://{0}@{1}:{2}/{4}/.git".format(self.user, self.hostname, self.base_path[1], - name, self.fix_name)) - self.remotes[name] = {"name": name, - "url": "ssh://{0}@{1}:{2}/{3}/.git".format(self.user, self.hostname, self.base_path[1], - self.fix_name)} + if self.if_base_init and self.if_get_project and self.if_fix_project: + stdout = os.popen("git remote add {3} ssh://{0}@{1}:{2}/{4}.git".format(self.user, self.hostname, + self.base_path[1], + name, self.fix_name)).read() + self.remotes[name] = {"name": name, + "url": "ssh://{0}@{1}:{2}/{3}.git".format(self.user, self.hostname, self.base_path[1], + self.fix_name)} + + self.if_get_remote = True + return stdout + else: + raise AttributeError("Get Project & Base Init & Fix Project First.") def fetch_remote(self, name): - if name in self.remotes.keys(): - os.popen("git fetch {0}".format(name)) + if self.if_fix_project and self.if_base_init and self.if_set_local: + if name in self.remotes.keys(): + os.popen("git fetch {0}".format(name)) + self.if_fetch_remote = True + else: + raise AttributeError("Set Local & Fix Project & Base Init First.") def get_remote(self): - ret_code = re.compile(r"[\t, ]") - for remote in os.popen("git remote -v").readlines(): - results = ret_code.split(remote) - print(results) - results = list(results) - if len(results) > 1: - for item in results: - if item[0] not in self.remotes.keys(): - self.remotes[results[0]] = {"name": results[0], "url": results[1]} + if self.if_fix_project and self.if_base_init and self.if_set_local: + ret_code = re.compile(r"[\t, ]") + for remote in os.popen("git remote -v").readlines(): + results = ret_code.split(remote) + results = list(results) + if len(results) >= 1: + self.remotes = {} + for item in results: + if item[0] not in self.remotes.keys(): + self.remotes[results[0]] = {"name": results[0], "url": results[1]} + self.if_get_remote = True + else: + raise AttributeError("Set Local & Fix Project & Base Init First.") def push_remote(self, name, branch): - print(self.projects[self.fix_name]["branch"]) - print(self.projects[self.fix_name]) - if branch in self.projects[self.fix_name]["branch"] and name in self.remotes.keys(): - os.popen("git push {0} {1}".format(name, branch)) + if self.if_fix_project and self.if_base_init and self.if_set_local \ + and self.if_get_remote: + if name in self.remotes.keys(): + return os.popen("git push {0} {1}".format(name, branch)).read() + else: + raise ValueError("Name Abnormal") else: - raise ValueError("Branch or Name Abnormal") + raise AttributeError("Set Local & Fix Project & Base Init & Get Remote First.") def pull_remote(self, name, branch): - if name in self.remotes.keys(): - os.popen("git pull {0} {1}".format(name, branch)) + if self.if_fix_project and self.if_base_init and self.if_set_local \ + and self.if_get_remote: + if name in self.remotes.keys(): + return os.popen("git pull {0} {1}".format(name, branch)).read() + else: + raise ValueError("Remote Error") else: - raise ValueError("Remote Error") + raise AttributeError("Set Local & Fix Project & Base Init & Get Remote & Get Branches First.") def fix_project(self, name): - if name in self.projects_list: - self.fix_name = name - stdout, stderr = self.run("cd {0}".format(self.projects[name]["path"])) - if self.check_error(stderr): - self.fix_cmd = "cd {0}".format(self.projects[name]["path"]) + ";" - os.chdir(os.path.join(self.local_path, self.fix_name)) - else: - raise ValueError("Project Path Abnormal") - - def commit(self, message): - if self.base_path[0]: - stdout, stderr = self.run(self.fix_cmd + "git commit --message={0}".format(message)) - if self.check_error(stderr): - print(stdout) - else: - raise ValueError(stderr) + if self.if_get_project and self.if_base_init: + if name+".git" in self.projects_list: + self.fix_name = name + stdout, stderr = self.run("cd {0}".format(self.projects[name+".git"]["path"])) + if self.check_error(stderr): + self.fix_cmd = "cd {0}".format(self.projects[name+".git"]["path"]) + ";" + self.if_fix_project = True + else: + raise ValueError("Project Path Abnormal") else: - raise UnboundLocalError("Init Base First") + raise AttributeError("Get Project & Base Init First.") def commit_local(self, message): - if self.base_path[0]: - os.popen("git commit --message={0}".format(message)) + if self.if_set_local and self.if_fix_local and self.if_base_init: + if self.base_path[0]: + stdout = os.popen("git commit --message={0}".format(message)).read() + return stdout + else: + raise UnboundLocalError("Init Base First") else: - raise UnboundLocalError("Init Base First") + raise AttributeError("Set Local & Fix Local & Base Init First.") - @staticmethod - def add(): - os.popen("git add *") + def add(self): + if self.if_set_local and self.if_fix_local and self.if_base_init: + stdout = os.popen("git add *") + return stdout + else: + raise AttributeError("Set Local & Fix Local & Base Init First.") def get_branch(self): - if self.base_path[0] and self.fix_name is not None: - stdout, stderr = self.run(self.fix_cmd + "git branch") - if self.check_error(stderr): - self.projects[self.fix_name]["branch"] = [] - reform = re.compile("\w+") - for branch in stdout: - branch_name = reform.search("*master").group().strip('\n') - self.projects[self.fix_name]["branch"].append(branch_name) + if self.if_get_project and self.if_base_init and self.if_fix_local: + stdout = os.popen("git branch").read() + self.projects[self.fix_name+".git"]["branch"] = [] + reform = re.compile("\w+") + for branch in stdout: + branch_name = reform.search("*master").group().strip('\n') + self.projects[self.fix_name+".git"]["branch"].append(branch_name) - if '*' in str(branch): - self.projects[self.fix_name]["active_branch"] = branch_name - else: - print(stderr) - raise ValueError("Command Error") + if '*' in str(branch): + self.projects[self.fix_name+".git"]["active_branch"] = branch_name + self.if_get_branches = True + else: + raise AttributeError("Get Project & Base Init & Fix Local First.") def list_branch(self): - for project in self.projects.items(): - for branch in project[1]["branch"]: - if branch == project[1]["active_branch"]: - print("(*)", branch) - else: - print(" ", branch) + if self.if_get_branches: + for project in self.projects.items(): + for branch in project[1]["branch"]: + if branch == project[1]["active_branch"]: + print("(*)", branch) + else: + print(" ", branch) if __name__ == "__main__": diff --git a/keygen.py b/keygen.py index b8867b7..1b560e8 100644 --- a/keygen.py +++ b/keygen.py @@ -10,7 +10,7 @@ class Key(ssh.SSH): self.connect(5) self.pub_key = None self.keygen = None - self.local_path = "C:\\Users\\Saturneric\\" + self.local_path = "/Users/huyibing/" self.public_key_path = self.local_path+".ssh/id_rsa.pub" self.public_key = None diff --git a/window.py b/window.py index 41efb7e..37c417c 100644 --- a/window.py +++ b/window.py @@ -1,5 +1,7 @@ from tkinter import * +from tkinter.messagebox import * import git +import os class Window(Frame): @@ -70,6 +72,7 @@ class Main(Window): self.push = Button(self) self.add = Button(self) self.commit = Button(self) + self.fix_local = Button(self) self.info = None self.git = None @@ -85,27 +88,32 @@ class Main(Window): self.connection_status["fg"] = "red" self.connection_status["text"] = "Succeed" self.connection_status["fg"] = "green" - - self.git.base_init() - self.git.update_projects() - self.broad.insert(INSERT, "--------------------------\n") - self.broad.insert(INSERT, "SSH Connection [Succeed]\n") - self.broad.insert(INSERT, self.hostname.get()+"\n") - self.broad.insert(INSERT, self.user_name.get()+"\n") + try: + self.git.base_init() + self.git.update_projects() + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "SSH Connection [Succeed]\n") + self.broad.insert(INSERT, self.hostname.get()+"\n") + self.broad.insert(INSERT, self.user_name.get()+"\n") + except AttributeError as errinfo: + showinfo(message=errinfo) + return def do_list(self): - self.git.list_projects() + try: + self.git.list_projects() + except AttributeError as errinfo: + showinfo(message=errinfo) + return self.broad.insert(INSERT, "--------------------------\n") self.broad.insert(INSERT, "Projects: \n") for project in self.git.projects.items(): - print(project) self.broad.insert(INSERT, "(*) "+project[0]+"\n") def new_project(self): self.info = Info(Tk()) self.info.information_label["text"] = "Project Name: " self.info.set_click(self.new_project_callback) - self.list_projects() def do_fix_project(self): self.info = Info(Tk()) @@ -115,25 +123,50 @@ class Main(Window): def new_project_callback(self): if self.info is not None: self.git.create_project(self.info.information.get()) + self.git.update_projects() self.info.master.destroy() self.info = None def do_fix_project_callback(self): if self.info is not None: - self.git.fix_project(self.info.information.get()) + try: + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "Fixing Project {0} \n".format(self.info.information.get())) + self.git.fix_project(self.info.information.get()) + except AttributeError as errinfo: + showinfo(message=errinfo) + return self.fix_project_label["text"] = "Fixed Project: {0}".format(self.info.information.get()) + try: + self.git.get_remote() + self.broad.insert(INSERT, "Get Remote...OK \n") + if os.path.exists(os.path.join(self.git.local_path, self.git.fix_name)): + self.broad.insert(INSERT, "Check Local Project...OK \n") + else: + self.do_clone_project() + self.broad.insert(INSERT, "Set Local Project...OK \n") + + self.do_fix_project_local() + self.broad.insert(INSERT, "Fix Local Project...OK \n") + self.git.get_branch() + self.broad.insert(INSERT, "Get Local Project's Branches...OK \n") + self.git.get_remote() + self.broad.insert(INSERT, "Set Local Project's Remote...OK \n") + except AttributeError as errinfo: + showinfo(message=errinfo) + return self.info.master.destroy() self.info = None - self.do_get_branch() - self.do_clone_project() - self.do_add_remote() - self.do_list_remote() def do_get_branch(self): - self.git.get_branch() + try: + self.git.get_branch() + except AttributeError as errinfo: + showinfo(message=errinfo) + return self.broad.insert(INSERT, "--------------------------\n") self.broad.insert(INSERT, "Branches: \n") - for branch in self.git.projects[self.git.fix_name]["branch"]: + for branch in self.git.projects[self.git.fix_name+".git"]["branch"]: print(branch) self.broad.insert(INSERT, "(*) " + branch + "\n") @@ -145,20 +178,41 @@ class Main(Window): def do_set_local_callback(self): if self.info is not None: - self.git.set_local(self.info.information.get()) + try: + if os.path.exists(self.info.information.get()): + self.git.set_local(self.info.information.get()) + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "Set Local Path: {0}\n".format(self.info.information.get())) + else: + showinfo(message="{0} not exists.".format(self.info.information.get())) + return + except AttributeError as errinfo: + showinfo(message=errinfo) + return + except FileNotFoundError as errinfo: + showinfo(message=errinfo) + return self.local_path_label["text"] = "Local Path: {0}".format(self.info.information.get()) self.info.master.destroy() self.info = None def do_clone_project(self): - self.git.clone_project() - self.git.add_remote() + self.git.init_project_local(self.git.fix_name) + # self.git.add_remote() def do_add_remote(self): - self.git.add_remote("origin") + try: + self.git.add_remote("origin") + except AttributeError as errinfo: + showinfo(errinfo) + return def do_list_remote(self): - self.git.get_remote() + try: + self.git.get_remote() + except AttributeError as errinfo: + showinfo(errinfo) + return self.broad.insert(INSERT, "--------------------------\n") self.broad.insert(INSERT, "Remotes: \n") for remote in self.git.remotes.items(): @@ -166,13 +220,39 @@ class Main(Window): self.broad.insert(INSERT, "(*) " + remote[1]["name"] + " " + remote[1]["url"] + "\n") def do_pull(self): - self.git.pull_remote("origin", "master") + try: + stdout = self.git.pull_remote("origin", "master") + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "{0}\n".format(stdout)) + + except AttributeError as errinfo: + showinfo(message=errinfo) + return def do_push(self): - self.git.push_remote("origin", "master") + try: + stdout = self.git.push_remote("origin", "master") + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "{0}\n".format(stdout)) + except AttributeError as errinfo: + showinfo(message=errinfo) + return def do_add(self): - self.git.add() + try: + stdout=self.git.add() + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "{0}\n".format(stdout)) + except AttributeError as errinfo: + showinfo(message=errinfo) + return + + def do_fix_project_local(self): + try: + self.git.fix_project_local() + except AttributeError as errinfo: + showinfo(message=errinfo) + return def do_commit(self): self.info = Info(Tk()) @@ -181,7 +261,13 @@ class Main(Window): self.info.set_click(self.do_commit_callback) def do_commit_callback(self): - self.git.commit_local(self.info.information.get()) + try: + stdout=self.git.commit_local(self.info.information.get()) + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "{0}\n".format(stdout)) + except AttributeError as errinfo: + showinfo(message=errinfo) + return self.info.master.destroy() self.info = None @@ -257,13 +343,17 @@ class Main(Window): self.commit["command"] = self.do_commit self.commit.grid(row=6, column=1) + self.fix_local["text"] = "Fix Local" + self.fix_local["command"] = self.do_fix_project_local + self.fix_local.grid(row=7, column=0) + self.broad.grid(row=1, column=2, columnspan=7, rowspan=6) if __name__ == "__main__": root = Tk() main = Main(root) - main.set_width(900) + main.set_width(1150) main.apply() main.mainloop()