Skip to content

Commit

Permalink
Merge pull request #45 from mogproject/topic-read-env-vars-#43
Browse files Browse the repository at this point in the history
Topic read env vars #43
  • Loading branch information
mogproject committed Sep 16, 2015
2 parents 1e8f060 + 3045e64 commit 502d8bf
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 35 deletions.
40 changes: 40 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
PYTHON = python
PROG = artifact-cli

build:
$(PYTHON) setup.py build

install:
$(PYTHON) setup.py install

uninstall: dev-uninstall
pip uninstall $(PROG)

dev-install:
$(PYTHON) setup.py develop

dev-uninstall:
$(PYTHON) setup.py develop -u

pep8:
pep8 --max-line-length 120 --ignore E402,E731 src tests

test: pep8
$(PYTHON) setup.py test

coverage:
coverage run --source=src setup.py test

clean:
$(PYTHON) setup.py clean

console:
cd src && $(PYTHON)

register:
$(PYTHON) setup.py register

publish:
$(PYTHON) setup.py sdist upload

.PHONY: build install uninstall dev_install dev_uninstall pep8 test coverage clean console register publish
72 changes: 44 additions & 28 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,32 @@ Features
Dependencies
------------

* Python >= 2.6
* Python 2.6 / 2.7
* pytz
* python-dateutil
* GitPython >= 0.3.5
* boto
* boto >= 2.38.0
* moto (for testing)

------------
Installation
------------

Just install via pip! (may need ``sudo`` command)::

pip install artifact-cli

---------
Upgrading
---------

::

pip install --upgrade artifact-cli
art --version
* ``pip`` command may need ``sudo``

+-------------------------+------------------------------------------+
| Operation | Command |
+=========================+==========================================+
| Install |``pip install artifact-cli`` |
+-------------------------+------------------------------------------+
| Upgrade |``pip install --upgrade artifact-cli`` |
+-------------------------+------------------------------------------+
| Uninstall |``pip uninstall artifact-cli`` |
+-------------------------+------------------------------------------+
| Check installed version |``artifact-cli --version`` |
+-------------------------+------------------------------------------+
| Help |``artifact-cli -h`` |
+-------------------------+------------------------------------------+

----------------
Quickstart Guide
Expand Down Expand Up @@ -97,8 +100,33 @@ Create ``~/.artifact-cli`` file and write credentials for AWS like this:
bucket = your-bucket-name
region = your-region (e.g. ap-northeast-1, us-east-1)
| Or you can use command line options instead of the configuration file.
| (``--access ACCESS_KEY --secret SECRET_KEY --bucket BUCKET_NAME --region REGION``)
You can override these settings by specifying command line options or environment variables.

* Command Line Options (will override the settings from environment variables)

+--------------------------+-------------------------------------+
| Option | Description |
+==========================+=====================================+
| ``--access ACCESS_KEY`` | AWS access key. |
+--------------------------+-------------------------------------+
| ``--secret SECRET_KEY`` | AWS secret key. |
+--------------------------+-------------------------------------+
| ``--bucket BUCKET_NAME`` | S3 bucket name. |
+--------------------------+-------------------------------------+
| ``--region REGION`` | Region name of the S3 bucket. |
+--------------------------+-------------------------------------+

* Environment Variables

+---------------------------+-------------------------------------+
| Variable Name | Description |
+===========================+=====================================+
| ``AWS_ACCESS_KEY_ID`` | AWS access key. |
+---------------------------+-------------------------------------+
| ``AWS_SECRET_ACCESS_KEY`` | AWS secret key. |
+---------------------------+-------------------------------------+
| ``AWS_DEFAULT_REGION`` | Region name of the S3 bucket. |
+---------------------------+-------------------------------------+

5. Check connection
-------------------
Expand Down Expand Up @@ -199,15 +227,3 @@ Notes
* This tool supports only artifact-id-level concurrency.

* Simultaneous uploading of the artifacts with same artifact id could let repository broken.


--------------
Uninstallation
--------------

::

pip uninstall artifact-cli

(may need ``sudo``)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_version():
'pytz',
'python-dateutil',
'GitPython>=0.3.5',
'boto',
'boto>=2.38.0',
],
tests_require=[
'moto',
Expand Down
2 changes: 1 addition & 1 deletion src/artifactcli/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.9"
__version__ = "0.1.10"
3 changes: 2 additions & 1 deletion src/artifactcli/artifactcli.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import sys
import os
from settings import Settings


def main():
"""
Main function
"""
settings = Settings().parse_args(sys.argv).load_config()
settings = Settings().parse_args(sys.argv).load_environ(os.environ).load_config()
return settings.configure_logging().operation.run(settings.repo)
8 changes: 4 additions & 4 deletions src/artifactcli/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ def _get_artifact(self, artifact_id, version, packaging, revision=None):

@classmethod
def _match_artifact(cls, artifact, artifact_id=None, version=None, packaging=None, revision=None):
return ((artifact_id is None or artifact.basic_info.artifact_id == artifact_id)
and (version is None or artifact.basic_info.version == version)
and (packaging is None or artifact.basic_info.packaging == packaging)
and (revision is None or artifact.basic_info.revision == revision))
return ((artifact_id is None or artifact.basic_info.artifact_id == artifact_id) and
(version is None or artifact.basic_info.version == version) and
(packaging is None or artifact.basic_info.packaging == packaging) and
(revision is None or artifact.basic_info.revision == revision))

def _get_artifacts(self, artifact_id=None, version=None, packaging=None, revision=None):
return [
Expand Down
18 changes: 18 additions & 0 deletions src/artifactcli/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import copy
from driver import S3Driver
from os.path import expanduser, expandvars
import operation as op
Expand Down Expand Up @@ -49,6 +50,23 @@ def parse_args(self, argv):

return Settings(operation, opt_dict, self.repo)

def load_environ(self, env):
"""
Load environment variables
"""
keys = [
('access_key', 'AWS_ACCESS_KEY_ID'),
('secret_key', 'AWS_SECRET_ACCESS_KEY'),
('region', 'AWS_DEFAULT_REGION'),
]
d = {} if self.options is None else self.options.copy()
updates = dict([(k, env[v]) for k, v in keys if k not in d and v in env])
if updates:
d.update(updates)
return Settings(self.operation, d, self.repo)
else:
return self

def load_config(self):
"""
Load configuration file and set repo
Expand Down
51 changes: 51 additions & 0 deletions tests/artifactcli/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,57 @@ def test_parse_args_unknown_option(self):
else:
self.fail('SystemExit exception expected')

def test_load_environ_none(self):
ret = Settings().load_environ({})
self.assertEqual(ret, Settings())

def test_load_environ_set_from_env(self):
opt = {'bucket': 'BUCKET'}
env = {
'AWS_ACCESS_KEY_ID': 'AKIAIOSFODNN7EXAMPLE',
'AWS_SECRET_ACCESS_KEY': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
'AWS_DEFAULT_REGION': 'us-west-2'
}
exp = {
'bucket': 'BUCKET',
'access_key': 'AKIAIOSFODNN7EXAMPLE',
'secret_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
'region': 'us-west-2'
}
ret = Settings(options=opt).load_environ(env)
self.assertEqual(ret, Settings(options=exp))

def test_load_environ_already_set(self):
opt = {'access_key': 'ACCESS_KEY', 'secret_key': 'SECRET_KEY', 'region': "us-east-1"}
env = {
'AWS_ACCESS_KEY_ID': 'AKIAIOSFODNN7EXAMPLE',
'AWS_SECRET_ACCESS_KEY': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
'AWS_DEFAULT_REGION': 'us-west-2'
}
exp = {
'access_key': 'ACCESS_KEY',
'secret_key': 'SECRET_KEY',
'region': 'us-east-1'
}
ret = Settings(options=opt).load_environ(env)
self.assertEqual(ret, Settings(options=exp))

def test_load_environ_mixed(self):
opt = {'access_key': 'ACCESS_KEY', 'region': "us-east-1"}
env = {
'AWS_ACCESS_KEY_ID': 'AKIAIOSFODNN7EXAMPLE',
'AWS_SECRET_ACCESS_KEY': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
'XXX': 123,
'YYY': 456
}
exp = {
'access_key': 'ACCESS_KEY',
'secret_key': 'wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY',
'region': 'us-east-1'
}
ret = Settings(options=opt).load_environ(env)
self.assertEqual(ret, Settings(options=exp))

def test_load_config_none(self):
ret = Settings().load_config()
self.assertEqual(ret, Settings())
Expand Down

0 comments on commit 502d8bf

Please sign in to comment.