Skip to content

Commit f0a6571

Browse files
authored
feat(librarian): python can infer output directory from library name (#3729)
It's more fiddly to infer the output directory from an API path than from a known library name. There's already code to infer an API path from a library name (not for Python, admittedly), so the expectation is that the library name is always present.
1 parent b6aa3fb commit f0a6571

5 files changed

Lines changed: 21 additions & 4 deletions

File tree

internal/librarian/generate.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,12 @@ func postGenerate(ctx context.Context, language string) error {
156156
}
157157
}
158158

159-
func defaultOutput(language, api, defaultOut string) string {
159+
func defaultOutput(language, name, api, defaultOut string) string {
160160
switch language {
161161
case languageRust:
162162
return rust.DefaultOutput(api, defaultOut)
163+
case languagePython:
164+
return python.DefaultOutputByName(name, defaultOut)
163165
default:
164166
return defaultOut
165167
}

internal/librarian/library.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func libraryOutput(language string, lib *config.Library, defaults *config.Defaul
107107
if defaults != nil {
108108
defaultOut = defaults.Output
109109
}
110-
return defaultOutput(language, apiPath, defaultOut)
110+
return defaultOutput(language, lib.Name, apiPath, defaultOut)
111111
}
112112

113113
// applyDefaults applies language-specific derivations and fills defaults.
@@ -126,7 +126,7 @@ func applyDefaults(language string, lib *config.Library, defaults *config.Defaul
126126
if lib.Veneer {
127127
return nil, fmt.Errorf("veneer %q requires an explicit output path", lib.Name)
128128
}
129-
lib.Output = defaultOutput(language, lib.APIs[0].Path, defaults.Output)
129+
lib.Output = defaultOutput(language, lib.Name, lib.APIs[0].Path, defaults.Output)
130130
}
131131
return fillDefaults(lib, defaults), nil
132132
}

internal/librarian/python/generate.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,10 @@ func cleanUpFilesAfterPostProcessing(repoRoot string) error {
290290

291291
return nil
292292
}
293+
294+
// DefaultOutputByName derives an output path from a library name and a default
295+
// output directory. Currently this just assumes each library is a directory
296+
// directly underneath the default output directory.
297+
func DefaultOutputByName(name, defaultOutput string) string {
298+
return filepath.Join(defaultOutput, name)
299+
}

internal/librarian/python/generate_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,14 @@ func TestGenerate(t *testing.T) {
374374
}
375375
}
376376

377+
func TestDefaultOutputByName(t *testing.T) {
378+
want := "packages/google-cloud-secret-manager"
379+
got := DefaultOutputByName("google-cloud-secret-manager", "packages")
380+
if diff := cmp.Diff(want, got); diff != "" {
381+
t.Errorf("mismatch (-want +got):\n%s", diff)
382+
}
383+
}
384+
377385
func requirePythonModule(t *testing.T, module string) {
378386
t.Helper()
379387
cmd := exec.Command("python3", "-c", fmt.Sprintf("import %s", module))

internal/librarian/tidy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func tidyLibrary(cfg *config.Config, lib *config.Library) error {
9191
}
9292

9393
func isDerivableOutput(cfg *config.Config, lib *config.Library) bool {
94-
derivedOutput := defaultOutput(cfg.Language, lib.APIs[0].Path, cfg.Default.Output)
94+
derivedOutput := defaultOutput(cfg.Language, lib.Name, lib.APIs[0].Path, cfg.Default.Output)
9595
return lib.Output == derivedOutput
9696
}
9797

0 commit comments

Comments
 (0)