OLD | NEW |
(Empty) | |
| 1 # This file is part of the Adblock Plus web scripts, |
| 2 # Copyright (C) 2006-present eyeo GmbH |
| 3 # |
| 4 # Adblock Plus is free software: you can redistribute it and/or modify |
| 5 # it under the terms of the GNU General Public License version 3 as |
| 6 # published by the Free Software Foundation. |
| 7 # |
| 8 # Adblock Plus is distributed in the hope that it will be useful, |
| 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 # GNU General Public License for more details. |
| 12 # |
| 13 # You should have received a copy of the GNU General Public License |
| 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 15 |
| 16 """Script handling interaction with the XTM Cloud REST API.""" |
| 17 |
| 18 import argparse |
| 19 import logging |
| 20 import sys |
| 21 import getpass |
| 22 import os |
| 23 |
| 24 from cms.bin.xtm_translations.xtm_api import ( |
| 25 XTMCloudException, get_token, |
| 26 ) |
| 27 import cms.bin.xtm_translations.constants as const |
| 28 from cms.bin.xtm_translations.projects_handler import ( |
| 29 main as handle_projects, |
| 30 create_project, upload_files, download_files, |
| 31 ) |
| 32 from cms.bin.xtm_translations.utils import input_fn |
| 33 |
| 34 |
| 35 def generate_token(args): |
| 36 """Generate an API token from username and password.""" |
| 37 username = input_fn('Username: ') |
| 38 user_id = input_fn('User ID: ') |
| 39 password = getpass.getpass(prompt='Pasword: ') |
| 40 |
| 41 logging.info(const.InfoMessages.GENERATING_TOKEN.format(username, user_id)) |
| 42 try: |
| 43 token = get_token(username, password, int(user_id)) |
| 44 logging.info(const.InfoMessages.TOKEN_GENERATED.format(token)) |
| 45 |
| 46 cmd = const.Token.SAVE_COMMAND.format(const.Token.ENV_VAR, token) |
| 47 sys.stdout.write(const.InfoMessages.TOKEN_SAVE_TO_ENV_VAR.format(cmd)) |
| 48 except XTMCloudException as err: |
| 49 sys.exit(err) |
| 50 except Exception as err: |
| 51 sys.exit(err) |
| 52 |
| 53 |
| 54 def parse_args(): |
| 55 parser = argparse.ArgumentParser() |
| 56 subparsers = parser.add_subparsers() |
| 57 |
| 58 # Universal arguments |
| 59 parser.add_argument('-v', '--verbose', action='store_true', |
| 60 help=const.ArgumentsHelp.VERBOSE) |
| 61 |
| 62 # Subparser for generating token |
| 63 token_parser = subparsers.add_parser('login', |
| 64 help=const.ArgumentsHelp.LOGIN) |
| 65 token_parser.set_defaults(func=generate_token) |
| 66 |
| 67 # Subparser for project creation. |
| 68 project_create_parser = subparsers.add_parser( |
| 69 'create', |
| 70 help=const.ArgumentsHelp.ProjectCreate.MAIN, |
| 71 ) |
| 72 project_create_parser.set_defaults(func=handle_projects) |
| 73 project_create_parser.set_defaults(projects_func=create_project) |
| 74 |
| 75 project_create_parser.add_argument( |
| 76 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR, |
| 77 default=os.getcwd(), nargs='?', |
| 78 ) |
| 79 |
| 80 project_create_parser.add_argument( |
| 81 '--name', required=True, |
| 82 help=const.ArgumentsHelp.ProjectCreate.NAME, |
| 83 ) |
| 84 project_create_parser.add_argument( |
| 85 '--desc', required=True, |
| 86 help=const.ArgumentsHelp.ProjectCreate.DESC, |
| 87 ) |
| 88 # TODO: Decide what the description field means right now. It doesn't |
| 89 # TODO: make sense to still have it as the URL to the GitLab issue, as |
| 90 # TODO: a project is associated to a website, not an issue. Maybe url to |
| 91 # TODO: website repo? |
| 92 project_create_parser.add_argument( |
| 93 '--client-id', required=True, type=int, |
| 94 help=const.ArgumentsHelp.ProjectCreate.CLIENT_ID, |
| 95 ) |
| 96 # TODO: Decide what the clientId would signify on our end (if anything) |
| 97 project_create_parser.add_argument( |
| 98 '--ref-id', required=True, |
| 99 help=const.ArgumentsHelp.ProjectCreate.REF_ID, |
| 100 ) |
| 101 # TODO: Decide what the referenceId would mean on our end |
| 102 project_create_parser.add_argument( |
| 103 '--workflow-id', required=True, type=int, |
| 104 help=const.ArgumentsHelp.ProjectCreate.WORKFLOW_ID, |
| 105 ) |
| 106 # TODO: Decide what the workflowId would mean on our end |
| 107 project_create_parser.add_argument( |
| 108 '--source-lang', default='en_US', |
| 109 help=const.ArgumentsHelp.ProjectCreate.SOURCE, |
| 110 ) |
| 111 project_create_parser.add_argument( |
| 112 '--save-id', action='store_true', default=False, |
| 113 help=const.ArgumentsHelp.ProjectCreate.SAVE_ID, |
| 114 ) |
| 115 |
| 116 # Subparser for uploading files to project |
| 117 project_upload_parser = subparsers.add_parser( |
| 118 'upload', help=const.ArgumentsHelp.ProjectUpload.MAIN, |
| 119 ) |
| 120 project_upload_parser.set_defaults(func=handle_projects) |
| 121 project_upload_parser.set_defaults(projects_func=upload_files) |
| 122 |
| 123 project_upload_parser.add_argument( |
| 124 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR, |
| 125 default=os.getcwd(), nargs='?', |
| 126 ) |
| 127 |
| 128 project_upload_parser.add_argument( |
| 129 '--no-overwrite', action='store_true', default=False, |
| 130 help=const.ArgumentsHelp.ProjectUpload.NO_OVERWRITE, |
| 131 ) |
| 132 |
| 133 # Subparser for downloading files from project |
| 134 download_parser = subparsers.add_parser( |
| 135 'download', help=const.ArgumentsHelp.PROJECT_DOWNLOAD, |
| 136 ) |
| 137 download_parser.set_defaults(func=handle_projects) |
| 138 download_parser.set_defaults(projects_func=download_files) |
| 139 |
| 140 download_parser.add_argument( |
| 141 'source_dir', help=const.ArgumentsHelp.PROJECT_SOURCE_DIR, |
| 142 default=os.getcwd(), nargs='?', |
| 143 ) |
| 144 |
| 145 return parser.parse_args() |
| 146 |
| 147 |
| 148 def main(): |
| 149 """Run XTM integration script.""" |
| 150 args = parse_args() |
| 151 |
| 152 if args.verbose: |
| 153 logging.basicConfig(level=logging.INFO) |
| 154 |
| 155 args.func(args) |
| 156 |
| 157 |
| 158 if __name__ == '__main__': |
| 159 main() |
OLD | NEW |