Skip to content

Commit

Permalink
Avoid using os.path.normpath on Windows with files destined for Linux. (
Browse files Browse the repository at this point in the history
  • Loading branch information
faube authored Sep 21, 2020
1 parent bd64baa commit d812ccf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
27 changes: 22 additions & 5 deletions container/build_tar.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import json
import os
import os.path
import posixpath
import subprocess
import sys
import re
Expand Down Expand Up @@ -48,14 +49,16 @@ def parse_pkg_name(metadata, filename):
return os.path.basename(os.path.splitext(filename)[0])

def __init__(self, output, directory, compression, root_directory,
default_mtime, enable_mtime_preservation, xz_path):
default_mtime, enable_mtime_preservation, xz_path,
force_posixpath):
self.directory = directory
self.output = output
self.compression = compression
self.root_directory = root_directory
self.default_mtime = default_mtime
self.enable_mtime_preservation = enable_mtime_preservation
self.xz_path = xz_path
self.force_posixpath = force_posixpath

def __enter__(self):
self.tarfile = archive.TarFileWriter(
Expand Down Expand Up @@ -92,7 +95,10 @@ def add_file(self, f, destfile, mode=None, ids=None, names=None):
ids = (0, 0)
if names is None:
names = ('', '')
dest = os.path.normpath(dest)
if self.force_posixpath:
dest = posixpath.normpath(dest)
else:
dest = os.path.normpath(dest)
self.tarfile.add_file(
dest,
file_content=f,
Expand Down Expand Up @@ -123,7 +129,10 @@ def add_empty_file(self, destfile, mode=None, ids=None, names=None,
ids = (0, 0)
if names is None:
names = ('', '')
dest = os.path.normpath(dest)
if self.force_posixpath:
dest = posixpath.normpath(dest)
else:
dest = os.path.normpath(dest)
self.tarfile.add_file(
dest,
content='' if kind == tarfile.REGTYPE else None,
Expand Down Expand Up @@ -187,7 +196,10 @@ def add_link(self, symlink, destination):
symlink: the name of the symbolic link to add.
destination: where the symbolic link point to.
"""
symlink = os.path.normpath(symlink)
if self.force_posixpath:
symlink = posixpath.normpath(symlink)
else:
symlink = os.path.normpath(symlink)
self.tarfile.add_file(symlink, tarfile.SYMTYPE, link=destination)

@contextmanager
Expand Down Expand Up @@ -337,7 +349,8 @@ def main(FLAGS):
# Add objects to the tar file
with TarFile(FLAGS.output, FLAGS.directory, FLAGS.compression,
FLAGS.root_directory, FLAGS.mtime,
FLAGS.enable_mtime_preservation, FLAGS.xz_path) as output:
FLAGS.enable_mtime_preservation, FLAGS.xz_path,
FLAGS.force_posixpath) as output:
def file_attributes(filename):
if filename.startswith('/'):
filename = filename[1:]
Expand Down Expand Up @@ -460,5 +473,9 @@ def validate_link(l):
parser.add_argument('--xz_path', type=str,
help='Specify the path to xz as a fallback when the Python '
'lzma module is unavailable.')

parser.add_argument('--force_posixpath', type=bool, default=False,
help='Force the use of posixpath when normalizing file paths. This is useful'
'when building in a non-posix environment.')

main(parser.parse_args())
1 change: 1 addition & 0 deletions container/layer.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def build_layer(
elif build_layer_exec.path.endswith(".exe"):
# Building on Windows, but not for Windows. Do not use the default root directory.
args.add("--root_directory=")
args.add("--force_posixpath=true")

all_files = [struct(src = f.path, dst = _magic_path(ctx, f, layer)) for f in files]
all_files += [struct(src = f.path, dst = path) for (path, f) in file_map.items()]
Expand Down

0 comments on commit d812ccf

Please sign in to comment.