#9336 closed enhancement (fixed)
[PATCH] trac-admin import/export permissions
Reported by: | Owned by: | Peter Suter | |
---|---|---|---|
Priority: | high | Milestone: | 1.0 |
Component: | admin/console | Version: | 0.12dev |
Severity: | normal | Keywords: | |
Cc: | leho@…, Thijs Triemstra | Branch: | |
Release Notes: |
Import and export Trac permissions with |
||
API Changes: | |||
Internal Changes: |
Description
trac-admin should allow to export permissions from old environment, edit them and import back
Attachments (7)
Change History (40)
comment:1 by , 15 years ago
Cc: | added |
---|
comment:2 by , 15 years ago
There is also invisible upvote arrow at the top of the page. I suppose it is like a typical Google star except that it probably does not subscribe.
comment:4 by , 14 years ago
Cc: | added |
---|
comment:5 by , 14 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Duplicate of #8489 it seems.
comment:6 by , 14 years ago
Milestone: | unscheduled |
---|
comment:7 by , 14 years ago
Not really a duplicate. #8489 is about transparent copy of permissions from one env to another. This proposal is to export to file/edit/then import from file.
comment:8 by , 14 years ago
Resolution: | duplicate |
---|---|
Status: | closed → reopened |
comment:9 by , 13 years ago
Milestone: | → unscheduled |
---|
… back to unscheduled then, please provide a patch.
by , 13 years ago
Attachment: | permission-import-export.patch added |
---|
CSV permission import and export trac-admin commands
follow-up: 11 comment:10 by , 13 years ago
Summary: | trac-admin import/export permissions → [PATCH] trac-admin import/export permissions |
---|
The above patch adds commands trac-admin permission import <filename>
and trac-admin permission export <filename>
to import and export simple CSV files of user, action pair rows.
- Is CSV a good idea?
- The ''csv'' library documenteation notes some problems with Unicode input. I'm not sure what needs o be done here.
- Importing a user, action pair that already exists in the DB results in a
IntegrityError: columns username, action are not unique
. I'm not sure how to best avoid this. Should this exception just be swallowed? - Command completion is not implemented. One could maybe suggest a list of existing files for import?
comment:11 by , 13 years ago
Replying to psuter <petsuter@…>:
The above patch adds commands
trac-admin permission import <filename>
andtrac-admin permission export <filename>
to import and export simple CSV files of user, action pair rows.
Excellent, thanks for working on this!
My two cents:
- Is CSV a good idea?
Reasonable, yes. I was going to suggest space-separated, but user names can have spaces IIRC, so that's a no go.
However, instead of using (user, perm) pairs on each line, I would write one line per user, structured as (user, perm1, perm2, perm3, …).
- Importing a user, action pair that already exists in the DB results in a
IntegrityError: columns username, action are not unique
. I'm not sure how to best avoid this. Should this exception just be swallowed?
The command should succeed, but not by swallowing the exception, as it's DB backend-specific, but by checking if the permission is already granted before trying to insert it.
- Command completion is not implemented. One could maybe suggest a list of existing files for import?
Yes, absolutely. Look at the wiki import
command in trac.wiki.admin
for an example how to do that (basically, use get_dir_list()
).
by , 13 years ago
Attachment: | permission-import-export2.patch added |
---|
one user per line; check existing perms; file completion
follow-up: 13 comment:12 by , 13 years ago
Thanks for the review & hints. I attached an updated patch that incorporates your suggestions.
Look at the wiki import command in trac.wiki.admin for an example how to do that (basically, use get_dir_list()).
I added this as well, but I can't seem to test it successfully. (On Windows, Python 2.7 with pyreadline 1.7) Command completion itself works; however path completion seems broken, even for e.g. wiki import.
follow-ups: 14 17 comment:13 by , 13 years ago
Milestone: | unscheduled → 0.13 |
---|---|
Owner: | set to |
Priority: | normal → high |
Status: | reopened → new |
Replying to psuter <petsuter@…>:
Thanks for the review & hints. I attached an updated patch that incorporates your suggestions.
Very good! I'll apply the patch shortly. If you want to get bonus points, you could add unit tests for the new commands (see trac/admin/tests/console.py and trac/admin/tests/console-tests.txt for inspiration).
I added this as well, but I can't seem to test it successfully. (On Windows, Python 2.7 with pyreadline 1.7) Command completion itself works; however path completion seems broken, even for e.g. wiki import.
Let me try… Yes, pyreadline seems to behave quite strangely indeed. I have a feeling that it's due to an incompatibility of pyreadline, but I may be wrong.
comment:14 by , 13 years ago
The problem seems to be that shlex.split
(called in trac.admin.console.TracAdmin arg_tokenize
) doesn't like Windows paths. Trailing backslashes like shlex.split('C:\\')
raise a ValueError: No escaped character
; others get lost: shlex.split('C:\\Code\\Trac')
evaluates to ['C:CodeTrac']
and matching fails.
One could use forward slashes instead, but os.path.join
called in get_dir_list
inserts and appends backslashes, so matching again fails. Matching with normalized strings in trac.admin.api.PathList
using os.path.normcase
helps marginally - the completion result still contains backslashes preventing further completion.
by , 13 years ago
Attachment: | trac-admin-completion-on-windows.patch added |
---|
Improve trac-admin path completion on Windows.
follow-up: 16 comment:15 by , 13 years ago
Sorry if this is getting somewhat off-topic. The above patch seems to improve trac-admin's path tab completion on Windows for me.
comment:16 by , 13 years ago
Replying to psuter <petsuter@…>:
The above patch seems to improve trac-admin's path tab completion on Windows for me.
Several unit tests fail with this patch applied, so it probably breaks some other things.
by , 13 years ago
Attachment: | permission-import-export-tests.patch added |
---|
Unit tests fixed and added for permission import / export (based on permission-import-export2.patch)
comment:17 by , 13 years ago
Replying to rblank:
If you want to get bonus points, you could add unit tests for the new commands (see trac/admin/tests/console.py and trac/admin/tests/console-tests.txt for inspiration).
Thanks again for the pointers. The above patch fixes the existing test_help_ok patch to include the help text for the two new commands, and adds a new test each for importing / exporting. Due to the nature of these commands I had to supply a filename, which I hardcoded as 'permissions.csv'.
(I noticed again that on Windows the file must be in the current directory, as for example shlex.split('C:\\Temp\\permissions.csv')
evaluates to ['C:Temppermissions.csv']
.)
follow-up: 21 comment:18 by , 13 years ago
permission-import-export2.patch and permission-import-export-tests.patch applied in [10714], with the following minor changes:
- Added
from __future__ import with_statement
intrac/admin/tests/console.py
. - Set the CSV writer to output with a platform-specific line terminator.
- Sorted the export by user and each line by permission for test repeatability.
- Used a single
try: except:
block in_do_import()
. - Used
read_file()
in the export test.
As you can tell, the changes are on the level of details, so great work!
Two possible extensions:
- Allow using a single dash (
-
) as a file name for exporting to / importing from stdout / stdin (the same as forwiki export
andwiki import
). This should also allow getting rid of the temporarypermissions.csv
in the unit tests. - Allow specifying for which users to export permissions, by passing a list of users after the file name in
permission export
.
I'll have a look at the completion issue, but reading that ticket, it seems we will have to have our own copy of shlex.split()
, at least for 2.5 (and the fix should go into 0.12.3).
follow-up: 20 comment:19 by , 13 years ago
trac-admin-completion-on-windows.patch indeed solves part of the issue with auto-completion, but it opens a can of worms. The failing tests are due to parsing in non-POSIX mode, where quoting doesn't operate the same, and the DB connection string when initializing functional tests is passed as 'sqlite:db/trac.db'
(including the quotes).
I have the feeling that we should rather leave the parsing in POSIX mode, but simply disable escaping on Windows. Something like the following (in addition to your changes in api.py
, which are fine):
-
trac/admin/console.py
diff --git a/trac/admin/console.py b/trac/admin/console.py
a b import cmd 16 16 import locale 17 17 import os.path 18 18 import pkg_resources 19 import shlex19 from shlex import shlex 20 20 import StringIO 21 21 import sys 22 22 import traceback … … class TracAdmin(cmd.Cmd): 71 71 try: 72 72 import readline 73 73 delims = readline.get_completer_delims() 74 for c in '-/:() ':74 for c in '-/:()\\': 75 75 delims = delims.replace(c, '') 76 76 readline.set_completer_delims(delims) 77 77 … … Type: '?' or 'help' for help on command 182 182 183 183 ... but shlex is not unicode friendly. 184 184 """ 185 return [unicode(token, 'utf-8') 186 for token in shlex.split(argstr.encode('utf-8'))] or [''] 185 lex = shlex(argstr.encode('utf-8'), posix=True) 186 lex.whitespace_split = True 187 lex.commenters = '' 188 if os.name == 'nt': 189 lex.escape = '' 190 return [unicode(token, 'utf-8') for token in lex] or [''] 187 191 188 192 def word_complete(self, text, words): 189 193 words = list(set(a for a in words if a.startswith(text)))
This makes completion work on Windows for me. It still feels weird, as it completes correctly but doesn't display all choices when hitting tab twice. But this may be due to pyrealine.
follow-up: 25 comment:20 by , 13 years ago
Replying to rblank:
I have the feeling that we should rather leave the parsing in POSIX mode, but simply disable escaping on Windows. Something like […] This makes completion work on Windows for me. It still feels weird, as it completes correctly but doesn't display all choices when hitting tab twice. But this may be due to pyrealine.
Sounds sensible. And works fine here, too, up to the same tab-twice issue. However, I could fix that by creating %HOMEPATH%\pyreadlineconfig.ini containing: show_all_if_ambiguous("on")
follow-up: 22 comment:21 by , 13 years ago
Replying to rblank:
applied in [10714], with the following minor changes: […]
Great.
- Allow using a single dash (
-
) as a file name for exporting to / importing from stdout / stdin (the same as forwiki export
andwiki import
). This should also allow getting rid of the temporarypermissions.csv
in the unit tests.
Yes, sounds useful. I assume for the wiki
commands this is handled by this code: if not filename: printout(text)
and if filename: ... else: data = sys.stdin.read()
? I can't seem to trigger that functionality using a dash (again a argument parsing problem on Windows?)
follow-up: 23 comment:22 by , 13 years ago
Replying to psuter <petsuter@…>:
Yes, sounds useful. I assume for the
wiki
commands this is handled by this code:if not filename: printout(text)
andif filename: ... else: data = sys.stdin.read()
? I can't seem to trigger that functionality using a dash (again a argument parsing problem on Windows?)
Yes, sorry, my bad. For the wiki commands, it is triggered by the absence of a path. We should probably do the same here… except then we cannot export the permissions of only a few users. That's ok, it probably isn't that useful anyway (and easy to implement with grep
).
by , 13 years ago
Attachment: | permission-import-export-stdin-stdout.patch added |
---|
Allow importing from stdin and exporting to stdout
by , 13 years ago
Attachment: | permission-import-export-unicode.patch added |
---|
Workaround csv library unicode issues (incomplete)
comment:23 by , 13 years ago
Replying to rblank:
it is triggered by the absence of a path. We should probably do the same here…
Ah of course, that makes sense. In the two patches above I gave this another try, and also experimented a bit with the suggested wrappers from the csv library documentation to work around the Unicode issues. (It seems to help, except when importing from stdin
, but I maybe don't yet understand the problem enough to fix it properly.)
follow-up: 26 comment:24 by , 13 years ago
Unit test modifications from permission-import-export-stdin-stdout.patch were applied in [10715]. In the import and export commands, instead of using a closure to avoid code duplication, I added a context manager that opens a file if the filename is non-empty, or uses the standard stream corresponding to the file mode otherwise. This should be reusable for the wiki commands as well, once the encoding issues are sorted out.
When exporting or importing to / from the standard streams, we should actually use the stream's encoding, instead of forcing UTF-8. The logic should probably be very similar to what's used in console_print().
comment:25 by , 13 years ago
Replying to psuter <petsuter@…>:
However, I could fix that by creating %HOMEPATH%\pyreadlineconfig.ini containing:
show_all_if_ambiguous("on")
Heh, and it's even documented. Oh well, who reads the docs, anyway? :) Installing a proper pyreadlineconfig.ini
makes completion work very nicely.
So, fix applied on 0.12-stable in [10716].
by , 13 years ago
Attachment: | permission-import-export-unicode2.patch added |
---|
Fix unicode issues by using the stream's encoding.
follow-up: 29 comment:26 by , 13 years ago
Replying to rblank:
When exporting or importing to / from the standard streams, we should actually use the stream's encoding, instead of forcing UTF-8. The logic should probably be very similar to what's used in console_print().
I gave this another shot in the patch above and extracted the code you indicated into get_console_encoding()
. Using that in permission import
and permission export
makes the unittests succeed for a test_userɑ
.
comment:27 by , 13 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Works great. Applied in [10722], with a few cosmetic changes. So… we're done!
comment:28 by , 13 years ago
Owner: | changed from | to
---|
follow-up: 30 comment:29 by , 13 years ago
Replying to psuter <petsuter@…>:
… the unittests succeed for a
test_userɑ
.
Great job, but note that non-ascii usernames in general are not yet fully supported (#6318).
comment:30 by , 13 years ago
comment:31 by , 13 years ago
Owner: | changed from | to
---|
comment:32 by , 13 years ago
Release Notes: | modified (diff) |
---|
comment:33 by , 7 years ago
Support for pyreadline
documented in 1.3/TracInstall@11 and TracAdmin@60.
yes, just had a case where i needed this. following.