summaryrefslogtreecommitdiff
path: root/lib/ansible/galaxy/token.py
diff options
context:
space:
mode:
authorBrian Coca <bcoca@users.noreply.github.com>2019-08-13 19:47:40 -0400
committerJordan Borean <jborean93@gmail.com>2019-08-14 09:47:40 +1000
commita8d01cf2a24505c8c9620d9a10771c591208b922 (patch)
treee5b1cd3805240369599ebd7a899378ada4204f33 /lib/ansible/galaxy/token.py
parent3f784caed1aa63d2a5a3e9df20c4606372910c19 (diff)
downloadansible-a8d01cf2a24505c8c9620d9a10771c591208b922.tar.gz
Made galaxy token file location configurable (#59387)
* Made galaxy token file location configurable also made file handling 'unicode safe' * only create a token on demand * convert into decorator in case other funcs need
Diffstat (limited to 'lib/ansible/galaxy/token.py')
-rw-r--r--lib/ansible/galaxy/token.py31
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/ansible/galaxy/token.py b/lib/ansible/galaxy/token.py
index 4af7511b4c..6c235ed652 100644
--- a/lib/ansible/galaxy/token.py
+++ b/lib/ansible/galaxy/token.py
@@ -26,30 +26,37 @@ from stat import S_IRUSR, S_IWUSR
import yaml
+from ansible import constants as C
+from ansible.module_utils._text import to_bytes, to_text
from ansible.utils.display import Display
display = Display()
class GalaxyToken(object):
- ''' Class to storing and retrieving token in ~/.ansible_galaxy '''
+ ''' Class to storing and retrieving local galaxy token '''
def __init__(self):
- self.file = os.path.expanduser("~") + '/.ansible_galaxy'
+ self.b_file = to_bytes(C.GALAXY_TOKEN_PATH)
self.config = yaml.safe_load(self.__open_config_for_read())
if not self.config:
self.config = {}
def __open_config_for_read(self):
- if os.path.isfile(self.file):
- display.vvv('Opened %s' % self.file)
- return open(self.file, 'r')
- # config.yml not found, create and chomd u+rw
- f = open(self.file, 'w')
- f.close()
- os.chmod(self.file, S_IRUSR | S_IWUSR) # owner has +rw
- display.vvv('Created %s' % self.file)
- return open(self.file, 'r')
+
+ f = None
+ action = 'Opened'
+ if not os.path.isfile(self.b_file):
+ # token file not found, create and chomd u+rw
+ f = open(self.b_file, 'w')
+ f.close()
+ os.chmod(self.b_file, S_IRUSR | S_IWUSR) # owner has +rw
+ action = 'Created'
+
+ f = open(self.b_file, 'r')
+ display.vvv('%s %s' % (action, to_text(self.b_file)))
+
+ return f
def set(self, token):
self.config['token'] = token
@@ -59,5 +66,5 @@ class GalaxyToken(object):
return self.config.get('token', None)
def save(self):
- with open(self.file, 'w') as f:
+ with open(self.b_file, 'w') as f:
yaml.safe_dump(self.config, f, default_flow_style=False)