Commit Graph

244 Commits

Author SHA1 Message Date
Petr Viktorin
06a678c159 Avoid builtins that were removed in Python 3
- `file` was removed in favor of `open`. Switch to the new spelling.
- `buffer` was removed in favor of a buffer protocol (and memoryview),
  and `reload` was moved to importlib.
  Both are used in py2-only blocks, so just placate PyLint.

https://fedorahosted.org/freeipa/ticket/5623

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
2016-01-20 11:59:21 +01:00
Petr Viktorin
465dd9829c Don't index exceptions directly
In Python 3, exceptions don't behave as tuples of their arguments;
instead of e[1] it's necessary to use e.args[1].

https://fedorahosted.org/freeipa/ticket/5623

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
Reviewed-By: Martin Basti <mbasti@redhat.com>
2016-01-20 11:59:21 +01:00
Jan Cholasta
7e56b4bbd7 ipapython: remove default_encoding_utf8
Replace the "import default_encoding_utf8" in ipalib/cli.py with equivalent
Python code.

https://fedorahosted.org/freeipa/ticket/5596

Reviewed-By: Tomas Babej <tbabej@redhat.com>
2016-01-15 13:39:52 +01:00
Martin Basti
ded70b6c6e Enable pylint empty-docstring check
Enables check and fixes:
************* Module ipalib.session
ipalib/session.py:671: [C0112(empty-docstring), SessionAuthManager]
Empty class docstring)
ipalib/session.py:705: [C0112(empty-docstring),
SessionAuthManager.logout] Empty method docstring)
************* Module ipalib.cli
ipalib/cli.py:364: [C0112(empty-docstring), textui.print_entry] Empty
method docstring)

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-12-23 07:59:22 +01:00
Martin Basti
e4075b1fe2 Remove unused imports
This patch removes unused imports, alse pylint has been configured to
check unused imports.

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-12-23 07:59:22 +01:00
Petr Viktorin
53b60546e4 Package ipapython, ipalib, ipaplatform, ipatests for Python 3
Running make with PYTHON=/usr/bin/python3 will build/install the
bits for Python 3.

Executable scripts in ipatests have symlinks Python version suffixes
as per Fedora guidelines. Suffix-less names point to the Python 2 versions.

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-12-17 10:52:57 +01:00
Robert Kuska
01da4a8de3 Replace StandardError with Exception
StandardError was removed in Python3 and instead
Exception should be used.

Signed-off-by: Robert Kuska <rkuska@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-30 10:51:36 +02:00
Jan Cholasta
ba5201979d Use bytes instead of str where appropriate
Under Python 2, "str" and "bytes" are synonyms.

Reviewed-By: Petr Viktorin <pviktori@redhat.com>
2015-09-17 11:08:43 +02:00
Jan Cholasta
23507e6124 Alias "unicode" to "str" under Python 3
The six way of doing this is to replace all occurences of "unicode"
with "six.text_type". However, "unicode" is non-ambiguous and
(arguably) easier to read. Also, using it makes the patches smaller,
which should help with backporting.

Reviewed-By: Petr Viktorin <pviktori@redhat.com>
2015-09-17 11:08:43 +02:00
Petr Viktorin
8de13bd7dd Use the print function
In Python 3, `print` is no longer a statement. Call it as a function
everywhere, and include the future import to remove the statement
in Python 2 code as well.

Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-01 11:42:01 +02:00
Petr Viktorin
ace63f4ea5 Replace uses of map()
In Python 2, map() returns a list; in Python 3 it returns an iterator.

Replace all uses by list comprehensions, generators, or for loops,
as required.

Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-01 11:42:01 +02:00
Petr Viktorin
c27cb295a5 Use six.moves.input instead of raw_input
In Python 3, raw_input() was renamed to input().
Import the function from six.moves to get the right version.

Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-01 11:42:01 +02:00
Petr Viktorin
3bf91eab25 Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.

Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.

When iterating through the entire dict, without modifying the dict,
 the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.

In other cases, helpers like six.itervalues are used.

Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-01 11:42:01 +02:00
Petr Viktorin
dd16cc98b0 Use six.string_types instead of "basestring"
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-09-01 11:42:01 +02:00
Petr Viktorin
5435a8a32a Use absolute imports
In Python 3, implicit relative imports will not be supported.
Use fully-qualified imports everywhere.

Reviewed-By: Tomas Babej <tbabej@redhat.com>
2015-08-12 18:17:23 +02:00
Petr Viktorin
27dabb4528 Modernize 'except' clauses
The 'as' syntax works from Python 2 on, and Python 3 will
drop the "comma" syntax.

Reviewed-By: Tomas Babej <tbabej@redhat.com>
2015-08-12 18:17:23 +02:00
Milan Kubík
61f41df949 ipalib: pass api instance into textui in doctest snippets
Reviewed-By: Tomas Babej <tbabej@redhat.com>
2015-07-10 15:05:19 +02:00
Martin Babinsky
ea7f392bb9 add option to skip client API version check
This can be either set in IPA config file or specified as
'ipa --skip-version-check [COMMAND]'.

part of https://fedorahosted.org/freeipa/ticket/4768

Reviewed-By: Martin Basti <mbasti@redhat.com>
2015-07-08 00:35:05 +02:00
Jan Cholasta
e39fe4ed31 plugable: Pass API to plugins on initialization rather than using set_api
https://fedorahosted.org/freeipa/ticket/3090

Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
2015-07-01 13:05:30 +00:00
Jan Cholasta
2d1515323a plugable: Load plugins only from modules imported by API
Previously all plugin modules imported from anywhere were added to the API.

https://fedorahosted.org/freeipa/ticket/3090

Reviewed-By: Martin Babinsky <mbabinsk@redhat.com>
2015-07-01 13:05:30 +00:00
Gabe
b98077ea68 Do not print traceback when pipe is broken
https://fedorahosted.org/freeipa/ticket/2284

Reviewed-By: Martin Basti <mbasti@redhat.com>
2015-06-02 13:56:19 +00:00
Gabe
387be8651c Allow ipa help command to run when ipa-client-install is not configured
https://fedorahosted.org/freeipa/ticket/3584

Reviewed-By: Martin Basti <mbasti@redhat.com>
2015-06-02 13:54:36 +00:00
Petr Vobornik
91b39acd6b cli: differentiate Flag a Bool when autofill is set
With previous behavior there was no difference between Flag and Bool if
- autofill == True
- default = some value

It prevented to have a boolean which is set by default to true, but could
be set to False if users wants to without prompting in interactive shell.

Reviewed-By: Martin Basti <mbasti@redhat.com>
2015-05-12 12:41:34 +02:00
Martin Basti
b9969c6afe CLI conversion of DNSName type
Part of ticket:
IPA should allow internationalized domain names
https://fedorahosted.org/freeipa/ticket/3169

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-06-03 15:55:32 +02:00
Petr Viktorin
4d7351ef07 ipalib.cli: Add filename argument to ipa console
This allows writing simple IPA scripts using the shebang
    #! /usr/bin/ipa console

https://fedorahosted.org/freeipa/ticket/4351

Reviewed-By: Nathaniel McCallum <npmccallum@redhat.com>
2014-05-22 18:22:57 +02:00
Tomas Babej
1df696f543 ipalib: Add DateTime parameter
Adds a parameter that represents a DateTime format using datetime.datetime
object from python's native datetime library.

In the CLI, accepts one of the following formats:
    Accepts LDAP Generalized time without in the following format:
       '%Y%m%d%H%M%SZ'

    Accepts subset of values defined by ISO 8601:
        '%Y-%m-%dT%H:%M:%SZ'
        '%Y-%m-%dT%H:%MZ'
        '%Y-%m-%dZ'

    Also accepts above formats using ' ' (space) as a separator instead of 'T'.

As a simplification, it does not deal with timezone info and ISO 8601
values with timezone info (+-hhmm) are rejected. Values are expected
to be in the UTC timezone.

Values are saved to LDAP as LDAP Generalized time values in the format
'%Y%m%d%H%SZ' (no time fractions and UTC timezone is assumed). To avoid
confusion, in addition to subset of ISO 8601 values, the LDAP generalized
time in the format '%Y%m%d%H%M%SZ' is also accepted as an input (as this is the
format user will see on the output).

Part of: https://fedorahosted.org/freeipa/ticket/3306

Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2014-05-05 18:57:29 +03:00
Jan Cholasta
4314d02fbf Allow primary keys to use different type than unicode.
Also return list of primary keys instead of a single unicode CSV value from
LDAPDelete-based commands.

This introduces a new capability 'primary_key_types' for backward
compatibility with old clients.

Reviewed-By: Tomas Babej <tbabej@redhat.com>
2014-04-18 14:59:20 +02:00
Petr Viktorin
ffd9bb2d7c cli: Add mechanism for deprecated option name aliases
Add a new Param kwarg, deprecated_cli_aliases, that lists
deprecated aliases.
The aliases will appear in a "Deprecated options" in the help,
and otherwise act as the normal variant.

Preparation for: https://fedorahosted.org/freeipa/ticket/4231
2014-03-21 12:49:21 +01:00
Petr Viktorin
84c401f7d6 cli: Show list of values in --help for all Enums
Previously only the StrEnum param type had the list of values
listed in the help.
Extend the functionality to any kind of Enum.
2014-03-21 12:49:21 +01:00
Petr Viktorin
d2e3af88eb cli: Clean up imports 2014-03-21 12:49:21 +01:00
Petr Viktorin
7ec4d58bf7 cli.print_attribute: Convert values to strings
When output_for_cli was called directly, rather than for values
received through XML or JSON API, joining multiple values failed
on non-strings such as DN objects.

Convert output to strings before printing it out.
2014-01-03 14:11:33 +01:00
Martin Basti
db7dbbb141 Changed CLI to allow to use FILE as optional param 2013-12-02 13:30:11 +01:00
Petr Viktorin
dadf7cddf0 Help plugin: don't fail if a topic's module is not found
Previously the help plugin failed when searching for the docstring
when a topic's module was not found. This can happen when some server
plugins are loaded (e.g. for tests).

Use empty documentation when the topic is not found.
2013-10-30 11:50:05 +01:00
Tomas Babej
89ffaf411d Add prompt_param method to avoid code duplication
Extracted common code from ipalib/plugins/cli.py and
ipalib/plugins/dns.py that provided way to prompt user
for the value of specific attribute.

Added prompt_param method to Command class in ipalib/frontend.py

Done as part of https://fedorahosted.org/freeipa/ticket/3602
2013-06-05 12:50:29 +02:00
Tomas Babej
8984e3e105 Remove redundant check for env.interactive
Fixed as part of
https://fedorahosted.org/freeipa/ticket/3602
2013-06-05 12:50:29 +02:00
Petr Viktorin
4a30bf55ac Display full command documentation in online help
ipa <command> -h only showed the summary string, not the full help.
Use the full docstring. Add a custom help formatter that disables
optparse's reformatting.

Test included

https://fedorahosted.org/freeipa/ticket/3543
2013-04-03 15:32:03 +02:00
Ana Krivokapic
5f6310ecc6 Fix internal error for ipa show-mappings
The run() method of the show_mappings command was missing
the **options parameter in its signature, causing the
ipa show-mappings to fail with an internal error.
2013-03-18 14:40:54 +01:00
Petr Viktorin
8b8859ed7d cli: Do interactive prompting after a context is created
Some commands require a connection for interactive prompting.
Prompt after the connection is created.

Option parsing is still done before connecting so that help
can be printed out without a Kerberos ticket.

https://fedorahosted.org/freeipa/ticket/3453
2013-02-26 18:19:16 +01:00
Petr Viktorin
3a96cbc518 Drop support for CSV in the CLI client
Ticket: https://fedorahosted.org/freeipa/ticket/3352
Design: http://freeipa.org/page/V3/Drop_CSV
2013-02-22 17:20:35 +01:00
Petr Viktorin
7336a176b4 Add the version option to all Commands
Several Commands were missing the 'version' option. Add it to those
that were missing it.

Do not remove the version option before calling commands. This means
methods such as execute(), forward(), run() receive it.
Several of these needed `**options` added to their signatures.
Commands in the Cert plugin passed any unknown options to the underlying
functions, these are changed to pass what's needed explicitly.
Some commands in DNS and Batch plugins now pass version to commands
they call.

When the option is not given, fill it in automatically. (In a subsequent
commit, a warning will be added in this case).

Note that the public API did not change: all RPC calls already accepted
a version option. There's no need for an API version bump (even though
API.txt changes substantially).

Design page: http://freeipa.org/page/V3/Messages
Tickets:
  https://fedorahosted.org/freeipa/ticket/2732
  https://fedorahosted.org/freeipa/ticket/3294
2013-02-21 16:26:09 +01:00
Jan Cholasta
cfbdeebe66 Run interactive_prompt callbacks after CSV values are split.
https://fedorahosted.org/freeipa/ticket/3334
2013-02-19 11:08:11 -05:00
Petr Viktorin
8fcc8bc8d5 In topic help text, mention how to get help for commands
This should prevent user confusion when topic help is requested
unintentionally, for example with `ipa help ping`.

See https://fedorahosted.org/freeipa/ticket/3247
2013-02-18 13:07:26 -05:00
Petr Viktorin
abe26d55c8 Parse command arguments before creating a context
This allows users to run `ipa COMMAND --help` even without
Kerberos credentials.

Part of the effort for https://fedorahosted.org/freeipa/ticket/3060
2013-02-18 13:07:17 -05:00
Petr Viktorin
f16c100f1e Mention ipa COMMAND --help as the preferred way to get command help
This avoids the problem with ambiguous command/topic names.

No functionality is changed; `ipa help <COMMAND>` still works as before
if there's no topic with the same name.

https://fedorahosted.org/freeipa/ticket/3247
2013-02-18 13:07:17 -05:00
Petr Viktorin
de1c4eeae2 Add command summary to ipa COMMAND --help output
This makes the output identical to `ipa help COMMAND`.

Part of the effort for https://fedorahosted.org/freeipa/ticket/3060
2013-02-18 13:07:17 -05:00
Petr Viktorin
6ac0e9b90f Simplify ipa help topics output
This brings the output closer to `ipa help commands` and removes
extraneous information.

Part of the effort for https://fedorahosted.org/freeipa/ticket/3060
2013-02-18 13:07:17 -05:00
Petr Viktorin
5ee2216f49 Store the OptionParser in the API, use it to print unified help messages
Make `ipa -h` and `ipa help` output the same message.

Since `ipa -h` output is generated by the OptionParser, we need to make
the parser available. Store it in `api.parser`.

Part of the effort for https://fedorahosted.org/freeipa/ticket/3060
2013-02-18 13:07:17 -05:00
Petr Viktorin
1e2437ece1 Print help to stderr on error
Whenever a command is used incorrectly, it should output
an error message (and possibly additional help) to stderr.

This patch adds a parameter to a bunch of places to allow
selecting either stdout or stderr for help output, and makes
badly called commands output to stderr only.

Part of the effort for https://fedorahosted.org/freeipa/ticket/3060
2013-02-18 13:07:17 -05:00
Petr Viktorin
a95eaeac8e Internationalization for public errors
Currently, we throw many public exceptions without proper i18n.
Wrap natural-language error messages in _() so they can be translated.

In the service plugin, raise NotFound errors using handle_not_found helper
so the error message contains the offending service.

Use ScriptError instead of NotFoundError in bindinstance install.

https://fedorahosted.org/freeipa/ticket/1953
2012-09-03 18:16:12 +02:00
Petr Viktorin
9960149e3f Rework the CallbackInterface
Fix several problems with the callback interface:
- Automatically registered callbacks (i.e. methods named
    exc_callback, pre_callback etc) were registered on every
    instantiation.
    Fix: Do not register callbacks in __init__; instead return the
    method when asked for it.
- The calling code had to distinguish between bound methods and
    plain functions by checking the 'im_self' attribute.
    Fix: Always return the "default" callback as an unbound method.
    Registered callbacks now always take the extra `self` argument,
    whether they happen to be bound methods or not.
    Calling code now always needs to pass the `self` argument.
- Did not work well with inheritance: due to the fact that Python
    looks up missing attributes in superclasses, callbacks could
    get attached to a superclass if it was instantiated early enough. *
    Fix: Instead of attribute lookup, use a dictionary with class keys.
- The interface included the callback types, which are LDAP-specific.
    Fix: Create generic register_callback and get_callback mehods,
    move LDAP-specific code to BaseLDAPCommand

Update code that calls the callbacks.
Add tests.
Remove lint exceptions for CallbackInterface.

* https://fedorahosted.org/freeipa/ticket/2674
2012-06-14 11:09:43 +02:00