def __CreateToken(scopes): account = Db().Accounts['Accounts'].find_one(Username=self.__username) if None is account: raise Exception('指定ユーザ {user} はDB未登録です。登録してください。') Db().Accounts['Accounts'].insert(self.__CreateRecordAccount()) twofactor = Db().Accounts['TwoFactors'].find_one(AccountId=account['Id']) if twofactor is None: else: """ # 再帰呼出になる! from web.service.github.api.v3.authentication.TwoFactorAuthentication import TwoFactorAuthentication auth = TwoFactorAuthentication(self.__username, account['Passowrd'], twofactor['Secret']) from web.service.github.api.v3.Client import Client j = Client().Authorizations.Create(scopes=scopes) Db().Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], j)) return j['token'] """ """ api = web.service.github.api.v3.Authorizations(self.__username, self.__password) j = api.Create(scopes=scopes) #j = api.Create(otp=self.__totp.now(), scopes=scopes) Db().Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], j)) return j['token'] """ otp = None if None is not self.__two_factor_secret: self.__totp = pyotp.TOTP(self.__two_factor_secret) otp = self.__totp.now() api = web.service.github.api.v3.Authorizations(self.__username, self.__password) j = api.Create(otp=self.__totp.now(), scopes=scopes) Db().Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], j)) return j['token']
def __init__(self, client, args): # def __init__(self, db, client, args): # self.__db = db self.__client = client self.__args = args self.__creator = cui.uploader.command.repository.Creator.Creator(self.__client, self.__args) self.__commiter = cui.uploader.command.repository.Commiter.Commiter(self.__client, self.__args) self.__deleter = cui.uploader.command.repository.Deleter.Deleter(self.__client, self.__args) self.__editor = cui.uploader.command.repository.Editor.Editor(self.__client, self.__args) self.__agg = cui.uploader.command.aggregate.Aggregate.Aggregate(self.__args) """ self.__creator = cui.uploader.command.repository.Creator.Creator(self.__db, self.__client, self.__args) self.__commiter = cui.uploader.command.repository.Commiter.Commiter(self.__db, self.__client, self.__args) self.__deleter = cui.uploader.command.repository.Deleter.Deleter(self.__db, self.__client, self.__args) self.__editor = cui.uploader.command.repository.Editor.Editor(self.__db, self.__client, self.__args) self.__agg = cui.uploader.command.aggregate.Aggregate.Aggregate(self.__db, self.__args) """ #self.__account = self.__db.Accounts['Accounts'].find_one(Username=self.__args.username) #self.__ssh_configures = self.__db.Accounts['SshConfigures'].find_one(AccountId=self.__account['Id']) self.__account = Db().Accounts['Accounts'].find_one(Username=self.__args.username) self.__ssh_configures = Db().Accounts['SshConfigures'].find_one(AccountId=self.__account['Id']) # print(self.__args.path_dir_pj) self.__repo_name = os.path.basename(self.__args.path_dir_pj) self.__repos = Db().Repositories[self.__args.username]['Repositories'].find_one(Name=self.__repo_name)
def __GetUserSecret(self): parser = argparse.ArgumentParser( description='GitHub Repository Uploader.', ) parser.add_argument('-u', '--username', '--user') args = parser.parse_args() username = args.username if None is username: if not ('GitHub' in Config()): raise Exception( 'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。yamlならGithub.Userキーにユーザ名を指定してください。' ) if not ('User' in Config()['Github']): raise Exception( 'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。yamlならGithub.Userキーにユーザ名を指定してください。' ) username = Config()['Github']['User'] Log().Logger.info("username = {0}".format(username)) account = Db().Accounts['Accounts'].find_one(Username=username) if None is account: raise Exception( 'ユーザ {0} はDBのAccountsテーブルに存在しません。登録してから再度実行してください。'.format( username)) twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if None is twofactor: raise Exception( 'ユーザ {0} はDBのTwoFactorsテーブルに存在しません。登録してから再度実行してください。'.format( username)) return twofactor['Secret']
def __LoadDb(self): self.__account = 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'])
def Route(self, http_method, endpoint, username=None): if None is username: username = self.__username account = Db().Accounts['Accounts'].find_one(Username=username) api = Db().Apis['Apis'].find_one(HttpMethod=http_method.upper(), Endpoint=endpoint) if None is api or None is api['AuthMethods'] or '' == api[ 'AuthMethods'].strip(): if None is username: return NonAuthentication() else: return self.__GetTokenAuth(account, grants) #else: return OAuthAuthentication(account['AccessToken']) else: if None is username: raise Exception( '指定したユーザ {0} はDBに未登録です。登録してから実行してください。'.format(username)) authMethods = [m.strip() for m in api['AuthMethods'].split(",")] grants = [g.strip() for g in api['Grants'].split(",")] print('AAAAAAAAAAAA:', authMethods) print('BBBBBBBBBBBB:', grants) #if 1 == len(authMethods) and '' == authMethods[0].strip(): authMethods = None if 1 == len(grants) and 0 == len(grants[0].strip()): grants = None print('CCCCCCCCCCCC:', grants) if "Token" in authMethods: return self.__GetTokenAuth(account, grants) elif "Basic" in authMethods: return self.__GetBasicAuth(account) elif "ClientId" in authMethods: raise NotImplementedError( 'ClientId認証は未実装です。Not implemented clientId authorization.') else: raise NotImplementedError( 'ApiDBに登録された次の認証方法は未実装です。: {0} {1} {2}'.format( api['HttpMethod'], api['Endpoint'], authMethods))
def SetAccessToken(self, scopes=None): sql = "SELECT * FROM AccessTokens WHERE AccountId == {0}".format( Db().Accounts['Accounts'].find_one(Username=self.__username)['Id']) if not (None is scopes) and isinstance(scopes, list) and 0 < len(scopes): #if not(None is scopes) and not('' == scopes.strip()) and isinstance(scopes, list) and 0 < len(scopes): sql = sql + " AND (" for s in scopes: sql = sql + "(',' || Scopes || ',') LIKE '%,{0},%'".format( s) + " OR " sql = sql.rstrip(" OR ") sql = sql + ')' print(sql) res = Db().Accounts.query(sql) ret = None for r in res: print(r) ret = r if None is ret: raise OAuthTokenFromDatabaseAuthentication.NotHasGrantsException( self.__username, scopes) #raise Exception('指定されたScope {scopes} のうち少なくとも1つを持ったTokenが存在しません。: username={username}'.format(scopes=scopes, username=self.__username)) # このときTokenを生成するAPIを使うにはパスワードが必要。Basic認証でないと使えない self.__token = ret['AccessToken'] # print(self.__token) return self.__token
def Run(self, args): Log().Logger.debug('Account.Delete') Log().Logger.debug(args) Log().Logger.debug('-u: {0}'.format(args.username)) Log().Logger.debug('--auto: {0}'.format(args.auto)) account = Db().Accounts['Accounts'].find_one(Username=args.username) Log().Logger.debug(account) if None is account: Log().Logger.warning('指定したユーザ {0} がDBに存在しません。削除を中止します。'.format( args.username)) return else: authentications = [] twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if None is not twofactor: authentications.append( TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append( BasicAuthentication(account['Username'], account['Password'])) client = web.service.github.api.v3.Client.Client(authentications) # 1. 指定ユーザの全Tokenを削除する(SSHKey設定したTokenのはずなのでSSHKeyも削除される self.__DeleteToken(account, client) # 2. SSHのconfigファイル設定の削除と鍵ファイルの削除 self.__DeleteSshFile(account['Id']) # 3. DB設定値(Account, Repository) self.__DeleteDatabase(account)
def Get(self, http_method, endpoint): api = Db().Apis['Apis'].find_one(HttpMethod=http_method.upper(), Endpoint=endpoint) # api = self.__db.Apis['Apis'].find_one(HttpMethod=http_method.upper(), Endpoint=endpoint) if None is api: for a in self.__authentications: if hasattr( a, 'SetAccessToken' ): # OAuthTokenFromDatabaseAndCreateApiAuthentication, OAuthTokenFromDatabaseAuthentication a.SetAccessToken() return a.GetRequestParameters() return NonAuthentication().GetRequestParameters() grants = api['Grants'].split(",") if ("Token" in api['AuthMethods']): for a in self.__authentications: if isinstance(a, OAuthAuthentication): if hasattr( a, 'SetAccessToken' ): # OAuthTokenFromDatabaseAndCreateApiAuthentication, OAuthTokenFromDatabaseAuthentication a.SetAccessToken() return a.GetRequestParameters() if ("Basic" in api['AuthMethods']): for a in self.__authentications: if isinstance(a, TwoFactorAuthentication): return a.GetRequestParameters() elif isinstance(a, BasicAuthentication): return a.GetRequestParameters() if ("ClientId" in api['AuthMethods']): raise Exception('Not implemented clientId authorization.') raise Exception('Not found AuthMethods: {0} {1} {2}'.format( api['HttpMethod'], api['Endpoint'], self.__GetClassNames()))
def Run(self, args): Log().Logger.debug('Account.Update') Log().Logger.debug(args) Log().Logger.debug('-u: {0}'.format(args.username)) Log().Logger.debug('-rn: {0}'.format(args.rename)) Log().Logger.debug('-p: {0}'.format(args.password)) Log().Logger.debug('-m: {0}'.format(args.mailaddress)) Log().Logger.debug('-s: {0}'.format(args.ssh_host)) Log().Logger.debug('-t: {0}'.format(args.two_factor_secret_key)) Log().Logger.debug('-r: {0}'.format(args.two_factor_recovery_code_file_path)) Log().Logger.debug('--auto: {0}'.format(args.auto)) account = Db().Accounts['Accounts'].find_one(Username=args.username) Log().Logger.debug(account) if None is account: Log().Logger.warning('指定したユーザ {0} がDBに存在しません。更新を中止します。'.format(args.username)) return creator = web.service.github.api.v3.AuthenticationsCreator.AuthenticationsCreator(args.username) authentications = creator.Create() client = web.service.github.api.v3.Client.Client(authentications) # Accountsテーブルを更新する(ユーザ名、パスワード、メールアドレス) self.__UpdateAccounts(args, account, client) # SSH鍵を更新する(APIの削除と新規作成で。ローカルで更新し~/.ssh/configで設定済みとする) self.__UpdateSsh(args, account, client)
def __CreateToken(scopes): #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) account = Db().Accounts['Accounts'].find_one(Username=args.username) if None is account: raise Exception('指定ユーザ {user} はDB未登録です。登録してください。') #self.__db.Accounts['Accounts'].insert(self.__CreateRecordAccount()) Db().Accounts['Accounts'].insert(self.__CreateRecordAccount()) otp = None if None is not self.__two_factor_secret: self.__totp = pyotp.TOTP(self.__two_factor_secret) otp = self.__totp.now() api = web.service.github.api.v3.Authorizations(self.__username, self.__password) j = api.Create(otp=self.__totp.now(), scopes=scopes) #self.__db.Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], j)) Db().Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], j)) return j['token']
def Run(self): if -1 != self.__Create(): if None is self.__repos: self.__repos = Db().Repositories[ self.__args.username]['Repositories'].find_one( Name=self.__repo_name) self.__Commit()
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 __GetAccessToken(self, scopes=None): sql = "SELECT * FROM AccessTokens WHERE AccountId == {0}".format( Db().Accounts['Accounts'].find_one(Username=self.__username)['Id']) if not (None is scopes) and isinstance(scopes, list) and 0 < len(scopes): sql = sql + " AND (" for s in scopes: sql = sql + "(',' || Scopes || ',') LIKE '%,{0},%'".format( s) + " OR " sql = sql.rstrip(" OR ") sql = sql + ')' res = Db().Accounts.query(sql) ret = None for r in res: ret = r['AccessToken'] break return ret
def __init__(self, args): self.__args = args self.__first_date = None self.__last_date = None self.__date_span = None self.__date_format = "%Y-%m-%dT%H:%M:%SZ" self.__sum_repo_count = 0 self.__sum_code_size = 0 self.__repoDb = Db().Repositories[self.__args.username]
def __init__(self, client, args): self.__client = client self.__args = args self.__creator = Creator(self.__client, self.__args) self.__commiter = Commiter(self.__client, self.__args) self.__deleter = Deleter(self.__client, self.__args) self.__editor = Editor(self.__client, self.__args) self.__agg = Aggregate(self.__args) self.__account = Db().Accounts['Accounts'].find_one( Username=self.__args.username) self.__ssh_configures = Db().Accounts['SshConfigures'].find_one( AccountId=self.__account['Id']) self.__repo_name = os.path.basename(self.__args.path_dir_pj) self.__repos = Db().Repositories[ self.__args.username]['Repositories'].find_one( Name=self.__repo_name)
def Run(self): parser = argparse.ArgumentParser( description='GitHub Repository Uploader.', ) parser.add_argument('path_dir_pj') parser.add_argument('-u', '--username') parser.add_argument('-d', '--description') parser.add_argument('-l', '--homepage', '--link', '--url') parser.add_argument('-m', '--messages', action='append') args = parser.parse_args() path_dir_db = Config()['Path']['Db'] Log().Logger.debug(path_dir_db) # os.path.basename()で空文字を返されないための対策 # https://docs.python.jp/3/library/os.path.html#os.path.basename if args.path_dir_pj.endswith('/'): args.path_dir_pj = args.path_dir_pj[:-1] if None is args.username: print(Config()['Github']['User']) args.username = Config()['Github']['User'] if None is Db().Accounts['Accounts'].find_one(Username=args.username): Log().Logger.warning( '指定したユーザ {0} はDBに存在しません。GitHubUserRegister.pyで登録してください。'. format(args.username)) return # Contributionsバックアップ usernames = [] if Config()['Github']['Contributions']['IsGet']: for a in Db().Accounts['Accounts'].find(): usernames.append(a['Username']) th = ContributionsThread(path_dir_db, usernames) th.start() # アップローダ起動 #creator = web.service.github.api.v3.AuthenticationsCreator.AuthenticationsCreator(args.username) #authentications = creator.Create() #client = web.service.github.api.v3.Client.Client(authentications, args) #main = cui.uploader.Main.Main(client, args) #main.Run() client = web.service.github.api.v3.Client.Client(args.username, args) main = cui.uploader.Main.Main(client, args).Run()
def __GetBasicAuth(self, account): twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if twofactor is None: return BasicAuthentication(account['Username'], account['Password']) else: return TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])
def Run(self, args): web.log.Log.Log().Logger.debug('Account.Delete') web.log.Log.Log().Logger.debug(args) web.log.Log.Log().Logger.debug('-u: {0}'.format(args.username)) web.log.Log.Log().Logger.debug('--auto: {0}'.format(args.auto)) #self.__db = database.Database.Database() #self.__db.Initialize() account = Db().Accounts['Accounts'].find_one(Username=args.username) #account = Db().Accounts.GetAccount(username=args.username) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) web.log.Log.Log().Logger.debug(account) if None is account: web.log.Log.Log().Logger.warning('指定したユーザ {0} がDBに存在しません。削除を中止します。'.format(args.username)) return else: authentications = [] twofactor = Db().Accounts['TwoFactors'].find_one(AccountId=account['Id']) #twofactor = self.__db.Accounts['TwoFactors'].find_one(AccountId=account['Id']) if None is not twofactor: authentications.append(TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append(BasicAuthentication(account['Username'], account['Password'])) #client = web.service.github.api.v3.Client.Client(self.__db, authentications) client = web.service.github.api.v3.Client.Client(authentications) # 1. 指定ユーザの全Tokenを削除する(SSHKey設定したTokenのはずなのでSSHKeyも削除される self.__DeleteToken(account, client) # 2. SSHのconfigファイル設定の削除と鍵ファイルの削除 self.__DeleteSshFile(account['Id']) # 3. DB設定値(Account, Repository) self.__DeleteDatabase(account) # * GitHubアカウントの退会はサイトから行うこと # 作成したアカウントのリポジトリDB作成や、作成にTokenが必要なライセンスDBの作成 #self.__db.Initialize() Db().Initialize() #return self.__db return Db()
def Create(self, username=None, scopes=None): if None is username: username = self.__username authentications = [] account = Db().Accounts['Accounts'].find_one(Username=username) if None is account: raise Exception( '指定したユーザ {0} はDBに未登録です。登録してから実行してください。'.format(username)) token = self.__GetAccessToken(scopes) if None is not token: authentications.append(OAuthAuthentication(token)) two_factor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if None is not two_factor: authentications.append( TwoFactorAuthentication(account['Username'], account['Password'], two_factor['Secret'])) else: authentications.append( BasicAuthentication(account['Username'], account['Password'])) return authentications
def __GetUserSecret(self): parser = argparse.ArgumentParser( description='GitHub Repository Uploader.', ) parser.add_argument('-u', '--username', '--user') args = parser.parse_args() username = args.username if None is username: config = configparser.ConfigParser() #config.read('./config.ini') config.read('./res/config.ini') if not ('GitHub' in config): raise Exception( 'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。iniならGitHubセクションUserキーにユーザ名を指定してください。' ) if not ('User' in config['GitHub']): raise Exception( 'ユーザ名が必要です。しかし起動引数にもconfig.iniにも存在しません。起動引数なら第一引数にユーザ名を渡してください。iniならGitHubセクションUserキーにユーザ名を指定してください。' ) username = config['GitHub']['User'] # print("username = {0}".format(username)) web.log.Log.Log().Logger.info("username = {0}".format(username)) # account = self.__db.Accounts['Accounts'].find_one(Username=username) account = Db().Accounts['Accounts'].find_one(Username=username) if None is account: raise Exception( 'ユーザ {0} はDBのAccountsテーブルに存在しません。登録してから再度実行してください。'.format( username)) # twofactor = self.__db.Accounts['TwoFactors'].find_one(AccountId=account['Id']) twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if None is twofactor: raise Exception( 'ユーザ {0} はDBのTwoFactorsテーブルに存在しません。登録してから再度実行してください。'.format( username)) return twofactor['Secret']
def __GetTokenAuth(self, account, grants): try: return OAuthTokenFromDatabaseAuthentication( account['Username'], grants) except OAuthTokenFromDatabaseAuthentication.NotHasGrantsException as e: # 指定のscopeを持ったTokenを作成する(あるいは既存のTokenに権限追加しほうが?) basicAuth = self.__GetBasicAuth(account) from web.service.github.api.v3.Client import Client j = Client().Authorizations.Create(scopes=grants) # DBに登録する Db().Accounts['AccessTokens'].insert( self.__CreateRecordToken(account['Id'], j)) return OAuthTokenFromDatabaseAuthentication( account['Username'], grants)
def __DeleteDatabase(self, account): path = Db().Paths['repo'].format(user=account['Username']) if os.path.isfile(path): os.remove(path) Db().Accounts['SshConfigures'].delete(AccountId=account['Id']) Db().Accounts['SshKeys'].delete(AccountId=account['Id']) Db().Accounts['TwoFactors'].delete(AccountId=account['Id']) Db().Accounts['AccessTokens'].delete(AccountId=account['Id']) Db().Accounts['Accounts'].delete(Id=account['Id'])
def __GetSvgDayofweekContribution(self, yearofweek, first_date, yc): first_date_week = self.__ConvertWeek(first_date.weekday()) dom_week = BeautifulSoup('<g transform="translate({x}, 0)"></g>'.format(x=13*yearofweek), 'html.parser') for dayofweek in range(0, 7): if dayofweek < first_date_week: continue targetDt = first_date + datetime.timedelta(days=(dayofweek - first_date_week)) # if self.__year < targetDt.year: continue if datetime.datetime.now() < targetDt: return (dom_week, None) # 未来の場合、次の日をNoneにして中断 #record = self.__db['Contributions'].find_one(Date="{0:%Y-%m-%d}".format(targetDt)) record = Db().Contributions['Contributions'].find_one(Date="{0:%Y-%m-%d}".format(targetDt)) if None is record: record = {} record['Date'] = "{0:%Y-%m-%d}".format(targetDt) record['Count'] = 0 dom_day = BeautifulSoup(self.__GetSvgOneDayContribution(record, dayofweek, yc.GetColor(record['Count'])), 'html.parser') dom_week.g.append(dom_day) return (dom_week, first_date + datetime.timedelta(days=(7 - first_date_week))) # next_date
def __SetUsernames(self, usernames): if None is usernames: """ path_db_account = os.path.join(self.__path_dir_db, 'GitHub.Accounts.sqlite3') if not os.path.isfile(path_db_account): raise Exception('DBファイルが存在しません。:', path_db_account) db = dataset.connect('sqlite:///' + path_db_account) self.__usernames = [] for record in db['Accounts'].find(): self.__usernames.append(record['Username']) """ self.__usernames = [ a['Username'] for a in Db().Accounts['Accounts'].find() ] elif isinstance(usernames, str): self.__usernames = [usernames] elif isinstance(usernames, (list, tuple)): self.__usernames = usernames else: raise Exception('引数usernamesはstr,list,tupleのいずれかの型にしてください。')
def __DeleteSshFile(self, account_id): sshconfigure = Db().Accounts['SshConfigures'].find_one(AccountId=account_id) #sshconfigure = self.__db.Accounts['SshConfigures'].find_one(AccountId=account_id) if None is sshconfigure: return hostname = sshconfigure['HostName'] sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() # SSH鍵ファイル削除 path_private = sshconf.GetPrivateKeyFilePath(hostname) path_public = sshconf.GetPublicKeyFilePath(hostname) if os.path.isfile(path_private): os.remove(path_private) if os.path.isfile(path_public): os.remove(path_public) # SSHconfigファイルの指定Host設定削除 if hostname in sshconf.Hosts: sshconf.DeleteHost(hostname)
def __UpdateAccounts(self, args, account, client): new_account = copy.deepcopy(account) # ユーザ名とパスワードを変更する if None is not args.rename or None is not args.password: j_user = self.__IsValidUsernameAndPassword(args, account, client) if None is not args.rename: new_account['Username'] = args.rename if None is not args.password: new_account['Password'] = args.password new_account['CreatedAt'] = j_user['created_at'] new_account['UpdatedAt'] = j_user['updated_at'] # メールアドレスを更新する if args.mailaddress: mail = self.__GetPrimaryMail(client) if mail != account['MailAddress']: new_account['MailAddress'] = mail else: Log().Logger.warning('MailAddressはDBと同一でした。: {0}'.format(mail)) # DBを更新する Db().Accounts['Accounts'].update(new_account, ['Id'])
def __UpdateSsh(self, args, account, client): if None is args.ssh_host: return sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() if not(args.ssh_host in sshconf.Hosts): raise Exception('指定したSSHホスト名 {0} は~/.ssh/config内に未定義です。定義してから再度実行してください。'.format(args.ssh_host)) if 1 < Db().Accounts['AccessTokens'].count(Username=account['Username']): #if 1 < self.__db.Accounts['AccessTokens'].count(Username=account['Username']): raise Exception('プログラムエラー。1ユーザ1Tokenのはずですが、Tokenが2つ以上あります。') # GitHubAPIでSSH鍵を削除する token = Db().Accounts['AccessTokens'].find_one(AccountId=account['Id']) #token = self.__db.Accounts['AccessTokens'].find_one(AccountId=account['Id']) Log().Logger.debug(token) if None is args.password: password = account['Password'] else: password = args.password client.SshKeys.Delete(token['SshKeyId']) # GitHubAPIでSSH鍵を生成する ssh_key_gen_params = self.__LoadSshKeyFile(args, sshconf) j_ssh = client.SshKeys.Create(ssh_key_gen_params['public_key'], title=account['MailAddress']) Log().Logger.debug(j_ssh) # SSH接続確認 self.__sshkeygen.CheckSshConnect(args.ssh_host, args.username) # DB更新 if 1 < Db().Accounts['AccessTokens'].count(AccountId=account['Id']): raise Exception('プログラムエラー。1ユーザ1Tokenのはずですが、Tokenが2つ以上あります。') if 1 < Db().Accounts['SshConfigures'].count(AccountId=account['Id']): raise Exception('プログラムエラー。1ユーザ1SshConfiguresレコードのはずですが、レコードが2つ以上あります。') if 1 < Db().Accounts['SshKeys'].count(AccountId=account['Id']): raise Exception('プログラムエラー。1ユーザ1SshKeysレコードのはずですが、レコードが2つ以上あります。') rec_token = Db().Accounts['AccessTokens'].find_one(AccountId=account['Id']) rec_token['SshKeyId'] = j_ssh['id'] Db().Accounts['AccessTokens'].update(rec_token, ['Id']) sshconfigures = Db().Accounts['SshConfigures'].find_one(AccountId=account['Id']) sshconfigures['HostName'] = args.ssh_host sshconfigures['PrivateKeyFilePath'] = ssh_key_gen_params['path_file_key_private'] sshconfigures['PublicKeyFilePath'] = ssh_key_gen_params['path_file_key_public'] sshconfigures['Type'] = ssh_key_gen_params['type'] sshconfigures['Bits'] = ssh_key_gen_params['bits'] sshconfigures['Passphrase'] = ssh_key_gen_params['passphrase'] Db().Accounts['SshConfigures'].update(sshconfigures, ['Id']) sshkeys = Db().Accounts['SshConfigures'].find_one(AccountId=account['Id']) sshkeys['IdOnGitHub'] = j_ssh['id'] sshkeys['Title'] = j_ssh['title'] sshkeys['Key'] = j_ssh['key'] sshkeys['PrivateKey'] = ssh_key_gen_params['private_key'] sshkeys['PublicKey'] = ssh_key_gen_params['public_key'] sshkeys['Verified'] = self.__j2s.BoolToInt(j_ssh['verified']) sshkeys['ReadOnly'] = self.__j2s.BoolToInt(j_ssh['read_only']) sshkeys['CreatedAt'] = j_ssh['created_at'] Db().Accounts['SshKeys'].update(sshkeys, ['Id'])
def Run(self): parser = argparse.ArgumentParser( description='GitHub Repository Uploader.', ) parser.add_argument('path_dir_pj') parser.add_argument('-u', '--username') parser.add_argument('-d', '--description') parser.add_argument('-l', '--homepage', '--link', '--url') parser.add_argument('-m', '--messages', action='append') args = parser.parse_args() # print(args) # print('path_dir_pj: {0}'.format(args.path_dir_pj)) # print('-u: {0}'.format(args.username)) # print('-d: {0}'.format(args.description)) # print('-l: {0}'.format(args.homepage)) #self.__setting = setting.Setting.Setting() # self.__setting = setting.Setting.Setting(os.path.abspath(os.path.dirname(os.path.dirname(__file__)))) # self.__setting = setting.Setting.Setting(os.path.abspath(os.path.dirname(__file__))) #path_dir_db = self.__setting.DbPath #path_dir_db = Config().PathDb path_dir_db = Config()['Path']['Db'] web.log.Log.Log().Logger.debug(path_dir_db) # os.path.basename()で空文字を返されないための対策 # https://docs.python.jp/3/library/os.path.html#os.path.basename if args.path_dir_pj.endswith('/'): args.path_dir_pj = args.path_dir_pj[:-1] if None is args.username: #print(self.__setting.GithubUsername) #args.username = self.__setting.GithubUsername print(Config()['Github']['User']) args.username = Config()['Github']['User'] #self.__db = database.Database.Database() # self.__db = database.Database.Database(os.path.abspath(os.path.dirname(__file__))) #self.__db.Initialize() #print(dir(Db())) print(hasattr(Db(), 'Accounts')) print(type(Db())) print(hasattr(Db().Accounts, 'Accounts')) print(type(Db().Accounts)) print(dir(Db().Accounts)) if None is Db().Accounts['Accounts'].find_one(Username=args.username): #if None is Db().Accounts['Accounts'].find_one(Username=args.username): #if None is self.__db.Accounts['Accounts'].find_one(Username=args.username): web.log.Log.Log().Logger.warning( '指定したユーザ {0} はDBに存在しません。GitHubUserRegister.pyで登録してください。'. format(args.username)) return # Contributionsバックアップ usernames = [] if Config()['Github']['Contributions']['IsGet']: for a in Db().Accounts['Accounts'].find(): usernames.append(a['Username']) th = ContributionsThread(path_dir_db, usernames) th.start() """ # Contributionsバックアップ usernames = [] #for a in self.__db.Accounts['Accounts'].find(): usernames.append(a['Username']) for a in Db().Accounts['Accounts'].find(): usernames.append(a['Username']) #th = ContributionsThread(path_dir_db, self.__db, usernames) th = ContributionsThread(path_dir_db, usernames) th.start() """ # アップローダ起動 # creator = web.service.github.api.v3.AuthenticationsCreator.AuthenticationsCreator(self.__db, args.username) creator = web.service.github.api.v3.AuthenticationsCreator.AuthenticationsCreator( args.username) authentications = creator.Create() # client = web.service.github.api.v3.Client.Client(self.__db, authentications, args) client = web.service.github.api.v3.Client.Client(authentications, args) # main = cui.uploader.Main.Main(self.__db, client, args) main = cui.uploader.Main.Main(client, args) main.Run()
def Run(self, args): web.log.Log.Log().Logger.debug('Account.Insert') web.log.Log.Log().Logger.debug(args) web.log.Log.Log().Logger.debug('-u: {0}'.format(args.username)) web.log.Log.Log().Logger.debug('-p: {0}'.format(args.password)) web.log.Log.Log().Logger.debug('-s: {0}'.format(args.ssh_host)) web.log.Log.Log().Logger.debug('-t: {0}'.format( args.two_factor_secret_key)) web.log.Log.Log().Logger.debug('-r: {0}'.format( args.two_factor_recovery_code_file_path)) web.log.Log.Log().Logger.debug('--auto: {0}'.format(args.auto)) self.__db = database.Database.Database() self.__db.Initialize() account = Db().Accounts.GetAccount(username=args.username) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) web.log.Log.Log().Logger.debug(account) if None is account: authentications = [] if None is not args.two_factor_secret_key: authentications.append( TwoFactorAuthentication(args.username, args.password, args.two_factor_secret_key)) else: authentications.append( BasicAuthentication(args.username, args.password)) client = web.service.github.api.v3.Client.Client( self.__db, authentications) # 1. Tokenの新規作成 token = client.Authorizations.Create( scopes=['repo', 'delete_repo', 'user', 'admin:public_key'], note='GitHubUserRegister.py {0}'.format( '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))) # 2. APIでメールアドレスを習得する。https://developer.github.com/v3/users/emails/ mailaddress = self.__GetPrimaryMail(client) # 3. SSHの生成と設定 # 起動引数`-s`がないなら if None is args.ssh_host: # 3A-1. SSH鍵の新規作成 ssh_key_gen_params = self.__SshKeyGen(args.username, mailaddress) sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() host = sshconf.AppendHost( args.username, ssh_key_gen_params['path_file_key_private']) # 3A-2. SSH鍵をGitHubに登録してDBに挿入する j_ssh = client.SshKeys.Create(ssh_key_gen_params['public_key'], title=mailaddress) web.log.Log.Log().Logger.debug(j_ssh) self.__setting = setting.Setting.Setting() if 'SSH' == self.__setting.GitRemote: # 3A-3. SSH接続確認 self.__sshkeygen.CheckSshConnect(host, args.username) else: # ラズパイでなぜかSSH接続確認するとフリーズするので省略。 print('SSH接続確認を省略します。') else: # 3B-1. ~/.ssh/configから指定されたHostデータを取得する sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() if not (args.ssh_host in sshconf.Hosts.keys()): raise Exception( '存在しないSSH Host名が指定されました。-s引数を指定しなければSSH鍵を新規作成して設定します。既存のSSH鍵を使用するなら~/.ssh/configファイルに設定すると自動で読み取ります。configファイルに設定済みのHost名は次の通りです。 {0}' .format(sshconf.Hosts.keys())) host = args.ssh_host ssh_key_gen_params = self.__LoadSshKeyFile(args, sshconf) # 3B-2.GitHubのSSHにすでに設定されているか確認する j_ssh = self.__GetGitHubSsh(client, args.username, mailaddress, ssh_key_gen_params['public_key']) # 4. 全部成功したらDBにアカウントを登録する Db().Accounts['Accounts'].insert( self.__CreateRecordAccount(args, mailaddress)) #self.__db.Accounts['Accounts'].insert(self.__CreateRecordAccount(args, mailaddress)) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) account = Db().Accounts['Accounts'].find_one( Username=args.username) web.log.Log.Log().Logger.debug(account) if None is not args.two_factor_secret_key: Db().Accounts['TwoFactors'].insert( self.__CreateRecordTwoFactor(account['Id'], args)) #self.__db.Accounts['TwoFactors'].insert(self.__CreateRecordTwoFactor(account['Id'], args)) """ self.__db.Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], token, j_ssh['id'])) self.__db.Accounts['SshConfigures'].insert(self.__CreateRecordSshConfigures(account['Id'], host, ssh_key_gen_params)) self.__db.Accounts['SshKeys'].insert(self.__CreateRecordSshKeys(account['Id'], ssh_key_gen_params['private_key'], ssh_key_gen_params['public_key'], j_ssh)) """ Db().Accounts['AccessTokens'].insert( self.__CreateRecordToken(account['Id'], token, j_ssh['id'])) Db().Accounts['SshConfigures'].insert( self.__CreateRecordSshConfigures(account['Id'], host, ssh_key_gen_params)) Db().Accounts['SshKeys'].insert( self.__CreateRecordSshKeys(account['Id'], ssh_key_gen_params['private_key'], ssh_key_gen_params['public_key'], j_ssh)) # 作成したアカウントのリポジトリDB作成や、作成にTokenが必要なライセンスDBの作成 #self.__db.Initialize() return self.__db
def __GetOneYearContributionCount(self): #records = self.__db.query('select SUM("Count") YearCount from "Contributions" where "Date" like "{year}-%"'.format(year=self.__year)) records = Db().Contributions.query('select SUM("Count") YearCount from "Contributions" where "Date" like "{year}-%"'.format(year=self.__year)) return records.next()['YearCount']