Posted
Filed under Computer/package
Universial Package Manager snap testing

1. Install snap package
2. Install snapcraft (package builder)
$ sudo snap install snapcraft --classic

3. Simple Python script packaging
$ mkdir ~/work
- make a package(test-app) directory
$ mkdir ~/work/test-app

4. make a python script package
$ mkdir ~/work/test-app/myapp
$ mkdir ~/work/test-app/myapp/lib
$ vi ~/work/test-app/myapp/lib/TM.py
------------------------------------------------------------
from datetime import datetime
def now():
    return datetime.now().strftime('%s')
------------------------------------------------------------
$ touch ~/work/test-app/myapp/lib/__init__.py
$ vi ~/work/test-app/myapp/test_run
------------------------------------------------------------
#!/usr/bin/env python3
from lib import TM
def main():
    print('Hello, I am main()')
    aa=TM.now()
    input("now second is:{}".format(aa))
if __name__ == '__main__':
    main()
------------------------------------------------------------

5. make a snap configuration
$ mkdir ~/work/test-app/snap
$ vi ~/work/test-app/snap/snapcraft.yaml
------------------------------------------------------------
name:           test-app # package name
version:        '1.1'        # package version
summary:        Example python snap app
description: |
    Example python snap app description
base: core20      # Common Base OS ( 18: ubuntu 18.04, 20: ubuntu 20.04, 22: ubuntu 22.04 )
grade:          stable
confinement:    devmode
apps:
  test-app:  #Package name
    command:    myapp/test_run  #running command path and filename (/snap/bin/<package name> will running the command line path file)
    ## If need special path for shell script
    #command: sh $SNAP/opt/foo/bin/foo.sh
parts:
  test-app:
    source: . #my local directory is source
    ## When copy a file(shell/python/built binary file) to somewhere
    plugin:     dump 
    organize:
          src/myapp: .     # start with "src/" and my files are in the src directory. (copy the myapp directory)
    ## When use python setup.py to install application then use python plugin
    #plugin:     python
    #python-version: python3
------------------------------------------------------------

6. build 
$ cd ~/work/test-app
$ snapcraft

7. Install
$ snap install test-app_1.1_amd64.snap --devmode

8. check 
$ snap list |grep test-app
test-app             1.1                         x1     -                -            devmode
$ df -h
~~~
/dev/loop3                 128K  128K     0 100% /snap/test-app/x1
$ ls -l /snap/bin/test-app 
lrwxrwxrwx 1 root root 13 Aug  1 14:18 /snap/bin/test-app -> /usr/bin/snap
$ /snap/bin/test-app 
Hello, I am main()
now second is:1659388777

ETC & Tools)
$ cat cleanup_multipath_snapcraft_built_garbage.sh 
-------------------------------------------------------------------------------------------------------------
#!/bin/bash -x
## snapcraft-created multipass VMs
for vm in $(multipass list | awk '{print $1}' | grep "^snapcraft-"); do
    multipass delete $vm --purge
done
## snapcraft-created LXD containers
#for c in $(lxc list | awk '{print $2}' | grep "^snapcraft-" | grep -v "snapcraft-dev"); do
#    lxc delete $c --force
#done
-------------------------------------------------------------------------------------------------------------

$ cat remove_disabled_snap.sh 
-------------------------------------------------------------------------------------------------------------
#!/bin/bash
set -eu
LANG=en_US.UTF-8 snap list --all | awk '/disabled/{print $1, $3}' |
    while read snapname revision; do
        snap remove "$snapname" --revision="$revision"
    done
-------------------------------------------------------------------------------------------------------------

Remove Package
$ snap remove <package name>
other commands: https://www.cyberithub.com/36-popular-snap-command-examples-in-linux/


Few other snap configuration files example:
a) automatically use bin directory in local directory (Some updated code)
    original : https://github.com/gocarlos/python-ubuntu-snap-app-example
$ ls                    
bin  LICENSE  myapp_module  mygreatapp_1.0_amd64.snap  README.md  requirements.txt  snap
$ cat requirements.txt 
numpy
$ cat bin/my_great_app 
#!/usr/bin/env python3
from myapp_module import my_great_app
"""
    my application.
"""
def main():
    """run the application"""
    print('I\'m inside main')
    my_great_app.tata()
    input("Press enter to exit ;)")
    input()
if __name__ == '__main__':
    main()
$ ls myapp_module/
__init__.py  my_great_app.py
$ cat myapp_module/my_great_app.py 
#!/usr/bin/env python3
def tata():
    print ('you could do great stuff inside this function.')
    
def fun(x):
    return x + 1
$ cat snap/snapcraft.yaml 
name:           mygreatapp
version:        '1.0'
summary:        Example python snap app
description: |
    Example python snap app
base: core18
grade:          stable
confinement:    devmode
apps:
  mygreatapp:
    command:    my_great_app
parts:
  mygreatapp:
    plugin:     python
    python-version: python3
    source: .

b)  Automatically build and package from remote source file.
$ cat snap/snapcraft.yaml 
name: hello
base: core18
version: '2.10'
summary: GNU Hello, the "hello world" snap
description: |
  GNU hello prints a friendly greeting.
grade: devel
confinement: devmode
parts:
  gnu-hello:
    source: http://ftp.gnu.org/gnu/hello/hello-2.10.tar.gz
    plugin: autotools
2022/08/02 06:32 2022/08/02 06:32
[로그인][오픈아이디란?]