@@ -6,14 +6,16 @@ package sysinfo
66
77import (
88 "fmt"
9- "os/exec"
109 "strings"
1110
11+ "github.com/linuxdeepin/go-lib/log"
1212 "github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/config/cache"
1313 runcmd "github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/pkg/utils/cmd"
1414 "github.com/linuxdeepin/lastore-daemon/src/lastore-update-tools/pkg/utils/fs"
1515)
1616
17+ var logger = log .NewLogger ("lastore/update-tools/sysinfo" )
18+
1719// CheckAppIsExist
1820func CheckAppIsExist (app string ) (bool , error ) {
1921 if err := fs .CheckFileExistState (app ); err != nil {
@@ -29,28 +31,31 @@ func GetCurrInstPkgStat(pkgs map[string]*cache.AppTinyInfo) error {
2931 }
3032
3133 // bash -c "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'"
32- outputStream , err := runcmd .RunnerOutput (10 , "bash" , "-c" , "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'" )
34+ outputStream , err := runcmd .RunnerOutput (
35+ 10 ,
36+ "dpkg-query" ,
37+ "-W" ,
38+ "-f=${db:Status-Abbrev} ${Package} ${Version}\n " ,
39+ )
3340 if err != nil {
34- return err
41+ return fmt . Errorf ( "failed to run dpkg-query: %w" , err )
3542 }
36- // logger.Debug(outputStream)
3743
44+ logger .Debugf ("dpkg-query output: %s" , outputStream )
3845 outputLines := strings .Split (outputStream , "\n " )
3946
40- // logger.Debugf("out:+%v", outputLines)
41-
4247 for _ , line := range outputLines {
43- spv := strings .Split (line , " " )
48+ spv := strings .Fields (line )
4449 if len (spv ) != 3 {
45- // logger.Debugf("skip line: %v", spv)
50+ logger .Debugf ("skip line: %v" , spv )
4651 continue
4752 }
4853 appInfo := cache.AppTinyInfo {
4954 Name : strings .Split (spv [1 ], ":" )[0 ],
5055 Version : spv [2 ],
5156 State : cache .PkgState (spv [0 ]),
5257 }
53- // logger.Debugf("pkg:%+v", appInfo)
58+ logger .Debugf ("pkg:%+v" , appInfo )
5459
5560 pkgs [appInfo .Name ] = & appInfo
5661 pkgs [fmt .Sprintf ("%s#%s" , appInfo .Name , appInfo .Version )] = & appInfo
@@ -62,19 +67,20 @@ func GetCurrInstPkgStat(pkgs map[string]*cache.AppTinyInfo) error {
6267
6368// ToDo:(DingHao)替换成袁老师的hash函数
6469func GetSysPkgStateAndVersion (pkgname string ) (string , string , error ) {
65- command := "bash"
66- arg1 := "-c"
67- arg2 := "dpkg -l | tail -n +6 | awk '{print $1,$2,$3}'|grep \" ^.. " + pkgname + " \" "
68- cmd := exec .Command (command , arg1 , arg2 )
69- output , err := cmd .Output ()
70+ output , err := runcmd .RunnerOutput (
71+ 10 ,
72+ "dpkg-query" ,
73+ "-W" ,
74+ "-f=${db:Status-Abbrev} ${Version}\n " ,
75+ pkgname ,
76+ )
7077 if err != nil {
7178 return "" , "" , err
7279 }
7380
74- pkgInfo := strings .Split (string (output ), " " )
75- if len (pkgInfo ) != 3 {
76- // log.Debugf("failed format: %s len: %d", pkgInfo, len(pkgInfo))
81+ pkgInfo := strings .Fields (output )
82+ if len (pkgInfo ) < 2 {
7783 return "" , "" , fmt .Errorf ("failed format: %s len: %d" , pkgInfo , len (pkgInfo ))
7884 }
79- return pkgInfo [0 ], pkgInfo [2 ], nil
85+ return pkgInfo [0 ], pkgInfo [1 ], nil
8086}
0 commit comments