Skip to content

Commit

Permalink
Stream preamble directly from --preamble_file to output file
Browse files Browse the repository at this point in the history
This should make `--preamble_file` more efficient when the source file
is large.
  • Loading branch information
chrisnovakovic committed Oct 15, 2024
1 parent 7c80eda commit d8ff401
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
7 changes: 4 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,13 @@ func main() {
opts.Zip.Preamble = mustReadPreamble(opts.Zip.PreambleFrom)
}
if opts.Zip.Preamble != "" {
must(f.WritePreamble([]byte(opts.Zip.Preamble + "\n")))
must(f.WritePreambleBytes([]byte(opts.Zip.Preamble + "\n")))
}
if opts.Zip.PreambleFile != "" {
b, err := ioutil.ReadFile(opts.Zip.PreambleFile)
pf, err := os.Open(opts.Zip.PreambleFile)
must(err)
must(f.WritePreamble(b))
defer pf.Close()
must(f.WritePreambleFile(pf))
}
if opts.Zip.MainClass != "" {
must(f.AddManifest(opts.Zip.MainClass))
Expand Down
18 changes: 13 additions & 5 deletions zip/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const modTimeDOS = 10785
type File struct {
f io.WriteCloser
w *zip.Writer
preambleLength int
preambleLength int64
filename string
input string
// Include and Exclude are prefixes of filenames to include or exclude from the zipfile.
Expand Down Expand Up @@ -485,14 +485,22 @@ func (f *File) WriteDir(filename string) error {
return nil
}

// WritePreamble writes a preamble to the zipfile.
func (f *File) WritePreamble(preamble []byte) error {
f.preambleLength += len(preamble)
f.w.SetOffset(int64(f.preambleLength))
// WritePreambleBytes writes a byte slice as a preamble to the zipfile.
func (f *File) WritePreambleBytes(preamble []byte) error {
f.preambleLength += int64(len(preamble))
f.w.SetOffset(f.preambleLength)
_, err := f.f.Write(preamble)
return err
}

// WritePreambleFile writes a file as a preamble to the zipfile.
func (f *File) WritePreambleFile(preamble fs.File) error {
length, err := io.Copy(f.f, preamble)
f.preambleLength += length
f.w.SetOffset(f.preambleLength)
return err
}

// StripBytecodeTimestamp strips a timestamp from a .pyc or .pyo file.
// This is important so our output is deterministic.
func (f *File) StripBytecodeTimestamp(filename string, contents []byte) error {
Expand Down

0 comments on commit d8ff401

Please sign in to comment.