0f5d58886b7c78e122e1ee15c2566e741c6f757b
[apps/core/preloaded/print-service.git] / drvopt / drvopt.go
1 /*
2 *       Printservice
3 *
4 * Copyright 2012  Samsung Electronics Co., Ltd
5
6 * Licensed under the Flora License, Version 1.1 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9
10 * http://floralicense.org/license/
11
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 */
19
20 package main
21
22 import (
23         "./drv"
24         "flag"
25         "fmt"
26         "os"
27         "runtime/pprof"
28         "strings"
29         "io/ioutil"
30         "path"
31         "os/exec"
32 )
33
34 var (
35         indir       string
36         inopt       string
37         outfile     string
38         show_help   bool
39         id          string
40         cpuprofile  string
41         memprofile  string
42         listopt     string
43         nameopt     string
44         listprod    string
45         prodopt     string
46 )
47
48 func init() {
49         flag.StringVar(&indir, "d", "", "input drv directory")
50         flag.StringVar(&inopt, "q", "", "input opt file")
51         flag.StringVar(&outfile, "o", "stripped.drv", "output file")
52         flag.StringVar(&cpuprofile, "p", "", "write cpu profile to file")
53         flag.StringVar(&memprofile, "m", "", "write memory profile to this file")
54         flag.StringVar(&listopt, "l", "", "list available printers by model")
55         flag.StringVar(&listprod, "c", "", "list available printers by product")
56         flag.StringVar(&nameopt, "n", "", "print drv by model name")
57         flag.StringVar(&prodopt, "x", "", "print drv by product name")
58 }
59
60 func main() {
61         flag.Parse()
62
63         if listopt != "" {
64                 drvm := drv.LoadDrvm(listopt)
65                 drvm.ListModels()
66                 return
67         }
68
69         if listprod != "" {
70                 drvm := drv.LoadDrvm(listprod)
71                 drvm.ListProducts()
72         }
73
74         if cpuprofile != "" {
75                 f, err := os.Create(cpuprofile)
76                 if err != nil {
77                         fmt.Print(err)
78                 }
79                 pprof.StartCPUProfile(f)
80                 defer pprof.StopCPUProfile()
81         }
82
83         if memprofile != "" {
84                 f, err := os.Create(memprofile)
85                 if err != nil {
86                         fmt.Print(err)
87                 }
88                 pprof.WriteHeapProfile(f)
89                 defer f.Close()
90         }
91
92         if indir != "" {
93                 drvm := new(drv.Drvm)
94                 files, err := ioutil.ReadDir(indir)
95                 if err != nil {
96                         fmt.Println(err)
97                         return
98                 }
99                 for _, fp := range files {
100                         fn := fp.Name()
101                         fmt.Println("\nfile:", fn)
102                         if !strings.HasSuffix(fn, ".ppd") { continue; }
103                         fn = path.Join(indir, fn)
104                         cmd := exec.Command("ppdi", "-o", "/tmp/tmp.drv", fn)
105                         err := cmd.Run()
106                         if err != nil {
107                                 fmt.Println(err)
108                                 return
109                         }
110                         err = drvm.ExtendDrvm("/tmp/tmp.drv")
111                         if err != nil {
112                                 fmt.Println(err)
113                                 return
114                         }
115                         cmd = exec.Command("rm", "/tmp/tmp.drv")
116                         err = cmd.Run()
117                 }
118
119                 drvm.SaveDrvm(outfile)
120         }
121         return
122 }