def opgit_setenv(self): #git config --global core.editor vim cmd = 'git config --global user.name "%s"' % (os.getenv('USER')) getoutput(cmd, quiet=True) cmd = 'git config --global user.email "*****@*****.**"' % ( os.getenv('USER')) getoutput(cmd, quiet=True)
def new_branch(self, dst='scripts', dst_branch='new'): if not os.path.exists(dst): self.info('No repo found. Will checkout a new one') self.checkout(dst) cwd = os.getcwd() os.chdir(dst) #- checkout or new created if not self.exists_branch(dst_branch): self.info('Create a new branch %s' % (dst_branch)) cmd = 'git checkout -b %s' % (dst_branch) self.stdout = getoutput(cmd) else: if self.current_branch != dst_branch: self.info('Checkout branch %s' % (dst_branch)) cmd = 'git checkout %s' % (dst_branch) self.stdout = getoutput(cmd) # check if checkout successfully if self.current_branch == dst_branch: self.info('Current Branch: %s' % (dst_branch)) cmd = 'git config receive.denyCurrentBranch ignore' self.stdout = getoutput(cmd) os.chdir(cwd) return True else: os.chdir(cwd) return False
def commit(self, repo_path=os.getcwd(), comment_in=''): cwd = os.getcwd() os.chdir(repo_path) self.info('Switch into repo: "%s"' % (repo_path)) if not self.clean: comment = '' for f in self.new_files: cmd = 'git add %s' % (f) #print cmd comment += 'add: %s; ' % (f) getoutput(cmd) for f in self.modified_files: cmd = 'git add %s' % (f) #print cmd comment += 'modified: %s; ' % (f) getoutput(cmd) for f in self.deleted_files: cmd = 'git rm %s' % (f) #print cmd comment += 'deleted: %s; ' % (f) getoutput(cmd) if comment_in != '': comment = comment_in cmd = 'git commit -m \'%s\'' % (comment) #print cmd getoutput(cmd) self.info('Commit Finished.') else: self.info('Repository is Clean. Nothing Commit.') os.chdir(cwd)
def checkout(self, src='', dst='', src_branch='master', dst_branch=''): ''' check out is the "git clone + git checkout" ''' if dst_branch == '': self.info('dst_branch will be %s' % (src_branch)) dst_branch = src_branch if src == '' or dst == '': self.err('Must define src/dst') return False current_dir = os.getcwd() self.dbg('Current Directory: %s' % (current_dir)) #- if not exists checkout checkout_cmd = 'git clone -b %s %s %s' % (src_branch, src, dst) self.stdout = getoutput(checkout_cmd) if self.no_src: self.err('No source repo \'%s\'.' % (src)) return False elif self.no_src_branch: self.err('No source Branch in repo \'%s\'.' % (src_branch, src)) return False elif self.cloned or self.initialized_empty_repo: self.info('Checkout Successful!') return self.new_branch(dst=dst, dst_branch=dst_branch) elif self.exists_local_repo: self.warn('destination repo \'%s\' already exists' % (dst)) return self.new_branch(dst=dst, dst_branch=dst_branch) else: self.err('Unknown Status!') return False
def clean(self): cmd = 'git status' self.stdout = getoutput(cmd, quiet=True) comp = re.compile(r'^nothing to commit, working directory clean$') if len(comp.findall(self.stdout[-1])): return True return False
def new_files(self): cmd = 'git status' self.stdout = getoutput(cmd, quiet=True) comp = re.compile(r'^#*\s*(\S+)$') lists = [] for line in self.stdout: if line == '#': continue f = comp.findall(line) if len(f): lists.append(f[0]) return lists
def __has_program(self, program): cmd = 'which %s' % (program) self.stdout = getoutput(cmd, quiet=True) comp = re.compile(r'^(\S+): Command not found\.') check_ok = True for line in self.stdout: f = comp.findall(line) if len(f): self.err( 'OP4 Requried Linux command "%s" Not Found. Please ask IT to install on Current Server.' % f[0]) check_ok = False return check_ok
def __libc_version(self): libcs = [ '/lib/x86_64-linux-gnu/libc.so.6', '/lib64/libc.so.6', ] for f in libcs: if not os.path.exists(f): continue comp = re.compile(r'release version (\S+),') for line in getoutput(f, quiet=True): f = comp.findall(line) if len(f): return f[0] return 'Unknown'
def exists_branch(self, branch): for i in getoutput('git branch', quiet=True): if re.match(r'^\**\s*%s' % (branch), i): return True return False
def current_branch(self): return [ i.split()[1] for i in getoutput('git branch', quiet=True) if re.match(r'^\*\s.*', i) ][0]
def monitor(self): self.info('Start Monitor task : %s' % (self.session_name)) if not os.path.exists(self.monitor_file): self.mkdir(self.monitor_file) else: self.warn('Monitor Lock file exists: %s' % (self.monitor_file)) self.warn( 'Maybe another client is under monitor. Or, another client is crashed.' ) confirm = raw_input('Continue Monitor? Y|[N]') if confirm.lower() == 'y': self.info('Contiue Monitor...') else: return -1 self.screen.attach() while True: time.sleep(5) if self.encounter_errors(): if not self.DEBUG: self.is_failed = True self.err('Stage "%s" Failed!' % (self.session_name)) self.quit_screens() getoutput('touch %s' % (self.failed_file), quiet=True) self.clean_lock_file() return False if self.screen.status == 'idle': self.is_ready = True self.err('Stage "%s" Finished Failed!' % (self.session_name)) self.quit_screens() getoutput('touch %s' % (self.ready_file), quiet=True) getoutput('touch %s.debug_mode' % (self.failed_file), quiet=True) self.clean_lock_file() return False if os.path.exists(self.early_complete_file): self.is_early_complete = True if self.screen.status == 'idle': self.is_ready = True self.is_early_complete = True self.info('Stage "%s" Finished successfully.' % (self.session_name), info_color=True) self.quit_screens() getoutput('touch %s' % (self.ready_file), quiet=True) self.clean_lock_file() return True if not self.screen.exists: self.warn('Unknown Task status.') self.clean_lock_file() return None