Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61087908
getmystuph_in_phab.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, May 4, 11:17
Size
5 KB
Mime Type
text/x-python
Expires
Mon, May 6, 11:17 (2 d)
Engine
blob
Format
Raw Data
Handle
17463294
Attached To
rPHAPI Phabricator API scripts
getmystuph_in_phab.py
View Options
#!/usr/bin/env python3
import
argparse
import
yaml
import
keyring
import
getpass
import
collections
import
phabricator
as
phab
import
getmystuph
from
getmystuph
import
colored
from
getmystuph.utils
import
get_password
import
getmystuph.importers
as
getimp
import
logging
import
logging.config
try
:
with
open
(
'.logging.conf'
,
'r'
)
as
fh
:
config
=
yaml
.
load
(
fh
)
logging
.
config
.
dictConfig
(
config
)
except
:
logging
.
basicConfig
(
level
=
logging
.
ERROR
)
_logger
=
logging
.
getLogger
(
'getmystuph'
)
.
getChild
(
'main'
)
class
PhabImporter
(
object
):
"""
Parses a YAML configuration file:
"""
_config
=
None
def
__init__
(
self
,
args
):
self
.
_config
=
yaml
.
load
(
args
.
config
)
_logger
.
debug
(
'ConfigurationParser: {0}'
.
format
(
self
.
_config
))
if
'phabricator'
not
in
self
.
_config
:
# this will use default infos from ~/.arcrc
self
.
_config
[
'phabricator'
]
=
{}
try
:
self
.
_phab
=
phab
.
Phabricator
(
**
self
.
_config
[
'phabricator'
])
self
.
_phab
.
update_interfaces
()
# this request is just to make an actual connection
self
.
_phab
.
user
.
whoami
()
except
Exception
as
e
:
_logger
.
error
(
'Could not connect to phabricator, either give the'
+
' connection information in the
\'
phabricator
\'
'
+
'section of the config file:
\n
'
+
' phabricator:
\n
'
+
' username: richart
\n
'
+
' host: https://c4science.ch
\n
'
+
' token: cli-g3amff25kdpnnv2tqvigmr4omnn7
\n
'
)
raise
e
self
.
_keyring
=
None
if
'use_keyring'
in
self
.
_config
and
self
.
_config
[
'use_keyring'
]:
self
.
_keyring
=
keyring
.
get_keyring
()
self
.
_imported_groups
=
{}
self
.
_users_db
=
getimp
.
ImportUserDB
(
self
.
_phab
)
def
_import_repository
(
self
,
name
,
info
):
_logger
.
info
(
'Getting repo {0}'
.
format
(
getmystuph
.
Repo
.
color_name
(
name
)))
_logger
.
debug
(
' --> repo info {0}'
.
format
(
colored
(
info
,
attrs
=
[
'bold'
])))
password
=
getmystuph
.
get_password
(
info
[
'backend'
],
info
[
'username'
],
keyring
=
self
.
_keyring
)
repo
=
getmystuph
.
Repo
(
name
,
password
=
password
,
**
info
)
_permissions
=
repo
.
permissions
_logger
.
debug
(
"Permissions for repo {0}: {1}"
.
format
(
repo
.
color_name
(
name
),
colored
(
_permissions
,
attrs
=
[
'bold'
])))
_users
=
self
.
_complete_users
(
repo
.
directory
,
_permissions
.
all_users
)
for
u
in
_users
:
print
(
u
)
if
'import_scheme'
in
info
and
\
info
[
'import_scheme'
]
!=
'all'
:
try
:
query
=
repo
.
get_query
()
with
query
:
print
(
query
.
list_branches
())
print
(
query
.
list_tags
())
except
:
_logger
.
warning
(
"No fine grain operation possible for repo {0}"
.
format
(
repo
.
color_name
(
name
)))
def
__get_full_info
(
self
,
info
,
_type
):
if
info
is
None
:
info
=
{}
if
'global'
in
self
.
_config
:
global_conf
=
self
.
_config
[
'global'
]
if
_type
in
global_conf
:
for
key
,
value
in
global_conf
[
_type
]
.
items
():
if
key
not
in
info
:
info
[
key
]
=
value
elif
type
(
value
)
==
dict
:
info
[
key
]
=
dict
(
value
,
**
info
[
key
])
import_always
=
[
'backend'
,
'username'
]
for
key
in
import_always
:
if
key
in
global_conf
and
key
not
in
info
:
info
[
key
]
=
global_conf
[
key
]
if
'username'
not
in
info
:
info
[
'username'
]
=
getpass
.
getuser
()
if
'import-scheme'
in
info
:
if
'backend'
not
in
info
[
'import-scheme'
]:
info
[
'import-scheme'
][
'backend'
]
=
'c4science'
info
[
'import-scheme'
][
'phabricator'
]
=
self
.
_phab
return
info
def
import_all
(
self
):
methods
=
collections
.
OrderedDict
(
[(
'groups'
,
'GroupImporter'
),
(
'repositories'
,
'RepoImporter'
)])
for
_type
in
methods
.
keys
():
if
_type
in
self
.
_config
:
all_info
=
self
.
_config
[
_type
]
if
type
(
all_info
)
==
list
:
all_info
=
{
key
:
{}
for
key
in
all_info
}
for
name
,
info
in
all_info
.
items
():
info
=
self
.
__get_full_info
(
info
,
_type
)
importer
=
getattr
(
getimp
,
methods
[
_type
])(
name
,
info
,
self
.
_users_db
)
importer
.
transfer
()
# Set up cli arguments
parser
=
argparse
.
ArgumentParser
(
description
=
'Import projects into c4science'
)
parser
.
add_argument
(
'--config'
,
help
=
'configuration file (YAML)'
,
type
=
argparse
.
FileType
(),
required
=
True
)
args
=
parser
.
parse_args
()
imp
=
PhabImporter
(
args
)
imp
.
import_all
()
Event Timeline
Log In to Comment