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 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 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']
class GitGittle(BaseGit): def __init__(self): self.repo = None 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 git_upload_changes(self): commit_id = "" try: logger.info("Commit changes in progress ..") # Stage modified files self.repo.stage(self.repo.pending_files) commit_message = conf.git.commit_message_format.format( self.repo.added_files) # Commit the change commit_id = self.repo.commit(conf.git.commit_user, conf.git.commit_email, commit_message) logger.info("Commit details: commit_user:{}, commit_email:{}, " "commit_message:{}, commit_id:{}".format( conf.git.commit_user, conf.git.commit_email, commit_message, commit_id)) # Push to repository self.repo.push() except Exception as exc: logger.error("GitGittle - Filed to upload file to git.") raise GitUploadError("Failed to upload file to Git") return commit_id def validate_git(self): pass
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 update_repo(cookie): # TODO: add commit info to the CookieCutter model if not os.path.isdir(cookie.repo_path): repo = Gittle.clone(cookie.url, cookie.repo_path) else: repo = Gittle(cookie.repo_path, cookie.url) repo.pull() cookie.options = {'repo_name': 'your repo'} options_file = os.path.join(cookie.repo_path, 'cookiecutter.json') if os.path.isfile(options_file): cookie.options.update(json.load(open(options_file))) cookie.save()
class GPassGit: #Creates a Git object def __init__(self, repoPath): self.repoPath = repoPath self.repo = None if self.isRepoSet(): self.repo = Gittle(self.repoPath) #Check For Git Repo def isRepoSet(self): if os.path.isfile(self.repoPath + '/.git/config'): return True return False def init(self, repoPath): self.repo = Gittle.init(repoPath) print("Created Repo") def add(self, filePath): self.repo.stage([filePath]) def commit(self, user, em, msg): self.repo.commit( name=user, email=em, message=msg ) # Authentication with RSA private key def auth(self, key): key_file = open(key) repo.auth(pkey=key_file) def push(self, key): self.auth(key) self.repo.push() def pull(self, key): self.auth(key) self.repo.pull() def acp(self, filepath, user, em, msg): self.add(filepath) self.commit(user, em, msg) self.push(key)
class Git(Base): ''' a git vcs object ''' def __init__(self, repo_url, repo_path): super(Git, self).__init__(repo_url, repo_path) self._cloned = path.exists(repo_path) def clone(self, key_file=None, username=None, password=None): ''' clone the git repo :param key_file: string : None location of private key if you want to connect using RSA :param username: string : None username if you wnat to connect using basic authentication :param password: string : None password if you wnat to connect using basic authentication ''' if self._cloned is False: if key_file is not None: # Authentication with RSA private key key_file = open(key_file) Gittle.clone(self.repo_url, self.repo_path, auth=GittleAuth(pkey=key_file)) elif username is not None and password is not None: # With username and password Gittle.clone(self.repo_url, self.repo_path, auth=GittleAuth(username=username, password=password)) else: # Without anything , is it even possible? Gittle.clone(self.repo_url, self.repo_path) def pull(self): ''' pull the latest version ''' self.clone() self.repo = Gittle(self.repo_path, origin_uri=self.repo_url) self.repo.pull() def push(self): ''' push to the remote repository ''' raise NotImplementedError
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 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 "" # 结束任务
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 pull g.pull()
# /usr/bin/python # coding=utf-8 '''help me to git add changes ''' from gittle import Gittle import sys conf = [ ['/home/zhang/www/php', '[email protected]:tianyunchong/php.git'] ] for con in conf: repo = Gittle(con[0], con[1]) #拉取远程代码到本地 key_file = open("/home/zhang/.ssh/id_rsa") repo.auth(key_file) repo.pull(con[1], "origin/master") sys.exit() untracks = repo.untracked_files if not untracks: print "暂时没有文件新增!" changes = repo.modified_files print changes
def _update_local(self, local_orgin_path, origin_uri): repo = Gittle(local_orgin_path, origin_uri=origin_uri) print "pull %s start" % origin_uri print repo.pull() print "pull %s end" % origin_uri