Python os.path.open() Examples
The following are 30
code examples of os.path.open().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
os.path
, or try the search function
.
Example #1
Source File: test_package.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_package_sync_does_not_touch_existing_local_file(mirror: Mirror) -> None: pkg_file_path_str = "web/packages/any/f/foo/foo.zip" pkg_file_path = Path(pkg_file_path_str) touch_files((pkg_file_path,)) with pkg_file_path.open("w") as f: f.write("") old_stat = pkg_file_path.stat() mirror.packages_to_sync = {"foo": 1} package = Package("foo", 1, mirror) await package.sync() assert not mirror.errors # Use Pathlib + create a new object to ensure no caching # Only compare the relevant stat fields assert old_stat.st_mtime == Path(pkg_file_path_str).stat().st_mtime assert old_stat.st_ctime == Path(pkg_file_path_str).stat().st_ctime
Example #2
Source File: core.py From poetry-setup with Apache License 2.0 | 6 votes |
def sync(self): document = self.get_requirements(optional=False) if document: path = self.path / self.requirements_name with path.open('w', encoding='utf-8') as f: f.write(document) document = self.get_requirements(optional=True) if document: path = self.path / self.constraints_name with path.open('w', encoding='utf-8') as f: f.write(document) document = self.get_setup() path = self.path / self.setup_name with path.open('w', encoding='utf-8') as f: f.write(document)
Example #3
Source File: core.py From poetry-setup with Apache License 2.0 | 6 votes |
def get_setup(self): # render template with self.setup_path.open(encoding='utf-8') as f: document = f.read() template = Environment().from_string(document) document = template.render( package=self.package, format_vcs=self._format_vcs, ) # format by yapf style = CreateGoogleStyle() document, _changed = FormatCode(document, style_config=style) # remove empty strings while '\n\n' in document: document = document.replace('\n\n', '\n') # format by autopep8 document = fix_code(document) return document
Example #4
Source File: core.py From poetry-setup with Apache License 2.0 | 6 votes |
def get_requirements(self, optional=False): if not any(r.is_optional() == optional for r in self.package.all_requires): return with self.requirements_path.open(encoding='utf-8') as f: document = f.read() template = Environment().from_string(document) document = template.render( package=self.package, optional=optional, format_vcs=self._format_vcs, ) # rm junk document = document.replace(' ', '') # sort lines lines = sorted(line for line in document.split('\n') if line) document = '\n'.join(lines) + '\n' return document
Example #5
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_find_package_indexes_in_dir_threaded(mirror: Mirror) -> None: directories = ( "web/simple/peerme", "web/simple/click", "web/simple/zebra", "web/simple/implicit", "web/simple/pyaib", "web/simple/setuptools", ) with TemporaryDirectory() as td: # Create local mirror first so we '_bootstrap' mirror_base = Path(td) local_mirror = Mirror(mirror_base, mirror.master, stop_on_error=True) # Create fake file system objects for directory in directories: (mirror_base / directory).mkdir(parents=True, exist_ok=True) with (mirror_base / "web/simple/index.html").open("w") as index: index.write("<html></html>") packages = local_mirror.find_package_indexes_in_dir(mirror_base / "web/simple") assert "index.html" not in packages # This should never be in the list assert len(packages) == 6 # We expect 6 packages with 6 dirs created assert packages[0] == "click" # Check sorted - click should be first
Example #6
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def mirror_sync_package_error_early_exit(mirror: Mirror) -> None: mirror.master.all_packages = asynctest.CoroutineMock( # type: ignore return_value={"foo": 1} ) with Path("web/simple/index.html").open("wb") as index: index.write(b"old index") mirror.errors = True mirror.stop_on_error = True with pytest.raises(SystemExit): await mirror.synchronize() assert """\ .lock generation todo web{0}packages{0}any{0}f{0}foo{0}foo.zip web{0}simple{0}foo{0}index.html web{0}simple{0}index.html""".format( sep ) == utils.find( mirror.homedir, dirs=False ) assert open("web{0}simple{0}index.html".format(sep)).read() == "old index" assert open("todo").read() == "1\n"
Example #7
Source File: test_package.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_package_sync_simple_page_with_files(mirror: Mirror) -> None: mirror.packages_to_sync = {"foo": 1} package = Package("foo", 1, mirror) await package.sync() assert not mirror.errors assert ( open("web/simple/foo/index.html").read() == """\ <!DOCTYPE html> <html> <head> <title>Links for foo</title> </head> <body> <h1>Links for foo</h1> {} </body> </html> <!--SERIAL 654321-->\ """.format( EXPECTED_REL_HREFS ) )
Example #8
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_mirror_filter_packages_match(tmpdir: Path) -> None: """ Packages that exist in the blacklist should be removed from the list of packages to sync. """ test_configuration = """\ [blacklist] plugins = blacklist_project packages = example1 """ Singleton._instances = {} with open("test.conf", "w") as testconfig_handle: testconfig_handle.write(test_configuration) BandersnatchConfig("test.conf") for plugin in filter_project_plugins(): plugin.initialize_plugin() m = Mirror(tmpdir, mock.Mock()) m.packages_to_sync = {"example1": "", "example2": ""} m._filter_packages() assert "example1" not in m.packages_to_sync.keys()
Example #9
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_mirror_filter_packages_nomatch_package_with_spec(tmpdir: Path) -> None: """ Package lines with a PEP440 spec on them should not be filtered from the list of packages. """ test_configuration = """\ [blacklist] packages = example3>2.0.0 """ Singleton._instances = {} with open("test.conf", "w") as testconfig_handle: testconfig_handle.write(test_configuration) BandersnatchConfig("test.conf") for plugin in filter_project_plugins(): plugin.initialize_plugin() m = Mirror(tmpdir, mock.Mock()) m.packages_to_sync = {"example1": "", "example3": ""} m._filter_packages() assert "example3" in m.packages_to_sync.keys()
Example #10
Source File: test_package.py From bandersnatch with Academic Free License v3.0 | 6 votes |
def test_package_sync_with_canonical_simple_page(mirror: Mirror) -> None: mirror.packages_to_sync = {"Foo": 1} package = Package("Foo", 1, mirror) await package.sync() # Cross-check that simple directory hashing is disabled. assert not os.path.exists("web/simple/f/foo/index.html") assert ( open("web/simple/foo/index.html").read() == """\ <!DOCTYPE html> <html> <head> <title>Links for Foo</title> </head> <body> <h1>Links for Foo</h1> {} </body> </html> <!--SERIAL 654321-->\ """.format( EXPECTED_REL_HREFS ) )
Example #11
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_empty_master_gets_index(mirror: Mirror) -> None: mirror.master.all_packages = asynctest.asynctest.CoroutineMock( # type: ignore return_value={} ) await mirror.synchronize() assert """\ last-modified local-stats local-stats{0}days packages simple simple{0}index.html""".format( sep ) == utils.find( mirror.webdir ) assert ( open("web{0}simple{0}index.html".format(sep)).read() == """\ <!DOCTYPE html> <html> <head> <title>Simple Index</title> </head> <body> </body> </html>""" ) assert open("status").read() == "0"
Example #12
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def touch_files(paths: List[Path]) -> None: for path in paths: if not path.parent.exists(): path.parent.mkdir(parents=True) with path.open("wb") as pfp: pfp.close()
Example #13
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_loads_serial(tmpdir: Path) -> None: with open(str(tmpdir / "generation"), "w") as generation: generation.write("5") with open(str(tmpdir / "status"), "w") as status: status.write("1234") m = Mirror(tmpdir, mock.Mock()) assert m.synced_serial == 1234
Example #14
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_recovers_from_inconsistent_serial(tmpdir: Path) -> None: with open(str(tmpdir / "generation"), "w") as generation: generation.write("") with open(str(tmpdir / "status"), "w") as status: status.write("1234") m = Mirror(tmpdir, mock.Mock()) assert m.synced_serial == 0
Example #15
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_generation_4_resets_status_files(tmpdir: Path) -> None: with open(str(tmpdir / "generation"), "w") as generation: generation.write("4") with open(str(tmpdir / "status"), "w") as status: status.write("1234") with open(str(tmpdir / "todo"), "w") as status: status.write("asdf") m = Mirror(tmpdir, mock.Mock()) assert m.synced_serial == 0 assert not os.path.exists(str(tmpdir / "todo")) assert not os.path.exists(str(tmpdir / "status")) assert open(str(tmpdir / "generation")).read() == "5"
Example #16
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_removes_empty_todo_list(tmpdir: Path) -> None: with open(str(tmpdir / "generation"), "w") as generation: generation.write("3") with open(str(tmpdir / "status"), "w") as status: status.write("1234") with open(str(tmpdir / "todo"), "w") as status: status.write("") Mirror(tmpdir, mock.Mock()) assert not os.path.exists(str(tmpdir / "todo"))
Example #17
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_removes_broken_todo_list(tmpdir: Path) -> None: with open(str(tmpdir / "generation"), "w") as generation: generation.write("3") with open(str(tmpdir / "status"), "w") as status: status.write("1234") with open(str(tmpdir / "todo"), "w") as status: status.write("foo") Mirror(tmpdir, mock.Mock()) assert not os.path.exists(str(tmpdir / "todo"))
Example #18
Source File: test_package.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_survives_exceptions_from_record_finished_package(mirror: Mirror) -> None: def record_finished_package(name: str) -> NoReturn: import errno raise OSError(errno.EBADF, "Some transient error?") mirror.packages_to_sync = {"Foo": 1} mirror.record_finished_package = record_finished_package # type: ignore package = Package("Foo", 1, mirror) await package.sync() assert ( Path("web/simple/foo/index.html").open().read() == """\ <!DOCTYPE html> <html> <head> <title>Links for Foo</title> </head> <body> <h1>Links for Foo</h1> {} </body> </html> <!--SERIAL 654321-->\ """.format( EXPECTED_REL_HREFS ) ) assert mirror.errors
Example #19
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_mirror_sync_package(mirror: Mirror) -> None: mirror.master.all_packages = asynctest.CoroutineMock( # type: ignore return_value={"foo": 1} ) mirror.json_save = True # Recall bootstrap so we have the json dirs mirror._bootstrap() await mirror.synchronize() assert """\ json{0}foo last-modified packages{0}2.7{0}f{0}foo{0}foo.whl packages{0}any{0}f{0}foo{0}foo.zip pypi{0}foo{0}json simple{0}foo{0}index.html simple{0}index.html""".format( sep ) == utils.find( mirror.webdir, dirs=False ) assert ( open("web{0}simple{0}index.html".format(sep)).read() == """\ <!DOCTYPE html> <html> <head> <title>Simple Index</title> </head> <body> <a href="foo/">foo</a><br/> </body> </html>""" ) assert open("status", "rb").read() == b"1"
Example #20
Source File: test_mirror.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_validate_todo(mirror: Mirror) -> None: valid_todo = "69\ncooper 69\ndan 1\n" invalid_todo = "cooper l33t\ndan n00b\n" with TemporaryDirectory() as td: test_mirror = Mirror(Path(td), mirror.master) for todo_data in (valid_todo, invalid_todo): with test_mirror.todolist.open("w") as tdfp: tdfp.write(todo_data) test_mirror._validate_todo() if todo_data == valid_todo: assert test_mirror.todolist.exists() else: assert not test_mirror.todolist.exists()
Example #21
Source File: tools.py From speechless with MIT License | 5 votes |
def read_text(path: Path, encoding=None) -> str: """ Not Path.read_text for compatibility with Python 3.4. """ with path.open(encoding=encoding) as f: return f.read()
Example #22
Source File: tools.py From speechless with MIT License | 5 votes |
def write_text(path: Path, text: str, encoding=None): """ Not Path.write_text for compatibility with Python 3.4. """ with path.open(mode='w', encoding=encoding) as f: f.write(text)
Example #23
Source File: test_records.py From coffeegrindsize with MIT License | 5 votes |
def test_recarray_fromfile(self): data_dir = path.join(path.dirname(__file__), 'data') filename = path.join(data_dir, 'recarray_from_file.fits') fd = open(filename, 'rb') fd.seek(2880 * 2) r1 = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.seek(2880 * 2) r2 = np.rec.array(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.close() assert_equal(r1, r2)
Example #24
Source File: test_records.py From coffeegrindsize with MIT License | 5 votes |
def test_tofile_fromfile(self): with temppath(suffix='.bin') as path: path = Path(path) np.random.seed(123) a = np.random.rand(10).astype('f8,i4,a5') a[5] = (0.5,10,'abcde') with path.open("wb") as fd: a.tofile(fd) x = np.core.records.fromfile(path, formats='f8,i4,a5', shape=10) assert_array_equal(x, a)
Example #25
Source File: test_package.py From bandersnatch with Academic Free License v3.0 | 5 votes |
def test_package_sync_downloads_release_file(mirror: Mirror) -> None: mirror.packages_to_sync = {"foo": ""} package = Package("foo", 1, mirror) await package.sync() assert not mirror.errors assert open("web/packages/any/f/foo/foo.zip").read() == ""
Example #26
Source File: test_records.py From recruit with Apache License 2.0 | 5 votes |
def test_tofile_fromfile(self): with temppath(suffix='.bin') as path: path = Path(path) np.random.seed(123) a = np.random.rand(10).astype('f8,i4,a5') a[5] = (0.5,10,'abcde') with path.open("wb") as fd: a.tofile(fd) x = np.core.records.fromfile(path, formats='f8,i4,a5', shape=10) assert_array_equal(x, a)
Example #27
Source File: test_records.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_recarray_fromfile(self): data_dir = path.join(path.dirname(__file__), 'data') filename = path.join(data_dir, 'recarray_from_file.fits') fd = open(filename, 'rb') fd.seek(2880 * 2) r1 = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.seek(2880 * 2) r2 = np.rec.array(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.close() assert_equal(r1, r2)
Example #28
Source File: test_records.py From Mastering-Elasticsearch-7.0 with MIT License | 5 votes |
def test_tofile_fromfile(self): with temppath(suffix='.bin') as path: path = Path(path) np.random.seed(123) a = np.random.rand(10).astype('f8,i4,a5') a[5] = (0.5,10,'abcde') with path.open("wb") as fd: a.tofile(fd) x = np.core.records.fromfile(path, formats='f8,i4,a5', shape=10) assert_array_equal(x, a)
Example #29
Source File: test_records.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_recarray_fromfile(self): data_dir = path.join(path.dirname(__file__), 'data') filename = path.join(data_dir, 'recarray_from_file.fits') fd = open(filename, 'rb') fd.seek(2880 * 2) r1 = np.rec.fromfile(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.seek(2880 * 2) r2 = np.rec.array(fd, formats='f8,i4,a5', shape=3, byteorder='big') fd.close() assert_equal(r1, r2)
Example #30
Source File: test_records.py From predictive-maintenance-using-machine-learning with Apache License 2.0 | 5 votes |
def test_tofile_fromfile(self): with temppath(suffix='.bin') as path: path = Path(path) np.random.seed(123) a = np.random.rand(10).astype('f8,i4,a5') a[5] = (0.5,10,'abcde') with path.open("wb") as fd: a.tofile(fd) x = np.core.records.fromfile(path, formats='f8,i4,a5', shape=10) assert_array_equal(x, a)