[
https://issues.apache.org/jira/browse/BEAM-4747?focusedWorklogId=128355&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-128355
]
ASF GitHub Bot logged work on BEAM-4747:
----------------------------------------
Author: ASF GitHub Bot
Created on: 27/Jul/18 23:46
Start Date: 27/Jul/18 23:46
Worklog Time Spent: 10m
Work Description: aaltay closed pull request #5903: [BEAM-4747] mkdirs if
they don't exist in localfilesystem
URL: https://github.com/apache/beam/pull/5903
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/sdks/python/apache_beam/io/localfilesystem.py
b/sdks/python/apache_beam/io/localfilesystem.py
index 23a1e8a846c..e373ad0b18a 100644
--- a/sdks/python/apache_beam/io/localfilesystem.py
+++ b/sdks/python/apache_beam/io/localfilesystem.py
@@ -144,6 +144,9 @@ def create(self, path, mime_type='application/octet-stream',
Returns: file handle with a close function for the user to use
"""
+ parent = os.path.dirname(path)
+ if not os.path.exists(parent):
+ os.makedirs(parent)
return self._path_open(path, 'wb', mime_type, compression_type)
def open(self, path, mime_type='application/octet-stream',
@@ -185,6 +188,10 @@ def _copy_path(source, destination):
if os.path.isdir(source):
shutil.copytree(source, destination)
else:
+ parent = os.path.dirname(destination)
+ if not os.path.exists(parent):
+ os.makedirs(parent)
+
shutil.copy2(source, destination)
except OSError as err:
raise IOError(err)
@@ -217,6 +224,10 @@ def rename(self, source_file_names,
destination_file_names):
def _rename_file(source, destination):
"""Rename a single file object"""
try:
+ parent = os.path.dirname(destination)
+ if not os.path.exists(parent):
+ os.makedirs(parent)
+
os.rename(source, destination)
except OSError as err:
raise IOError(err)
diff --git a/sdks/python/apache_beam/io/localfilesystem_test.py
b/sdks/python/apache_beam/io/localfilesystem_test.py
index d6d8eb4c217..5d032db59a1 100644
--- a/sdks/python/apache_beam/io/localfilesystem_test.py
+++ b/sdks/python/apache_beam/io/localfilesystem_test.py
@@ -196,8 +196,8 @@ def test_copy_error(self):
[(path1, path2)])
def test_copy_directory(self):
- path_t1 = os.path.join(self.tmpdir, 't1')
- path_t2 = os.path.join(self.tmpdir, 't2')
+ path_t1 = os.path.join(self.tmpdir, 't1/11')
+ path_t2 = os.path.join(self.tmpdir, 't2/22')
self.fs.mkdirs(path_t1)
self.fs.mkdirs(path_t2)
@@ -209,6 +209,19 @@ def test_copy_directory(self):
self.fs.copy([path_t1], [path_t2])
self.assertTrue(filecmp.cmp(path1, path2))
+ def test_create_mkdirs_open(self):
+ path = os.path.join(self.tmpdir, 't1/t2/t3')
+ with self.fs.create(path) as f:
+ f.write("yay")
+
+ with self.fs.open(path) as f:
+ self.assertEqual(f.read(), "yay")
+
+ def test_open_error(self):
+ path = os.path.join(self.tmpdir, 't1')
+ with self.assertRaisesRegexp(IOError, r'No such file or directory'):
+ self.fs.open(path)
+
def test_rename(self):
path1 = os.path.join(self.tmpdir, 'f1')
path2 = os.path.join(self.tmpdir, 'f2')
@@ -244,6 +257,22 @@ def test_rename_directory(self):
self.assertTrue(self.fs.exists(path2))
self.assertFalse(self.fs.exists(path1))
+ def test_rename_mkdirs(self):
+ path_t1 = os.path.join(self.tmpdir, 't1')
+ path_t2 = os.path.join(self.tmpdir, 't2/t3/t4')
+ self.fs.mkdirs(path_t1)
+
+ path1 = os.path.join(path_t1, 'f1')
+ path2 = os.path.join(path_t2, 'f1')
+ with open(path1, 'a') as f:
+ f.write('Hello')
+
+ self.fs.rename([path_t1], [path_t2])
+ self.assertTrue(self.fs.exists(path_t2))
+ self.assertFalse(self.fs.exists(path_t1))
+ self.assertTrue(self.fs.exists(path2))
+ self.assertFalse(self.fs.exists(path1))
+
def test_exists(self):
path1 = os.path.join(self.tmpdir, 'f1')
path2 = os.path.join(self.tmpdir, 'f2')
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 128355)
Time Spent: 50m (was: 40m)
> Python LocalFileSystem directory-creation semantics
> ---------------------------------------------------
>
> Key: BEAM-4747
> URL: https://issues.apache.org/jira/browse/BEAM-4747
> Project: Beam
> Issue Type: Improvement
> Components: sdk-py-core
> Affects Versions: 2.5.0
> Reporter: Ryan Williams
> Assignee: Ryan Williams
> Priority: Minor
> Time Spent: 50m
> Remaining Estimate: 0h
>
> Coming out of discussion on
> [BEAM-4742|https://issues.apache.org/jira/browse/BEAM-4742] /
> [#5903|https://github.com/apache/beam/pull/5903] is a question of whether
> {{LocalFileSystem.{open,create,copy,rename}}} should create
> intermediate (destination) directories, or fail with {{IOError}}'s (as the
> stdlib {{os}} module generally will).
> If the semantics of {{LocalFileSystem}} should mimic those of distributed
> filesystems (in the spirit of [recent discussion about {{DirectRunner}} being
> more like a local simulation of a distributed runner than a production-grade
> local
> runner|https://www.mail-archive.com/[email protected]/msg08410.html]), then
> this makes sense, and it sounds like [~lcwik] and [~angoenka] are in favor of
> this interpretation.
> I'll repurpose [#5903|https://github.com/apache/beam/pull/5903] to this end
> unless I hear otherwise.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)