Commit Graph

94 Commits

Author SHA1 Message Date
Isidro Arias
4484397a0c examples: take lines out of loop (refactor)
since they will only be executed once.

Also, an error is raised if a connection is not found
2024-08-06 10:17:13 +00:00
Isidro Arias
f141f4bbe7 examples: fix gnome.org link 2024-08-06 10:17:13 +00:00
Isidro Arias
75350b3e76 examples: fix print parenthesis place in get_ips.py
Fixes: 797d9c4403 ('python: make dbus, gi examples, and debug-helper.py python3 ready')
2024-08-06 10:17:13 +00:00
Jan Vaclav
47f01c805d python: reformat with black-24.1.1-1.fc39.noarch 2024-03-01 07:51:21 +00:00
Thomas Haller
28a1afca63 python: reformat with black-23.7.0-2.fc39.noarch
The base image for the "check-tree" test got bumped to Fedora 39.  This
brings a new python-black version (23.7.0 vs. 22.8.0) and requires
reformatting.

Maybe we should stick to 22.8.0, via `pip install`. But it seems better
to just follow the latest black version (the one from current Fedora).

So do the reformatting instead.

https://black.readthedocs.io/en/stable/change_log.html#id38
2023-12-06 11:56:11 +01:00
Thomas Haller
69d7249244 examples: support older libnm without NM.SettingOvsOtherConfig in "ovs-external-ids.py"
This example script is used by our NM-ci. Make it work also with libnm
versions that don't yet support NM.SettingOvsOtherConfig.
2023-02-16 10:17:48 +01:00
Thomas Haller
004ffb91cf examples: rework "python/gi/show-wifi-networks.py" example
- only printing the scan list is not gonna cut it. It's usually stale,
  and we need to request a new scan.

- don't hard-code the GEnum and GFlags values that we understand. We
  have libnm, which provides us some meta information about the data.
  Use it.

- Some code cleanup.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1531
2023-02-13 10:32:45 +01:00
Thomas Haller
a259303e1d ovs: add support for "other_config" settings
See `man ovs-vswitchd.conf.db` for documentation of "other_config" keys.

https://bugzilla.redhat.com/show_bug.cgi?id=2151455
2023-01-11 21:49:36 +01:00
Thomas Haller
a467f55bef examples: add python example for reapply 2022-12-14 17:31:17 +01:00
Thomas Haller
135bbcc0e4 examples: fix handling "--last" argument in "examples/python/gi/checkpoint.py"
Fixes: 3ce3ed4c92 ('examples: improve finding last checkpoint in "checkpoint.py"')
2022-12-09 10:16:36 +01:00
Thomas Haller
37ccc08abf examples: fix code formatting in "gmaincontext.py" 2022-10-25 16:47:48 +02:00
Thomas Haller
c34c8fdb82 examples: avoid unreachable code in "gmaincontext.py"
lgtm.com warns about this. Avoid it.
2022-10-25 12:36:43 +02:00
Thomas Haller
85d5fb210b examples: drop unused import from "gmaincontext.py" example
Complained by lgtm.com.
2022-10-25 12:11:19 +02:00
Thomas Haller
9b80860ff4 examples: avoid lgtm warning about calling traceback.format_exception()
lgtm.com says:

  Call to function format_exception with too few arguments; should be no
  fewer than 3.
2022-10-25 12:01:54 +02:00
Thomas Haller
88724ff169 libnm: add nm_client_wait_shutdown() function for cleaning up NMClient
Add a fire-and-forget function to wait for shutdown to be complete.

It's not entirely trivial to ensure all resources of NMClient are
cleaned up. That matters only if NMClient uses a temporary GMainContext
that the user wants to release while the application continues. For
example, to do some short-lived operations an a worker thread. It's
not trivial also because glib provides no convenient API to integrate
a GMainContext in another GMainContext. We have that code as
nm_utils_g_main_context_create_integrate_source(), so add a helper
function to allow the user to do this.

The function allows to omit the callback, in which case the caller
wouldn't know when shutdown is complete. That would still be useful
however, when integrating the client's context into the caller's
context, so that the client's context gets automatically iterated
until completion.

The following test script will run out of file descriptors,
when wait_shutdown() is not used:

   #!/bin/python

   import gi

   gi.require_version("NM", "1.0")
   from gi.repository import NM, GLib

   for i in range(1200):
       print(f">>>{i}")

       ctx = GLib.MainContext()
       ctx.push_thread_default()
       nmc = NM.Client.new()
       ctx.pop_thread_default()

       def cb(unused, result, i):
           try:
               NM.Client.wait_shutdown_finish(result)
           except Exception:
               # cannot happen
               assert False
           else:
               print(f">>>>> {i} complete")

       nmc.wait_shutdown(True, None, cb, i)

       while GLib.MainContext.default().iteration(False):
           pass
2022-10-14 17:48:24 +02:00
Thomas Haller
9511ac7d20 examples: fix tracking destruction in destroy_nmc()
While iterating the context (once), multiple sources can be dispatched.
So if we get a timeout and shortly after the weak-ref callback, then
we still need to honor the weak-ref.

In particular, because weak_ref.unref() is not safe to do after
the object was already destroyed. So we need to be correct about
tracking destruction.
2022-07-13 10:05:12 +02:00
Thomas Haller
b3181cfbee example: add python example for libnm, NMClient, GMainContext and async
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1290
2022-07-05 18:36:01 +02:00
Thomas Haller
3ce3ed4c92 examples: improve finding last checkpoint in "checkpoint.py"
This is a python example. We should do nice things, like
using max() for finding the maximum, instead of sorting.
2022-05-02 18:04:37 +02:00
Thomas Haller
4d70c9c4db examples: add "--last" argument to "examples/python/gi/checkpoint.py"
"examples/python/gi/checkpoint.py" is not only an example. It's also a
useful script for testing checkpoints.

Support a "--last" argument to specify the last checkpoint created.
Otherwise, when you are using this example from a test script, it
can be cumbersome to find the right checkpoint point.

Also, rename "client" and "nm_client" variables to "nmc". The purpose of
using the same variable name for the same thing is readability, but also
it works better when copy+paste snippets into the Python REPL.
2022-02-23 17:07:16 +01:00
Thomas Haller
95e6a0a6e2 examples/python: avoid Python2 "print" statement
Recent python-black (22.0) dropped support for Python 2 and thus fail
for those files. Make the examples Python3 compatible.
2022-02-14 17:02:34 +01:00
Thomas Haller
4521e2aa89 docs: update URL for latest online documentation 2021-09-24 14:41:35 +02:00
Thomas Haller
938f9b075f examples: rework nm-up-many.py for ratelimiting parallel activations
The previous implementation did some ratelimiting, namely how many parallel
ActivateConnection D-Bus calls are in fly. This way we are able to kick off
many parallel calls, but the activations themselves were not ratelimited.

Rework the code. Now there are two rate limits (that can be set via environment
variables):

  NUM_PARALLEL_STARTING
  NUM_PARALLEL_IN_PROGRESS

This allows more control about how much is happening in parallel. If we are
going to activate 1000 profiles, then it matters that we do things in parallel,
but not everything at the same time.
2021-06-22 09:47:44 +02:00
Thomas Haller
1b895d98c0 example: drop unused import from "nm-up-many.py"
lgtm.com complains about this. Avoid the warning.
2021-06-17 15:09:48 +02:00
Thomas Haller
be4b997e4f examples: add "examples/python/gi/nm-up-many.py"
It's an example for how to use libnm and asynchronous API.
But it's also a script I will use to test activating many
profiles in parallel.

Also add a test script that creates many veth interfaces and connection
profiles. So now you can do:

   sudo NUM_DEVS=100 contrib/scripts/test-create-many-device-setup.sh setup
   ./examples/python/gi/nm-up-many.py c-a{1..100}

and cleanup with

   nmcli connection down c-a{1..100}
   sudo contrib/scripts/test-create-many-device-setup.sh cleanup

Of course, be careful to do this on your production machine.
2021-06-11 22:48:41 +02:00
Thomas Haller
f63723f75e examples/trivial: fix black formatting of python code 2021-05-27 14:08:47 +02:00
Thomas Haller
fdc0285e68 examples: explicitly catch "Exception" in "ovs-external-ids.py"
lgtm.com warns about unqualified "except". This catches any "BaseException",
when we really only should handle "Exception" here.
2021-05-27 09:13:26 +02:00
Thomas Haller
07193dabd1 examples: fix printing applied connection in "ovs-external-ids.py"
lgtm.com also complained about "connection_after" being unused. This
hopefully works around that warning.
2021-05-27 08:56:20 +02:00
Thomas Haller
6c919dc897 examples: drop unused variable in "examples/python/gi/ovs-external-ids.py" 2021-05-27 08:53:35 +02:00
Jagadeesh Kotra
3df607f3c2 examples: use python3 compatible exception syntax
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/781
2021-03-16 07:55:12 +01:00
Thomas Haller
d446cde260 examples/python: some style update to avoid changes in black code formatting
Fedora 33's version of python-black is the authoritative version we
use for formatting -- note that formatting may look different depending
on the black version.

Recently, the package was updated to 20.8~b1-1.fc33, which treats
these lines different.

Work around that by adjusting the code so that both older and current
versions of python black behave the same.
2021-01-28 10:25:25 +01:00
Thomas Haller
977ea352a0 all: update deprecated SPDX license identifiers
These SPDX license identifiers are deprecated ([1]). Update them.

[1] https://spdx.org/licenses/

  sed \
     -e '1 s%^/\* SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+ \*/$%/* SPDX-License-Identifier: \1-or-later */%' \
     -e '1,2 s%^\(--\|#\|//\) SPDX-License-Identifier: \(GPL-2.0\|LGPL-2.1\)+$%\1 SPDX-License-Identifier: \2-or-later%' \
     -i \
     $(git grep -l SPDX-License-Identifier -- \
         ':(exclude)shared/c-*/' \
         ':(exclude)shared/n-*/' \
         ':(exclude)shared/systemd/src' \
         ':(exclude)src/systemd/src')
2021-01-05 09:46:21 +01:00
Thomas Haller
4e49c417eb examples: remove editor hint to mark source code as UTF-8
All our source code is UTF-8. We don't need to mark it as such.
2021-01-05 09:46:20 +01:00
Thomas Haller
c45041f1fc examples: extend "ovs-external-ids.py" to support nmcli device modify
The python example is (also) used to test the feature. That is because
currently nmcli does not yet have support for ovs.external-ids and this
API is only accessible via D-Bus (or a tool like this example).
2020-11-17 19:49:03 +01:00
Thomas Haller
7e39e23f64 examples: add "ovs-external-ids.py" example script
And example script for getting and setting OVS external-ids.

Since currently there is no nmcli support for these properties yet,
the script becomes more interesting.

This "example" is rather long, and it showcases less the usage of
libnm (which is rather trivial, with respect to configuring
NMSettingOvsExternalIDs). Instead, it aims to provide a useful
command line tool for debugging. Hence, it's mostly concerned with
an elaborate command line syntax and useful print output.
2020-11-09 17:53:15 +01:00
Thomas Haller
a9e6774670 example: add python gi example for keyfile handling 2020-10-28 14:30:49 +01:00
Thomas Haller
1acd64b7a2 examples: run python black on "examples/python/gi/nm-wg-set"
black by default only considers files that have a ".py" extension.
2020-07-02 17:37:12 +02:00
Sayed Shah
be822b52e6 all: reformat python files with python black
Part of !537.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/537
2020-06-15 16:40:38 +02:00
Thomas Haller
8ad448444a examples: avoid "x == None" checks in python examples
lgmt.com says "Testing for None should use the is operator".
2020-05-07 13:58:15 +02:00
Thomas Haller
c7d0a86050 examples: avoid "except" for BaseException in examples
lgtm.com flags this as "Except block directly handles BaseException".
2020-05-07 13:58:14 +02:00
Thomas Haller
41a7a26259 examples: remove unused imports from python examples 2020-05-07 13:58:13 +02:00
Thomas Haller
a5614dc469 examples: remove unused assignment in "examples/python/gi/wifi-p2p.py"
lgtm.com warns about this.
2020-05-07 13:58:12 +02:00
Beniamino Galvani
2c4d19c1dc examples: add Wi-Fi P2P example
Add a python example using GObject introspection that shows how to
scan for Wi-Fi P2P peers and connect to one of them.
2020-04-27 15:54:28 +02:00
Beniamino Galvani
e397582cca libnm: export interface flags
Add libnm support for the new InterfaceFlags property of NMDevice.
2019-11-22 10:18:26 +01:00
Thomas Haller
3b69f02164 all: unify format of our Copyright source code comments
```bash

readarray -d '' FILES < <(
  git ls-files -z \
    ':(exclude)po' \
    ':(exclude)shared/c-rbtree' \
    ':(exclude)shared/c-list' \
    ':(exclude)shared/c-siphash' \
    ':(exclude)shared/c-stdaux' \
    ':(exclude)shared/n-acd' \
    ':(exclude)shared/n-dhcp4' \
    ':(exclude)src/systemd/src' \
    ':(exclude)shared/systemd/src' \
    ':(exclude)m4' \
    ':(exclude)COPYING*'
  )

sed \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[-–] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C1pyright#\5 - \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) *[,] *\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C2pyright#\5, \7#\9/' \
  -e 's/^\(--\|#\| \*\) *\(([cC]) *\)\?Copyright \+\(\(([cC])\) \+\)\?\(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/\1 C3pyright#\5#\7/' \
  -e 's/^Copyright \(\(20\|19\)[0-9][0-9]\) \+\([^ ].*\)$/C4pyright#\1#\3/' \
  -i \
  "${FILES[@]}"

echo ">>> untouched Copyright lines"
git grep Copyright "${FILES[@]}"

echo ">>> Copyright lines with unusual extra"
git grep '\<C[0-9]pyright#' "${FILES[@]}" | grep -i reserved

sed \
  -e 's/\<C[0-9]pyright#\([^#]*\)#\(.*\)$/Copyright (C) \1 \2/' \
  -i \
  "${FILES[@]}"

```

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/merge_requests/298
2019-10-02 17:03:52 +02:00
Thomas Haller
abff46cacf all: manually drop code comments with file description 2019-10-01 07:50:52 +02:00
Lubomir Rintel
24028a2246 all: SPDX header conversion
$ find * -type f |xargs perl contrib/scripts/spdx.pl
  $ git rm contrib/scripts/spdx.pl
2019-09-10 11:19:56 +02:00
Thomas Haller
041a952297 examples: improve usage/synposis for nm-update2.py and nm-add-connection2.py 2019-08-08 10:53:03 +02:00
Thomas Haller
815acb9996 example: print WireGuard parameters in nm-wg-set example script
(cherry picked from commit e966f4b272)
2019-07-31 10:44:38 +02:00
Thomas Haller
10353a996f examples: add examples/python/gi/nm-update2.py example script
This is useful for manually testing Update2() D-Bus API.
2019-07-25 22:02:00 +02:00
Thomas Haller
360d26e27c examples: add examples/python/gi/nm-add-connection2.py example script
This is useful for manually testing AddConnection2() D-Bus API.
2019-07-25 15:26:49 +02:00