Merge lp:~kyrofa/crackly/desktop_file_support into lp:crackly

Proposed by Kyle Fazzari on 2015-10-30
Status: Merged
Approved by: Kyle Fazzari on 2015-10-30
Approved revision: 2
Merged at revision: 2
Proposed branch: lp:~kyrofa/crackly/desktop_file_support
Merge into: lp:crackly
Diff against target: 271 lines (+245/-0)
5 files modified
main.go (+43/-0)
operations/generate_desktop_files.go (+104/-0)
operations/install.go (+51/-0)
operations/remove.go (+26/-0)
snap/metadata.go (+21/-0)
To merge this branch: bzr merge lp:~kyrofa/crackly/desktop_file_support
Reviewer Review Type Date Requested Status
Unity API Team 2015-10-30 Pending
Review via email: mp+276267@code.launchpad.net

Commit message

Add install/remove wrappers to deal with .desktop files.

Description of the change

Add install/remove wrappers to deal with .desktop files.

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added file 'main.go'
2--- main.go 1970-01-01 00:00:00 +0000
3+++ main.go 2015-10-30 14:11:44 +0000
4@@ -0,0 +1,43 @@
5+package main
6+
7+import (
8+ "fmt"
9+ "launchpad.net/crackly/operations"
10+ "log"
11+ "os"
12+)
13+
14+func main() {
15+ arguments := os.Args[1:]
16+
17+ if len(arguments) != 2 {
18+ printUsageAndExit("Invalid number of arguments!")
19+ }
20+
21+ switch arguments[0] {
22+ case "install":
23+ err := operations.Install(arguments[1])
24+ if err != nil {
25+ log.Fatalf(`Unable to install "%s": %s`, arguments[1], err)
26+ }
27+ case "remove":
28+ err := operations.Remove(arguments[1])
29+ if err != nil {
30+ log.Fatalf(`Unable to remove "%s": %s`, arguments[1], err)
31+ }
32+ default:
33+ printUsageAndExit("Unsupported action: %s", arguments[0])
34+ }
35+}
36+
37+func printUsageAndExit(format string, a ...interface{}) {
38+ fmt.Printf(format, a...)
39+ fmt.Println("\n")
40+ printUsage()
41+ os.Exit(1)
42+}
43+
44+func printUsage() {
45+ fmt.Println("Usage:")
46+ fmt.Printf("%s install <snap>\n", os.Args[0])
47+}
48
49=== added directory 'operations'
50=== added file 'operations/generate_desktop_files.go'
51--- operations/generate_desktop_files.go 1970-01-01 00:00:00 +0000
52+++ operations/generate_desktop_files.go 2015-10-30 14:11:44 +0000
53@@ -0,0 +1,104 @@
54+package operations
55+
56+import (
57+ "fmt"
58+ "path"
59+ "os"
60+ "io/ioutil"
61+ "path/filepath"
62+
63+ "launchpad.net/crackly/snap"
64+ "launchpad.net/snappy/snappy"
65+)
66+
67+func GenerateDesktopFiles(part snappy.Part, metadata snap.Metadata) error {
68+ rootDirectory := os.Getenv("SNAPPY_GLOBAL_ROOT")
69+ if rootDirectory == "" {
70+ rootDirectory = "/"
71+ }
72+
73+ desktopFileRoot, err := getDesktopFileRoot()
74+ if err != nil {
75+ return fmt.Errorf("Unable to determine .desktop file destination: %s",
76+ err)
77+ }
78+
79+ qualifiedPartName := snappy.QualifiedName(part)
80+ for _, binary := range metadata.Binaries {
81+ qualifiedName := qualifiedPartName+"."+binary.Name
82+ desktopFileName := fmt.Sprintf("%s.desktop", qualifiedName)
83+
84+ desktopFilePath := path.Join(desktopFileRoot, desktopFileName)
85+
86+ // .desktop file contents
87+ desktopFile := "[Desktop Entry]\n"
88+ desktopFile += "Type=Application\n"
89+ desktopFile += "Terminal=false\n"
90+ desktopFile += "X-Ubuntu-Touch=true\n"
91+ desktopFile += fmt.Sprintf("Name=%s\n", binary.Name)
92+ desktopFile += fmt.Sprintf("Icon=%s\n", part.Icon())
93+// desktopFile += fmt.Sprintf("Exec=%s/%s -- --desktop_file_hint=%s",
94+// rootDirectory+"apps/bin", metadata.Name+"."+binary.Name,
95+// desktopFilePath)
96+
97+ desktopFile += fmt.Sprintf("Exec=%s/%s",
98+ rootDirectory+"apps/bin", metadata.Name+"."+binary.Name)
99+
100+ err = ioutil.WriteFile(desktopFilePath, []byte(desktopFile), 0644)
101+ if err != nil {
102+ return fmt.Errorf(`Unable to write .desktop file "%s": %s`, desktopFilePath, err)
103+ }
104+ }
105+
106+ return nil
107+}
108+
109+func RemoveDesktopFiles(part snappy.Part) error {
110+ desktopFileRoot, err := getDesktopFileRoot()
111+ if err != nil {
112+ return fmt.Errorf("Unable to determine .desktop file destination: %s",
113+ err)
114+ }
115+
116+ desktopFiles, err := filepath.Glob(
117+ filepath.Join(desktopFileRoot, snappy.QualifiedName(part) + ".*"))
118+
119+ for _, desktopFilePath := range desktopFiles {
120+ err = os.Remove(desktopFilePath)
121+ if err != nil {
122+ return fmt.Errorf(`Unable to remove "%s": %s`, desktopFilePath, err)
123+ }
124+ }
125+
126+ return nil
127+}
128+
129+func getDesktopFileRoot() (string, error) {
130+// currentUser, err := user.Current()
131+// if err != nil {
132+// return "", fmt.Errorf("Unable to find current user: %s", err)
133+// }
134+
135+// // Don't use root, use the person who called `sudo`
136+// if currentUser.Uid == "0" {
137+// userName := os.Getenv("SUDO_USER")
138+// if userName == "" {
139+// return "", fmt.Errorf("Unable to determine appropriate user")
140+// }
141+
142+// currentUser, err = user.Lookup(userName)
143+// if err != nil {
144+// return "", fmt.Errorf(`Unable to find user with username "%s": %s`,
145+// userName, err)
146+// }
147+// }
148+
149+// desktopFileRoot := path.Join(currentUser.HomeDir,
150+// ".local/share/applications")
151+
152+// fmt.Println("Current user:", currentUser)
153+
154+// return desktopFileRoot, nil
155+
156+ return "/usr/share/applications", nil
157+}
158
159=== added file 'operations/install.go'
160--- operations/install.go 1970-01-01 00:00:00 +0000
161+++ operations/install.go 2015-10-30 14:11:44 +0000
162@@ -0,0 +1,51 @@
163+package operations
164+
165+import (
166+ "fmt"
167+ "launchpad.net/crackly/snap"
168+ "launchpad.net/snappy/clickdeb"
169+ "launchpad.net/snappy/progress"
170+ "launchpad.net/snappy/snappy"
171+)
172+
173+func Install(snapFile string) error {
174+ deb, err := clickdeb.Open(snapFile)
175+ if err != nil {
176+ return fmt.Errorf("Unable to open snap: %s", err)
177+ }
178+
179+ defer deb.Close()
180+
181+ yamlData, err := deb.MetaMember("package.yaml")
182+ if err != nil {
183+ return fmt.Errorf("Unable to obtain package.yaml: %s", err)
184+ }
185+
186+ metadata, err := snap.ParseMetadata(yamlData)
187+ if err != nil {
188+ return fmt.Errorf("Unable to parse package.yaml: %s", err)
189+ }
190+
191+ if len(metadata.Services) > 0 {
192+ return fmt.Errorf("Cannot install a snap that contains services")
193+ }
194+
195+ packageName, err := snappy.Install(snapFile, snappy.AllowUnauthenticated|snappy.DoInstallGC, progress.MakeProgressBar())
196+ if err != nil {
197+ return fmt.Errorf(`Error installing snap "%s": %s`, snapFile, err)
198+ }
199+
200+ fmt.Println("Package name:", packageName)
201+
202+ part := snappy.ActiveSnapByName(metadata.Name)
203+ if part == nil {
204+ return fmt.Errorf("Unable to find snap after install")
205+ }
206+
207+ err = GenerateDesktopFiles(part, metadata)
208+ if err != nil {
209+ return fmt.Errorf("Error generating .desktop files: %s", err)
210+ }
211+
212+ return nil
213+}
214
215=== added file 'operations/remove.go'
216--- operations/remove.go 1970-01-01 00:00:00 +0000
217+++ operations/remove.go 2015-10-30 14:11:44 +0000
218@@ -0,0 +1,26 @@
219+package operations
220+
221+import (
222+ "fmt"
223+ "launchpad.net/snappy/progress"
224+ "launchpad.net/snappy/snappy"
225+)
226+
227+func Remove(snapName string) error {
228+ part := snappy.ActiveSnapByName(snapName)
229+ if part == nil {
230+ return fmt.Errorf(`No snap named "%s"`, snapName)
231+ }
232+
233+ err := snappy.Remove(snapName, snappy.DoRemoveGC, progress.MakeProgressBar())
234+ if err != nil {
235+ return fmt.Errorf(`Error removing snap "%s": %s`, snapName, err)
236+ }
237+
238+ err = RemoveDesktopFiles(part)
239+ if err != nil {
240+ return fmt.Errorf("Error removing .desktop files: %s", err)
241+ }
242+
243+ return nil
244+}
245
246=== added directory 'snap'
247=== added file 'snap/metadata.go'
248--- snap/metadata.go 1970-01-01 00:00:00 +0000
249+++ snap/metadata.go 2015-10-30 14:11:44 +0000
250@@ -0,0 +1,21 @@
251+package snap
252+
253+import (
254+ "gopkg.in/yaml.v2"
255+ "launchpad.net/snappy/snappy"
256+)
257+
258+type Metadata struct {
259+ Name string
260+ Version string
261+ Vendor string
262+ Icon string
263+
264+ Services []snappy.ServiceYaml `yaml:"services,omitempty"`
265+ Binaries []snappy.Binary `yaml:"binaries,omitempty"`
266+}
267+
268+func ParseMetadata(data []byte) (metadata Metadata, err error) {
269+ err = yaml.Unmarshal(data, &metadata)
270+ return
271+}

Subscribers

People subscribed via source and target branches

to all changes: