From 6d8d5d8c8aed6bd1b7ff2f2f0da89fa828d2ec53 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 11 Mar 2019 18:58:59 +0800 Subject: [PATCH] Fixed and Added. --- .idea/workspace.xml | 158 ++++++++++++++++++++------ git.py | 77 +++++++++---- keygen.py | 20 ++-- window.py | 270 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 460 insertions(+), 65 deletions(-) create mode 100644 window.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 02116a9..32bad9b 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,15 +2,10 @@ - - - - + - - - - + + - + + + - + + + + + + + + + + + + + + + + + + @@ -210,12 +279,20 @@ - @@ -247,6 +324,10 @@ + + + @@ -259,27 +340,40 @@ - - - - - - - - - + + - + - - + + + + + + + + + + + + + + + + + + + + + + diff --git a/git.py b/git.py index 0058867..91e3f2a 100644 --- a/git.py +++ b/git.py @@ -41,7 +41,7 @@ class Git(ssh.SSH): print("Create directory.") else: raise ValueError(stderr) - self.base_path = [True, "{0}/{1}".format(self.path, "giteasy")] + self.base_path = [True, "{0}{1}".format(self.path, "giteasy")] else: raise ValueError("Home Path Abnormal") @@ -67,14 +67,28 @@ class Git(ssh.SSH): else: raise UnboundLocalError("Init Base 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)) + def init_project_local(self, name): - cmd = self.fix_cmd - cmd += "mkdir {0};".format(name) - cmd += "git init;" - cmd = "git config --global user.email \"{0}\";".format(self.git_email) - cmd += "git config --global user.name \"{0}\";".format(self.git_user) + os.chdir(self.local_path) + try: + os.mkdir(name) + except FileExistsError: + 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) + 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()) + def update_projects(self): if self.base_path[0]: stdout, stderr = self.run("ls {0}".format(self.base_path[1])) @@ -104,9 +118,8 @@ class Git(ssh.SSH): os.chdir(self.local_path) 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)) + 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)} @@ -119,11 +132,16 @@ class Git(ssh.SSH): ret_code = re.compile(r"[\t, ]") for remote in os.popen("git remote -v").readlines(): results = ret_code.split(remote) - for item in results: - if item[0] not in self.remotes.keys(): - self.remotes[item[0]] = {"name": item[0], "url": item[1]} + 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]} 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)) else: @@ -135,13 +153,13 @@ class Git(ssh.SSH): else: raise ValueError("Remote Error") - def fix(self, name): + 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): - print("Fixed.") 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") @@ -155,10 +173,21 @@ class Git(ssh.SSH): else: raise UnboundLocalError("Init Base First") + def commit_local(self, message): + if self.base_path[0]: + os.popen("git commit --message={0}".format(message)) + else: + raise UnboundLocalError("Init Base First") + + @staticmethod + def add(): + os.popen("git add *") + def get_branch(self): - if self.base_path[0] and self.fix_name is None: + 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') @@ -182,17 +211,15 @@ class Git(ssh.SSH): if __name__ == "__main__": compute = Git("compute.bktus.com", "/home/git", "git", "123456") compute.global_init(git_user="saturneric", git_email="eric.bktu@gmail.com") + compute.global_init_local() compute.base_init() # compute.create_project("lcs") compute.update_projects() compute.list_projects() - compute.fix("lcs") - - # compute.get_branch("lcs") - compute.set_local("C:/Users/Saturneric/Documents/Code/") - compute.init_project_local("test") - # compute.add_remote(); - # compute.get_remote() - # compute.get_branch() - # compute.list_branch() - print(compute.projects) + compute.fix_project("lcs") + compute.get_branch() + compute.set_local("C:\\Users\\Saturneric\\Documents\\Code\\") + # compute.init_project_local("test") + compute.add_remote() + compute.get_remote() + compute.list_branch() diff --git a/keygen.py b/keygen.py index 4c4a1f4..b8867b7 100644 --- a/keygen.py +++ b/keygen.py @@ -10,22 +10,26 @@ class Key(ssh.SSH): self.connect(5) self.pub_key = None self.keygen = None + self.local_path = "C:\\Users\\Saturneric\\" + self.public_key_path = self.local_path+".ssh/id_rsa.pub" + self.public_key = None def add_key(self): - self.run("echo \"{0}\" >> ~/.ssh/authorized_keys".format(self.pub_key)) + self.run("echo \"{0}\" >> ~/.ssh/authorized_keys".format(self.public_key)) - def create_key(self): - ret_code = subprocess.Popen(["ssh-keygen", "-b 4096", "-t rsa"], shell=True, + @staticmethod + def create_key(): + ret_code = subprocess.Popen(["ssh-keygen", "-b 4096"], shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE) - stdout, stderr = ret_code.communicate(input=b"\ny \n \n \n") - self.get_key() + stdout, stderr = ret_code.communicate(input=b"\n \n \n \n") def get_key(self): - self.pub_key = os.popen("cat ~/.ssh/id_rsa.pub").read() + self.public_key = open(self.public_key_path, 'r').readline() if __name__ == "__main__": new_key = Key("compute.bktus.com", "/home/git", "git", "123456", ssh_key=False) # new_key.create_key() - # new_key.get_key() - # new_key.add_key() + new_key.get_key() + print(new_key.public_key) + new_key.add_key() diff --git a/window.py b/window.py new file mode 100644 index 0000000..41efb7e --- /dev/null +++ b/window.py @@ -0,0 +1,270 @@ +from tkinter import * +import git + + +class Window(Frame): + def __init__(self, master = None): + Frame.__init__(self, master) + self.master = master + self.master = master + self.pack() + self.width = 800 + self.height = 600 + self.position = [10, 10] + + def set_width(self, width): + self.width = width + + def set_height(self, height): + self.height = height + + def set_position(self, x=10, y=10): + self.position = [x, y] + + def apply(self): + self.master.geometry("{0}x{1}+{2}+{3}".format(self.width, self.height, self.position[0], self.position[1])) + + +class Info(Window): + def __init__(self, master=None): + super().__init__(master) + self.information_label = Label(self) + self.information = Entry(self) + self.ok = Button(self) + self.draw_widgets() + + def draw_widgets(self): + self.information_label["text"] = "Info: " + self.information_label.grid(row=0, column=0) + self.information.grid(row=0, column=1) + self.ok["text"] = "OK" + self.ok.grid(row=0, column=2) + + def set_click(self, func): + self.ok["command"] = func + + +class Main(Window): + def __init__(self, master=None): + super().__init__(master) + self.connect = None + self.hostname_label = Label(self) + self.user_name_label = Label(self) + self.password_label = Label(self) + self.connection_status = Label(self) + self.hostname = Entry(self) + self.user_name = Entry(self) + self.password = Entry(self) + self.list_projects = Button(self) + self.fix_project = Button(self) + self.create_project = Button(self) + self.broad = Text(self) + self.fix_project_label = Label(self) + self.local_path_label = Label(self) + self.get_branch = Button(self) + self.set_local = Button(self) + self.add_remote = Button(self) + self.list_remote = Button(self) + self.clone_project = Button(self) + self.pull = Button(self) + self.push = Button(self) + self.add = Button(self) + self.commit = Button(self) + self.info = None + + self.git = None + self.draw_widget() + + def do_connect(self): + try: + self.git = git.Git(hostname=self.hostname.get(), + user=self.user_name.get(), passwd=self.password.get(), + path="/home/git/") + finally: + self.connection_status["text"] = "Failed" + 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") + + def do_list(self): + self.git.list_projects() + 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()) + self.info.information_label["text"] = "Project Name: " + self.info.set_click(self.do_fix_project_callback) + + def new_project_callback(self): + if self.info is not None: + self.git.create_project(self.info.information.get()) + 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()) + self.fix_project_label["text"] = "Fixed Project: {0}".format(self.info.information.get()) + 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() + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "Branches: \n") + for branch in self.git.projects[self.git.fix_name]["branch"]: + print(branch) + self.broad.insert(INSERT, "(*) " + branch + "\n") + + def do_set_local(self): + self.info = Info(Tk()) + self.info.information_label["text"] = "Local Path: " + self.info.information.insert(END, "C:\\Users\\Saturneric\\Documents\\Code\\") + self.info.set_click(self.do_set_local_callback) + + def do_set_local_callback(self): + if self.info is not None: + self.git.set_local(self.info.information.get()) + 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() + + def do_add_remote(self): + self.git.add_remote("origin") + + def do_list_remote(self): + self.git.get_remote() + self.broad.insert(INSERT, "--------------------------\n") + self.broad.insert(INSERT, "Remotes: \n") + for remote in self.git.remotes.items(): + print(remote) + self.broad.insert(INSERT, "(*) " + remote[1]["name"] + " " + remote[1]["url"] + "\n") + + def do_pull(self): + self.git.pull_remote("origin", "master") + + def do_push(self): + self.git.push_remote("origin", "master") + + def do_add(self): + self.git.add() + + def do_commit(self): + self.info = Info(Tk()) + self.info.information_label["text"] = "Local Path: " + self.info.information.insert(END, "Default") + self.info.set_click(self.do_commit_callback) + + def do_commit_callback(self): + self.git.commit_local(self.info.information.get()) + self.info.master.destroy() + self.info = None + + def draw_widget(self): + self.hostname_label["text"] = "Hostname: " + self.hostname_label.grid(row=0, column=0) + self.hostname.insert(END, "compute.bktus.com") + self.hostname.grid(row=0, column=1) + self.user_name_label["text"] = "Username: " + self.user_name_label.grid(row=0, column=2) + self.user_name.insert(END, "git") + self.user_name.grid(row=0, column=3) + self.password_label["text"] = "Password: " + self.password_label.grid(row=0, column=4) + self.password.grid(row=0, column=5) + self.connection_status.grid(row=0, column=7) + + self.connect = Button(self) + self.connect["text"] = "Connect" + self.connect["command"] = self.do_connect + self.connect.grid(row=0, column=6) + + self.list_projects["text"] = "List Projects" + self.list_projects["command"] = self.do_list + self.list_projects.grid(row=1, column=0) + + self.fix_project["text"] = " Fix Project " + self.fix_project["command"] = self.do_fix_project + self.fix_project.grid(row=2, column=0) + + self.create_project["text"] = "Create Project" + self.create_project["command"] = self.new_project + self.create_project.grid(row=1, column=1) + + self.fix_project_label["text"] = "Fixed Project: None" + self.fix_project_label.grid(row=8, column=6) + self.local_path_label["text"] = "Local Path: None" + self.local_path_label.grid(row=9, column=0, columnspan=6) + + self.get_branch["text"] = "Get Branch" + self.get_branch["command"] = self.do_get_branch + self.get_branch.grid(row=3,column=0) + + self.set_local["text"] = "Set Local" + self.set_local["command"] = self.do_set_local + self.set_local.grid(row=3, column=1) + + self.clone_project["text"] = "Clone Project" + self.clone_project["command"] = self.do_clone_project + self.clone_project.grid(row=2, column=1) + + self.add_remote["text"] = "Add Remote" + self.add_remote["command"] = self.do_add_remote + self.add_remote.grid(row=4, column=0) + + self.list_remote["text"] = "List Remote" + self.list_remote["command"] = self.do_list_remote + self.list_remote.grid(row=4, column=1) + + self.pull["text"] = "Pull" + self.pull["command"] = self.do_pull + self.pull.grid(row=5, column=0) + + self.push["text"] = "Push" + self.push["command"] = self.do_push + self.push.grid(row=5, column=1) + + self.add["text"] = "Add" + self.add["command"] = self.do_add + self.add.grid(row=6, column=0) + + self.commit["text"] = "Commit" + self.commit["command"] = self.do_commit + self.commit.grid(row=6, column=1) + + self.broad.grid(row=1, column=2, columnspan=7, rowspan=6) + + +if __name__ == "__main__": + root = Tk() + main = Main(root) + main.set_width(900) + main.apply() + main.mainloop() + +