def mk_profiles(self, profiles, base='profiles', arches=None): os.mkdir(pjoin(self.dir, 'metadata')) # write empty masters to suppress warnings write_file(pjoin(self.dir, 'metadata', 'layout.conf'), 'w', 'masters=') loc = pjoin(self.dir, base) os.mkdir(loc) if base != 'profiles': # write empty masters to suppress warnings os.mkdir(pjoin(loc, 'metadata')) write_file(pjoin(loc, 'metadata', 'layout.conf'), 'w', 'masters=') for profile in profiles: assert ensure_dirs(pjoin( loc, profile)), f"failed creating profile {profile!r}" if arches is None: arches = set(val[0] for val in profiles.values()) write_file(pjoin(loc, 'arch.list'), 'w', "\n".join(arches)) write_file(pjoin(loc, 'repo_name'), 'w', 'testing') write_file(pjoin(loc, 'eapi'), 'w', '5') with open(pjoin(loc, 'profiles.desc'), 'w') as fd: for profile, vals in profiles.items(): l = len(vals) if l == 1 or not vals[1]: fd.write(f"{vals[0]}\t{profile}\tstable\n") else: fd.write(f"{vals[0]}\t{profile}\t{vals[1]}\n") if l == 3 and vals[2]: with open(pjoin(loc, profile, 'deprecated'), 'w') as f: f.write("foon\n#dar\n") with open(pjoin(loc, profile, 'make.defaults'), 'w') as f: f.write(f"ARCH={vals[0]}\n") with open(pjoin(loc, profile, 'eapi'), 'w') as f: f.write('5')
def test_zero_length(self): path = pjoin(self.dir, 'target') write_file(path, 'w', '') m, f = self.func(path) assert m is None assert f.read() == b'' f.close()
def get_options(self, **kwds): repo_base = tempfile.mkdtemp(dir=self.dir) base = pjoin(repo_base, 'profiles') os.mkdir(base) fileutils.write_file( pjoin(base, "arch.list"), 'w', "\n".join(kwds.pop("arches", ("x86", "ppc", "amd64")))) fileutils.write_file( pjoin(base, "use.desc"), "w", "\n".join("%s - %s" % (x, x) for x in kwds.pop("use_desc", ("foo", "bar")))) fileutils.write_file( pjoin(base, "use.local.desc"), "w", "\n".join("dev-util/diffball:%s - blah" % x for x in kwds.pop("use_local_desc", ("lfoo", "lbar")))) fileutils.write_file(pjoin(base, 'repo_name'), 'w', 'monkeys') os.mkdir(pjoin(repo_base, 'metadata')) fileutils.write_file(pjoin(repo_base, 'metadata', 'layout.conf'), 'w', "masters = ") kwds['target_repo'] = misc.Options( config=repo_objs.RepoConfig(repo_base)) return misc.Options(**kwds)
def test_mmap(self, data=b'foonani'): path = pjoin(self.dir, 'target') write_file(path, 'wb', data) m, f = self.func(path) assert len(m) == len(data) assert m.read(len(data)) == data m.close() assert f is None
def test_normal_ops(self): fp = pjoin(self.dir, "target") write_file(fp, "w", "me") af = self.kls(fp) af.write("dar") assert fileutils.readfile_ascii(fp) == "me" af.close() assert fileutils.readfile_ascii(fp) == "dar"
def test_none_on_missing(self): fp = pjoin(self.dir, 'nonexistent') with pytest.raises(FileNotFoundError): self.func(fp) assert tuple(self.func(fp, False, True)) == () write_file(fp, 'wb', self.convert_data('dar', 'ascii')) assert tuple(self.func(fp, True)) == (self.none_on_missing_ret_data, ) assert tuple(self.func(pjoin(fp, 'missing'), False, True)) == ()
def test_del(self): fp = pjoin(self.dir, "target") write_file(fp, "w", "me") assert fileutils.readfile_ascii(fp) == "me" af = self.kls(fp) af.write("dar") del af assert fileutils.readfile_ascii(fp) == "me" assert len(os.listdir(self.dir)) == 1
def test_it(self): f = self.func path = pjoin(self.dir, 'target') f(path) write_file(path, 'w', '') f(path) assert not os.path.exists(path) # and once more for good measure... f(path)
def test_none_on_missing(self): fp = pjoin(self.dir, 'nonexistent') with pytest.raises(FileNotFoundError): self.func(fp) assert self.func(fp, True) == None write_file(fp, 'wb', self.convert_data('dar', 'ascii')) assert self.func(fp, True) == self.none_on_missing_ret_data # ensure it handles paths that go through files- # still should be suppress assert self.func(pjoin(fp, 'extra'), True) == None
def mk_profiles(self, profiles, base='profiles', arches=None): os.mkdir(pjoin(self.dir, 'metadata')) # write masters= to suppress logging complaints. write_file(pjoin(self.dir, 'metadata', 'layout.conf'), 'w', 'masters=') loc = pjoin(self.dir, base) os.mkdir(loc) for profile in profiles: self.assertTrue(ensure_dirs(pjoin(loc, profile)), msg="failed creating profile %r" % profile) if arches is None: arches = set(val[0] for val in profiles.itervalues()) write_file(pjoin(loc, 'arch.list'), 'w', "\n".join(arches)) write_file(pjoin(loc, 'repo_name'), 'w', 'testing') write_file(pjoin(loc, 'eapi'), 'w', '5') with open(pjoin(loc, 'profiles.desc'), 'w') as fd: for profile, vals in profiles.iteritems(): l = len(vals) if l == 1 or not vals[1]: fd.write("%s\t%s\tstable\n" % (vals[0], profile)) else: fd.write("%s\t%s\t%s\n" % (vals[0], profile, vals[1])) if l == 3 and vals[2]: with open(pjoin(loc, profile, 'deprecated'), 'w') as f: f.write("foon\n#dar\n") with open(pjoin(loc, profile, 'make.defaults'), 'w') as f: f.write("ARCH=%s\n" % vals[0]) with open(pjoin(loc, profile, 'eapi'), 'w') as f: f.write('5')
def test_defaults(self): self.mk_profiles({"profile1":["x86"], "profile1/2":["x86"]}, base='profiles') os.mkdir(pjoin(self.dir, 'metadata')) # write masters= to suppress logging complaints. write_file(pjoin(self.dir, 'metadata', 'layout.conf'), 'w', 'masters=') options = self.process_check(None, [], profiles_enabled=[], profiles_disabled=[], profile_ignore_deprecated=False, profiles_desc_enabled=True, profile_ignore_dev=False) # override the default check = self.addon_kls(options) self.assertEqual(sorted(check.official_arches), ['x86']) self.assertEqual(sorted(check.desired_arches), ['x86']) self.assertEqual(sorted(check.profile_evaluate_dict), ['x86', '~x86']) self.assertProfiles(check, 'x86', 'profile1', 'profile1/2')
def send_env(self, env_dict, async_req=False, tmpdir=None): """Transfer the ebuild's desired env (env_dict) to the running daemon. :type env_dict: mapping with string keys and values. :param env_dict: the bash env. """ data = self._generate_env_str(env_dict) old_umask = os.umask(0o002) if tmpdir: path = pjoin(tmpdir, 'ebd-env-transfer') fileutils.write_file(path, 'wb', data.encode()) self.write(f"start_receiving_env file {path}") else: self.write(f"start_receiving_env bytes {len(data)}\n{data}", append_newline=False) os.umask(old_umask) return self.expect("env_received", async_req=async_req, flush=True)
def test_discard(self): fp = pjoin(self.dir, "target") write_file(fp, "w", "me") assert fileutils.readfile_ascii(fp) == "me" af = self.kls(fp) af.write("dar") af.discard() assert not os.path.exists(af._temp_fp) af.close() assert fileutils.readfile_ascii(fp) == "me" # finally validate that it handles multiple discards properly. af = self.kls(fp) af.write("dar") af.discard() af.discard() af.close()
def test_it(self): fp = pjoin(self.dir, 'testfile') for expected in self.test_cases: raised = None encoding = self.default_encoding if isinstance(expected, tuple): if len(expected) == 3: raised = expected[2] if expected[1] is not None: encoding = expected[1] expected = expected[0] write_file(fp, 'wb', self.convert_data(expected, encoding)) if raised: with pytest.raises(raised): self.assertFunc(fp, expected) else: self.assertFunc(fp, expected)
def mk_pkg(self, files={}, pkg=None): if pkg is None: # generate random cat/PN category = uuid.uuid4().hex PN = uuid.uuid4().hex else: category, PN = pkg.split(os.path.sep) pkg = f"{category}/{PN}-0.7.1" repo = FakeRepo(repo_id='repo', location=self.dir) self.filesdir = pjoin(repo.location, category, PN, 'files') os.makedirs(self.filesdir) # create specified files in FILESDIR for fn, contents in files.items(): fileutils.write_file(pjoin(self.filesdir, fn), 'w', contents) return misc.FakeFilesDirPkg(pkg, repo=repo)
def mk_pkg(self, files={}, category=None, package=None, version='0.7.1', revision=''): # generate random cat/PN category = uuid.uuid4().hex if category is None else category package = uuid.uuid4().hex if package is None else package pkg = f"{category}/{package}-{version}{revision}" self.filesdir = pjoin(self.repo.location, category, package, 'files') # create files dir with random empty subdir os.makedirs(pjoin(self.filesdir, uuid.uuid4().hex), exist_ok=True) # create dirs that should be ignored for d in getattr(self.check_kls, 'ignore_dirs', ()): os.makedirs(pjoin(self.filesdir, d), exist_ok=True) # create specified files in FILESDIR for fn, contents in files.items(): fileutils.write_file(pjoin(self.filesdir, fn), 'w', contents) return misc.FakeFilesDirPkg(pkg, repo=self.repo)
def test_it(self): path = pjoin(self.dir, 'target') data = b'asdfasdf' write_file(path, 'wb', [data]) fd, m = None, None try: fd = os.open(path, os.O_RDONLY) m = _fileutils.mmap_and_close(fd, len(data), mmap.MAP_PRIVATE, mmap.PROT_READ) # and ensure it closed the fd... with pytest.raises(EnvironmentError): os.read(fd, 1) fd = None assert len(m) == len(data) assert m.read(len(data)) == data finally: if m is not None: m.close() if fd is not None: os.close(fd)
def test_empty_assign(self): write_file(self.valid_file.name, 'w', "foo=\ndar=blah\n") assert self.invoke_and_close(self.valid_file.name) == {'foo': '', 'dar': 'blah'} write_file(self.valid_file.name, 'w', "foo=\ndar=\n") assert self.invoke_and_close(self.valid_file.name) == {'foo': '', 'dar': ''} write_file(self.valid_file.name, 'w', "foo=blah\ndar=\n") assert self.invoke_and_close(self.valid_file.name) == {'foo': 'blah', 'dar': ''}
def mk_profiles(self, profiles, base='default', arches=None): loc = pjoin(self.dir, base) os.mkdir(loc) for profile in profiles: self.assertTrue(ensure_dirs(pjoin(loc, profile)), msg="failed creating profile %r" % profile) if arches is None: arches = set(val[0] for val in profiles.itervalues()) write_file(pjoin(loc, 'arch.list'), 'w', "\n".join(arches)) write_file(pjoin(loc, 'repo_name'), 'w', 'testing') fd = open(pjoin(loc, 'profiles.desc'), 'w') for profile, vals in profiles.iteritems(): l = len(vals) if l == 1 or not vals[1]: fd.write("%s\t%s\tstable\n" % (vals[0], profile)) else: fd.write("%s\t%s\tdev\n" % (vals[0], profile)) if l == 3 and vals[2]: open(pjoin(loc, profile, 'deprecated'), 'w').write("foon\n#dar\n") open(pjoin(loc, profile, 'make.defaults'), 'w').write( "ARCH=%s\n" % vals[0]) fd.close()
def test_strip_whitespace(self): fp = pjoin(self.dir, 'data') write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3\n', 'ascii')) results = tuple(self.func(fp, True)) expected = ('dar1', 'dar2', 'dar3') if self.encoding_mode == 'bytes': expected = tuple(x.encode("ascii") for x in expected) assert results == expected # this time without the trailing newline... write_file(fp, 'wb', self.convert_data(' dar1 \ndar2 \n dar3', 'ascii')) results = tuple(self.func(fp, True)) assert results == expected # test a couple of edgecases; underly c extension has gotten these # wrong before. write_file(fp, 'wb', self.convert_data('0', 'ascii')) results = tuple(self.func(fp, True)) expected = ('0', ) if self.encoding_mode == 'bytes': expected = tuple(x.encode("ascii") for x in expected) assert results == expected write_file(fp, 'wb', self.convert_data('0\n', 'ascii')) results = tuple(self.func(fp, True)) expected = ('0', ) if self.encoding_mode == 'bytes': expected = tuple(x.encode("ascii") for x in expected) assert results == expected write_file(fp, 'wb', self.convert_data('0 ', 'ascii')) results = tuple(self.func(fp, True)) expected = ('0', ) if self.encoding_mode == 'bytes': expected = tuple(x.encode("ascii") for x in expected) assert results == expected
def get_options(self, **kwds): repo_base = tempfile.mkdtemp(dir=self.dir) base = pjoin(repo_base, 'profiles') os.mkdir(base) fileutils.write_file( pjoin(base, "arch.list"), 'w', "\n".join(kwds.pop("arches", ("x86", "ppc", "amd64")))) fileutils.write_file( pjoin(base, "use.desc"), "w", "\n".join("%s - %s" % (x, x) for x in kwds.pop("use_desc", ("foo", "bar")))) fileutils.write_file(pjoin(base, 'repo_name'), 'w', 'monkeys') os.mkdir(pjoin(repo_base, 'metadata')) fileutils.write_file(pjoin(repo_base, 'metadata', 'layout.conf'), 'w', "masters = ") kwds['target_repo'] = repository._UnconfiguredTree(repo_base) return misc.Options(**kwds)
def get_options(self, **kwds): repo_base = tempfile.mkdtemp(dir=self.dir) base = pjoin(repo_base, 'profiles') os.mkdir(base) fileutils.write_file( pjoin(base, "arch.list"), 'w', "\n".join(kwds.pop("arches", ("x86", "ppc", "amd64")))) fileutils.write_file( pjoin(base, "use.desc"), "w", "\n".join("%s - %s" % (x, x) for x in kwds.pop("use_desc", ("foo", "bar")))) fileutils.write_file(pjoin(base, 'repo_name'), 'w', 'monkeys') os.mkdir(pjoin(repo_base, 'metadata')) fileutils.write_file( pjoin(repo_base, 'metadata', 'layout.conf'), 'w', "masters = ") kwds['target_repo'] = repository._UnconfiguredTree(repo_base) return misc.Options(**kwds)
def write_file(self, path, mode, stream, encoding=None): return fileutils.write_file(path, mode, stream, encoding=encoding)