日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Ubuntusnap包的制作過程

如果你看過譯者以前翻譯的 snappy 文章,不知有沒有感覺相關(guān)主題都是淺嘗輒止,講得不夠透徹,看得也不太過癮?如果有的話,相信這篇詳細(xì)講解如何從零開始制作一個 snap 包的文章應(yīng)該不會讓你失望。

成都創(chuàng)新互聯(lián)主要從事成都做網(wǎng)站、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)遼陽縣,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

在這篇文章中,我們將看到如何為名為 timg 的實用程序制作對應(yīng)的 snap 包。如果這是你第一次聽說 snap 安裝包,你可以先看看 如何創(chuàng)建你的第一個 snap 包。

今天我們將學(xué)習(xí)以下有關(guān)使用 snapcraft 制作 snap 包的內(nèi)容:

  • timg 源碼中的 Makefile 文件是手工編寫,我們需要修改一些 make 插件參數(shù)。
  • 這個程序是用 C++ 語言寫的,依賴幾個額外的庫文件。我們需要把相關(guān)的代碼添加到 snap 包中。
  • 嚴(yán)格限制還是傳統(tǒng)限制?我們將會討論如何在它們之間進行選擇。

首先,我們了解下 timg 有什么用?

 

背景

Linux 終端模擬器已經(jīng)變得非常炫酷,并且還能顯示顏色!

1.png-19.9kB

除了標(biāo)準(zhǔn)的顏色,大多數(shù)終端模擬器(如上圖顯示的 GNOME 終端)都支持真彩色(1600 萬種顏色)。

圖片.png-61.9kB

是的!終端模擬器已經(jīng)支持真彩色了!從這個頁面“ 多個終端和終端應(yīng)用程序已經(jīng)支持真彩色(1600 萬種顏色)” 可以獲取 AWK 代碼自己進行測試。你可以看到在代碼中使用了一些 轉(zhuǎn)義序列 來指定 RGB 的值(256 * 256 * 256 ~= 1600 萬種顏色)。

 

timg 是什么?

好了,言歸正傳,timg 有什么用?它能將輸入的圖片重新調(diào)整為終端窗口字符所能顯示范圍的大?。ū热纾?0 x 25),然后在任何分辨率的終端窗口用彩色字符顯示圖像。

圖片.png-37.3kB

這幅圖用彩色塊字符顯示了 Ubuntu 的 logo,原圖是一個 PNG 格式的文件。

圖片.png-165kB

這是 @Doug8888 拍攝的花。

如果你通過遠(yuǎn)程連接服務(wù)器來管理自己的業(yè)務(wù),并想要查看圖像文件,那么 timg 將會特別有用。

除了靜態(tài)圖片,timg 同樣也可以顯示 gif 動圖。

那么讓我們開始 snap 之旅吧!

 

熟悉 timg 的源碼

timg 的源碼可以在 https://github.com/hzeller/timg 找到。讓我們試著手動編譯它,以了解它有什么需求。

圖片.png-128.4kB

Makefilesrc/ 子文件夾中而不是項目的根文件夾中。在 github 頁面上,他們說需要安裝兩個開發(fā)包(GraphicsMagic++ 和 WebP),然后使用 make 就能生成可執(zhí)行文件。在截圖中可以看到我已經(jīng)將它們安裝好了(在我讀完相關(guān)的 Readme.md 文件后)。

因此,在編寫 snapcraft.yaml 文件時已經(jīng)有了四條腹稿:

  1. Makefilesrc/ 子文件夾中而不是項目的根文件夾中。
  2. 這個程序編譯時需要兩個開發(fā)庫。
  3. 為了讓 timg 以 snap 包形式運行,我們需要將這兩個庫捆綁在 snap 包中(或者靜態(tài)鏈接它們)。
  4. timg 是用 C++ 編寫的,所以需要安裝 g++。在編譯之前,讓我們通過 snapcraft.yaml 文件來檢查 build-essential 元包是否已經(jīng)安裝。

 

從 snapcraft 開始

讓我們新建一個名為 timg-snap/ 的文件夾,并在其中運行 snapcraft init 這條命令來創(chuàng)建 snapcraft.yaml 工作的框架。

  1. Ubuntu@snaps:~$ mkdir timg-snap
  2. ubuntu@snaps:~$ cd timg-snap/
  3. ubuntu@snaps:~/timg-snap$ snapcraft init
  4. Created snap/snapcraft.yaml.
  5. Edit the file to your liking or run `snapcraft` to get started
  6. ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
  7. name:my-snap-name # you probably want to 'snapcraft register '
  8. version:'0.1'# just for humans, typically '1.2+git'or'1.3.2'
  9. summary:Single-line elevator pitch for your amazing snap #79charlong summary
  10. description:|
  11. Thisismy-snap's description. You have a paragraph or two to tell the most important story about your snap. Keep it under 100 words though, we live in tweetspace and your description wants to look good in the snap store.
  12. grade: devel # must be 'stable' to release into candidate/stable channels
  13. confinement: devmode # use 'strict' once you have the right plugs and slots
  14. parts:
  15. my-part:
  16. # See 'snapcraft plugins'
  17. plugin: nil

 

填充元數(shù)據(jù)

snapcraft.yaml 配置文件的上半部分是元數(shù)據(jù)。我們需要一個一個把它們填滿,這算是比較容易的部分。元數(shù)據(jù)由以下字段組成:

  1. name (名字)—— snap 包的名字,它將公開在 Ubuntu 商店中。
  2. version (版本)—— snap 包的版本號。可以是源代碼存儲庫中一個適當(dāng)?shù)姆种Щ蛘邩?biāo)記,如果沒有分支或標(biāo)記的話,也可以是當(dāng)前日期。
  3. summary (摘要)—— 不超過 80 個字符的簡短描述。
  4. description (描述)—— 長一點的描述, 100 個字以下。
  5. grade (等級)—— stable (穩(wěn)定)或者 devel (開發(fā))??因為我們想要在 Ubuntu 商店的穩(wěn)定通道中發(fā)布這個 snap 包,所以在 snap 包能正常工作后,就把它設(shè)置成 stable。
  6. confinement (限制)—— 我們首先設(shè)置為 devmode (開發(fā)模式),這樣系統(tǒng)將不會以任何方式限制 snap 包。一旦它在 devmode下能正常工作,我們再考慮選擇 strict (嚴(yán)格)還是 classic (傳統(tǒng))限制。

我們將使用 timg 這個名字:

  1. ubuntu@snaps:~/timg-snap$ snapcraft register timg
  2. Registering timg.
  3. You already own the name 'timg'.

是的,這個名字我已經(jīng)注冊了 :-)。

接下來,我們應(yīng)該選擇哪個版本的 timg?

圖片.png-72.7kB

當(dāng)在倉庫中尋找分支或標(biāo)記時,我們會發(fā)現(xiàn)有一個 v0.9.5 標(biāo)簽,其中有 2016 年 6 月 27 日最新提交的代碼。

圖片.png-71.4kB

然而主分支(master)中有兩個看起來很重要的提交。因此我們使用主分支而不用 v0.9.5 標(biāo)簽的那個。我們使用今天的日期—— 20170226 做為版本號。

我們從倉庫中搜集了摘要和描述。其中摘要的內(nèi)容為 A terminal image viewer,描述的內(nèi)容為 A viewer that uses 24-Bit color capabilities and unicode character blocks to display images in the terminal。

最后,將 grade (等級)設(shè)置為 stable (穩(wěn)定),將 confinement 限制設(shè)置為 devmode (開發(fā)模式)(一直到 snap 包真正起作用)。

這是更新后的 snapcraft.yaml,帶有所有的元數(shù)據(jù):

  1. ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
  2. name: timg
  3. version:'20170226'
  4. summary: A terminal image viewer
  5. description:|
  6. A viewer that uses 24-Bit color capabilities and unicode character blocks to display images in the terminal.
  7. grade: stable
  8. confinement: devmode
  9. parts:
  10. my-part:
  11. #See'snapcraft plugins'
  12. plugin:nil

 

弄清楚 parts: 是什么

現(xiàn)在我們需要將上面已經(jīng)存在的 parts: 部分替換成真實的 parts:。

Git 倉庫的 URL。

存在 Makefile,因此我們需要 make 插件。

我們已經(jīng)知道 git 倉庫的 URL 鏈接,并且 timg 源碼中已有了 Makefile 文件。至于 snapcraft make 插件 的 Makefile 命令,正如文檔所言,這個插件總是會運行 make 后再運行 make install。為了確認(rèn) make 插件的用法,我查看了 snapcraft 可用插件列表。

因此,我們將最初的配置:

  1. parts:
  2. my-part:
  3. #See'snapcraft plugins'
  4. plugin:nil

修改為:

  1. parts:
  2. timg:
  3. source: https://github.com/hzeller/timg.git
  4. plugin:make

這是當(dāng)前 snapcraft.yaml 文件的內(nèi)容:

  1. name: timg
  2. version:'20170226'
  3. summary: A terminal image viewer
  4. description:|
  5. A viewer that uses 24-Bit color capabilities and unicode character blocks
  6. to display images in the terminal.
  7. grade: stable
  8. confinement: devmode
  9. parts:
  10. timg:
  11. source: https://github.com/hzeller/timg.git
  12. plugin:make

讓我們運行下 snapcraft prime 命令看看會發(fā)生什么:

  1. ubuntu@snaps:~/timg-snap$ snapcraft prime
  2. Preparing to pull timg
  3. Pulling timg
  4. Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
  5. remote:Counting objects:144,done.
  6. remote:Total144(delta 0), reused 0(delta 0), pack-reused 144
  7. Receiving objects:100%(144/144),116.00KiB|0 bytes/s,done.
  8. Resolving deltas:100%(89/89),done.
  9. Checking connectivity...done.
  10. Preparing to build timg
  11. Building timg
  12. make-j4
  13. make:***No targets specified andno makefile found.Stop.
  14. Command'['/bin/sh', '/tmp/tmpem97fh9d', 'make', '-j4']' returned non-zero exit status 2
  15. ubuntu@snaps:~/timg-snap$

我們可以看到 snapcraft 無法在源代碼中找到 Makefile 文件,正如我們之前所暗示的,Makefile 位于 src/ 子文件夾中。那么,我們可以讓 snapcraft 使用 src/ 文件夾中的 Makefile 文件嗎?

每個 snapcraft 插件都有自己的選項,并且有一些通用選項是所有插件共享的。在本例中,我們希望研究那些與源代碼相關(guān)的 snapcraft 選項。我們開始吧:

source-subdir:path

snapcraft 會檢出checkoutsource 關(guān)鍵字所引用的倉庫或者解壓歸檔文件到 parts//src/ 中,但是它只會將特定的子目錄復(fù)制到 parts//build/ 中。

我們已經(jīng)有了適當(dāng)?shù)倪x項,下面更新下 parts

  1. parts:
  2. timg:
  3. source: https://github.com/hzeller/timg.git
  4. source-subdir: src
  5. plugin:make

然后再次運行 snapcraft prime

  1. ubuntu@snaps:~/timg-snap$ snapcraft prime
  2. The'pull' step of 'timg'is out of date:
  3. The'source-subdir' part property appears to have changed.
  4. Please clean that part's 'pull' step in order to continue
  5. ubuntu@snaps:~/timg-snap$ snapcraft clean
  6. Cleaning up priming area
  7. Cleaning up staging area
  8. Cleaning up parts directory
  9. ubuntu@snaps:~/timg-snap$ snapcraft prime
  10. Skipping pull timg (already ran)
  11. Preparing to build timg
  12. Building timg
  13. make -j4
  14. g++ `GraphicsMagick++-config --cppflags --cxxflags` -Wall -O3 -fPIC -c -o timg.o timg.cc
  15. g++ -Wall -O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
  16. /bin/sh: 1: GraphicsMagick++-config: not found
  17. timg.cc:33:22: fatal error: Magick++.h: No such file or directory
  18. compilation terminated.
  19. Makefile:10: recipe for target 'timg.o' failed
  20. make: *** [timg.o] Error 1
  21. make: *** Waiting for unfinished jobs....
  22. Command '['/bin/sh','/tmp/tmpeeyxj5kw','make','-j4']' returned non-zero exit status 2
  23. ubuntu@snaps:~/timg-snap$

從錯誤信息我們可以得知 snapcraft 找不到 GraphicsMagick++ 這個開發(fā)庫文件。根據(jù) snapcraft 常見關(guān)鍵字 可知,我們需要在 snapcraft.yaml 中指定這個庫文件,這樣 snapcraft 才能安裝它。

build-packages:[deb, deb, deb…]

列出構(gòu)建 part 前需要在主機中安裝的 Ubuntu 包。這些包通常不會進入最終的 snap 包中,除非它們含有 snap 包中二進制文件直接依賴的庫文件(在這種情況下,可以通過 ldd 發(fā)現(xiàn)它們),或者在 stage-package 中顯式地指定了它們。

讓我們尋找下這個開發(fā)包的名字:

  1. ubuntu@snaps:~/timg-snap$ apt-cache search graphicsmagick++|grep dev
  2. graphicsmagick-libmagick-dev-compat/xenial 1.3.23-1build1 all
  3. libgraphicsmagick++1-dev/xenial 1.3.23-1build1 amd64
  4. format-independent image processing - C++ development files
  5. libgraphicsmagick1-dev/xenial 1.3.23-1build1 amd64
  6. format-independent image processing - C development files
  7. ubuntu@snaps:~/timg-snap$

可以看到包名為 libgraphicsmagick++1-dev,下面是更新后的 parts

  1. parts:
  2. timg:
  3. source: https://github.com/hzeller/timg.git
  4. source-subdir: src
  5. plugin:make
  6. build-packages:
  7. - libgraphicsmagick++1-dev

再次運行 snapcraft

  1. ubuntu@snaps:~/timg-snap$ snapcraft
  2. Installing build dependencies: libgraphicsmagick++1-dev
  3. [...]
  4. The following NEW packages will be installed:
  5. libgraphicsmagick++-q16-12 libgraphicsmagick++1-dev libgraphicsmagick-q16-3
  6. libgraphicsmagick1-dev libwebp5
  7. [...]
  8. Building timg
  9. make-j4
  10. g++`GraphicsMagick++-config --cppflags --cxxflags`-Wall-O3 -fPIC -c -o timg.o timg.cc
  11. g++-Wall-O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
  12. g++-o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
  13. /usr/bin/ld: cannot find-lwebp
  14. collect2: error:ld returned 1exit status
  15. Makefile:7: recipe for target 'timg' failed
  16. make:***[timg]Error1
  17. Command'['/bin/sh', '/tmp/tmptma45jzl', 'make', '-j4']' returned non-zero exit status 2
  18. ubuntu@snaps:~/timg-snap$

雖然只指定了開發(fā)庫 libgraphicsmagick+1-dev,但 Ubuntu 還安裝了一些代碼庫,包括 libgraphicsmagick ++-q16-12,以及動態(tài)代碼庫 libwebp。

這里仍然有一個錯誤,這個是因為缺少開發(fā)版本的 webp 庫(一個靜態(tài)庫)。我們可以通過下面的命令找到它:

  1. ubuntu@snaps:~/timg-snap$ apt-cache search libwebp |grep dev
  2. libwebp-dev -Lossy compression of digital photographic images.
  3. ubuntu@snaps:~/timg-snap$

上面安裝的 libwebp5 包只提供了一個動態(tài)庫(.so)。通過 libwebp-dev 包,我們可以得到相應(yīng)的靜態(tài)庫(.a)。好了,讓我們更新下 parts: 部分:

  1. parts:
  2. timg:
  3. source: https://github.com/hzeller/timg.git
  4. source-subdir: src
  5. plugin:make
  6. build-packages:
  7. - libgraphicsmagick++1-dev
  8. - libwebp-dev

下面是更新后的 snapcraft.yaml 文件的內(nèi)容:

  1. name: timg
  2. version:'20170226'
  3. summary: A terminal image viewer
  4. description:|
  5. A viewer that uses 24-Bit color capabilities and unicode character blocks
  6. to display images in the terminal.
  7. grade: stable
  8. confinement: devmode
  9. parts:
  10. timg:
  11. source: https://github.com/hzeller/timg.git
  12. source-subdir: src
  13. plugin:make
  14. build-packages:
  15. - libgraphicsmagick++1-dev
  16. - libwebp-dev

讓我們運行下 snapcraft prime

  1. ubuntu@snaps:~/timg-snap$ snapcraft prime
  2. Skipping pull timg (already ran)
  3. Preparing to build timg
  4. Building timg
  5. make-j4
  6. g++`GraphicsMagick++-config --cppflags --cxxflags`-Wall-O3 -fPIC -c -o timg.o timg.cc
  7. g++-Wall-O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
  8. g++-o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
  9. make install DESTDIR=/home/ubuntu/timg-snap/parts/timg/install
  10. install timg /usr/local/bin
  11. install: cannot create regular file'/usr/local/bin/timg':Permission denied
  12. Makefile:13: recipe for target 'install' failed
  13. make:***[install]Error1
  14. Command'['/bin/sh', '/tmp/tmptq_s1itc', 'make', 'install', 'DESTDIR=/home/ubuntu/timg-snap/parts/timg/install']' returned non-zero exit status 2
  15. ubuntu@snaps:~/timg-snap$

我們遇到了一個新問題。由于 Makefile 文件是手工編寫的,不符合 snapcraft make 插件 的參數(shù)設(shè)置,所以不能正確安裝到 prime/ 文件夾中。Makefile 會嘗試安裝到 usr/local/bin 中。

我們需要告訴 snapcraft make 插件 不要運行 make install,而是找到 timg 可執(zhí)行文件然后把它放到 prime/ 文件夾中。根據(jù)文檔的描述:

  1. - artifacts:
  2. (列表)
  3. 將make生成的指定文件復(fù)制或者鏈接到 snap 包安裝目錄。如果使用,則`make install`這步操作將被忽略。

所以,我們需要將一些東西放到 artifacts: 中。但是具體是哪些東西?

  1. ubuntu@snaps:~/timg-snap/parts/timg$ ls build/src/
  2. Makefile terminal-canvas.h timg* timg.o
  3. terminal-canvas.cc terminal-canvas.o timg.cc
  4. ubuntu@snaps:~/timg-snap/parts/timg$

build/ 子目錄中,我們可以找到 make 的輸出結(jié)果。由于我們設(shè)置了 source-subdir:src,所以 artifacts: 的基目錄為 build/src。在這里我們可以找到可執(zhí)行文件 timg,我們需要將它設(shè)置為 artifacts: 的一個參數(shù)。通過 artifacts:,我們可以把 make 輸出的某些文件復(fù)制到 snap 包的安裝目錄(在 prime/ 中)。

下面是更新后 snapcraft.yaml 文件 parts: 部分的內(nèi)容:

  1. parts:
  2. timg:
  3. source: https://github.com/hzeller/timg.git
  4. source-subdir: src
  5. plugin:make
  6. build-packages:
  7. - libgraphicsmagick++1-dev
  8. - libwebp-dev
  9. artifacts:[timg]

讓我們運行 snapcraft prime

  1. ubuntu@snaps:~/timg-snap$ snapcraft prime
  2. Preparing to pull timg
  3. Pulling timg
  4. Cloning into '/home/ubuntu/timg-snap/parts/timg/src'...
  5. remote:Counting objects:144,done.
  6. remote:Total144(delta 0), reused 0(delta 0), pack-reused 144
  7. Receiving objects:100%(144/144),116.00KiB|207.00KiB/s,done.
  8. Resolving deltas:100%(89/89),done.
  9. Checking connectivity...done.
  10. Preparing to build timg
  11. Building timg
  12. make-j4
  13. g++`GraphicsMagick++-config --cppflags --cxxflags`-Wall-O3 -fPIC -c -o timg.o timg.cc
  14. g++-Wall-O3 -fPIC -c -o terminal-canvas.o terminal-canvas.cc
  15. g++-o timg timg.o terminal-canvas.o `GraphicsMagick++-config --ldflags --libs`
  16. Staging timg
  17. Priming timg
  18. ubuntu@snaps:~/timg-snap$

我們還將繼續(xù)迭代。

 

導(dǎo)出命令

到目前為止,snapcraft 生成了可執(zhí)行文件,但沒有導(dǎo)出給用戶使用的命令。接下來我們需要通過 apps: 導(dǎo)出一個命令。

首先我們需要知道命令在 prime/ 的哪個子文件夾中:

  1. ubuntu@snaps:~/timg-snap$ ls prime/
  2. meta/ snap/ timg* usr/
  3. ubuntu@snaps:~/timg-snap$

它在 prime/ 子文件夾的根目錄中?,F(xiàn)在,我們已經(jīng)準(zhǔn)備好要在 snapcaft.yaml 中增加 apps: 的內(nèi)容:

  1. ubuntu@snaps:~/timg-snap$ cat snap/snapcraft.yaml
  2. name: timg
  3. version:'20170226'
  4. summary: A terminal image viewer
  5. description:|
  6. A viewer that uses 24-Bit color capabilities and unicode character blocks
  7. to display images in the terminal.
  8. grade: stable
  9. confinement: devmode
  10. apps:
  11. timg:
  12. command: timg
  13. parts:
  14. timg:
  15. source: https://github.com/hzeller/timg.git
  16. source-subdir: src
  17. plugin:make
  18. build-packages:
  19. - libgraphicsmagick++1-dev
  20. - libwebp-dev
  21. artifacts:[timg]

讓我們再次運行 snapcraft prime,然后測試下生成的 snap 包:

  1. ubuntu@snaps:~/timg-snap$ snapcraft prime
  2. Skipping pull timg (already ran)
  3. Skipping build timg (already ran)
  4. Skipping stage timg (already ran)
  5. Skipping prime timg (already ran)
  6. ubuntu@snaps:~/timg-snap$ snap try --devmode prime/
  7. timg 20170226 mounted from/home/ubuntu/timg-snap/prime
  8. ubuntu@snaps:~/timg-snap$

圖片來源: https://www.flickr.com/photos/mustangjoe/6091603784/

我們可以通過 snap try --devmode prime/ 啟用該 snap 包然后測試 timg 命令。這是一種高效的測試方法,可以避免生成 .snap 文件,并且無需安裝和卸載它們,因為 snap try prime/ 直接使用了 prime/ 文件夾中的內(nèi)容。

 

限制 snap

到目前為止,snap 包一直是在不受限制的開發(fā)模式下運行的。讓我們看看如何限制它的運行:

  1. ubuntu@snaps:~/timg-snap$ snap list
  2. NameVersionRevDeveloperNotes
  3. core 16-21337 canonical -
  4. timg 20170226 x1 devmode,try
  5. ubuntu@snaps:~/timg-snap$ snap try--jailmode prime
  6. timg 20170226 mounted from/home/ubuntu/timg-snap/prime
  7. ubuntu@snaps:~/timg-snap$ snap list
  8. NameVersionRevDeveloperNotes
  9. core 16-21337 canonical -
  10. timg 20170226 x2 jailmode,try
  11. ubuntu@snaps:~/timg-snap$ timg pexels-photo-149813.jpeg
  12. Trouble loading pexels-photo-149813.jpeg(Magick:Unable to open file(pexels-photo-149813.jpeg) reported by magick/blob.c:2828(OpenBlob))
  13. ubuntu@snaps:~/timg-snap$

通過這種方式,我們可以無需修改 snapcraft.yaml 文件就從開發(fā)模式(devmode)切換到限制模式(jailmode)(confinement: strict)。正如預(yù)期的那樣,timg 無法讀取圖像,因為我們沒有開放訪問文件系統(tǒng)的權(quán)限。

現(xiàn)在,我們需要作出決定。使用限制模式,我們可以很容易授予某個命令訪問用戶 $HOME 目錄中文件的權(quán)限,但是只能訪問那里。如果圖像文件位于其它地方,我們總是需要復(fù)制到 $HOME 目錄并在 $HOME 的副本上運行 timg。如果我們覺得可行,那我們可以設(shè)置 snapcraf.yaml 為:

  1. name: timg
  2. version:'20170226'
  3. summary: A terminal image viewer
  4. description:|
  5. A viewer that uses 24-Bit color capabilities and unicode character blocks
  6. to display images in the terminal.
  7. grade: stable
  8. confinement: strict
  9. apps:
  10. timg:
  11. command: timg
  12. plugs:[home]
  13. parts:
  14. timg:
  15. source: https://github.com/hzeller/timg.git
  16. source-subdir: src
  17. plugin:make
  18. build-packages:
  19. - libgraphicsmagick++1-dev
  20. - libwebp-dev
  21. artifacts:[timg]

另一方面,如果希望 timg snap 包能訪問整個文件系統(tǒng),我們可以設(shè)置傳統(tǒng)限制來實現(xiàn)。對應(yīng)的 snapcraft.yaml 內(nèi)容如下:

  1. name: timg
  2. version:'20170226'
  3. summary: A terminal image viewer
  4. description:|
  5. A viewer that uses 24-Bit color capabilities and unicode character blocks
  6. to display images in the terminal.
  7. grade: stable
  8. confinement: classic
  9. apps:
  10. timg:
  11. command: timg
  12. parts:
  13. timg:
  14. source: https://github.com/hzeller/timg.git
  15. source-subdir: src
  16. plugin:make
  17. build-packages:
  18. - libgraphicsmagick++1-dev
  19. - libwebp-dev
  20. artifacts:[timg]

接下來我們將選擇嚴(yán)格(strict)約束選項。因此,圖像應(yīng)該只能放在 $HOME 中。

 

打包和測試

讓我們打包這個 snap,也就是制作 .snap 文件,然后在新安裝的 Ubuntu 系統(tǒng)上對它進行測試。

  1. ubuntu@snaps:~/timg-snap$ snapcraft
  2. Skipping pull timg (already ran)
  3. Skipping build timg (already ran)
  4. Skipping stage timg (already ran)
  5. Skipping prime timg (already ran)
  6. Snapping'timg' \
  7. Snapped timg_20170226_amd64.snap
  8. ubuntu@snaps:~/timg-snap$

我們?nèi)绾卧趲酌腌妰?nèi)得到一個全新安裝的 Ubuntu 系統(tǒng)來對 snap 包進行測試?

請查看 嘗試在 Ubuntu 上使用 LXD 容器,并在你的系統(tǒng)上設(shè)置 LXD。然后回到這里,嘗試運行下面的命令:

  1. $ lxc launch ubuntu:x snaptesting
  2. Creating snaptesting
  3. Starting snaptesting
  4. $ lxc file push timg_20170226_amd64.snap snaptesting/home/ubuntu/
  5. $ lxc exec snaptesting --sudosu- ubuntu
  6. To run a command as administrator (user "root"),use"sudo ".
  7. See"man sudo_root"for details.
  8. ubuntu@snaptesting:~$ ls
  9. timg_20170226_amd64.snap
  10. ubuntu@snaptesting:~$ snap install timg_20170226_amd64.snap
  11. error: access denied (trywithsudo)
  12. ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap
  13. error: cannot find signatures with metadata for snap "timg_20170226_amd64.snap"
  14. ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
  15. error: cannot perform the following tasks:
  16. -Mount snap "core"(1337)([start snap-core-1337.mount] failed withexit status 1:Jobfor snap-core-1337.mount failed.See"systemctl status snap-core-1337.mount"and"journalctl -xe"for details.
  17. )
  18. ubuntu@snaptesting:~$ sudo apt install squashfuse
  19. [...]
  20. Setting up squashfuse (0.1.100-0ubuntu1~ubuntu16.04.1)...
  21. ubuntu@snaptesting:~$ sudo snap install timg_20170226_amd64.snap --dangerous
  22. timg 20170226 installed
  23. ubuntu@snaptesting:~$ wget https://farm7.staticflickr.com/6187/6091603784_d6960c8be2_z_d.jpg
  24. [...]
  25. 2017-02-2622:12:18(636 KB/s)-‘6091603784_d6960c8be2_z_d.jpg’ saved [240886/240886]
  26. ubuntu@snaptesting:~$ timg 6091603784_d6960c8be2_z_d.jpg
  27. [it worked!]
  28. ubuntu@snaptesting:~$

我們啟動了一個名為 snaptesting 的 LXD 容器,并將 .snap 文件復(fù)制進去。然后,通過普通用戶連接到容器,并嘗試安裝 snap 包。最初,我們安裝失敗了,因為在無特權(quán)的 LXD 容器中安裝 snap 包需要使用 sudo 。接著又失敗了,因為 .snap 沒有經(jīng)過簽名(我們需要使用 --dangerous 參數(shù))。然而還是失敗了,這次是因為我們需要安裝 squashfuse 包(Ubuntu 16.04 鏡像中沒有預(yù)裝)。最后,我們成功安裝了snap,并設(shè)法查看了圖像。

在一個全新安裝的 Linux 系統(tǒng)中測試 snap 包是很重要的,因為這樣才能確保 snap 包中包含所有必須的代碼庫。在這個例子中,我們使用了靜態(tài)庫并運行良好。

 

發(fā)布到 Ubuntu 商店

  1. $ snapcraft push timg_20170226_amd64.snap
  2. Pushing'timg_20170226_amd64.snap' to the store.
  3. Uploading timg_20170226_amd64.snap []0%
  4. Uploading timg_20170226_amd64.snap [=======================================]100%
  5. Ready to release!|
  6. Revision6 of 'timg' created.
  7. $ snapcraft release timg 6 stable
  8. TrackArchSeriesChannelVersionRevision
  9. latest amd64 16 stable 201702266
  10. candidate ^^
  11. beta 0.9.55
  12. edge 0.9.55
  13. The'stable' channel is now open.

我們把 .snap 包推送到 Ubuntu 商店后,得到了一個 Revision 6。然后,我們將 timg Revision 6 發(fā)布到了 Ubuntu 商店的穩(wěn)定通道。

在候選通道中沒有已發(fā)布的 snap 包,它繼承的是穩(wěn)定通道的包,所以顯示 ^ 字符。

在之前的測試中,我將一些較老版本的 snap 包上傳到了測試和邊緣通道。這些舊版本使用了 timg 標(biāo)簽為 0.9.5 的源代碼。

我們可以通過將穩(wěn)定版本發(fā)布到測試和邊緣通道來移除舊的 0.9.5 版本的包。

  1. $ snapcraft release timg 6 beta
  2. TrackArchSeriesChannelVersionRevision
  3. latest amd64 16 stable 201702266
  4. candidate ^^
  5. beta 201702266
  6. edge 0.9.55
  7. $ snapcraft release timg 6 edge
  8. TrackArchSeriesChannelVersionRevision
  9. latest amd64 16 stable 201702266
  10. candidate ^^
  11. beta 201702266
  12. edge 201702266

 

使用 timg

讓我們不帶參數(shù)運行 timg

  1. ubuntu@snaptesting:~$ timg
  2. Expected image filename.
  3. usage:/snap/timg/x1/timg [options][...]
  4. Options:
  5. -gx:Output pixel geometry.Defaultfrom terminal 80x48
  6. -s[]:Scroll horizontally (optionally: delay ms (60)).
  7. -d: delta x and delta y when scrolling (default:1:0).
  8. -w:If multiple images given:Waittime between (default:0.0).
  9. -t:Only animation or scrolling: stop after thistime.
  10. -c:OnlyAnimationor scrolling: number of runs through a full cycle.
  11. -C :Clearscreen before showing image.
  12. -F :Print filename before showing picture.
  13. -v :Print version andexit.
  14. If both -c and-t are given, whatever comes first stops.
  15. If both -wand-t are given for some animation/scroll,-t takes precedence
  16. ubuntu@snaptesting:~$

這里提到當(dāng)前我們終端模擬器的縮放級別,即分辨率為:80 × 48。

讓我們縮小一點,并最大化 GNOME 終端窗口。

  1. -gx:Output pixel geometry.Defaultfrom terminal 635x428

這是一個更好的解決方案,但我?guī)缀蹩床坏阶址?,因為他們太小了。讓我們調(diào)用前面的命令再次顯示這輛車。

圖片.png-904.9kB

你所看到的是調(diào)整后的圖像(1080p)。雖然它是用彩色文本字符顯示的,但看起來依舊很棒。

接下來呢?timg 其實也可以播放 gif 動畫哦!

  1. $ wget https://m.popkey.co/9b7141/QbAV_f-maxage-0.gif -O JonahHillAmazed.gif$ timg JonahHillAmazed.gif

你可以試著安裝 timg 來體驗 gif 動畫。要是不想自己動手,可以在 asciinema 上查看相關(guān)記錄 (如果視頻看上去起伏不定的,請重新運行它)。

謝謝閱讀!

譯者簡介:

經(jīng)?;燠E于 snapcraft.io,對 Ubuntu Core、Snaps 和 Snapcraft 有著濃厚的興趣,并致力于將這些還在快速發(fā)展的新技術(shù)通過翻譯或原創(chuàng)的方式介紹到中文世界。有興趣的小伙伴也可以關(guān)注譯者個人公眾號: Snapcraft

via:https://blog.simos.info/how-to-create-a-snap-for-timg-with-snapcraft-on-ubuntu/

作者:Mi blog lah! 譯者:Snapcrafter 校對:wxy

本文由 LCTT 原創(chuàng)編譯,Linux中國 榮譽推出


新聞標(biāo)題:詳解Ubuntusnap包的制作過程
網(wǎng)頁地址:http://m.5511xx.com/article/dhcespp.html