def detect_vcs_conflicts(options, vcs): """Determine if the checkout has problems like cvs conflicts. If you want more vcs support here just keep adding if blocks... This could be better. TODO(antarus): Also this should probably not call sys.exit() as repoman is run on >1 packages and one failure should not cause subsequent packages to fail. Args: vcs - A string identifying the version control system in use Returns: None (calls sys.exit on fatal problems) """ retval = ("","") if vcs == 'cvs': logging.info("Performing a " + output.green("cvs -n up") + \ " with a little magic grep to check for updates.") retval = subprocess_getstatusoutput("cvs -n up 2>&1 | " + \ "egrep '^[^\?] .*' | " + \ "egrep -v '^. .*/digest-[^/]+|^cvs server: .* -- ignored$'") if vcs == 'svn': logging.info("Performing a " + output.green("svn status -u") + \ " with a little magic grep to check for updates.") retval = subprocess_getstatusoutput("svn status -u 2>&1 | " + \ "egrep -v '^. +.*/digest-[^/]+' | " + \ "head -n-1") if vcs in ['cvs', 'svn']: mylines = retval[1].splitlines() myupdates = [] for line in mylines: if not line: continue if line[0] not in "UPMARD": # Updates,Patches,Modified,Added,Removed/Replaced(svn),Deleted(svn) logging.error(red("!!! Please fix the following issues reported " + \ "from cvs: ")+green("(U,P,M,A,R,D are ok)")) logging.error(red("!!! Note: This is a pretend/no-modify pass...")) logging.error(retval[1]) sys.exit(1) elif vcs == 'cvs' and line[0] in "UP": myupdates.append(line[2:]) elif vcs == 'svn' and line[8] == '*': myupdates.append(line[9:].lstrip(" 1234567890")) if myupdates: logging.info(green("Fetching trivial updates...")) if options.pretend: logging.info("(" + vcs + " update " + " ".join(myupdates) + ")") retval = os.EX_OK else: retval = os.system(vcs + " update " + " ".join(myupdates)) if retval != os.EX_OK: logging.fatal("!!! " + vcs + " exited with an error. Terminating.") sys.exit(retval)
def get_term_size(): """ Get the number of lines and columns of the tty that is connected to stdout. Returns a tuple of (lines, columns) or (-1, -1) if an error occurs. The curses module is used if available, otherwise the output of `stty size` is parsed. """ if not sys.stdout.isatty(): return -1, -1 try: import curses try: curses.setupterm() return curses.tigetnum('lines'), curses.tigetnum('cols') except curses.error: pass except ImportError: pass st, out = subprocess_getstatusoutput('stty size') if st == os.EX_OK: out = out.split() if len(out) == 2: try: return int(out[0]), int(out[1]) except ValueError: pass return -1, -1
def get_term_size(): """ Get the number of lines and columns of the tty that is connected to stdout. Returns a tuple of (lines, columns) or (-1, -1) if an error occurs. The curses module is used if available, otherwise the output of `stty size` is parsed. """ if not sys.stdout.isatty(): return -1, -1 try: import curses try: curses.setupterm() return curses.tigetnum('lines'), curses.tigetnum('cols') except curses.error: pass except ImportError: pass st, out = subprocess_getstatusoutput('stty size') if st == os.EX_OK: out = out.split() if len(out) == 2: try: return int(out[0]), int(out[1]) except ValueError: pass return -1, -1
def _execcmd(self, *cmd, **kwargs): cmdstr = " ".join(cmd) status, output = subprocess_getstatusoutput(cmdstr) if status != 0: sys.stderr.write(cmdstr) sys.stderr.write("\n") sys.stderr.write(output) raise Error("command failed: %s\n%s" % (cmdstr, output)) return status, output
def _execcmd(self, *cmd, **kwargs): cmdstr = " ".join(cmd) status, output = subprocess_getstatusoutput(cmdstr) if status != 0: sys.stderr.write(cmdstr) sys.stderr.write("\n") sys.stderr.write(output) raise Error("command failed: %s\n%s" % (cmdstr, output)) return status, output
def find_updated_config_files(target_root, config_protect): """ Return a tuple of configuration files that needs to be updated. The tuple contains lists organized like this: [ protected_dir, file_list ] If the protected config isn't a protected_dir but a procted_file, list is: [ protected_file, None ] If no configuration files needs to be updated, None is returned """ os = _os_merge if config_protect: # directories with some protect files in them for x in config_protect: files = [] x = os.path.join(target_root, x.lstrip(os.path.sep)) if not os.access(x, os.W_OK): continue try: mymode = os.lstat(x).st_mode except OSError: continue if stat.S_ISLNK(mymode): # We want to treat it like a directory if it # is a symlink to an existing directory. try: real_mode = os.stat(x).st_mode if stat.S_ISDIR(real_mode): mymode = real_mode except OSError: pass if stat.S_ISDIR(mymode): mycommand = \ "find '%s' -name '.*' -type d -prune -o -name '._cfg????_*'" % x else: mycommand = "find '%s' -maxdepth 1 -name '._cfg????_%s'" % \ os.path.split(x.rstrip(os.path.sep)) mycommand += " ! -name '.*~' ! -iname '.*.bak' -print0" a = subprocess_getstatusoutput(mycommand) if a[0] == 0: files = a[1].split('\0') # split always produces an empty string as the last element if files and not files[-1]: del files[-1] if files: if stat.S_ISDIR(mymode): yield (x, files) else: yield (x, None)
def _get_target(self): global VERSION if VERSION is not self: return VERSION if os.path.isdir(os.path.join(PORTAGE_BASE_PATH, '.git')): status, output = subprocess_getstatusoutput( "cd %s ; git describe --tags" % \ _shell_quote(PORTAGE_BASE_PATH)) if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: VERSION = output return VERSION VERSION = 'HEAD' return VERSION
def _get_target(self): global VERSION if VERSION is not self: return VERSION if os.path.isdir(os.path.join(PORTAGE_BASE_PATH, '.git')): status, output = subprocess_getstatusoutput( "cd %s ; git describe --tags" % \ _shell_quote(PORTAGE_BASE_PATH)) if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: VERSION = output return VERSION VERSION = 'HEAD' return VERSION
def diffstatusoutput_len(cmd): """ Execute the string cmd in a shell with getstatusoutput() and return a 2-tuple (status, output_length). If getstatusoutput() raises UnicodeDecodeError (known to happen with python3.1), return a 2-tuple (1, 1). This provides a simple way to check for non-zero output length of diff commands, while providing simple handling of UnicodeDecodeError when necessary. """ try: status, output = subprocess_getstatusoutput(cmd) return (status, len(output)) except UnicodeDecodeError: return (1, 1)
def diffstatusoutput_len(cmd): """ Execute the string cmd in a shell with getstatusoutput() and return a 2-tuple (status, output_length). If getstatusoutput() raises UnicodeDecodeError (known to happen with python3.1), return a 2-tuple (1, 1). This provides a simple way to check for non-zero output length of diff commands, while providing simple handling of UnicodeDecodeError when necessary. """ try: status, output = subprocess_getstatusoutput(cmd) return (status, len(output)) except UnicodeDecodeError: return (1, 1)
def chflags(cls, path, flags, opts=""): cmd = 'chflags %s %o %s' % (opts, flags, _shell_quote(path)) status, output = subprocess_getstatusoutput(cmd) if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: return # Try to generate an ENOENT error if appropriate. if 'h' in opts: _os_merge.lstat(path) else: _os_merge.stat(path) # Make sure the binary exists. if not portage.process.find_binary('chflags'): raise portage.exception.CommandNotFound('chflags') # Now we're not sure exactly why it failed or what # the real errno was, so just report EPERM. e = OSError(errno.EPERM, output) e.errno = errno.EPERM e.filename = path e.message = output raise e
def chflags(cls, path, flags, opts=""): cmd = 'chflags %s %o %s' % (opts, flags, _shell_quote(path)) status, output = subprocess_getstatusoutput(cmd) if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: return # Try to generate an ENOENT error if appropriate. if 'h' in opts: _os_merge.lstat(path) else: _os_merge.stat(path) # Make sure the binary exists. if not portage.process.find_binary('chflags'): raise portage.exception.CommandNotFound('chflags') # Now we're not sure exactly why it failed or what # the real errno was, so just report EPERM. e = OSError(errno.EPERM, output) e.errno = errno.EPERM e.filename = path e.message = output raise e
def chk_updated_info_files(root, infodirs, prev_mtimes, retval): if os.path.exists("/usr/bin/install-info"): out = portage.output.EOutput() regen_infodirs=[] for z in infodirs: if z=='': continue inforoot=normpath(root+z) if os.path.isdir(inforoot): infomtime = os.stat(inforoot)[stat.ST_MTIME] if inforoot not in prev_mtimes or \ prev_mtimes[inforoot] != infomtime: regen_infodirs.append(inforoot) if not regen_infodirs: portage.writemsg_stdout("\n") out.einfo("GNU info directory index is up-to-date.") else: portage.writemsg_stdout("\n") out.einfo("Regenerating GNU info directory index...") dir_extensions = ("", ".gz", ".bz2") icount=0 badcount=0 errmsg = "" for inforoot in regen_infodirs: if inforoot=='': continue if not os.path.isdir(inforoot) or \ not os.access(inforoot, os.W_OK): continue file_list = os.listdir(inforoot) file_list.sort() dir_file = os.path.join(inforoot, "dir") moved_old_dir = False processed_count = 0 for x in file_list: if x.startswith(".") or \ os.path.isdir(os.path.join(inforoot, x)): continue if x.startswith("dir"): skip = False for ext in dir_extensions: if x == "dir" + ext or \ x == "dir" + ext + ".old": skip = True break if skip: continue if processed_count == 0: for ext in dir_extensions: try: os.rename(dir_file + ext, dir_file + ext + ".old") moved_old_dir = True except EnvironmentError as e: if e.errno != errno.ENOENT: raise del e processed_count += 1 myso=subprocess_getstatusoutput("LANG=C LANGUAGE=C /usr/bin/install-info --dir-file="+inforoot+"/dir "+inforoot+"/"+x)[1] existsstr="already exists, for file `" if myso!="": if re.search(existsstr,myso): # Already exists... Don't increment the count for this. pass elif myso[:44]=="install-info: warning: no info dir entry in ": # This info file doesn't contain a DIR-header: install-info produces this # (harmless) warning (the --quiet switch doesn't seem to work). # Don't increment the count for this. pass else: badcount=badcount+1 errmsg += myso + "\n" icount=icount+1 if moved_old_dir and not os.path.exists(dir_file): # We didn't generate a new dir file, so put the old file # back where it was originally found. for ext in dir_extensions: try: os.rename(dir_file + ext + ".old", dir_file + ext) except EnvironmentError as e: if e.errno != errno.ENOENT: raise del e # Clean dir.old cruft so that they don't prevent # unmerge of otherwise empty directories. for ext in dir_extensions: try: os.unlink(dir_file + ext + ".old") except EnvironmentError as e: if e.errno != errno.ENOENT: raise del e #update mtime so we can potentially avoid regenerating. prev_mtimes[inforoot] = os.stat(inforoot)[stat.ST_MTIME] if badcount: out.eerror("Processed %d info files; %d errors." % \ (icount, badcount)) writemsg_level(errmsg, level=logging.ERROR, noiselevel=-1) else: if icount > 0: out.einfo("Processed %d info files." % (icount,))
pass # There is only one implementation for size def getsize(filename): size = os.stat(filename).st_size return (size, size) hashfunc_map["size"] = getsize # end actual hash functions prelink_capable = False if os.path.exists(PRELINK_BINARY): results = subprocess_getstatusoutput(PRELINK_BINARY + " --version > /dev/null 2>&1") if (results[0] >> 8) == 0: prelink_capable = 1 del results def perform_md5(x, calc_prelink=0): return perform_checksum(x, "MD5", calc_prelink)[0] def _perform_md5_merge(x, **kwargs): return perform_md5( _unicode_encode(x, encoding=_encodings['merge'], errors='strict'), **kwargs)
hashorigin_map["MD5"] = "python-fchksum" except ImportError: pass # There is only one implementation for size def getsize(filename): size = os.stat(filename).st_size return (size, size) hashfunc_map["size"] = getsize # end actual hash functions prelink_capable = False if os.path.exists(PRELINK_BINARY): results = subprocess_getstatusoutput(PRELINK_BINARY+" --version > /dev/null 2>&1") if (results[0] >> 8) == 0: prelink_capable=1 del results def perform_md5(x, calc_prelink=0): return perform_checksum(x, "MD5", calc_prelink)[0] def _perform_md5_merge(x, **kwargs): return perform_md5(_unicode_encode(x, encoding=_encodings['merge'], errors='strict'), **kwargs) def perform_all(x, calc_prelink=0): mydict = {} for k in hashfunc_map: mydict[k] = perform_checksum(x, hashfunc_map[k], calc_prelink)[0]