-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
139 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
from os import path | ||
|
||
__version__ = "0.15.1" | ||
__version__ = "0.15.2" | ||
|
||
co_path = path.dirname(__file__) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -173,7 +173,7 @@ def get_groups(self): | |
groups_df = pd.DataFrame(groups) | ||
return groups_df | ||
|
||
def get_account_list(self): | ||
def get_account_list(self, ignore_admin=False): | ||
"""Retrieve accounts within institutional instance""" | ||
url = self.endpoint("accounts") | ||
|
||
|
@@ -183,6 +183,15 @@ def get_account_list(self): | |
|
||
accounts_df = pd.DataFrame(accounts) | ||
accounts_df = accounts_df.drop(columns='institution_id') | ||
|
||
if ignore_admin: | ||
self.log.info("Excluding administrative and test accounts") | ||
|
||
drop_index = list(accounts_df[accounts_df['email'] == | ||
'[email protected]'].index) | ||
drop_index += list(accounts_df[accounts_df['email'].str.contains('[email protected]')].index) | ||
|
||
accounts_df = accounts_df.drop(drop_index).reset_index(drop=True) | ||
return accounts_df | ||
|
||
def get_account_group_roles(self, account_id): | ||
|
@@ -192,14 +201,15 @@ def get_account_group_roles(self, account_id): | |
roles = issue_request('GET', url, self.headers) | ||
return roles | ||
|
||
def get_account_details(self): | ||
def get_account_details(self, flag=True, ignore_admin=False): | ||
""" | ||
Retrieve account details. This includes number of articles, projects, | ||
collections, group association, and administrative and reviewer flags | ||
""" | ||
|
||
# Retrieve accounts | ||
accounts_df = self.get_account_list() | ||
accounts_df = self.get_account_list(ignore_admin=ignore_admin) | ||
|
||
n_accounts = accounts_df.shape[0] | ||
|
||
# Retrieve groups | ||
|
@@ -209,8 +219,9 @@ def get_account_details(self): | |
num_projects = np.zeros(n_accounts, dtype=np.int) | ||
num_collections = np.zeros(n_accounts, dtype=np.int) | ||
|
||
admin_flag = [''] * n_accounts | ||
reviewer_flag = [''] * n_accounts | ||
if flag: | ||
admin_flag = [''] * n_accounts | ||
reviewer_flag = [''] * n_accounts | ||
group_assoc = ['N/A'] * n_accounts | ||
|
||
# Determine group roles for each account | ||
|
@@ -237,19 +248,21 @@ def get_account_details(self): | |
|
||
for key in roles.keys(): | ||
for t_dict in roles[key]: | ||
if t_dict['id'] == 2: | ||
admin_flag[n] = 'X' | ||
if t_dict['id'] == 49: | ||
reviewer_flag[n] = 'X' | ||
if t_dict['id'] == 11: | ||
group_assoc[n] = key | ||
if flag: | ||
if t_dict['id'] == 2: | ||
admin_flag[n] = 'X' | ||
if t_dict['id'] == 49: | ||
reviewer_flag[n] = 'X' | ||
|
||
accounts_df['Articles'] = num_articles | ||
accounts_df['Projects'] = num_projects | ||
accounts_df['Collections'] = num_collections | ||
|
||
accounts_df['Admin'] = admin_flag | ||
accounts_df['Reviewer'] = reviewer_flag | ||
if flag: | ||
accounts_df['Admin'] = admin_flag | ||
accounts_df['Reviewer'] = reviewer_flag | ||
|
||
for group_id, group_name in zip(groups_df['id'], groups_df['name']): | ||
self.log.info(f"{group_id} - {group_name}") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#!/usr/bin/env python | ||
|
||
from os.path import dirname, exists, join | ||
from os import mkdir, stat | ||
|
||
import argparse | ||
|
||
from datetime import date, datetime | ||
import pytz | ||
|
||
from ldcoolp.curation.api.figshare import FigshareInstituteAdmin | ||
from ldcoolp.logger import LogClass, get_user_hostname | ||
from ldcoolp.admin import permissions | ||
|
||
# Version and branch info | ||
from ldcoolp import __version__ | ||
from ldcoolp.git_info import get_active_branch_name, get_latest_commit | ||
from ldcoolp import __file__ as library_path | ||
|
||
# Config loader | ||
from ldcoolp.config import dict_load | ||
|
||
today = date.today() | ||
|
||
tz_AZ = pytz.timezone('US/Arizona') | ||
now = datetime.now(tz_AZ) | ||
|
||
library_root_path = dirname(dirname(library_path)) # Retrieve parent directory to ldcoolp | ||
|
||
|
||
if __name__ == '__main__': | ||
parser = argparse.ArgumentParser(description='Command-line driver for LD-Cool-P prerequisite set-up.') | ||
parser.add_argument('--config', required=True, help='path to configuration file') | ||
parser.add_argument('--write_file', action='store_true', help='Write CSV file containing results') | ||
parser.add_argument('--path', help='Full path to write CSV file') | ||
parser.add_argument('--simple', action='store_true', | ||
help='Generate a basic list without detailed information') | ||
args = parser.parse_args() | ||
|
||
if not exists(args.config): | ||
raise FileNotFoundError(f"WARNING!!! Config file not found: {args.config}") | ||
|
||
if args.write_file: | ||
if not exists(args.path): | ||
raise FileNotFoundError(f"WARNING!!! Path not found: {args.path}") | ||
|
||
branch_name = get_active_branch_name(library_root_path) | ||
git_commit, git_short_commit = get_latest_commit(library_root_path) | ||
|
||
# Load configuration | ||
config_dict = dict_load(args.config) | ||
|
||
curation_dict = config_dict['curation'] | ||
|
||
# Define logfile | ||
root_directory_main = curation_dict[curation_dict['log_parent_dir']] | ||
|
||
log_dir = join(root_directory_main, curation_dict['log_dir']) | ||
if not exists(log_dir): | ||
mkdir(log_dir) | ||
logfile_prefix = 'get_user_details' | ||
logfile = f"{logfile_prefix}.{today.strftime('%Y-%m-%d')}.log" | ||
|
||
log = LogClass(log_dir, logfile).get_logger() | ||
|
||
log.info("****************************") | ||
|
||
log.debug(f"LD-Cool-P branch: {branch_name}") | ||
log.debug(f"LD-Cool-P version: {__version__} ({git_short_commit})") | ||
log.debug(f"LD-Cool-P commit hash: {git_commit}") | ||
|
||
# Retrieve username, hostname, IP | ||
sys_info = get_user_hostname() | ||
log.debug(f"username : {sys_info['user']}") | ||
log.debug(f"hostname : {sys_info['hostname']}") | ||
log.debug(f"IP Addr : {sys_info['ip']}") | ||
log.debug(f"Op. Sys. : {sys_info['os']}") | ||
|
||
fs_admin = FigshareInstituteAdmin(figshare_dict=config_dict['figshare'], | ||
log=log) | ||
|
||
if not args.simple: | ||
accounts_df = fs_admin.get_account_details(flag=False, ignore_admin=True) | ||
|
||
log.info(f"Number of users: {len(accounts_df)}") | ||
if not args.write_file: | ||
print(accounts_df) | ||
else: | ||
csv_outfile = join(args.path, | ||
f"redata_user_details.{now.strftime('%Y-%m-%d_%H:%M')}.csv") | ||
|
||
log.info(f"Writing file : {csv_outfile}") | ||
|
||
accounts_df.to_csv(csv_outfile, index=False) | ||
else: | ||
accounts_df = fs_admin.get_account_list(ignore_admin=True) | ||
|
||
log.info(f"Number of users: {len(accounts_df)}") | ||
print(accounts_df) | ||
|
||
# Change permission to mode=666 (rw for all) | ||
status = stat(join(log_dir, logfile)) | ||
if oct(status.st_mode)[-3:] == '666': | ||
log.debug("Permissions set for logfile") | ||
else: | ||
log.debug("Changing permissions on logfile...") | ||
permissions.curation(join(log_dir, logfile), mode=0o666) | ||
|
||
log.info("****************************") | ||
log.info("Exit 0") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters