diff --git a/.travis.yml b/.travis.yml index 879cd92..749c16d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,18 +2,20 @@ language: python python: - "2.7" install: - - "pip install ." + - "pip -v install ." script: - echo "no tests yet" -before_deploy: +before_install: - "sudo apt-get install pandoc" - "echo `pwd`" - - "pandoc --from=markdown --to=rst --output=README.rst README.md" + - "pandoc --from=markdown --to=rst --output=$TRAVIS_BUILD_DIR/README.rst README.md" + - "ls -alhrt README*" deploy: # test pypi - provider: pypi distributions: "sdist bdist_wheel" server: https://testpypi.python.org/pypi + skip_cleanup: true user: "deepcompute" password: secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U=" @@ -23,6 +25,7 @@ deploy: # pypi - provider: pypi distributions: "sdist bdist_wheel" + skip_cleanup: true user: "deepcompute" password: secure: "fkHbFReSrTMDhMP1T26rlk0jVwmWyz8AkJqjThLWd0JBzxpAzLpeDCweCFuJk9hk1bnVnbJN/SgRBcRyYDkGCrarEtu3WkesGnIaqFKbMnu5Zwytex3ghPDK4ZK6+GVYSyljbYJUYXDZLMRoumiMOm7IHGVZ8soA02XNy7lBe36o5Dj3QfpRGUZLb2/nbMWIBvltecuhMk66yWKkSlwy0RjsOTn9XwjtTr5hGC6XD9kdC6NUeK6eSmdU/pa6DyOVSoKXEBims100g4gfOck5P8gXq8ssGVnWDkU9pvTKz085ayeepVDMygI5xug1U2zM3dTvTsRnsjr3B4FsAewfc9uh/sfFpr3v2oOIdUARf2sphJEja+0Z606s9DfxFMWHopouG9JDmzPwZBJNhJRIVW93VfODsUPejKtWJd259SfNRioj+ORP4b7wVQVfjvZM5CifDKrlMTBp1adt4bE8C/DepcezwgKLjRiGU0N0ndg7oJ9XcxUppzOwtEAPx0rLymWCz03O2gCXbr5NrtFZ/kbzPpMhDW1cnSW8zTzSQpzpXrqj5la+KIBynIp+EQZdxvM3NOyHFkpswOr1bsaFSmJ96ejrohIUq95FamQI6l8yo8iJiqROTLw6YUbcLa+F8qEP8ZdthPv98uV0ZAddduO9nysWzu2Hit3bXT4dQ3U=" diff --git a/basescript/basescript.py b/basescript/basescript.py index 8a3a0bd..9d4b04f 100644 --- a/basescript/basescript.py +++ b/basescript/basescript.py @@ -53,11 +53,20 @@ def define_log_processors(self): """ # these processors should accept logger, method_name and event_dict # and return a new dictionary which will be passed as event_dict to the next one. - return [ + + # NOTE if we are using a tty, then we must add our own timestamp. + processors = [] + + if sys.stderr.isatty(): + processors.append(structlog.processors.TimeStamper(fmt="%Y-%m-%d %H:%M%S")) + + processors.extend([ structlog.stdlib.PositionalArgumentsFormatter(), structlog.processors.StackInfoRenderer(), structlog.processors.format_exc_info, - ] + ]) + + return processors def define_log_renderer(self): """ @@ -66,6 +75,16 @@ def define_log_renderer(self): # it must accept a logger, method_name and event_dict (just like processors) # but must return the rendered string, not a dictionary. # TODO tty logic + if self.args.log_format == "json": + return structlog.processors.JSONRenderer() + + if self.args.log_format == "pretty": + return structlog.dev.ConsoleRenderer() + + # log format is None, we need to guess from the tty + if sys.stderr.isatty(): + return structlog.dev.ConsoleRenderer() + return structlog.processors.JSONRenderer() def define_log_pre_format_hooks(self): @@ -117,7 +136,7 @@ def processor(logger, method_name, event_dict): structlog.configure( processors=processors, context_class=dict, - logger_factory=LevelLoggerFactory(level=level), + logger_factory=LevelLoggerFactory(stream=sys.stderr, level=level), wrapper_class=BoundLevelLogger, cache_logger_on_first_use=True, ) @@ -161,6 +180,13 @@ def define_baseargs(self, parser): help='Name to identify this instance') parser.add_argument('--log-level', default=self.LOG_LEVEL, help='Logging level as picked from the logging module') + parser.add_argument('--log-format', default=None, + # TODO add more formats + choices=("json", "pretty",), + help=("Force the format of the logs. By default, if the " + "command is from a terminal, print colorful logs. " + "Otherwise print json."), + ) def define_args(self, parser): ''' diff --git a/setup.py b/setup.py index 0a7ca4a..31bbea5 100644 --- a/setup.py +++ b/setup.py @@ -1,15 +1,29 @@ from setuptools import setup, find_packages import os -long_description = "" -rst_readme = os.path.join( - os.path.dirname(__file__), "README.rst" -) -if os.path.exists(rst_readme): - with open(rst_readme) as fp: - long_description = rst_readme.read() +HERE = os.path.abspath(os.path.dirname(__file__)) +def get_long_description(): + dirs = [ HERE ] + if os.getenv("TRAVIS"): + dirs.append(os.getenv("TRAVIS_BUILD_DIR")) + + long_description = "" + + for d in dirs: + rst_readme = os.path.join(d, "README.rst") + if not os.path.exists(rst_readme): + print "failed to find %s" % rst_readme + continue + + print "found rst readme %s" % rst_readme + with open(rst_readme) as fp: + long_description = fp.read() + + return long_description + +long_description = get_long_description() -version = '0.1.6' +version = '0.1.7' setup( name="basescript", version=version, @@ -23,6 +37,7 @@ license='MIT License', install_requires=[ "structlog", + "colorama", ], package_dir={'basescript': 'basescript'}, packages=find_packages('.'),