This repository was archived by the owner on Feb 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_layer_wheel.go
More file actions
112 lines (105 loc) · 3.56 KB
/
cmd_layer_wheel.go
File metadata and controls
112 lines (105 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"sigs.k8s.io/yaml"
"github.com/datawire/ocibuild/pkg/cliutil"
"github.com/datawire/ocibuild/pkg/fsutil"
"github.com/datawire/ocibuild/pkg/python"
"github.com/datawire/ocibuild/pkg/python/pypa/bdist"
"github.com/datawire/ocibuild/pkg/python/pypa/entry_points"
"github.com/datawire/ocibuild/pkg/python/pypa/recording_installs"
)
func init() {
var platFile string
cmd := &cobra.Command{
Use: "wheel [flags] IN_WHEELFILE.whl >OUT_LAYERFILE",
Short: "Turn a Python wheel in to a layer",
Long: "Given a Python wheel file, transform it in to a layer." +
"\n\n" +
"In order to transform the wheel in to a layer, ocibuild needs to know a " +
"few things about the target environment. You must supply this to " +
"ocibuild using the --platform-file flag, pointing it at a YAML file that " +
"is as follows:" +
"\n\n" +
" # This entire file can be generated with the `ocibuild python inspect`\n" +
" # command.\n" +
"\n" +
" # file locations\n" +
" ConsoleShebang: /usr/bin/python3.9\n" +
" GraphicalShebang: /usr/bin/python3.9\n" +
" # You can obtain the scheme paths for a running Python instance with\n" +
" # import json\n" +
" # from pip._internal.locations import get_scheme\n" +
" # scheme=get_scheme(')\n" +
" # print(json.dumps({slot: getattr(scheme, slot) for slot in scheme.__slots__}))\n" +
" Scheme:\n" +
" purelib: /usr/lib/python3.9/site-packages\n" +
" platlib: /usr/lib/python3.9/site-packages\n" +
" headers: /usr/include/site/python3.9/\n" +
" scripts: /usr/bin\n" +
" data: /usr\n" +
"\n" +
" # user account\n" +
" UID: 0\n" +
" GID: 0\n" +
" UName: root\n" +
" GName: root\n" +
"\n" +
" # command to run on the host (not target) system to generate .pyc\n" +
" # files. The Python version number must match the target Python's\n" +
" # version number rather precisely; or rather their\n" +
" # `importlib.util.MAGIC_NUMBER` values must match.\n" +
" PyCompile: ['python3.9', '-m', 'compileall']\n" +
"\n" +
"LIMITATION: While checksums are verified, signatures are not.",
Args: cliutil.WrapPositionalArgs(cobra.ExactArgs(1)),
RunE: func(flags *cobra.Command, args []string) error {
yamlBytes, err := os.ReadFile(platFile)
if err != nil {
return err
}
var plat struct {
python.Platform
PyCompile []string
}
if err := yaml.Unmarshal(yamlBytes, &plat, yaml.DisallowUnknownFields); err != nil {
return fmt.Errorf("%s: %w", platFile, err)
}
plat.Platform.PyCompile, err = python.ExternalCompiler(plat.PyCompile...)
if err != nil {
return err
}
ctx := flags.Context()
layer, err := bdist.InstallWheel(ctx,
plat.Platform,
time.Time{}, // minTime: zero; don't enforce minTime
time.Time{}, // maxTime: zero; auto based on the timestamps in the wheel
args[0], // filename
bdist.PostInstallHooks(
entry_points.CreateScripts(plat.Platform),
recording_installs.Record(
"sha256",
"ocibuild layer wheel",
nil, // direct_url
),
),
)
if err != nil {
return err
}
if err := fsutil.WriteLayer(layer, os.Stdout); err != nil {
return err
}
return nil
},
}
cmd.Flags().StringVar(&platFile, "platform-file", "",
"Read `IN_YAML_FILE` to determine details about the target platform")
if err := cmd.MarkFlagRequired("platform-file"); err != nil {
panic(err)
}
argparserLayer.AddCommand(cmd)
}