Problems faced while creating snap of python code


#1

I wanted to create a snap of python code but unable to create it Below is my yaml file:

parts:
       cherryserver:
                       plugin: python
                      source: /home/mra/System/Webserver/
                      requirements: ./home/mra/System/Webserver/requirements.txt

I followed this tutorial:https://tutorials.ubuntu.com/tutorial/snap-a-python-application#2

And as done in the tutorial after adding parts they used
snapcraft prime
But I am getting error after this command. Like:

What am I missing in the yaml file?


#2

Can you please try snapcraft from the candidate channel?


#3

Hi,
How can I try with candidate channel?


#4
sudo snap refresh --channel=candidate snapcraft

#5

Hi @Lin-Buo-Ren,

This gives following error:

    error: cannot perform the following tasks:
   - Download snap "snapcraft" (2374) from channel "candidate" (invalid credentials)

#6

Try running snap login in the terminal, or relogin your Ubuntu One account in GNOME/Ubuntu Software


#7

Hi @Lin-Buo-Ren,
I installed the snapcraft from candidate channel still the problem as shown in question is not solved. Now I am completely following the tutorial and removed my yaml file.


#8

you seem to be on bionic. You might need to add base: core18 to your snapcraft.yaml if you want to build using a bionic base.


#10

@sergiusens Yes I am using Ubuntu 18.04.1(Bionic beaver), where I should add the base: core18 in parts?


#11

I would suggest building the snap in a Ubuntu 16.04 LXD container, it will (probably) avoid all problems mentioned above: https://docs.snapcraft.io/build-on-lxd/4157


#12

Hi @sergiusens,
Now the tutorial snap built and executed successfully. But will you please explain how this worked? After using the base: core18 in snap.yaml file, and executing it with snapcraft in terminal, it downloaded VM, and core 16 and needed dependecies like python, pip, bin-utils,etc. So does this mean that the snap is built in a VM?

I will try same with my python app.


#13

I tried with my python script. Now my yaml file looks like this:

 name: cherrypyserver
 version: '0.1'
 summary: This is a test for cherry py webserver 
description: |
     This is a test for cherryPy webserver which hosts some html

grade: stable 
confinement: devmode 
base: core18

parts:
  cherryserver:
    plugin: python
    source: /home/mra/System/Webserver/setup.py 
    requirements: [/home/mra/System/Webserver/requirements.txt] # this needs to be array (pointed by error) is this correct?

But now the snapcraft gives error:

Failed to pull source: unable to determine source type of '/home/mra/System/Webserver/setup.py'.
Check that the URL is correct or consider specifying `source-type` for this part. See `snapcraft help sources` for more information.

How it was not able to pull the source?

My setup.py is:

#!/usr/bin/env python
# coding=utf-8

from setuptools import setup


package_name = 'cherryserver'
filename = package_name + '.py'


def get_version():
    import ast

    with open(filename) as input_file:
        for line in input_file:
            if line.startswith('__version__'):
                return ast.parse(line).body[0].value.s


def get_long_description():
    try:
        with open('README.md', 'r') as f:
            return f.read()
    except IOError:
        return ''


setup(
    name=package_name,
    version=get_version(),
    author='MVA',
    author_email='my emailid',
    description='Simple cherrypy server test',
    long_description=get_long_description(),
    py_modules=[package_name],
    entry_points={
        'console_scripts': [
            'cherryserver = cherryserver:main'
        ]
},

)

Please guide me.


#14

Try:

  cherryserver:
    plugin: python
    source: /home/mra/System/Webserver
    # source-type: local

You should also run the build step-by-step and verify the results under parts/_part_name_/*, by using the snapcraft pull/build/stage/prime _part_name_ and snapcraft clean --step=pull/build/stage/prime _part_name_ commands


#15

hi @Lin-Buo-Ren,

I tried with snapcraft pull /build/stage/prime _part_name_ but now the error is :
An error occurred when trying to execute 'sudo -i snapcraft pull' with 'multipass': returned exit code 1.

The above error is when I keep source-type: local and if I keep it commented then the error is:
Failed to pull source: unable to determine source type of '/home/mra/System/Webserver'. Check that the URL is correct or consider specifyingsource-typefor this part. An error occurred when trying to execute 'sudo -i snapcraft pull' with 'multipass': returned exit code 2.


#16

There should be a specific error message above this one. Try running snapcraft with the --debug switch so it will break into a in-build-environment shell whenever an error occurred to check for problems


#17

@Lin-Buo-Ren By keeping the source-type:local below is the screenshot of terminal.

By looking at terminal, I think that I am accessing the VM as its terminal is seen.


#18

Please check out (or paste) the program traceback to the internet, you may use the pastebinit command provided by the pastebinit package:

https://manpages.ubuntu.com/manpages/bionic/man1/pastebinit.1.html

NOTE: May contain personal information, review the content before publishing it.


#19

@Lin-Buo-Ren I checked the traceback, it is :

Traceback (most recent call last):
  File "/snap/snapcraft/2374/bin/snapcraft", line 11, in <module>
    load_entry_point('snapcraft==3.0.1', 'console_scripts', 'snapcraft')()
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/cli/__main__.py", line 81, in run
    run_snapcraft(prog_name="snapcraft")
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/core.py", line 722, in __call__
    return self.main(*args, **kwargs)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/core.py", line 697, in main
    rv = self.invoke(ctx)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/cli/lifecycle.py", line 169, in pull
    _execute(steps.PULL, parts, **kwargs)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/cli/lifecycle.py", line 94, in _execute
    lifecycle.execute(step, project_config, parts)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/lifecycle/_runner.py", line 80, in execute
    executor.run(step, part_names)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/lifecycle/_runner.py", line 134, in run
    self._handle_step(part_names, part, step, current_step, cli_config)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/lifecycle/_runner.py", line 148, in _handle_step
    getattr(self, "_run_{}".format(current_step.name))(part)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/lifecycle/_runner.py", line 190, in _run_pull
    self._run_step(step=steps.PULL, part=part, progress="Pulling")
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/lifecycle/_runner.py", line 267, in _run_step
    getattr(part, step.name)()
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 417, in pull
    self._runner.pull()
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/pluginhandler/_runner.py", line 70, in pull
    "override-pull", self._override_pull_scriptlet, self._sourcedir
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/pluginhandler/_runner.py", line 137, in _run_scriptlet
    scriptlet_name, function_call.strip()
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/pluginhandler/_runner.py", line 193, in _handle_builtin_function
    function(**function_args)
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/pluginhandler/__init__.py", line 436, in _do_pull
    self.source_handler.pull()
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/internal/sources/_local.py", line 40, in pull
    copy_function=self.copy_function,
  File "/snap/snapcraft/2374/lib/python3.5/site-packages/snapcraft/file_utils.py", line 200, in link_or_copy_tree
    raise NotADirectoryError("{!r} is not a directory".format(source_tree))
NotADirectoryError: '/home/mra/System/Webserver' is not a directory

This error is certain as this error is occuring in the VM and not my machine and the directory for source is not in the VM.


#20

This is due to the fact that this directory isn’t in the snapcraft build environment(e.g. the VM). Snapcraft should’ve mapped the directory containing your snapcraft YAML file (e.g. The working directory you run snapcraft in) into the VM, but not including your files reside in /home/mra/System/Webserver

You may try bind-mount this directory under snap/local/Webserver and use this path as the source key’s value.


#21

Hi @Lin-Buo-Ren,
Still the same error.

NotADirectoryError: ‘/home/mra/snap/local/webserver’ is not a directory