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 DbFilePath(self): #print(dir(self.__setting)) #return os.path.join(self.__setting.PathDb, self.DbFileName) print(Config()['Path']['Db']) #print(Config().PathDb) print(self.DbFileName) #return os.path.join(Config().PathDb, self.DbFileName) return os.path.join(Config()['Path']['Db'], self.DbFileName)
def run(self): import batch.Contributions.Main m = batch.Contributions.Main.Main(self.__path_dir_db) for username in self.__usernames: m.Run(username) if Config()['Github']['Contributions']['IsMake']: self.__create_svg()
def __GetDb(self, username): #path = os.path.join(self.__path_dir_db, self.__GetDbFilename(username)) path = os.path.join(Config()['Path']['Db'], self.__GetDbFilename(username)) try: return dataset.connect('sqlite:///' + path) except: print('DBファイルを開けませんでした。:', path) raise
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 __CreateLocalRepository(self): 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)))
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 = Config().GitRemote.GetRepositoryUri(self.__args.username, self.__repo_name) #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 __init__(cls, name, bases, attrs): # 単一DB print(attrs) for initer in attrs['_{0}__Initializers'.format(name)].values(): initer.Initialize() attrs[initer.DbId] = property(lambda cls: initer.Db) # Database.Accounts = database.init.AccountsDbInitializer.AccountsDbInitializer().Db = dataset.connect('sqlite:///' + '.../') #attrs['_{0}__Initializers'.format(name)][initer.DbId] = property(lambda cls: initer) # Database.__Initializers['Accounts'] = database.init.AccountsDbInitializer.AccountsDbInitializer().Db = dataset.connect('sqlite:///' + '.../') print(type(attrs['_{0}__Initializers'.format(name)])) if 0 == attrs['_{0}__Initializers'.format(name)]['Accounts'].Db['Accounts'].count(): raise Exception('登録ユーザがひとつもありません。UserRegister.pyで登録してから再実行してください。') # ユーザ単位DB(AccountsDB生成後でないと作れない) """ attrs['_{0}__InitializersByMultiUsers'.format(name)] = OrderedDict() accountsDb = attrs['_{0}__Initializers'.format(name)]['Accounts'].Db for initer in [cls(accountsDb) for cls in [Repositories, Contributions]]: attrs['_{0}__InitializersByMultiUsers'.format(name)][initer.DbId] = initer # Database.__Initializers['Accounts'] = database.init.AccountsDbInitializer.AccountsDbInitializer() for initer in attrs['_{0}__InitializersByMultiUsers'.format(name)].values(): initer.Initialize() attrs[initer.DbId] = property(lambda cls: initer.Db) # Database.Accounts = database.init.AccountsDbInitializer.AccountsDbInitializer().Db = dataset.connect('sqlite:///' + '.../') """ attrs['_{0}__InitializersByMultiUsers'.format(name)] = OrderedDict() accountsDb = attrs['_{0}__Initializers'.format(name)]['Accounts'].Db for initer in [cls(accountsDb) for cls in [Repositories]]: attrs['_{0}__InitializersByMultiUsers'.format(name)][initer.DbId] = initer # Database.__Initializers['Accounts'] = database.init.AccountsDbInitializer.AccountsDbInitializer() for initer in attrs['_{0}__InitializersByMultiUsers'.format(name)].values(): initer.Initialize() attrs[initer.DbId] = property(lambda cls: initer.Db) # Database.Accounts = database.init.AccountsDbInitializer.AccountsDbInitializer().Db = dataset.connect('sqlite:///' + '.../') from setting.Config import Config #import setting.Config if Config()['Github']['Contributions']['IsGet']: th = ContributionsThread(accountsDb) th.start() """
def __create_svg(self): from batch.Contributions.SvgCreator import SvgCreator SvgCreator(Config()['Path']['Db']).Create()
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 __CreateFilepath(self): #import setting.Setting #setting = setting.Setting.Setting() for username in self.__GetUsernames(): self.__filepaths[username] = os.path.join( Config()['Path']['Db'], self.__filenames[username])
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) client = web.service.github.api.v3.Client.Client(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) # 3A-3. SSH接続確認 self.__setting = setting.Setting.Setting() if 'SSH' == Config()['Git']['Remote'].__name__.upper(): self.__sshkeygen.CheckSshConnect(host, args.username) """ 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にアカウントを登録する #self.__db.Accounts['Accounts'].insert(self.__CreateRecordAccount(args, mailaddress)) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) Db().Accounts['Accounts'].insert( self.__CreateRecordAccount(args, mailaddress)) 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))
def DbFilePath(self): return os.path.join(Config()['Path']['Db'], self.DbFileName)