def git_info(config, fo=None): ''' Print info about a Git repo. ''' assert isdir(config.work_dir) # Ensure to explicitly set GIT_DIR as some Linux machines will not # properly execute without it. env = os.environ.copy() env['GIT_DIR'] = join(config.work_dir, '.git') env = {str(key): str(value) for key, value in env.items()} for cmd, check_error in [ ('git log -n1', True), ('git describe --tags --dirty', False), ('git status', True)]: p = Popen(cmd.split(), stdout=PIPE, stderr=PIPE, cwd=config.work_dir, env=env) stdout, stderr = p.communicate() encoding = locale.getpreferredencoding() if not fo: encoding = sys.stdout.encoding encoding = encoding or 'utf-8' stdout = stdout.decode(encoding, 'ignore') stderr = stderr.decode(encoding, 'ignore') if check_error and stderr and stderr.strip(): raise Exception("git error: %s" % stderr) if fo: fo.write(u'==> %s <==\n' % cmd) if config.verbose: fo.write(stdout + u'\n') else: if config.verbose: print(u'==> %s <==\n' % cmd) safe_print_unicode(stdout + u'\n')
def git_info(src_dir, verbose=True, fo=None): ''' Print info about a Git repo. ''' assert isdir(src_dir) git = external.find_executable('git') if not git: log = get_logger(__name__) log.warn( "git not installed in root environment. Skipping recording of git info." ) return if verbose: stderr = None else: FNULL = open(os.devnull, 'w') stderr = FNULL # Ensure to explicitly set GIT_DIR as some Linux machines will not # properly execute without it. env = os.environ.copy() env['GIT_DIR'] = join(src_dir, '.git') env = {str(key): str(value) for key, value in env.items()} for cmd, check_error in [('git log -n1', True), ('git describe --tags --dirty', False), ('git status', True)]: try: stdout = check_output_env(cmd.split(), stderr=stderr, cwd=src_dir, env=env) except CalledProcessError as e: if check_error: raise Exception("git error: %s" % str(e)) encoding = locale.getpreferredencoding() if not fo: encoding = sys.stdout.encoding encoding = encoding or 'utf-8' if hasattr(stdout, 'decode'): stdout = stdout.decode(encoding, 'ignore') if fo: fo.write(u'==> %s <==\n' % cmd) if verbose: fo.write(stdout + u'\n') else: if verbose: print(u'==> %s <==\n' % cmd) safe_print_unicode(stdout + u'\n')
def git_info(src_dir, verbose=True, fo=None): ''' Print info about a Git repo. ''' assert isdir(src_dir) git = external.find_executable('git') if not git: log = get_logger(__name__) log.warn("git not installed in root environment. Skipping recording of git info.") return if verbose: stderr = None else: FNULL = open(os.devnull, 'w') stderr = FNULL # Ensure to explicitly set GIT_DIR as some Linux machines will not # properly execute without it. env = os.environ.copy() env['GIT_DIR'] = join(src_dir, '.git') env = {str(key): str(value) for key, value in env.items()} for cmd, check_error in [ ('git log -n1', True), ('git describe --tags --dirty', False), ('git status', True)]: try: stdout = check_output_env(cmd.split(), stderr=stderr, cwd=src_dir, env=env) except CalledProcessError as e: if check_error: raise Exception("git error: %s" % str(e)) encoding = locale.getpreferredencoding() if not fo: encoding = sys.stdout.encoding encoding = encoding or 'utf-8' if hasattr(stdout, 'decode'): stdout = stdout.decode(encoding, 'ignore') if fo: fo.write(u'==> %s <==\n' % cmd) if verbose: fo.write(stdout + u'\n') else: if verbose: print(u'==> %s <==\n' % cmd) safe_print_unicode(stdout + u'\n')