1212import pathlib
1313import tarfile
1414import os
15+ import posixpath
1516import io
1617
1718import readline
@@ -50,28 +51,28 @@ def add(self, local_src, vfs_dest=None):
5051 raise Exception ('Must specify a destination file path to write to' )
5152 elif isinstance (local_src , str ) or isinstance (local_src , pathlib .PurePath ):
5253 if vfs_dest is None :
53- vfs_dest = os . path .basename (local_src )
54+ vfs_dest = posixpath .basename (local_src )
5455 local_src = str (local_src )
5556
5657 vfs_dest = self ._resolve_path (vfs_dest )
5758
5859 # build up path along the way if not present
59- dest_parts = list (pathlib .Path (vfs_dest ).parts )
60+ dest_parts = list (pathlib .PurePosixPath (vfs_dest ).parts )
6061 built_parts = '/'
6162 new_filename = dest_parts .pop ()
6263 for part in dest_parts :
63- built_parts = os . path .join (built_parts , part )
64+ built_parts = posixpath .join (built_parts , part )
6465 self .mkdir (built_parts )
6566
6667 parent_dirnode = self ._path_to_dir_node (built_parts )
6768
6869 if isinstance (local_src , bytes ) or isinstance (local_src , bytearray ):
6970 parent_dirnode [new_filename ] = local_src
70- elif os . path .isdir (local_src ):
71+ elif posixpath .isdir (local_src ):
7172 self .mkdir (vfs_dest )
7273 dir_list = os .listdir (local_src )
7374 for sub_path in dir_list :
74- self .add (os . path . join (local_src , sub_path ), os . path .join (vfs_dest , sub_path ))
75+ self .add (posixpath . join (local_src , sub_path ), posixpath .join (vfs_dest , sub_path ))
7576 else :
7677 with open (local_src , 'rb' ) as f :
7778 content = f .read ()
@@ -89,12 +90,12 @@ def _resolve_path(self, path):
8990 if path .startswith ('~' ):
9091 path = path .replace ('~' , self .home , 1 )
9192 if not path .startswith ('/' ):
92- path = os . path .join (self .cwd , path )
93- return os . path .normpath (path )
93+ path = posixpath .join (self .cwd , path )
94+ return posixpath .normpath (path )
9495
9596 def _path_to_dir_node (self , path ):
9697 dirnode = self .vfs
97- parts = pathlib .Path (path ).parts
98+ parts = pathlib .PurePosixPath (path ).parts
9899 for part in parts :
99100 if part not in dirnode :
100101 return None
@@ -109,16 +110,17 @@ def _flatten_vfs(self, dirnode=None, path_so_far='/'):
109110 if key == '/' :
110111 continue
111112 if isinstance (dirnode [key ], dict ):
112- files .append ((os . path .join (path_so_far , key ), None ))
113- files = files + self ._flatten_vfs (dirnode [key ], os . path .join (path_so_far , key ))
113+ files .append ((posixpath .join (path_so_far , key ), None ))
114+ files = files + self ._flatten_vfs (dirnode [key ], posixpath .join (path_so_far , key ))
114115 else :
115- files .append ((os . path .join (path_so_far , key ), dirnode [key ]))
116+ files .append ((posixpath .join (path_so_far , key ), dirnode [key ]))
116117 return files
117118
118119 def to_gzip_tar (self ) -> bytes :
119120 """Serializes the vfs into a zip-compressed tarball."""
120121 tar_stream = io .BytesIO ()
121122 with tarfile .open (fileobj = tar_stream , mode = 'w:gz' ) as tar :
123+
122124 for (file_path , file_content ) in self ._flatten_vfs ():
123125 tar_info = tarfile .TarInfo (name = file_path )
124126 if file_content is None :
@@ -141,7 +143,7 @@ def write_to_file(self, file_path):
141143 def mkdir (self , new_dir ):
142144 """For debugging."""
143145 new_dir = self ._resolve_path (new_dir )
144- parts = pathlib .Path (new_dir ).parts
146+ parts = pathlib .PurePosixPath (new_dir ).parts
145147 prev_dirnode = self .vfs
146148 for part in parts :
147149 if part not in prev_dirnode :
@@ -174,7 +176,7 @@ def ls(self, dir_to_list=None):
174176 dir_to_list = self .cwd
175177 else :
176178 dir_to_list = self ._resolve_path (dir_to_list )
177- parts = pathlib .Path (dir_to_list ).parts
179+ parts = pathlib .PurePosixPath (dir_to_list ).parts
178180 dir_node = self .vfs
179181 for part in parts :
180182 dir_node = dir_node [part ]
0 commit comments