2016-08-10 06:53:33 +00:00
|
|
|
#coding: UTF-8
|
|
|
|
|
2018-01-16 09:10:26 +00:00
|
|
|
import logging
|
2016-08-10 06:53:33 +00:00
|
|
|
import os
|
|
|
|
import re
|
2018-01-16 09:10:26 +00:00
|
|
|
import sys
|
2016-08-10 06:53:33 +00:00
|
|
|
from contextlib import contextmanager
|
2018-01-16 09:10:26 +00:00
|
|
|
from os.path import abspath, basename, exists, expanduser, join
|
|
|
|
from subprocess import PIPE, CalledProcessError, Popen
|
2016-08-10 06:53:33 +00:00
|
|
|
|
|
|
|
import requests
|
2018-01-16 09:10:26 +00:00
|
|
|
import termcolor
|
|
|
|
|
|
|
|
try:
|
|
|
|
from functools import lru_cache
|
|
|
|
except ImportError:
|
|
|
|
from backports.functools_lru_cache import lru_cache
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2016-08-10 06:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _color(s, color):
|
|
|
|
return s if not os.isatty(sys.stdout.fileno()) \
|
|
|
|
else termcolor.colored(str(s), color)
|
|
|
|
|
|
|
|
|
|
|
|
def green(s):
|
|
|
|
return _color(s, 'green')
|
|
|
|
|
|
|
|
|
|
|
|
def red(s):
|
|
|
|
return _color(s, 'red')
|
|
|
|
|
|
|
|
|
|
|
|
def debug(fmt, *a):
|
|
|
|
logger.debug(green(fmt), *a)
|
|
|
|
|
|
|
|
|
|
|
|
def info(fmt, *a):
|
|
|
|
logger.info(green(fmt), *a)
|
|
|
|
|
|
|
|
|
|
|
|
def warning(fmt, *a):
|
|
|
|
logger.warn(red(fmt), *a)
|
|
|
|
|
|
|
|
|
2018-01-16 09:10:26 +00:00
|
|
|
def shell(cmd, inputdata=None, wait=True, **kw):
|
2016-08-10 06:53:33 +00:00
|
|
|
info('calling "%s" in %s', cmd, kw.get('cwd', os.getcwd()))
|
|
|
|
kw['shell'] = not isinstance(cmd, list)
|
|
|
|
kw['stdin'] = PIPE if inputdata else None
|
|
|
|
p = Popen(cmd, **kw)
|
|
|
|
if inputdata:
|
|
|
|
p.communicate(inputdata)
|
2018-01-16 09:10:26 +00:00
|
|
|
if wait:
|
|
|
|
p.wait()
|
|
|
|
if p.returncode:
|
|
|
|
raise CalledProcessError(p.returncode, cmd)
|
|
|
|
else:
|
|
|
|
return p
|
2016-08-10 06:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def cd(path):
|
|
|
|
olddir = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(olddir)
|
|
|
|
|
|
|
|
|
|
|
|
def chdir(func):
|
|
|
|
def wrapped(self, *w, **kw):
|
|
|
|
with cd(self.projectdir):
|
|
|
|
return func(self, *w, **kw)
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
2018-01-16 09:10:26 +00:00
|
|
|
|
2016-08-10 06:53:33 +00:00
|
|
|
def setup_logging():
|
|
|
|
kw = {
|
|
|
|
'format': '[%(asctime)s][%(module)s]: %(message)s',
|
|
|
|
'datefmt': '%m/%d/%Y %H:%M:%S',
|
|
|
|
'level': logging.DEBUG,
|
|
|
|
'stream': sys.stdout,
|
|
|
|
}
|
|
|
|
|
|
|
|
logging.basicConfig(**kw)
|
2018-01-16 09:10:26 +00:00
|
|
|
logging.getLogger('requests.packages.urllib3.connectionpool'
|
|
|
|
).setLevel(logging.WARNING)
|
|
|
|
|
|
|
|
|
|
|
|
def mkdirs(*paths):
|
|
|
|
for path in paths:
|
|
|
|
if not exists(path):
|
|
|
|
os.mkdir(path)
|
|
|
|
|
2020-01-14 02:55:55 +00:00
|
|
|
def on_github_actions():
|
|
|
|
return 'GITHUB_ACTIONS' in os.environ
|
2018-01-16 09:10:26 +00:00
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def cd(path):
|
|
|
|
path = expanduser(path)
|
|
|
|
olddir = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(olddir)
|