def process_update(process): process_control(process, 'stop') # stop process if process == 'bgpapi_8108': filename = 'bgpapi_' + time.strftime( '%Y%m%d%H%M%S', time.localtime(time.time())) + '.tar.gz' path = '/home/API/bgpapi' mv_path = '/home/API/backup' make_targz('/home/API/' + filename, path) # backup os.system('mv %s %s' % ('/home/API/' + filename, mv_path)) os.system("rm -rf %s" % path) # delete repo_url = 'git@mgrser:root/bgpapi.git' Gittle.clone(repo_url, path) # git clone repo = Gittle(path, origin_uri='git@mgrser:root/bgpapi.git') repo.switch_branch('master') repo.pull() elif process == 'bgpapi_8109': path = '/home/API/bgpapi_ha' os.system("rm -rf %s" % path) # delete repo_url = 'git@mgrser:root/bgpapi.git' Gittle.clone(repo_url, path) # git clone repo = Gittle(path, origin_uri='git@mgrser:root/bgpapi.git') repo.switch_branch('master') repo.pull() else: print('You cannot do with this %s' % process) process_control(process, 'start') # start process print('%s has been updated.' % process)
def git_push(args): import argparse parser = argparse.ArgumentParser(prog='git push' , usage='git push [http(s)://<remote repo>] [-u username[:password]]' , description="Push to a remote repository") parser.add_argument('url', type=str, nargs='?', help='URL to push to') parser.add_argument('-u', metavar='username[:password]', type=str, required=False, help='username[:password]') result = parser.parse_args(args) user, sep, pw = result.u.partition(':') if result.u else (None,None,None) repo = Gittle('.') #Try to get the remote origin if not result.url: result.url = repo.remotes.get('origin','') branch_name = os.path.join('refs','heads', repo.active_branch) #'refs/heads/%s' % repo.active_branch print "Attempting to push to: {0}, branch: {1}".format(result.url, branch_name) if user: if not pw: pw = getpass.getpass('Enter password for {0}: '.format(user)) opener = auth_urllib2_opener(None, result.url, user, pw) print porcelain.push(repo.repo, result.url, branch_name, opener=opener) else: print porcelain.push(repo.repo, result.url, branch_name)
def gitPull(): logging.info('*** Pulling changed files from remote repository ***') destinations = [dataDestination, PDFdestination] remotes = ['dataRemote', 'PDFRemote'] for d, r in zip(destinations, remotes): repo = Gittle(d, origin_uri=config.get('Git', r)) repo.pull()
def get_repo_info(repo_path,is_my_repo): if not os.path.exists(repo_path): return dict( info=[], error=True, msg='repo not exist' ) git_path = os.path.join(repo_path, '.git') if not os.path.exists(git_path): return dict( info=[], error=True, msg='.git file lost' ) repo = Gittle(repo_path) list_dir = os.listdir(repo_path) folders = [] print list_dir for i in range(len(list_dir)): if os.path.isdir(os.path.join(repo_path, list_dir[i])): folders.append(list_dir[i]) return dict( commit_info=repo.commit_info(start=0), remote_branches=repo.remote_branches, local_branches=repo.branches, active_branch=repo.active_branch, folders=folders, is_my_repo=is_my_repo )
def git_checkout(args): if len(args) == 1: repo = Gittle('.') repo.clean_working() #repo.checkout('refs/heads/{0}'.format(args[0])) repo.switch_branch('{0}'.format(args[0])) else: print command_help['checkout']
def _get_repo(self): try: repopath = self._repo_path() repobase = self._find_repo(repopath) if repobase: return Gittle(repobase) except: return None
def git_remote(args): '''List remote repos''' if len(args) == 0: repo = Gittle('.') for key, value in repo.remotes.items(): print key, value else: print command_help['remote']
def git_branch(args): if len(args) == 0: repo = Gittle('.') active = repo.active_branch for key, value in repo.branches.items(): print ('* ' if key == active else '') + key, value else: print command_help['branch']
def git_commit(args): if len(args) == 3: try: repo = Gittle('.') print repo.commit(name=args[1],email=args[2],message=args[0]) except: print 'Error: {0}'.format(sys.exc_value) else: print command_help['commit']
def gitPush(): logging.info('*** Versioning files and pushing to remote repository ***') destinations = [dataDestination, PDFdestination] remotes = ['dataRemote', 'PDFRemote'] for d, r in zip(destinations, remotes): repo = Gittle(d, origin_uri=config.get('Git', r)) repo.stage(repo.pending_files) repo.commit(message=commitMessage) repo.push()
def __init__(self, path): try: self.gittle = Gittle(path) except NotGitRepository: self.gittle = Gittle.init(path) # Dulwich repo self.repo = self.gittle.repo self.path = path
def git_pull(args): if len(args) <= 1: repo = Gittle('.') url = args[0] if len(args)==1 else repo.remotes.get('origin','') if url: repo.pull(origin_uri=url) else: print 'No pull URL.' else: print command_help['git pull']
def createTask(taskId, gitKey, gitAddress): """ fortify 命令行调用 fortify 使用手册 HPE_SCA_Guide_16.20.pdf :param taskId: :param gitKey: :param gitAddress: :return: """ clonePath = os.path.join(BASE_DIR, "gitClone") outputPath = os.path.join(BASE_DIR, "reports") if not os.path.exists(clonePath): os.mkdir(clonePath) if not os.path.exists(outputPath): os.mkdir(outputPath) fortifyLogMsg(taskId=taskId, msg=u"拉取代码" + gitAddress, code=1) localPath = gitAddress.replace("http://", "").replace("https://", "").replace("/", "_").split("?")[0] localPath = localPath.replace("git@", "").replace(":", "_").replace(".git", "") localPath = os.path.join(clonePath, localPath) buildName = "_".join(localPath.split("_")[1:]) try: if gitKey and gitAddress: from gittle.auth import GittleAuth # https://github.com/FriendCode/gittle/issues/18 authkey = GittleAuth(pkey=gitKey) if not os.path.exists(localPath): Gittle.clone(gitAddress, local_path=localPath, auth=authkey) else: repo = Gittle(localPath, origin_uri=gitAddress, auth=authkey) repo.pull() else: if not os.path.exists(localPath): Gittle.clone(gitAddress, localPath) else: repo = Gittle(localPath, gitAddress) repo.pull() except Exception, e: # raise e fortifyLogMsg(taskId, u"任务拉取代码遇到异常" + str(e.message), "", code=2) return "" # 结束任务
def getRepo(path): from dulwich.errors import NotGitRepository repo = None try: repo = Gittle(path) except NotGitRepository as e: print e return repo
def is_git_repo(path): ''' 判断一个路径是不是git仓库 :param path: 要判断的路径 :return: 是否是git路径 ''' try: repo = Gittle(path) return True except Exception, e: return False
def git_status(args): if len(args) == 0: repo = Gittle('.') status = porcelain.status(repo.repo) print status #repo.diff_working() #repo.diff(diff_type='changes') #print repo.modified_files.intersection(repo.added_files) #repo.tracked_files.intersection(repo.added_files) #print repo.added_files else: print command_help['git_staged']
def getRepo(path): ''' get local repo. ''' from dulwich.errors import NotGitRepository repo = None try: repo = Gittle(path) except NotGitRepository as e: info_logger.info(e) return repo
def handle(self, *args, **options): projects = Project.objects.all() for project in projects: print('Checking {0} for new commits'.format(project)) if project.git_url: repo_path = '/tmp/' + project.slug try: repo = Gittle(repo_path, project.git_url) repo.pull() except: try: repo = Gittle.clone(project.git_url, repo_path) except: # put some logging here repo = None if repo: new_commits = [] for commit in repo.commit_info(): try: prev_commit = repo.get_previous_commit( commit['sha']) time = (datetime.fromtimestamp(commit['time']) + timedelta(hours=commit['timezone'] / (60 * 60))).replace( tzinfo=pytz.utc) try: user_author = get_user_model().objects.get( email=commit['author']['email']) string_author = None except: string_author = commit['author']['name'] user_author = None summary = commit['message'].split('\n')[0][:45] pcommit = ProjectCommit.objects.create( project=project, chash=commit['sha'], message=commit['message'], summary=summary, user_author=user_author, string_author=string_author, created=time, time=time, diff=repo.diff(commit['sha'], prev_commit).next()['diff']) print(pcommit, ' added.') new_commits.append(pcommit) except: pass
def clone(repo_info): repo_context = None try: repo_context = AgentGitHandler.create_git_repo_context(repo_info) if os.path.isdir(repo_context.local_repo_path): # delete local repo path if exists cartridgeagentutils.delete_folder_tree( repo_context.local_repo_path) # create local repo path cartridgeagentutils.create_dir(repo_context.local_repo_path) #TODO: remove gittle stuff auth = AgentGitHandler.create_auth_configuration(repo_context) if auth is not None: # authentication is required, use Gittle gittle_repo = Gittle.clone(repo_context.repo_url, repo_context.local_repo_path, auth=auth) repo = Repo(repo_context.local_repo_path) else: # authentication is not required, use GitPython repo = Repo.clone_from(repo_context.repo_url, repo_context.local_repo_path) gittle_repo = Gittle(repo_context.local_repo_path) repo_context.cloned = True repo_context.gittle_repo = gittle_repo repo_context.repo = repo AgentGitHandler.add_repo_context(repo_context) AgentGitHandler.log.info( "Git clone operation for tenant %r successful" % repo_context.tenant_id) except urllib2.URLError: AgentGitHandler.log.exception( "Accessing remote git repository failed for tenant %r" % repo_context.tenant_id) except OSError: AgentGitHandler.log.exception( "Permission denied for repository path for tenant %r" % repo_context.tenant_id) except: AgentGitHandler.log.exception( "Git clone operation for tenant %r failed" % repo_context.tenant_id) finally: return repo_context
def git_pull(self, TMP_DIR, CODE_DIR): ''' git pull code use commit id tag you code lastest = [info['sha'] for info in repo.commit_info()[1:3]] print((repo.diff(*lastest, diff_type='classic'))) ''' print("========%s: git pull for env:test=========" % self.PRO_NAME) repo = Gittle(self.CODE_DIR, self.REPO_URL) command = 'cd %s && git pull' % self.CODE_DIR os.system('command') COMMIT_ID = repo.commit()[0:-30] CVER = "%s_%s_%s" % (self.PRO_NAME, self.CTIME, COMMIT_ID) CODE_TMP = '%s/%s' % (TMP_DIR, CVER) shutil.copytree(CODE_DIR, CODE_TMP) echo_info("git pull success") return CVER
def git_init(self): try: # Init repository logger.debug("Local repository path:{}, Git server url: {}".format( conf.git.local_repository_path, conf.git.git_server_url)) self.repo = Gittle(conf.git.local_repository_path, origin_uri=conf.git.git_server_url) logger.info("Pulling from git..") # Update local working copy self.repo.pull() logger.info("GitGittle - Git is up to date !") except Exception as exc: logger.error( "GitGittle - Failed to initialize Git. Reason: {}".format( exc.message)) raise GitInitError(exc.message)
def GitUpload(file, Name, Email, Password, Repository, Message="Some uploads"): origin_uris = Repository if not origin_uris.startswith(Name): origin_uris = Name + "/" + origin_uris if not origin_uris.startswith("https://github.com/"): origin_uris = "https://github.com/" + origin_uris if not origin_uris.endswith(".git"): origin_uris = origin_uris + ".git" path = os.getcwd() # Gittle.clone() 会将当前目录下的文件下载到本地,并初始化git工作路径,上传文件正常 # repo = Gittle.clone("https://github.com/fengfeng0918/gittle.git", path) # Gittle.init() 初始化git工作路径,并没有将远程仓库信息拉到本地,上传(push)文件时会将远程库清空 # repo = Gittle.init(path,origin_uri="https://github.com/fengfeng0918/gittle.git") # git init 以下流程正常!!! if not os.path.exists(".git"): local_repo = Gittle.init(path) # local_repo = Gittle.clone(origin_uris,path) bares = False #不会删除远端,重写本地 else: local_repo = Repo(path) bares = True # 不会删除远端,不重写本地 repo = Gittle(local_repo, origin_uris) repo.fetch(bare=bares) # Stage file if not isinstance(file, list): file = [file] repo.stage(file) # Commiting repo.commit(name=Name, email=Email, message=Message, files=file) # add remote repo.add_remote('origin', origin_uris) # Push repo.auth(username=Name, password=Password) # Auth for pushing repo.push()
from gittle import Gittle repo = Gittle('.') versions = repo.get_file_versions('gittle/gittle.py') print("Found %d versions out of a total of %d commits" % (len(versions), repo.commit_count()))
break osName = osList.get(os_name, None) osBit = os_bits() branchForTarget = "master" if osName != None and osBit != None: branchForTarget = osName + osBit def report_transfer(size, message): print "size : " + size + ", message : " + message if isInstalled: repo = Gittle(repo_path, origin_uri=repo_url, report_activity=report_transfer) else: repo = Gittle.clone(repo_url, repo_path, report_activity=report_transfer) repo.switch_branch(branchForTarget) commit_info = repo.commit_info(start=0, end=20) print "repo.active_branch : " + str(repo.active_branch) print "repo.branches : " + str(repo.branches) print "repo.commits() : " + str(repo.commits()) print "repo.head : " + repo.head print commit_info # repo.pull(branch_name=branchForTarget)
from gittle import Gittle from config import repo_path, repo_url, key_file # Gittle repo g = Gittle(repo_path, origin_uri=repo_url) # Authentication g.auth(pkey=key_file) # Do push g.push()
def test_repo(repo_path): repo = Gittle(repo_path) return all([test(repo) for test in TESTS])
def git_modified(args): repo = Gittle('.') for mod_file in repo.modified_files: print mod_file
def git_rm(args): if len(args) > 0: repo = Gittle('.') repo.rm(args) else: print command_help['rm']
def _get_repo(): repo_dir = _find_repo(os.getcwd()) if not repo_dir: raise Exception("Current directory isn't a git repository") return Gittle(repo_dir)
def git_add(args): if len(args) > 0: repo = Gittle('.') repo.stage(args) else: print command_help['add']