class Deleter: def __init__(self, client, args): self.__client = client self.__args = args self.__userRepo = Db().Repositories[self.__args.username] self.__repo_name = os.path.basename(self.__args.path_dir_pj) def ShowDeleteRecords(self): repo = self.__userRepo['Repositories'].find_one(Name=self.__repo_name) Log().Logger.info(repo) Log().Logger.info( self.__userRepo['Counts'].find_one(RepositoryId=repo['Id'])) for record in self.__userRepo['Languages'].find( RepositoryId=repo['Id']): Log().Logger.info(record) def Delete(self): self.__DeleteLocalRepository() self.__client.Repositories.delete() self.__DeleteDb() def __DeleteLocalRepository(self): import shutil shutil.rmtree(os.path.join(self.__args.path_dir_pj, '.git')) def __DeleteDb(self): repo = self.__userRepo['Repositories'].find_one(Name=self.__repo_name) self.__userRepo.begin() self.__userRepo['Languages'].delete(RepositoryId=repo['Id']) self.__userRepo['Counts'].delete(RepositoryId=repo['Id']) self.__userRepo['Repositories'].delete(Id=repo['Id']) self.__userRepo.commit()
class Commiter: def __init__(self, client, args): self.__client = client self.__args = args self.__userRepo = Db().Repositories[self.__args.username] self.__repo_name = os.path.basename(self.__args.path_dir_pj) def ShowCommitFiles(self): cui.sh.Client.Client.Run("git add -n .", cwd=self.__args.path_dir_pj) def AddCommitPushIssue(self, commit_messages): if 1 == len(commit_messages): issue = self.__client.Issues.create(commit_messages[0]) elif 2 <= len(commit_messages) and '' != commit_messages[1]: commit_messages.insert(1, '') issue = self.__client.Issues.create(commit_messages[0], body='\n'.join(commit_messages[2:])) else: issue = self.__client.Issues.create(commit_messages[0], body='\n'.join(commit_messages[2:])) # http://surumereflection.hatenadiary.jp/entry/2016/09/14/223838 # git commit -m "fix #Issue番号 1行目" -m "2行目" -m "3行目" ... message_command = '' for i, line in enumerate(commit_messages): if 0 == i: message_command += ' -m "fix #' + str(issue['number']) + ' ' + line + '" ' else: message_command += ' -m "' + line + '" ' client = cui.sh.Client.Client({'cwd': self.__args.path_dir_pj}) client.run("git add .") client.run("git commit {0}".format(message_command)) client.run("git push origin master") time.sleep(3) self.__InsertLanguages(self.__client.Repositories.list_languages()) def AddCommitPush(self, commit_message): client = cui.sh.Client.Client(cwd=self.__args.path_dir_pj) client.run("git add .") client.run("git commit -m '{0}'".format(commit_message)) client.run("git push origin master") time.sleep(3) self.__InsertLanguages(self.__client.Repositories.list_languages()) def __InsertLanguages(self, j): self.__userRepo.begin() repo_id = self.__userRepo['Repositories'].find_one(Name=os.path.basename(self.__args.path_dir_pj))['Id'] self.__userRepo['Languages'].delete(RepositoryId=repo_id) for key in j.keys(): self.__userRepo['Languages'].insert(dict( RepositoryId=repo_id, Language=key, Size=j[key] )) self.__userRepo.commit()
class Commiter: def __init__(self, client, args): # def __init__(self, db, client, args): # self.__db = db self.__client = client self.__args = args self.__userRepo = Db().Repositories[self.__args.username] #self.__userRepo = self.__db.Repositories[self.__args.username] self.__repo_name = os.path.basename(self.__args.path_dir_pj) def ShowCommitFiles(self): cui.sh.Client.Client.Run("git add -n .", cwd=self.__args.path_dir_pj) #subprocess.call(shlex.split("git add -n ."), cwd=self.__args.path_dir_pj) def AddCommitPushIssue(self, commit_messages): if 1 == len(commit_messages): issue = self.__client.Issues.create(commit_messages[0]) elif 2 <= len(commit_messages) and '' != commit_messages[1]: commit_messages.insert(1, '') issue = self.__client.Issues.create(commit_messages[0], body='\n'.join( commit_messages[2:])) else: issue = self.__client.Issues.create(commit_messages[0], body='\n'.join( commit_messages[2:])) # http://surumereflection.hatenadiary.jp/entry/2016/09/14/223838 # git commit -m "fix #Issue番号 1行目" -m "2行目" -m "3行目" ... message_command = '' for i, line in enumerate(commit_messages): if 0 == i: message_command += ' -m "fix #' + str( issue['number']) + ' ' + line + '" ' else: message_command += ' -m "' + line + '" ' client = cui.sh.Client.Client({'cwd': self.__args.path_dir_pj}) client.run("git add .") client.run("git commit {0}".format(message_command)) client.run("git push origin master") """ subprocess.call(shlex.split("git add .", cwd=self.__args.path_dir_pj)) subprocess.call(shlex.split("git commit {0}".format(message_command), cwd=self.__args.path_dir_pj)) subprocess.call(shlex.split("git push origin master", cwd=self.__args.path_dir_pj)) """ """ subprocess.call(shlex.split("git add .")) subprocess.call(shlex.split('git commit ' + message_command)) subprocess.call(shlex.split("git push origin master")) """ time.sleep(3) self.__InsertLanguages(self.__client.Repositories.list_languages()) def AddCommitPush(self, commit_message): #client = cui.sh.Client.Client({'cwd': self.__args.path_dir_pj}) client = cui.sh.Client.Client(cwd=self.__args.path_dir_pj) client.run("git add .") client.run("git commit -m '{0}'".format(commit_message)) client.run("git push origin master") time.sleep(3) self.__InsertLanguages(self.__client.Repositories.list_languages()) """ subprocess.call(shlex.split("git add ."), cwd=self.__args.path_dir_pj) print("git add .") subprocess.call(shlex.split("git commit -m '{0}'".format(commit_message)), cwd=self.__args.path_dir_pj) print("git commit -m '{0}'".format(commit_message)) # 2018-02-15 追加 start # https://github.com/{0}/{1}.git #subprocess.call(shlex.split("git remote add origin [email protected]:{0}/{1}.git".format(self.__args.username, self.__repo_name))) # 2018-02-15 追加 end subprocess.call(shlex.split("git push origin master"), cwd=self.__args.path_dir_pj) print("git push origin master") time.sleep(3) print("ローカルDBに追加 start") self.__InsertLanguages(self.__client.Repositories.list_languages()) print("ローカルDBに追加 end") """ def __InsertLanguages(self, j): self.__userRepo.begin() repo_id = self.__userRepo['Repositories'].find_one( Name=os.path.basename(self.__args.path_dir_pj))['Id'] self.__userRepo['Languages'].delete(RepositoryId=repo_id) for key in j.keys(): self.__userRepo['Languages'].insert( dict(RepositoryId=repo_id, Language=key, Size=j[key])) self.__userRepo.commit()
class Creator: def __init__(self, client, args): # def __init__(self, db, client, args): # self.__db = db self.__client = client self.__args = args self.__userRepo = Db().Repositories[self.__args.username] #self.__userRepo = self.__db.Repositories[self.__args.username] self.__repo_name = os.path.basename(self.__args.path_dir_pj) def Create(self): self.__LoadDb() self.__CreateLocalRepository() j = self.__client.Repositories.create(self.__repo_name, description=self.__args.description, homepage=self.__args.homepage) self.__InsertRemoteRepository(j) def __LoadDb(self): self.__account = Db().Accounts['Accounts'].find_one(Username=self.__args.username) #self.__account = self.__db.Accounts['Accounts'].find_one(Username=self.__args.username) if None is self.__account: raise Exception('未登録のアカウントです。登録してから再度実行してください。') self.__sshconfigures = Db().Accounts['SshConfigures'].find_one(AccountId=self.__account['Id']) #self.__sshconfigures = self.__db.Accounts['SshConfigures'].find_one(AccountId=self.__account['Id']) def __CreateLocalRepository(self): subprocess.call(shlex.split("git init")) print("git init") subprocess.call(shlex.split("git config --local user.name '{0}'".format(self.__args.username))) print("git config --local user.name '{0}'".format(self.__args.username)) subprocess.call(shlex.split("git config --local user.email '{0}'".format(self.__account['MailAddress']))) print("git config --local user.email '{0}'".format(self.__account['MailAddress'])) # HTTPS, SSL どちらかによってリポジトリ文字列を変える repo_str = self.__RemoteRepositoryName() """ なぜかできない。LinuxMint17.3ではできたが、RaspberryPi3のRaspbianではできなかった。 SSH通信ができない。ので、HTTPS通信に変更する。SSHよりセキュリティが弱い。 subprocess.call(shlex.split("git remote add origin git@{0}:{1}/{2}.git".format(self.__sshconfigures['HostName'], self.__args.username, self.__repo_name))) print("git remote add origin git@{0}:{1}/{2}.git".format(self.__sshconfigures['HostName'], self.__args.username, self.__repo_name)) """ # https://{user}:{pass}@github.com/{user}/{repo}.git # subprocess.call(shlex.split("git remote add origin https://{0}:{1}@github.com/{0}/{2}.git".format(self.__args.username, self.__account['Password'], self.__repo_name))) # print("git remote add origin https://{0}:{1}@github.com/{0}/{2}.git".format(self.__args.username, self.__account['Password'], self.__repo_name)) subprocess.call(shlex.split("git remote add origin {0}".format(repo_str))) print("git remote add origin {0}".format(repo_str)) def __InsertRemoteRepository(self, j): self.__userRepo.begin() repo = self.__userRepo['Repositories'].find_one(Name=j['name']) # Repositoriesテーブルに挿入する if None is repo: self.__userRepo['Repositories'].insert(self.__CreateRecordRepositories(j)) repo = self.__userRepo['Repositories'].find_one(Name=j['name']) # 何らかの原因でローカルDBに既存の場合はそのレコードを更新する else: self.__userRepo['Repositories'].update(self.__CreateRecordRepositories(j), ['Name']) # Countsテーブルに挿入する cnt = self.__userRepo['Counts'].count(RepositoryId=repo['Id']) if 0 == cnt: self.__userRepo['Counts'].insert(self.__CreateRecordCounts(self.__userRepo['Repositories'].find_one(Name=j['name'])['Id'], j)) # 何らかの原因でローカルDBに既存の場合はそのレコードを更新する else: self.__userRepo['Counts'].update(self.__CreateRecordCounts(repo['Id'], j), ['RepositoryId']) self.__userRepo.commit() def __CreateRecordRepositories(self, j): return dict( IdOnGitHub=j['id'], Name=j['name'], Description=j['description'], Homepage=j['homepage'], CreatedAt=j['created_at'], PushedAt=j['pushed_at'], UpdatedAt=j['updated_at'], CheckedAt="{0:%Y-%m-%dT%H:%M:%SZ}".format(datetime.datetime.now(pytz.utc)) ) def __CreateRecordCounts(self, repo_id, j): return dict( RepositoryId=repo_id, Forks=j['forks_count'], Stargazers=j['stargazers_count'], Watchers=j['watchers_count'], Issues=j['open_issues_count'] ) def __RemoteRepositoryName(self): # HTTPS, SSL どちらかによってリポジトリ文字列を変える #(Uploader.pyですでに実行しているのに2度目の実行をしてる。文字列比較ダサい。もっとスマートに実装できないか) self.__setting = setting.Setting.Setting() if 'HTTPS' == self.__setting.GitRemote: return "https://{0}:{1}@github.com/{0}/{2}.git".format(self.__args.username, self.__account['Password'], self.__repo_name) elif 'SSH' == self.__setting.GitRemote: return "git@{0}:{1}/{2}.git".format(self.__sshconfigures['HostName'], self.__args.username, self.__repo_name) else: raise Exception('config.iniの[Git]Remoteは HTTPS か SSL のみ有効です。: {0}'.format(self.__setting.GitRemote))
class Creator: def __init__(self, client, args): # def __init__(self, db, client, args): # self.__db = db self.__client = client self.__args = args self.__userRepo = Db().Repositories[self.__args.username] #self.__userRepo = self.__db.Repositories[self.__args.username] self.__repo_name = os.path.basename(self.__args.path_dir_pj) def Create(self): self.__LoadDb() self.__CreateLocalRepository() j = self.__client.Repositories.create( self.__repo_name, description=self.__args.description, homepage=self.__args.homepage) self.__InsertRemoteRepository(j) def __LoadDb(self): self.__account = Db().Accounts['Accounts'].find_one( Username=self.__args.username) #self.__account = self.__db.Accounts['Accounts'].find_one(Username=self.__args.username) if None is self.__account: raise Exception('未登録のアカウントです。登録してから再度実行してください。') self.__sshconfigures = Db().Accounts['SshConfigures'].find_one( AccountId=self.__account['Id']) #self.__sshconfigures = self.__db.Accounts['SshConfigures'].find_one(AccountId=self.__account['Id']) def __CreateLocalRepository(self): #client = cui.sh.Client.Client({'cwd': self.__args.path_dir_pj}) client = cui.sh.Client.Client(cwd=self.__args.path_dir_pj) client.run("git init") client.run("git config --local user.name '{0}'".format( self.__args.username)) client.run("git config --local user.email '{0}'".format( self.__account['MailAddress'])) client.run("git remote add origin {0}".format( Config()['Git']['Remote'].GetRepositoryUri(self.__args.username, self.__repo_name))) """ subprocess.call(shlex.split("git init"), cwd=self.__args.path_dir_pj) print("git init") subprocess.call(shlex.split("git config --local user.name '{0}'".format(self.__args.username)), cwd=self.__args.path_dir_pj) print("git config --local user.name '{0}'".format(self.__args.username)) subprocess.call(shlex.split("git config --local user.email '{0}'".format(self.__account['MailAddress'])), cwd=self.__args.path_dir_pj) print("git config --local user.email '{0}'".format(self.__account['MailAddress'])) # HTTPS, SSL どちらかによってリポジトリ文字列を変える repo_str = Config()['Git']['Remote'].GetRepositoryUri(self.__args.username, self.__repo_name) subprocess.call(shlex.split("git remote add origin {0}".format(repo_str)), cwd=self.__args.path_dir_pj) print("git remote add origin {0}".format(repo_str)) """ def __InsertRemoteRepository(self, j): self.__userRepo.begin() repo = self.__userRepo['Repositories'].find_one(Name=j['name']) # Repositoriesテーブルに挿入する if None is repo: self.__userRepo['Repositories'].insert( self.__CreateRecordRepositories(j)) repo = self.__userRepo['Repositories'].find_one(Name=j['name']) # 何らかの原因でローカルDBに既存の場合はそのレコードを更新する else: self.__userRepo['Repositories'].update( self.__CreateRecordRepositories(j), ['Name']) # Countsテーブルに挿入する cnt = self.__userRepo['Counts'].count(RepositoryId=repo['Id']) if 0 == cnt: self.__userRepo['Counts'].insert( self.__CreateRecordCounts( self.__userRepo['Repositories'].find_one( Name=j['name'])['Id'], j)) # 何らかの原因でローカルDBに既存の場合はそのレコードを更新する else: self.__userRepo['Counts'].update( self.__CreateRecordCounts(repo['Id'], j), ['RepositoryId']) self.__userRepo.commit() def __CreateRecordRepositories(self, j): return dict(IdOnGitHub=j['id'], Name=j['name'], Description=j['description'], Homepage=j['homepage'], CreatedAt=j['created_at'], PushedAt=j['pushed_at'], UpdatedAt=j['updated_at'], CheckedAt="{0:%Y-%m-%dT%H:%M:%SZ}".format( datetime.datetime.now(pytz.utc))) def __CreateRecordCounts(self, repo_id, j): return dict(RepositoryId=repo_id, Forks=j['forks_count'], Stargazers=j['stargazers_count'], Watchers=j['watchers_count'], Issues=j['open_issues_count']) """