How does erlang -import work? - erlang

I'm trying to write some app using mochiweb. App tree:
tree
.
├── aniaggregator2.iml
├── deps
│   └── mochiweb
│   ├── CHANGES.md
│   ├── doc
│   ├── ebin
│   │   ├── ....
│   │   ├── mochiweb_html.beam
│   │   ├── ....
│   ├── examples
│   │   ├── ......
│   ├── include
│   │   └── ......
│   ├── LICENSE
│   ├── Makefile
│   ├── README
│   ├── rebar
│   ├── rebar.config
│   ├── scripts
│   │   ├── entities.erl
│   │   └── new_mochiweb.erl
│   ├── src
│   │   ├── .....
│   │   ├── mochiweb_html.erl
│   │   ├── .....
│   ├── support
│   │   ├── .....
│   └── test
│   .....
├── ebin
│   ├── aniaggregator.app
│   ├── aniaggregator_app.beam
│   ├── aniaggregator.beam
│   ├── aniaggregator_deps.beam
│   ├── aniaggregator_sup.beam
│   └── aniaggregator_web.beam
├── LICENSE
├── Makefile
├── priv
│   └── www
│   └── index.html
├── README.md
├── rebar
├── rebar.config
├── src
│   ├── .....
│   └── parsers
│   ├── default_parser.beam
│   ├── default_parser.erl
│   ├── default_parser_tests.beam
│   ├── default_parser_tests.erl
│   └── erl_crash.dump
└── start-dev.sh
default_parser.erl looks like
-module(default_parser).
-author("psih").
-import(mochiweb_html, [parse/1]).
%% API
-export([parse_from_url/1, fetch_names_from_animelist/0]).
parse_from_url(Url) ->
{ok, {Status, Headers, Body}} = httpc:request(Url),
Status.
fetch_names_from_smth() ->
Url = "some_url",
{ok, {Status, Headers, Body}} = httpc:request(Url),
{String, Attributes, Other} = parse(Body),
Attributes.
default_parser_tests.erl looks like:
-module(default_parser_tests).
-include_lib("eunit/include/eunit.hrl").
start() ->
application:start(inets),
ok.
stop(_) ->
application:stop(inets),
ok.
do_some_testing(_) ->
erlang:display(default_parser:fetch_names_from_smth()),
[?_assert(true =:= true)].
do_some_test_() ->
{"Performs some default parsing stuff!",
{setup,
fun start/0,
fun stop/1,
fun do_some_testing/1
}
}.
output of !erlc default_parser.erl && erlc default_parser_tests.erl && erl -pa -noshell -eval "eunit:test(default_parser)" -s init stop is something like:
*** instantiation of subtests failed ***
::error:undef
in function mochiweb_html1:parse/1
called as parse(....)
in call from default_parser:fetch_names_from_smth/0
in call from default_parser_tests:do_some_testing/1
=======================================================
Failed: 0. Skipped: 0. Passed: 0.
One or more tests were cancelled.
So, my question is - how can I import modules from mochiweb* to my particular app? also I will appreciate any links to documentation/articles about erlang's module's discovering algorithms.

The usage in erlang is not to import any function. As there is no link phase during compilation, and as the call is solved at run time it is useless. By the way import does not import anything but simply shorten the writing of the code. The import directive -import(mochiweb_html1, [parse/1]). will simply replace any call to parse(Foo) by mochiweb_html1:parse(Foo) before compilation , nothing more is done, nothing is checked. the function may not exist at the time you compile the code, it is not an issue.
So everything is a matter of code path and library path at run time. You could have a look to the excellent site learnyousomeerlang particularly to these chapter: modules and if you have already a good knowledge of OTP library-applications

Related

ROS2 colcon build fails - can't copy: doesn't exist or not a regular file

I have a ROS2 package which is failing to build. I'm following the ROS2 "Setting Up a Robot Simulation (Webots)" tutorial, and when I got to section 6 "Modify the setup.py file", I changed my file like so:
from setuptools import setup
from glob import glob
package_name = 'ad_boxtra'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
('share/' + package_name, glob('launch/*.py')), # include all launch files
('share/' + package_name, glob('resource/*.urdf')), # include all webots-related resource files
('share/' + package_name + '/webots_simulation/worlds', # include desired webots worlds
['worlds/adboxtra_2022_simplified.wbt']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='redacted',
maintainer_email='redacted',
description="redacted",
license='MIT License',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'my_robot_driver = ad_boxtra.my_robot_driver:main',
],
},
)
I then ran colcon build with debug info from the root of my workspace and got:
user#host:~/my_ws $ colcon build --packages-select ad_boxtra --event-handlers console_direct+
Starting >>> ad_boxtra
running egg_info
writing ../../build/ad_boxtra/ad_boxtra.egg-info/PKG-INFO
writing dependency_links to ../../build/ad_boxtra/ad_boxtra.egg-info/dependency_links.txt
writing entry points to ../../build/ad_boxtra/ad_boxtra.egg-info/entry_points.txt
writing requirements to ../../build/ad_boxtra/ad_boxtra.egg-info/requires.txt
writing top-level names to ../../build/ad_boxtra/ad_boxtra.egg-info/top_level.txt
reading manifest file '../../build/ad_boxtra/ad_boxtra.egg-info/SOURCES.txt'
writing manifest file '../../build/ad_boxtra/ad_boxtra.egg-info/SOURCES.txt'
running build
running build_py
running install
running install_lib
running install_data
error: can't copy 'adboxtra_2022_simplified.wbt': doesn't exist or not a regular file
--- stderr: ad_boxtra
error: can't copy 'adboxtra_2022_simplified.wbt': doesn't exist or not a regular file
---
Failed <<< ad_boxtra [0.63s, exited with code 1]
Summary: 0 packages finished [0.70s]
1 package failed: ad_boxtra
1 package had stderr output: ad_boxtra
---
Failed <<< ad_boxtra [0.62s, exited with code 1]
Summary: 0 packages finished [0.71s]
1 package failed: ad_boxtra
1 package had stderr output: ad_boxtra
Why is this colcon build failing? If it helps, here's my directory tree:
AdBoxtra
├── ad_boxtra
│   ├── __init__.py
│   └── my_robot_driver.py
├── launch
│   └── my_robot_webots.launch.py
├── LICENSE
├── package.xml
├── README.md
├── resource
│   ├── ad_boxtra
│   └── my_robot.urdf
├── setup.cfg
├── setup.py
├── test
│   ├── test_copyright.py
│   ├── test_flake8.py
│   └── test_pep257.py
├── urdf
└── webots_simulation
├── controllers
│   └── arm_sweep
│      └── arm_sweep.py
├── libraries
├── plugins
│   ├── physics
│   ├── remote_controls
│   └── robot_windows
├── protos
└── worlds
└── adboxtra_2022_simplified.wbt
27 directories, 63 files
I did observe that in the install/ directory of my workspace, it seems that share/ad_boxtra/webots_simulation/worlds/ exists, but there's no .wbt files inside it. I don't know why this is though:
$ tree ~/rover_ws/install/ad_boxtra/
/home/redacted/rover_ws/install/ad_boxtra/
├── lib
│   ├── ad_boxtra
│   │   ├── conversationalist
│   │   ├── drive_control_serial
│   │   └── turtle_ad_boxtra
│   └── python3.8
│   └── site-packages
│   ├── ad_boxtra
│   │   ├── conversationalist.py
│   │   ├── drive_control_serial.py
│   │   ├── __init__.py
│   │   ├── my_robot_driver.py
│   │   ├── __pycache__
│   │   │   ├── conversationalist.cpython-38.pyc
│   │   │   ├── drive_control_serial.cpython-38.pyc
│   │   │   ├── __init__.cpython-38.pyc
│   │   │   ├── my_robot_driver.cpython-38.pyc
│   │   │   └── turtle_ad_boxtra.cpython-38.pyc
│   │   └── turtle_ad_boxtra.py
│   └── ad_boxtra-0.0.0-py3.8.egg-info
│   ├── dependency_links.txt
│   ├── entry_points.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   ├── top_level.txt
│   └── zip-safe
└── share
├── ad_boxtra
│   ├── drive_control.launch.py
│   ├── hook
│   │   ├── ament_prefix_path.dsv
│   │   ├── ament_prefix_path.ps1
│   │   ├── ament_prefix_path.sh
│   │   ├── pythonpath.dsv
│   │   ├── pythonpath.ps1
│   │   └── pythonpath.sh
│   ├── my_robot.urdf
│   ├── my_robot_webots.launch.py
│   ├── package.bash
│   ├── package.dsv
│   ├── package.ps1
│   ├── package.sh
│   ├── package.xml
│   ├── package.zsh
│   ├── __pycache__
│   │   └── drive_control.launch.cpython-38.pyc
│   └── webots_simulation
│   └── worlds
├── ament_index
│   └── resource_index
│   └── packages
│   └── ad_boxtra
└── colcon-core
└── packages
└── ad_boxtra
18 directories, 38 files
From the Python documentation:
data_files specifies a sequence of (directory, files) pairs in the following way...
You wrote
('share/' + package_name + '/webots_simulation/worlds',['worlds/adboxtra_2022_simplified.wbt']), but it should be ('share/' + package_name + '/webots_simulation/worlds',['adboxtra_2022_simplified.wbt']).
You could check the build folder too. Just delete build and install and try again. It could be that.

How to import luarocks local modules

I have installed cjson using Lua Rocks locally.
luarocks install --local lua-cjson
But I cannot access the module.
> require('cjson')
stdin:1: module 'cjson' not found:
Packages were installed in to ~/.luarocks. Following is the file structure
.
├── bin
│   ├── json2lua
│   └── lua2json
├── lib
│   ├── lua
│   │   └── 5.4
│   │   └── cjson.so
│   └── luarocks
│   └── rocks-5.4
│   ├── lua-cjson
│   │   └── 2.1.0.6-1
│   │   ├── bin
│   │   │   ├── json2lua
│   │   │   └── lua2json
│   │   ├── lua-cjson-2.1.0.6-1.rockspec
│   │   ├── rock_manifest
│   │   └── tests
│   │   ├── agentzh.t
│   │   ├── bench.lua
│   │   ├── example1.json
│   │   ├── example2.json
│   │   ├── example3.json
│   │   ├── example4.json
│   │   ├── example5.json
│   │   ├── genutf8.pl
│   │   ├── numbers.json
│   │   ├── octets-escaped.dat
│   │   ├── README
│   │   ├── rfc-example1.json
│   │   ├── rfc-example2.json
│   │   ├── test.lua
│   │   ├── TestLua.pm
│   │   └── types.json
│   └── manifest
└── share
└── lua
└── 5.4
├── cjson
│   └── util.lua
├── json2lua.lua
└── lua2json.lua
What are the environment variables that should be in place for Lua to be able to find the packages?
luarocks path prints everything that should be added
➜ ~ luarocks path
export LUA_PATH='/usr/share/lua/5.4/?.lua;/usr/share/lua/5.4/?/init.lua;/usr/lib/lua/5.4/?.lua;/usr/lib/lua/5.4/?/init.lua;./?.lua;./?/init.lua;/home/s1n7ax/.luarocks/share/lua/5.4/?.lua;/home/s1n7ax/.luarocks/share/lua/5.4/?/init.lua'
export LUA_CPATH='/usr/lib/lua/5.4/?.so;/usr/lib/lua/5.4/loadall.so;./?.so;/home/s1n7ax/.luarocks/lib/lua/5.4/?.so'
export PATH='/home/s1n7ax/.luarocks/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/usr/lib/jvm/default/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl:/home/s1n7ax/.s1n7ax/bin:/home/s1n7ax/.yarn/bin:/home/s1n7ax/.local/bin'
It just prints them NOT run them. So that needs to be ran. I added following line to init script to add the path at system boot
eval $(luarocks path)

How can I import my old tweak written and compiled on iOS device with Theos into Xcode?

I have some old tweak which doesn't work on iOS 8.4 and I wanted to debug it and possibly fix it. I am in no way good with writing tweaks, this was something I did for a friend and me and people used it since I published it on BigBoss.
Anyway at the time, I did not have OS X device and I written everything on iPhone using vi over ssh and then I compiled it on iPhone with Theos.
Now I have OS X device and I wanted to import this into Xcode. Is this even possible? If so how can I do this in most easiest way? I already have iOSOpenDev. Here is folder tree:
├── Makefile
├── NoIconLabels.plist
├── Tweak.xm
├── Tweak.xm.bak
├── _
│   ├── DEBIAN
│   │   └── control
│   └── Library
│   ├── MobileSubstrate
│   │   └── DynamicLibraries
│   │   ├── NoIconLabels.dylib
│   │   └── NoIconLabels.plist
│   ├── PreferenceBundles
│   │   └── NoIconLabels.bundle
│   │   ├── Info.plist
│   │   ├── NoIconLabels
│   │   └── NoIconLabels.plist
│   └── PreferenceLoader
│   └── Preferences
│   └── NoIconLabels.plist
├── control
├── me.itn.noiconlabels_0.1-2_iphoneos-arm.deb
├── me.itn.noiconlabels_0.1-3_iphoneos-arm.deb
├── me.itn.noiconlabels_0.1-4_iphoneos-arm.deb
└── noiconlabels
├── Makefile
├── NoIconLabels.mm
├── Resources
│   ├── Info.plist
│   └── NoIconLabels.plist
├── entry.plist
└── obj
├── NoIconLabels.bundle
│   ├── Info.plist
│   ├── NoIconLabels
│   └── NoIconLabels.plist
├── NoIconLabels.mm.46c9b2c9.o
└── NoIconLabels.mm.ea49af4e.o
Thanks!

Error when start erlang application (relx) release

When I try to start erlang release prepared by relx it crashes with reason:
{"init terminating in do_boot",{badarg,[{erl_prim_loader,check_file_result,3,[]},{init,get_boot,1,[]},{init,get_boot,2,[]},{init,do_boot,3,[]}]}}
Directory listing:
├── bin
├── lib
│   ├── asn1-2.0.2
│   │   ├── ebin
│   │   ├── priv
│   │   └── src
│   ├── bear-0.8.1
│   │   ├── ebin
│   │   └── src
│   ├── cache-1.0.0
│   │   ├── ebin
│   │   ├── priv
│   │   └── src
│   ├── cberl-1
│   │   ├── c_src
│   │   ├── ebin
│   │   ├── include
│   │   ├── priv
│   │   └── src
│   ├── compiler-4.9.2
│   │   ├── ebin
│   │   └── src
│   ├── cowboy-0.8.6
│   │   ├── ebin
│   │   └── src
....................
│   ├── crypto-3.0
│   │   ├── ebin
│   │   ├── priv
│   │   │   └── lib
│   │   └── src
└── releases
└── 0.5.2
All libraries on it's own places.
I tried to start application with the command erl -env ERL_LIBS ../libs -erlconto and it successfully started.
It seems, that problem in boot file, but i don't know how to approach this problem.
Any tips or way to solve, please.
Tnx!
I added
{extended_start_script, true}.
to relx.conf an application now work!
Tnx.
This usually happens when OTP application's .app file is missing a required dependency. relx uses the contents of .app to resolve dependencies to go into the release.
Make sure all of your release applications include, at least, kernel and stdlib in the applications tuple in .app.
Here is an example .app file listing kernel, stdlib, and elk as dependencies:
{application, prop, [
{description, "Prop Template Generator"},
{vsn, "0.0.1"},
{registered, []},
{applications, [kernel, stdlib, elk]},
{modules, [prop, prop_otp, prop_generator]},
{env, []}
]}.

Mustache conflict at release build

I use wooga's fork of mustache.erl (https://github.com/wooga/mustache.erl) in my project. And when I try to create release I get an error because rebar uses this mustache.beam file instead of a file included in its source.
What should I do to resolve this conflict?
The reason is not in rebar but is that some app in release tool search path contains the mustache.erl file as you release does.
Usually rel.config file contains {lib_dirs, ["../.."]} that force release tool search libs (apps) up your release directory. See example below. To fix it, move your release directory dipper (for example -> you_release/you_release) or move app that contains the same file (mustache.erl) to other place.
├── your_release_directory
│   ├── deps
│   │   └── mustache
│   │   ├── ebin
│   │   │   ├── mustache.app
│   │   │   └── mustache.beam
│   │   └── src
│   │   ├── mustache.app.src
│   │   └── mustache.erl
│   ├── ebin
│   │   └── mustache.beam
├── rebar (or any other app contains mustache.erl)
│   ├── ebin
│   │   └── mustache.beam
│   ├── src
│   │   └── mustache.erl

Resources