def testBashSyntax(self): for parent, dirs, files in os.walk(PORTAGE_BIN_PATH): parent = _unicode_decode(parent, encoding=_encodings['fs'], errors='strict') for x in files: x = _unicode_decode(x, encoding=_encodings['fs'], errors='strict') ext = x.split('.')[-1] if ext in ('.py', '.pyc', '.pyo'): continue x = os.path.join(parent, x) st = os.lstat(x) if not stat.S_ISREG(st.st_mode): continue # Check for bash shebang f = open( _unicode_encode(x, encoding=_encodings['fs'], errors='strict'), 'rb') line = _unicode_decode(f.readline(), encoding=_encodings['content'], errors='replace') f.close() if line[:2] == '#!' and \ 'bash' in line: cmd = "%s -n %s" % (_shell_quote(BASH_BINARY), _shell_quote(x)) status, output = subprocess_getstatusoutput(cmd) self.assertEqual(os.WIFEXITED(status) and \ os.WEXITSTATUS(status) == os.EX_OK, True, msg=output)
def _testLockNonblock(self): tempdir = tempfile.mkdtemp() try: path = os.path.join(tempdir, 'lock_me') lock1 = portage.locks.lockfile(path) pid = os.fork() if pid == 0: portage.locks._close_fds() # Disable close_fds since we don't exec # (see _setup_pipes docstring). portage.process._setup_pipes({0:0, 1:1, 2:2}, close_fds=False) rval = 2 try: try: lock2 = portage.locks.lockfile(path, flags=os.O_NONBLOCK) except portage.exception.TryAgain: rval = os.EX_OK else: rval = 1 portage.locks.unlockfile(lock2) except SystemExit: raise except: traceback.print_exc() finally: os._exit(rval) self.assertEqual(pid > 0, True) pid, status = os.waitpid(pid, 0) self.assertEqual(os.WIFEXITED(status), True) self.assertEqual(os.WEXITSTATUS(status), os.EX_OK) portage.locks.unlockfile(lock1) finally: shutil.rmtree(tempdir)
def get_commit_message_with_editor(editor, message=None, prefix=""): """ Execute editor with a temporary file as it's argument and return the file content afterwards. @param editor: An EDITOR value from the environment @type: string @param message: An iterable of lines to show in the editor. @type: iterable @param prefix: Suggested prefix for the commit message summary line. @type: string @rtype: string or None @return: A string on success or None if an error occurs. """ commitmessagedir = tempfile.mkdtemp(".repoman.msg") filename = os.path.join(commitmessagedir, "COMMIT_EDITMSG") try: with open(filename, "wb") as mymsg: mymsg.write( _unicode_encode( _(prefix + "\n\n# Please enter the commit message " "for your changes.\n# (Comment lines starting " "with '#' will not be included)\n"), encoding=_encodings['content'], errors='backslashreplace')) if message: mymsg.write(b"#\n") for line in message: mymsg.write( _unicode_encode("#" + line, encoding=_encodings['content'], errors='backslashreplace')) retval = os.system(editor + " '%s'" % filename) if not (os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == os.EX_OK): return None try: with io.open(_unicode_encode(filename, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace') as f: mylines = f.readlines() except OSError as e: if e.errno != errno.ENOENT: raise del e return None return "".join(line for line in mylines if not line.startswith("#")) finally: try: shutil.rmtree(commitmessagedir) except OSError: pass
def get_commit_message_with_editor(editor, message=None): """ Execute editor with a temporary file as it's argument and return the file content afterwards. @param editor: An EDITOR value from the environment @type: string @param message: An iterable of lines to show in the editor. @type: iterable @rtype: string or None @returns: A string on success or None if an error occurs. """ from tempfile import mkstemp fd, filename = mkstemp() try: os.write(fd, _unicode_encode( "\n# Please enter the commit message " + \ "for your changes.\n# (Comment lines starting " + \ "with '#' will not be included)\n", encoding=_encodings['content'], errors='backslashreplace')) if message: os.write( fd, _unicode_encode("#\n", encoding=_encodings['content'], errors='backslashreplace')) for line in message: os.write( fd, _unicode_encode("#" + line, encoding=_encodings['content'], errors='backslashreplace')) os.close(fd) retval = os.system(editor + " '%s'" % filename) if not (os.WIFEXITED(retval) and os.WEXITSTATUS(retval) == os.EX_OK): return None try: mylines = codecs.open(_unicode_encode(filename, encoding=_encodings['fs'], errors='strict'), mode='r', encoding=_encodings['content'], errors='replace').readlines() except OSError as e: if e.errno != errno.ENOENT: raise del e return None return "".join(line for line in mylines if not line.startswith("#")) finally: try: os.unlink(filename) except OSError: pass
def testBashSyntax(self): locations = [PORTAGE_BIN_PATH] misc_dir = os.path.join(PORTAGE_BASE_PATH, "misc") if os.path.isdir(misc_dir): locations.append(misc_dir) for parent, dirs, files in chain.from_iterable( os.walk(x) for x in locations): parent = _unicode_decode(parent, encoding=_encodings["fs"], errors="strict") for x in files: x = _unicode_decode(x, encoding=_encodings["fs"], errors="strict") ext = x.split(".")[-1] if ext in (".py", ".pyc", ".pyo"): continue x = os.path.join(parent, x) st = os.lstat(x) if not stat.S_ISREG(st.st_mode): continue # Check for bash shebang f = open( _unicode_encode(x, encoding=_encodings["fs"], errors="strict"), "rb") line = _unicode_decode(f.readline(), encoding=_encodings["content"], errors="replace") f.close() if line[:2] == "#!" and "bash" in line: cmd = [BASH_BINARY, "-n", x] cmd = [ _unicode_encode(x, encoding=_encodings["fs"], errors="strict") for x in cmd ] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = _unicode_decode(proc.communicate()[0], encoding=_encodings["fs"]) status = proc.wait() self.assertEqual( os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK, True, msg=output, )
def testBashSyntax(self): locations = [PORTAGE_BIN_PATH] misc_dir = os.path.join(PORTAGE_BASE_PATH, "misc") if os.path.isdir(misc_dir): locations.append(misc_dir) for parent, dirs, files in \ chain.from_iterable(os.walk(x) for x in locations): parent = _unicode_decode(parent, encoding=_encodings['fs'], errors='strict') for x in files: x = _unicode_decode(x, encoding=_encodings['fs'], errors='strict') ext = x.split('.')[-1] if ext in ('.py', '.pyc', '.pyo'): continue x = os.path.join(parent, x) st = os.lstat(x) if not stat.S_ISREG(st.st_mode): continue # Check for bash shebang f = open( _unicode_encode(x, encoding=_encodings['fs'], errors='strict'), 'rb') line = _unicode_decode(f.readline(), encoding=_encodings['content'], errors='replace') f.close() if line[:2] == '#!' and \ 'bash' in line: cmd = [BASH_BINARY, "-n", x] if sys.hexversion < 0x3000000 or sys.hexversion >= 0x3020000: # Python 3.1 does not support bytes in Popen args. cmd = [ _unicode_encode(x, encoding=_encodings['fs'], errors='strict') for x in cmd ] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = _unicode_decode(proc.communicate()[0], encoding=_encodings['fs']) status = proc.wait() self.assertEqual(os.WIFEXITED(status) and \ os.WEXITSTATUS(status) == os.EX_OK, True, msg=output)
def _set_returncode(self, wait_retval): """ Set the returncode in a manner compatible with subprocess.Popen.returncode: A negative value -N indicates that the child was terminated by signal N (Unix only). """ self._unregister() pid, status = wait_retval if os.WIFSIGNALED(status): retval = -os.WTERMSIG(status) else: retval = os.WEXITSTATUS(status) self.returncode = retval
# cache all supported hash methods in a frozenset hashfunc_keys = frozenset(hashfunc_map) # end actual hash functions prelink_capable = False if os.path.exists(PRELINK_BINARY): cmd = [PRELINK_BINARY, "--version"] cmd = [_unicode_encode(x, encoding=_encodings['fs'], errors='strict') for x in cmd] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc.communicate() status = proc.wait() if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: prelink_capable = 1 del cmd, proc, status def is_prelinkable_elf(filename): f = _open_file(filename) try: magic = f.read(17) finally: f.close() return (len(magic) == 17 and magic.startswith(b'\x7fELF') and magic[16:17] in (b'\x02', b'\x03')) # 2=ET_EXEC, 3=ET_DYN def perform_md5(x, calc_prelink=0): return perform_checksum(x, "MD5", calc_prelink)[0]
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 """ encoding = _encodings['fs'] 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" cmd = shlex_split(mycommand) if sys.hexversion < 0x3000000 or sys.hexversion >= 0x3020000: # Python 3.1 does not support bytes in Popen args. cmd = [_unicode_encode(arg, encoding=encoding, errors='strict') for arg in cmd] proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) output = _unicode_decode(proc.communicate()[0], encoding=encoding) status = proc.wait() if os.WIFEXITED(status) and os.WEXITSTATUS(status) == os.EX_OK: files = output.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)