def _writeInstalled(self, installed_db): if not self.__cygwinPlatform: raise PlatformException( "fail to create {0} only supported under Cygwin." "".format(installed_db)) sys.stderr.write("creating {0} ... ".format(installed_db)) db_contents = CygApt.INSTALLED_DB_MAGIC cygcheck_path = self.__pm.mapPath("/bin/cygcheck") if os.path.exists(cygcheck_path): cmd = [cygcheck_path, "-cd"] proc = Process(cmd) proc.mustRun() lines = proc.getOutput().splitlines(True) # remove first two lines pkgs = lines[2:] for pkg in pkgs: pkg = pkg.split() db_contents += "{0} {0}-{1}.tar.bz2 0\n".format( pkg[0], pkg[1]) f = open(installed_db, 'w') f.write(db_contents) f.close() sys.stderr.write("OK\n")
def testGpgImport(self): if not self._var_cygwin_p: self.skipTest("requires cygwin or linux"); self.obj._gpgImport(self.obj.GPG_CYG_PUBLIC_RING_URI); cmd = " ".join([ "gpg", "--no-secmem-warning", "--list-public-keys", "--fingerprint", ]); p = Process(cmd); p.mustRun(); lines = p.getOutput().splitlines(True); findout = False; for line in lines: if isinstance(line, bytes): marker = self.obj.GPG_GOOD_FINGER.encode(); else: marker = self.obj.GPG_GOOD_FINGER; if marker in line: findout = True; break; self.assertTrue(findout);
def testGpgImport(self): if not self._var_cygwin_p: self.skipTest("requires cygwin or linux") self.obj._gpgImport(self.obj.GPG_CYG_PUBLIC_RING_URI) cmd = " ".join([ "gpg", "--no-secmem-warning", "--list-public-keys", "--fingerprint", ]) p = Process(cmd) p.mustRun() lines = p.getOutput().splitlines(True) findout = False for line in lines: if isinstance(line, bytes): marker = self.obj.GPG_GOOD_FINGER.encode() else: marker = self.obj.GPG_GOOD_FINGER if marker in line: findout = True break self.assertTrue(findout)
def _writeInstalled(self, installed_db): if not self.__cygwinPlatform: raise PlatformException( "fail to create {0} only supported under Cygwin." "".format(installed_db) ); sys.stderr.write("creating {0} ... ".format(installed_db)); db_contents = CygApt.INSTALLED_DB_MAGIC; cygcheck_path = self.__pm.mapPath("/bin/cygcheck"); if os.path.exists(cygcheck_path): cmd = [cygcheck_path, "-cd"]; proc = Process(cmd); proc.mustRun(); lines = proc.getOutput().splitlines(True); # remove first two lines pkgs = lines[2:]; for pkg in pkgs: pkg = pkg.split(); db_contents += "{0} {0}-{1}.tar.bz2 0\n".format(pkg[0], pkg[1]); f = open(installed_db, 'w'); f.write(db_contents); f.close(); sys.stderr.write("OK\n");
def testRunReturnAlwaysExitCodeEvenOnCommandFailed(self): proc = Process( 'nonexistingcommandIhopeneversomeonewouldnameacommandlikethis') actual = proc.run() self.assertTrue( 0 < actual, "Failed asserting that {0} is greater than 0".format(actual))
def testExitCodeCommandFailed(self): process = Process( 'nonexistingcommandIhopeneversomeonewouldnameacommandlikethis') process.run() actual = process.getExitCode() self.assertTrue( 0 < actual, "Failed asserting that {0} is greater than 0".format(actual))
def __init__(self, root, cygwin_p): self.__root = root self.__mountRoot = "/" self.__cygwinPlatform = cygwin_p self.__map = {} p = Process([self.__root + "/bin/mount"]) p.run() mountout = p.getOutput().splitlines(True) self._addMapping(mountout)
def __init__(self, root, cygwin_p): self.__root = root; self.__mountRoot = "/"; self.__cygwinPlatform = cygwin_p; self.__map = {}; p = Process([self.__root + "/bin/mount"]); p.run(); mountout = p.getOutput().splitlines(True); self._addMapping(mountout);
def _checkForSetupExe(self): # It's far from bulletproof, but it's surprisingly hard to detect # setup.exe running since it doesn't lock any files. p = Process([self.__pm.mapPath("/bin/ps"), "-W"]); p.run(); psout = p.getOutput().splitlines(True); setup_re = re.compile(r"(?<![a-z0-9_ -])setup(|-1\.7|-x86|-x86_64)\.exe", re.IGNORECASE); for l in psout: m = setup_re.search(l); if m: raise AppConflictException( "Please close {0} while " "running {1}".format(m.group(0), self.__appName) );
def _checkForSetupExe(self): # It's far from bulletproof, but it's surprisingly hard to detect # setup.exe running since it doesn't lock any files. p = Process([self.__pm.mapPath("/bin/ps"), "-W"]) p.run() psout = p.getOutput().splitlines(True) setup_re = re.compile( r"(?<![a-z0-9_ -])setup(|-1\.7|-x86|-x86_64)\.exe", re.IGNORECASE) for l in psout: m = setup_re.search(l) if m: raise AppConflictException("Please close {0} while " "running {1}".format( m.group(0), self.__appName))
def _doInstallExternal(self, ball): # Currently we use a temporary directory and extractall() then copy: # this is very slow. The Python documentation warns more sophisticated # approaches have pitfalls without specifying what they are. tf = cautils.open_tarfile(ball, self.__dosXz) members = tf.getmembers() tempdir = tempfile.mkdtemp() try: tf.extractall(tempdir) for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name) if not os.path.exists(path): os.makedirs(path, m.mode) for m in members: if m.isdir(): path = self.__pm.mapPath("/" + m.name) if not os.path.exists(path): os.makedirs(path, m.mode) else: path = self.__pm.mapPath("/" + m.name) dirname = os.path.dirname(path) if not os.path.exists(dirname): os.makedirs(dirname) if os.path.exists(path): os.chmod(path, 0o777) os.remove(path) # Windows extract() is robust but can't form Cygwin links # (It produces copies instead: bulky and bugbait.) # Convert to links if possible -- depends on coreutils being installed if m.issym() and self.__lnExists: link_target = m.linkname Process([self.__dosLn, "-s", link_target, path]).run(True) elif m.islnk() and self.__lnExists: # Hard link -- expect these to be very rare link_target = m.linkname mapped_target = self.__pm.mapPath("/" + m.linkname) # Must ensure target exists before forming hard link if not os.path.exists(mapped_target): shutil.move(os.path.join(tempdir, link_target), mapped_target) Process([self.__dosLn, mapped_target, path]).run(True) else: shutil.move(os.path.join(tempdir, m.name), path) finally: tf.close() cautils.rmtree(tempdir)
def _runScript(self, file_name, optional=True): mapped_file = self.__pm.mapPath(file_name) mapped_file_done = mapped_file + ".done" if os.path.isfile(mapped_file): sys.stderr.write("running: {0}\n".format(file_name)) cmd = ["bash"] + self.SH_OPTIONS + [mapped_file] if not self.__cygwinPlatform: cmd[0] = self.__dosBash cwd = None extension = os.path.splitext(mapped_file)[1] if ".dash" == extension: cmd = ["dash"] + self.DASH_OPTIONS + [mapped_file] if not self.__cygwinPlatform: cmd[0] = self.__dosDash if extension in [".bat", ".cmd"]: cmd = ["cmd" ] + self.CMD_OPTIONS + [os.path.basename(mapped_file)] cwd = os.path.dirname(mapped_file) retval = Process(cmd, cwd).run(True) if os.path.exists(mapped_file_done): os.remove(mapped_file_done) if retval == 0 and os.path.basename(file_name)[:3] not in [ '0p_', 'zp_' ]: shutil.move(mapped_file, mapped_file_done) else: if not optional: sys.stderr.write("{0}: WARNING couldn't find {1}.\n".format( self.__appName, mapped_file))
def open_tarfile(ball, xzPath='xz'): """Opens a tar file like `tarfile.open`. Supports also LZMA compressed tarballs. @param ball: str A tar file, it can be compressed @param xzPath: str A path to the lzma program @return: TarFile An appropriate TarFile instance """ assert isinstance(xzPath, str) ball_orig = ball if ball.lower().endswith('.tar.xz'): ball_orig = ball ball = ball[:-3] # remove .xz extension remove_if_exists(ball) Process([xzPath, '-k', '-d', ball_orig]).mustRun() tf = tarfile.open(ball) if ball_orig != ball: tf_close_orig = tf.close def tf_close(): retValue = tf_close_orig() remove_if_exists(ball) return retValue tf.close = tf_close return tf
def _integrityControl(self, checklist=None): if None is checklist: checklist = list() options = ["-c"] if self.__verbose: options.append("-v") if len(checklist) == 0: checklist.append(self.__pkgName) command = ["/bin/cygcheck"] + options + checklist if not self.__cygwinPlatform: command = subprocess.list2cmdline(command) command = [self.__dosBash] + self.SH_OPTIONS + ["-c"] + [command] p = Process(command) p.run() outputlines = p.getOutput().splitlines(True) unformat = "" start = False incomplete = [] for res in outputlines: try: res_split = res.split() package, version, status = res_split except ValueError: if len(res_split) > 0: unformat += res continue if package == 'Package' \ and version == 'Version' \ and status == 'Status': start = True unformat = '' elif not start: continue if start and status == 'Incomplete': print(res[:-2]) print(unformat) unformat = "" incomplete.append(package) return incomplete
def _integrityControl(self, checklist=None): if None is checklist : checklist = list(); options = ["-c"]; if self.__verbose: options.append("-v"); if len(checklist) == 0: checklist.append(self.__pkgName); command = ["/bin/cygcheck"] + options + checklist; if not self.__cygwinPlatform: command = subprocess.list2cmdline(command); command = [self.__dosBash] + self.SH_OPTIONS + ["-c"] + [command]; p = Process(command); p.run(); outputlines = p.getOutput().splitlines(True); unformat = ""; start = False; incomplete = []; for res in outputlines: try: res_split = res.split(); package, version, status = res_split; except ValueError: if len(res_split) > 0: unformat += res; continue; if package == 'Package' \ and version == 'Version' \ and status == 'Status': start = True; unformat = ''; elif not start: continue; if start and status == 'Incomplete': print(res[:-2]); print(unformat); unformat = ""; incomplete.append(package); return incomplete;
def testResponsesCommandFailed(self, expected, method): process = Process( 'python -c \'import sys; sys.stdout.write("foo"); sys.stderr.write("bar");\'' ) process.run() process.setCommandLine( 'nonexistingcommandIhopeneversomeonewouldnameacommandlikethis') process.run() self.assertEqual(getattr(process, method)(), expected)
def testResponsesCommandFailed(self, expected, method): process = Process('python -c \'import sys; sys.stdout.write("foo"); sys.stderr.write("bar");\'') process.run() process.setCommandLine("nonexistingcommandIhopeneversomeonewouldnameacommandlikethis") process.run() self.assertEqual(getattr(process, method)(), expected)
def testRunWorkingDirectoryIsUsed(self, startMethod): popen = subprocess.Popen directory = os.path.dirname(__file__) def mock(*args, **kargs): self.assertEqual(kargs["cwd"], directory) return popen(*args, **kargs) subprocess.Popen = mock try: proc = Process("python --version") proc.setWorkingDirectory(directory) getattr(proc, startMethod)() finally: subprocess.Popen = popen
def testRunWorkingDirectoryIsUsed(self, startMethod): popen = subprocess.Popen directory = os.path.dirname(__file__) def mock(*args, **kargs): self.assertEqual(kargs['cwd'], directory) return popen(*args, **kargs) subprocess.Popen = mock try: proc = Process('python --version') proc.setWorkingDirectory(directory) getattr(proc, startMethod)() finally: subprocess.Popen = popen
def _gpgImport(self, uri): if not self.__cygwinPlatform: return cautils.uri_get(self.__tmpDir, uri, verbose=self.__verbose) tmpfile = os.path.join(self.__tmpDir, os.path.basename(uri)) cmd = ["gpg"] cmd.append("--no-secmem-warning") cmd += ["--import", tmpfile] Process(cmd).mustRun()
def testProcessPipes(self, size, getter, code): expected = '*' * 1024 * size + '!' expectedLength = 1024 * size + 1 proc = Process("python -c '{0}'".format(code)) proc.setInput(expected) proc.run() self.assertEqual(len(getattr(proc, getter)()), expectedLength) self.assertEqual(proc.getExitCode(), 0)
def _compressFollowingTargetExtension(self, srcPath, targetPath): compression = targetPath.split(".")[-1] if "bz2" == compression: f = open(srcPath, 'rb') contents = f.read() compressed = bz2.compress(contents) f.close() f = open(targetPath, 'wb') f.write(compressed) f.close() os.remove(srcPath) elif "xz" == compression: Process(['xz', '-f', srcPath]).mustRun()
def testProcessPipes(self, size, getter, code): expected = "*" * 1024 * size + "!" expectedLength = 1024 * size + 1 proc = Process("python -c '{0}'".format(code)) proc.setInput(expected) proc.run() self.assertEqual(len(getattr(proc, getter)()), expectedLength) self.assertEqual(proc.getExitCode(), 0)
def testRunInheritOutput(self, startMethod): popen = subprocess.Popen def mock(*args, **kargs): self.assertTrue(None is kargs['stdout']) self.assertTrue(None is kargs['stderr']) return popen(*args, **kargs) subprocess.Popen = mock try: proc = Process("python -c ''") getattr(proc, startMethod)(True) finally: subprocess.Popen = popen
def testSetStreamAsInput(self, size, getter, code): expected = '*' * 1024 * size + '!' expectedLength = 1024 * size + 1 stream = TemporaryFile() stream.write(expected) stream.seek(0) proc = Process("python -c '{0}'".format(code)) proc.setInput(stream) proc.run() stream.close() self.assertEqual(len(getattr(proc, getter)()), expectedLength) self.assertEqual(proc.getExitCode(), 0)
def testDefaultGetterSetter(self, fn, value): proc = Process('python') setter = 'set' + fn getter = 'get' + fn self.assertTrue( None is getattr(proc, setter)(value), "Failed asserting that setter method return None", ) self.assertEqual(getattr(proc, getter)(), value) if not isinstance(value, str): self.assertFalse( value is getattr(proc, getter)(), "Failed asserting that getter method return a cloned value", )
def testSetStreamAsInput(self, size, getter, code): expected = "*" * 1024 * size + "!" expectedLength = 1024 * size + 1 stream = TemporaryFile() stream.write(expected) stream.seek(0) proc = Process("python -c '{0}'".format(code)) proc.setInput(stream) proc.run() stream.close() self.assertEqual(len(getattr(proc, getter)()), expectedLength) self.assertEqual(proc.getExitCode(), 0)
def cygpath(path): p = Process(["cygpath", path]); p.run(); dospath = p.getOutput().strip(); return dospath;
def testGetErrorOutputProcessNotStarted(self): proc = Process("python --version") proc.getErrorOutput()
def testConstructorSetTheCwd(self): proc = Process('foo', 'bar') self.assertEqual(proc.getWorkingDirectory(), 'bar')
def testConstructor(self): proc = Process('foo') self.assertEqual(proc.getCommandLine(), 'foo') self.assertEqual(proc.getWorkingDirectory(), None) self.assertEqual(proc.getInput(), None)
def testProcessResponses(self, expected, getter, code): proc = Process("python -c '{0}'".format(code)) proc.run() self.assertEqual(getattr(proc, getter)(), expected)
def update(self, cyg_apt_rc, verify, main_mirror=None): """fetch current package database from mirror""" sig_name = None self.__rc = cautils.parse_rc(cyg_apt_rc) if (not self.__cygwinPlatform): self.__pm = PathMapper(self.__rc.ROOT[:-1], False) if (main_mirror): mirror = main_mirror else: mirror = self.__rc.mirror if not mirror: raise UnexpectedValueException( "A mirror must be specified on the configuration file \"{0}\" " "or with the command line option \"--mirror\". " "See cygwin.com/mirrors.html for the list of mirrors." "".format(cyg_apt_rc)) if not mirror[-1] == "/": sep = "/" else: sep = "" setup_ini_names = [ "setup.bz2", "setup.ini", ] bag = zip(setup_ini_names, list(range(len(setup_ini_names)))) platform_dir = self.__arch + "/" for (setup_ini_name, index) in bag: setup_ini_url = '{0}{1}{2}{3}'.format(mirror, sep, platform_dir, setup_ini_name) try: cautils.uri_get(self.__tmpDir, setup_ini_url, verbose=self.__verbose) except ApplicationException as e: # Failed to find a possible .ini if index == len(setup_ini_names) - 1: raise e else: continue # Not an error to fail to find the first one # Take the first one we find break if setup_ini_name[-4:] == ".bz2": bz_file = os.path.join(self.__tmpDir, setup_ini_name) f = open(bz_file, "rb") compressed = f.read() f.close() decomp = bz2.decompress(compressed) os.remove(bz_file) setup_ini_name = "setup.ini" f = open(os.path.join(self.__tmpDir, setup_ini_name), "wb") f.write(decomp) f.close() if not self.__cygwinPlatform: sys.stderr.write( "WARNING can't verify setup.ini outside Cygwin.\n") verify = False if verify: sig_name = "{0}.sig".format(setup_ini_name) sig_url = "{0}{1}{2}{3}".format(mirror, sep, platform_dir, sig_name) try: cautils.uri_get(self.__tmpDir, sig_url, verbose=self.__verbose) except RequestException as e: msg = ("Failed to download signature {0} Use -X to ignore " "signatures.".format(sig_url)) raise RequestException(msg, previous=e) if self.__cygwinPlatform: gpg_path = "gpg" else: if self._cygwinVersion() < 1.7: gpg_path = "/usr/bin/gpg" else: gpg_path = "/usr/local/bin/gpg" cmd = [gpg_path, "--verify", "--no-secmem-warning"] cmd.append("{0}/{1}".format(self.__tmpDir, sig_name)) cmd.append("{0}/{1}".format(self.__tmpDir, setup_ini_name)) p = Process(cmd) p.run() verify = p.getErrorOutput() if isinstance(verify, bytes): marker = self.GPG_GOOD_FINGER.encode() else: marker = self.GPG_GOOD_FINGER if not marker in verify: msg = ("{0} not signed by Cygwin's public key. " "Use -X to ignore signatures.".format(setup_ini_url)) raise SignatureException(msg) downloads = os.path.join( self.__pm.mapPath(self.__rc.cache), urllib.quote(mirror + ('' if mirror.endswith('/') else '/'), '').lower(), platform_dir, ) if not os.path.exists(downloads): os.makedirs(downloads) shutil.copy(os.path.join(self.__tmpDir, setup_ini_name), os.path.join(downloads, setup_ini_name)) # BC layer for `setup_ini` configuration field if self.__rc.setup_ini: setup_ini = self.__pm.mapPath(self.__rc.setup_ini) if os.path.exists(setup_ini): shutil.copy(setup_ini, "{0}.bak".format(setup_ini)) shutil.copy(os.path.join(downloads, setup_ini_name), setup_ini) if os.path.exists(os.path.join(self.__tmpDir, setup_ini_name)): os.remove(os.path.join(self.__tmpDir, setup_ini_name)) if sig_name: if os.path.exists(os.path.join(self.__tmpDir, sig_name)): os.remove(os.path.join(self.__tmpDir, sig_name))
def testRunReturnAlwaysExitCode(self, expected, getter, code): proc = Process("python -c '{0}'".format(code)) actual = proc.run() self.assertEqual(actual, proc.getExitCode())
def testConstructor(self): proc = Process("foo") self.assertEqual(proc.getCommandLine(), "foo") self.assertEqual(proc.getWorkingDirectory(), None) self.assertEqual(proc.getInput(), None)
def testExitCodeTextIsNoneWhenExitCodeIsNone(self): process = Process('') self.assertEqual(process.getExitCodeText(), None)
def testInvalidInput(self, value): process = Process("python --version") process.setInput(value)
def testExitCodeTextIsNoneWhenExitCodeIsNone(self): process = Process("") self.assertEqual(process.getExitCodeText(), None)
def _generateDistFiles(self, distname='curr'): dirname = os.path.join(self._tmpdir, self.name + self.version.__dict__[distname]) os.makedirs(dirname) usr_d = os.path.join(dirname, "usr") etc_d = os.path.join(dirname, "etc") var_d = os.path.join(dirname, "var") bin_d = os.path.join(dirname, "usr", "bin") share_d = os.path.join(dirname, "usr", "share", self.name) version_d = os.path.join(share_d, self.version.__dict__[distname]) postinstall_d = os.path.join(dirname, "etc", "postinstall") postremove_d = os.path.join(dirname, "etc", "postremove") preremove_d = os.path.join(dirname, "etc", "preremove") marker_d = os.path.join(dirname, "var", self.name) os.makedirs(bin_d) os.makedirs(postinstall_d) os.makedirs(postremove_d) os.makedirs(preremove_d) os.makedirs(marker_d) os.makedirs(share_d) os.makedirs(version_d) bin_f = os.path.join(bin_d, self.name) link_bin_f = os.path.join(bin_d, self.name + "-link") link_version_d = os.path.join(share_d, "current") hardlink_bin_f = os.path.join(bin_d, self.name + "-hardlink") postinstall_f = os.path.join(postinstall_d, self.name + self._scriptsExt) postremove_f = os.path.join(postremove_d, self.name + self._scriptsExt) preremove_f = os.path.join(preremove_d, self.name + self._scriptsExt) marker_f = os.path.join(marker_d, "version") # create exec "#!/usr/bin/sh\necho running;" <pkg> > root/usr/bin # link # hard link f = open(bin_f, 'w') f.write('#!/bin/sh\necho "running";') f.close() Process(['ln', '-s', self.name, link_bin_f]).mustRun() Process(['ln', bin_f, hardlink_bin_f]).mustRun() Process([ 'ln', '-s', os.path.relpath(version_d, os.path.dirname(link_version_d)), link_version_d, ]).mustRun() self._writeScript(postinstall_f, 0) self._writeScript(preremove_f, 0) self._writeScript(postremove_f, 0) # create version marker > root/var/<pkg>/<version> f = open(marker_f, 'w') f.write(self.version.__dict__[distname]) f.close() # build install tar tar_name = os.path.join(self._localMirror, self.install.__dict__[distname].url) tarInstallPath = ".".join(tar_name.split(".")[:-1]) tar = tarfile.open(tarInstallPath, mode='w') for name in [usr_d, etc_d, var_d]: tar.add(name, os.path.basename(name)) members = tar.getmembers() tar.close() self._compressFollowingTargetExtension(tarInstallPath, tar_name) del tarInstallPath # Force slash to the end of each directories lst = [] for m in members: if m.isdir() and not m.name.endswith("/"): lst.append(m.name + "/") else: lst.append(m.name) f = open("{0}.lst".format(tar_name), 'w') f.write("\n".join(lst)) f.close() # build source tar tar_src_name = os.path.join(self._localMirror, self.source.__dict__[distname].url) tarSrcPath = ".".join(tar_src_name.split(".")[:-1]) tar = tarfile.open(tarSrcPath, mode='w') tar.add(dirname, "{0}-{1}".format(self.name, self.version.__dict__[distname])) tar.close() self._compressFollowingTargetExtension(tarSrcPath, tar_src_name) del tarSrcPath md5sum = self._md5Sum(tar_name) md5sum_src = self._md5Sum(tar_src_name) md5_sum_f = os.path.join(os.path.dirname(tar_name), "md5.sum") f = open(md5_sum_f, 'a') f.write("{0} {1}{LF}" "{2} {3}{LF}" "".format(md5sum, os.path.basename( self.install.__dict__[distname].url), md5sum_src, os.path.basename(self.source.__dict__[distname].url), LF="\n")) f.close()
def testConstructorSetTheCwd(self): proc = Process("foo", "bar") self.assertEqual(proc.getWorkingDirectory(), "bar")
def testValidInput(self, expected, value): process = Process("python --version") process.setInput(value) self.assertEqual(process.getInput(), expected)
def testMustRunThrowsException(self): process = Process('python -c \'import sys; sys.stdout.write("foo"); sys.stderr.write("bar"); exit(1);\'') process.mustRun()
def cygpath(path): p = Process(["cygpath", path]) p.run() dospath = p.getOutput().strip() return dospath
def testMustRun(self): process = Process('python -c \'import sys; sys.stdout.write("foo");\'') process.mustRun() self.assertEqual(process.getOutput(), 'foo') self.assertEqual(process.getExitCode(), 0)
def update(self, cyg_apt_rc, verify, main_mirror=None): """fetch current package database from mirror""" sig_name = None; self.__rc = cautils.parse_rc(cyg_apt_rc); if(not self.__cygwinPlatform): self.__pm = PathMapper(self.__rc.ROOT[:-1], False); if (main_mirror): mirror = main_mirror; else: mirror = self.__rc.mirror; if not mirror : raise UnexpectedValueException( "A mirror must be specified on the configuration file \"{0}\" " "or with the command line option \"--mirror\". " "See cygwin.com/mirrors.html for the list of mirrors." "".format(cyg_apt_rc) ); if not mirror[-1] == "/": sep = "/"; else: sep = ""; setup_ini_names = [ "setup.bz2", "setup.ini", ]; bag = zip(setup_ini_names, list(range(len(setup_ini_names)))); platform_dir = self.__arch+"/"; for (setup_ini_name, index) in bag: setup_ini_url = '{0}{1}{2}{3}'.format(mirror, sep, platform_dir, setup_ini_name); try: cautils.uri_get( self.__tmpDir, setup_ini_url, verbose=self.__verbose ); except ApplicationException as e: # Failed to find a possible .ini if index == len(setup_ini_names) - 1: raise e; else: continue; # Not an error to fail to find the first one # Take the first one we find break; if setup_ini_name[-4:] == ".bz2": bz_file = os.path.join(self.__tmpDir, setup_ini_name); f = open(bz_file, "rb"); compressed = f.read(); f.close(); decomp = bz2.decompress(compressed); os.remove(bz_file); setup_ini_name = "setup.ini"; f = open(os.path.join(self.__tmpDir, setup_ini_name), "wb"); f.write(decomp); f.close(); if not self.__cygwinPlatform: sys.stderr.write("WARNING can't verify setup.ini outside Cygwin.\n"); verify = False; if verify: sig_name = "{0}.sig".format(setup_ini_name); sig_url = "{0}{1}{2}{3}".format(mirror, sep, platform_dir, sig_name); try: cautils.uri_get(self.__tmpDir, sig_url, verbose=self.__verbose); except RequestException as e: msg = ( "Failed to download signature {0} Use -X to ignore " "signatures.".format(sig_url) ); raise RequestException(msg, previous=e); if self.__cygwinPlatform: gpg_path = "gpg"; else: if self._cygwinVersion() < 1.7: gpg_path = "/usr/bin/gpg"; else: gpg_path = "/usr/local/bin/gpg"; cmd = [gpg_path, "--verify", "--no-secmem-warning"]; cmd.append("{0}/{1}".format(self.__tmpDir, sig_name)); cmd.append("{0}/{1}".format(self.__tmpDir, setup_ini_name)); p = Process(cmd); p.run(); verify = p.getErrorOutput(); if isinstance(verify, bytes): marker = self.GPG_GOOD_FINGER.encode(); else: marker = self.GPG_GOOD_FINGER; if not marker in verify: msg = ( "{0} not signed by Cygwin's public key. " "Use -X to ignore signatures.".format(setup_ini_url) ); raise SignatureException(msg); downloads = os.path.join( self.__pm.mapPath(self.__rc.cache), urllib.quote(mirror+('' if mirror.endswith('/') else '/'), '').lower(), platform_dir, ); if not os.path.exists(downloads): os.makedirs(downloads); shutil.copy( os.path.join(self.__tmpDir, setup_ini_name), os.path.join(downloads, setup_ini_name) ); # BC layer for `setup_ini` configuration field if self.__rc.setup_ini : setup_ini = self.__pm.mapPath(self.__rc.setup_ini); if os.path.exists(setup_ini): shutil.copy(setup_ini, "{0}.bak".format(setup_ini)); shutil.copy( os.path.join(downloads, setup_ini_name), setup_ini ); if os.path.exists(os.path.join(self.__tmpDir, setup_ini_name)): os.remove(os.path.join(self.__tmpDir, setup_ini_name)); if sig_name: if os.path.exists(os.path.join(self.__tmpDir, sig_name)): os.remove(os.path.join(self.__tmpDir, sig_name));
def testMustRun(self): process = Process("python -c 'import sys; sys.stdout.write(\"foo\");'") process.mustRun() self.assertEqual(process.getOutput(), "foo") self.assertEqual(process.getExitCode(), 0)
def testMustRunThrowsException(self): process = Process( 'python -c \'import sys; sys.stdout.write("foo"); sys.stderr.write("bar"); exit(1);\'' ) process.mustRun()
def testGetErrorOutputProcessNotStarted(self): proc = Process('python --version') proc.getErrorOutput()
def testRunReturnAlwaysExitCodeEvenOnCommandFailed(self): proc = Process("nonexistingcommandIhopeneversomeonewouldnameacommandlikethis") actual = proc.run() self.assertTrue(0 < actual, "Failed asserting that {0} is greater than 0".format(actual))
def testValidInput(self, expected, value): process = Process('python --version') process.setInput(value) self.assertEqual(process.getInput(), expected)
def testExitCodeCommandFailed(self): process = Process("nonexistingcommandIhopeneversomeonewouldnameacommandlikethis") process.run() actual = process.getExitCode() self.assertTrue(0 < actual, "Failed asserting that {0} is greater than 0".format(actual))