Debian Codegraph @

po/tx-credentials

raw
1 #!/usr/bin/python3
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2014,2022 Samuel Degrande
5 #
6 # This file is part of Freedroid
7 #
8 # Freedroid is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # Freedroid is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Freedroid; see the file COPYING. If not, write to the
20 # Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 # MA 02111-1307 USA
22 #
23
24 #
25 # Get transifex credentials from user's .transifexrc
26 #
27 # Code based on the old python txlib (transifex-client written in python)
28 #
29
30 import os
31 import argparse
32 import configparser
33
34 class ProjectNotInit(Exception):
35 pass
36
37 def find_dot_tx(path=os.path.curdir, previous=None):
38 """Return the path where .tx folder is found.
39
40 The 'path' should be a DIRECTORY.
41 This process is functioning recursively from the current directory to each
42 one of the ancestors dirs.
43 """
44 path = os.path.abspath(path)
45 if path == previous:
46 return None
47 joined = os.path.join(path, ".tx")
48 if os.path.isdir(joined):
49 return path
50 else:
51 return find_dot_tx(os.path.dirname(path), path)
52
53 def _get_tx_dir_path():
54 root_path = find_dot_tx()
55 if not root_path:
56 msg = "Cannot find any .tx directory!"
57 raise ProjectNotInit(msg)
58 return root_path
59
60 def _get_config_file_path(root_path):
61 """Check the .tx/config file exists."""
62 config_file = os.path.join(root_path, ".tx", "config")
63 if not os.path.exists(config_file):
64 msg = "Cannot find the config file (.tx/config)!"
65 raise ProjectNotInit(msg)
66 return config_file
67
68 def _read_config_file(config_file):
69 """Parse the config file and return its contents."""
70 config = configparser.ConfigParser()
71 try:
72 config.read(config_file)
73 except Exception as err:
74 msg = "Cannot open/parse .tx/config file: %s" % err
75 raise ProjectNotInit(msg)
76 return config
77
78 def _get_transifex_file(directory=None):
79 """Fetch the path of the .transifexrc file.
80 It is in the home directory of the user by default.
81 """
82 if directory is not None:
83 return os.path.join(directory, ".transifexrc")
84
85 directory = os.path.expanduser('~')
86 txrc_file = os.path.join(directory, ".transifexrc")
87 if not os.path.exists(txrc_file):
88 return None
89 return txrc_file
90
91 def _get_transifex_config(txrc_file):
92 """Read the configuration from the .transifexrc file."""
93 txrc = configparser.ConfigParser()
94 try:
95 if txrc_file:
96 txrc.read(txrc_file)
97 except Exception as e:
98 print("Cannot read .transifex file: %s." % e)
99
100 return txrc
101
102 def get_host_credentials(txrc, host):
103 """
104 Read .transifexrc and report user,pass for a specific host else ask the
105 user for input.
106 """
107 try:
108 username = txrc.get(host, 'username')
109 passwd = txrc.get(host, 'password')
110 rest_hostname = txrc.get(host, 'rest_hostname')
111 token = txrc.get(host, 'token')
112 except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
113 username = None
114 passwd = None
115
116 return username, passwd, rest_hostname, token
117
118 #======================================
119
120 def main():
121 arg_parser = argparse.ArgumentParser(description='Choose one option')
122 arg_parser.add_argument('-u', '--user', action='store_true')
123 arg_parser.add_argument('-p', '--password', action='store_true')
124 arg_parser.add_argument('-r', '--rest_hostname', action='store_true')
125 arg_parser.add_argument('-t', '--token', action='store_true')
126 args = arg_parser.parse_args()
127
128 try:
129 root = _get_tx_dir_path()
130 config_file = _get_config_file_path(root)
131 config = _read_config_file(config_file)
132 host = config.get('main', 'host')
133 txrc_file = _get_transifex_file()
134 if not txrc_file :
135 return
136 txrc = _get_transifex_config(txrc_file)
137 except ProjectNotInit as e:
138 print(unicode(e))
139 return
140
141 username, password, rest_hostname, token = get_host_credentials(txrc, host)
142 if args.user and username:
143 print(username)
144 if args.password and password:
145 print(password)
146 if args.rest_hostname and rest_hostname:
147 print(rest_hostname)
148 if args.token and token:
149 print(token)
150
151 if __name__ == "__main__":
152 main()