@@ -8,27 +8,29 @@ import (
88 "net/http"
99 "os"
1010 "path/filepath"
11+ "strings"
1112
1213 "github.com/replicate/cog/pkg/api"
1314 "github.com/replicate/cog/pkg/config"
1415 "github.com/replicate/cog/pkg/dockerignore"
16+ "github.com/replicate/cog/pkg/global"
1517 "github.com/replicate/cog/pkg/procedure"
1618)
1719
1820func PipelinePush (ctx context.Context , image string , projectDir string , apiClient * api.Client , client * http.Client , cfg * config.Config ) error {
19- err := procedure .Validate (projectDir , client , cfg , false )
21+ err := procedure .Validate (projectDir , client , cfg , true )
2022 if err != nil {
2123 return err
2224 }
2325
24- tarball , err := createTarball (projectDir )
26+ tarball , err := createTarball (projectDir , cfg )
2527 if err != nil {
2628 return err
2729 }
2830 return apiClient .PostNewPipeline (ctx , image , tarball )
2931}
3032
31- func createTarball (folder string ) (* bytes.Buffer , error ) {
33+ func createTarball (folder string , cfg * config. Config ) (* bytes.Buffer , error ) {
3234 var buf bytes.Buffer
3335 tw := tar .NewWriter (& buf )
3436
@@ -37,6 +39,25 @@ func createTarball(folder string) (*bytes.Buffer, error) {
3739 return nil , err
3840 }
3941
42+ // Track if we need to add downloaded requirements to the tarball
43+ var downloadedRequirementsPath string
44+ var downloadedRequirementsContent []byte
45+
46+ // If config points to downloaded requirements (outside project directory),
47+ // we need to include them in the tarball as requirements.txt
48+ if cfg .Build .PythonRequirements != "" {
49+ reqPath := cfg .RequirementsFile (folder )
50+ if ! strings .HasPrefix (reqPath , folder ) || strings .Contains (reqPath , global .CogBuildArtifactsFolder ) {
51+ // This is a downloaded requirements file, read its content
52+ content , err := os .ReadFile (reqPath )
53+ if err != nil {
54+ return nil , err
55+ }
56+ downloadedRequirementsPath = "requirements.txt"
57+ downloadedRequirementsContent = content
58+ }
59+ }
60+
4061 err = dockerignore .Walk (folder , matcher , func (path string , info os.FileInfo , err error ) error {
4162 if err != nil {
4263 return err
@@ -51,6 +72,12 @@ func createTarball(folder string) (*bytes.Buffer, error) {
5172 return err
5273 }
5374
75+ // If this is the local requirements.txt and we have downloaded requirements,
76+ // skip the local one (we'll add the downloaded version instead)
77+ if downloadedRequirementsPath != "" && relPath == "requirements.txt" {
78+ return nil
79+ }
80+
5481 file , err := os .Open (path )
5582 if err != nil {
5683 return err
@@ -78,6 +105,25 @@ func createTarball(folder string) (*bytes.Buffer, error) {
78105 return nil , err
79106 }
80107
108+ // Add downloaded requirements as requirements.txt if we have them
109+ if downloadedRequirementsPath != "" {
110+ header := & tar.Header {
111+ Name : downloadedRequirementsPath ,
112+ Mode : 0o644 ,
113+ Size : int64 (len (downloadedRequirementsContent )),
114+ }
115+
116+ err = tw .WriteHeader (header )
117+ if err != nil {
118+ return nil , err
119+ }
120+
121+ _ , err = tw .Write (downloadedRequirementsContent )
122+ if err != nil {
123+ return nil , err
124+ }
125+ }
126+
81127 if err := tw .Close (); err != nil {
82128 return nil , err
83129 }
0 commit comments