2525from pkg .private import manifest
2626
2727
28+ def normpath (path ):
29+ """Normalize a path to the format we need it.
30+
31+ os.path.normpath changes / to \ on windows, but tarfile needs / style paths.
32+
33+ Args:
34+ path: (str) path to normalize.
35+ """
36+ return os .path .normpath (path ).replace (os .path .sep , '/' )
37+
38+
2839class TarFile (object ):
2940 """A class to generates a TAR file."""
3041
@@ -53,8 +64,7 @@ def __exit__(self, t, v, traceback):
5364 self .tarfile .close ()
5465
5566 def normalize_path (self , path : str ) -> str :
56- # Note: normpath changes / to \ on windows, but tarfile needs / style paths.
57- dest = os .path .normpath (path ).replace (os .path .sep , '/' )
67+ dest = normpath (path )
5868 # paths should not have a leading ./
5969 if dest .startswith ('./' ):
6070 dest = dest [2 :]
@@ -118,7 +128,7 @@ def add_empty_file(self,
118128 ids = (0 , 0 )
119129 if names is None :
120130 names = ('' , '' )
121- dest = os . path . normpath (dest ). replace ( os . path . sep , '/' )
131+ dest = normpath (dest )
122132 self .tarfile .add_file (
123133 dest ,
124134 content = '' if kind == tarfile .REGTYPE else None ,
@@ -169,7 +179,7 @@ def add_link(self, symlink, destination, mode=None, ids=None, names=None):
169179 names: (username, groupname) for the file to set ownership. An empty
170180 file will be created as `destfile` in the layer.
171181 """
172- symlink = os . path . normpath (symlink ). replace ( os . path . sep , '/' )
182+ symlink = normpath (symlink )
173183 self .tarfile .add_file (
174184 symlink ,
175185 tarfile .SYMTYPE ,
@@ -218,14 +228,14 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None):
218228 copied to `self.directory/destfile` in the layer.
219229 """
220230 # We expect /-style paths.
221- tree_top = os . path . normpath (tree_top ). replace ( os . path . sep , '/' )
231+ tree_top = normpath (tree_top )
222232
223233 dest = destpath .strip ('/' ) # redundant, dests should never have / here
224234 if self .directory and self .directory != '/' :
225235 dest = self .directory .lstrip ('/' ) + '/' + dest
226236
227237 # Again, we expect /-style paths.
228- dest = os . path . normpath (dest ). replace ( os . path . sep , '/' )
238+ dest = normpath (dest )
229239 if ids is None :
230240 ids = (0 , 0 )
231241 if names is None :
@@ -235,7 +245,7 @@ def add_tree(self, tree_top, destpath, mode=None, ids=None, names=None):
235245 for root , dirs , files in os .walk (tree_top ):
236246 # While `tree_top` uses '/' as a path separator, results returned by
237247 # `os.walk` and `os.path.join` on Windows may not.
238- root = os . path . normpath (root ). replace ( os . path . sep , '/' )
248+ root = normpath (root )
239249
240250 dirs = sorted (dirs )
241251 rel_path_from_top = root [len (tree_top ):].lstrip ('/' )
0 commit comments