def package(): with safe_cd(SRC): for folder in ["build", "dist", PROJECT_NAME + ".egg-info"]: execute("rm", "-rf", folder) with safe_cd(SRC): execute(PYTHON, "-m", "build") os.system('say "package complete."')
def lint(): """ Lint """ with safe_cd(SRC): if os.path.isfile("lint.txt"): execute("rm", "lint.txt") with safe_cd(SRC): command = ( "{0} pylint --rcfile=pylintrc.ini {2}".format( PIPENV, PROJECT_NAME ) .strip() .replace(" ", " ") ) print(command) command = command.split(" ") # keep out of src tree, causes extraneous change detections lint_output_file_name = "lint.txt" with open(lint_output_file_name, "w") as outfile: env = config_pythonpath() subprocess.call(command, stdout=outfile, env=env) fatal_errors = sum( 1 for line in open(lint_output_file_name) if "no-member" in line or "no-name-in-module" in line or "import-error" in line ) if fatal_errors > 0: for line in open(lint_output_file_name): if ( "no-member" in line or "no-name-in-module" in line or "import-error" in line ): print(line) print("Fatal lint errors : {0}".format(fatal_errors)) exit(-1) cutoff = 100 num_lines = sum( 1 for line in open(lint_output_file_name) if "*************" not in line and "---------------------" not in line and "Your code has been rated at" not in line ) if num_lines > cutoff: raise TypeError( "Too many lines of lint : {0}, max {1}".format(num_lines, cutoff) )
def docs(): """ Docs """ with safe_cd(SRC): with safe_cd("docs"): my_env = config_pythonpath() command = "{0} make html".format(PIPENV).strip() print(command) execute_with_environment(command, env=my_env)
def package(): """ package, but don't upload """ with safe_cd(SRC): for folder in ["build", "dist", PROJECT_NAME + ".egg-info"]: execute("rm", "-rf", folder) with safe_cd(SRC): execute(PYTHON, "setup.py", "sdist", "--formats=gztar,zip")
def check_setup_py(): with safe_cd(SRC): if IS_TRAVIS: execute(PYTHON, *("setup.py check -r -s".split(" "))) else: execute(*("{0} {1} setup.py check -r -s".format( PIPENV, PYTHON).strip().split(" ")))
def pytests(): with safe_cd(SRC): my_env = config_pythonpath() command = "{0} {1} -m pytest {2}".format(PIPENV, PYTHON, "tests").strip() print(command) execute_with_environment(command, env=my_env)
def dead_code(): """ This also finds code you are working on today! """ with safe_cd(SRC): exclusions = "--exclude *settings.py,migrations/,*models.py,*_fake.py,*tests.py,*ui/admin.py" if IS_GITHUB_CI: command = ( "{0} vulture {1} {2}".format(PYTHON, PROJECT_NAME, exclusions) .strip() .split() ) else: command = ( "{0} vulture {1} {2}".format(PIPENV, PROJECT_NAME, exclusions) .strip() .split() ) output_file_name = "dead_code.txt" with open(output_file_name, "w") as outfile: env = config_pythonpath() subprocess.call(command, stdout=outfile, env=env) cutoff = 120 num_lines = sum(1 for line in open(output_file_name) if line) if num_lines > cutoff: print( "Too many lines of dead code : {0}, max {1}".format(num_lines, cutoff) ) exit(-1)
def test_change_yield_revert(self): """Safe cd should change directory, yield and revert back""" with safe_cd(self.temp_dir): os.path.exists(self.temp_dir) self.assertEqual(os.getcwd(), self.cwd, "Working directory was not restored.")
def pin_dependencies(): """ Create requirement*.txt """ with safe_cd(SRC): execute( *("{0} pipenv_to_requirements".format(PIPENV).strip().split(" ")))
def test_change_error_revert(self): """Should restore directory after an exception during yield""" try: with safe_cd(self.temp_dir): raise ValueError except ValueError: pass self.assertEqual(os.getcwd(), self.cwd, "Working directory was not restored.")
def prospector(): """ Catch a few things with a non-strict propector run """ with safe_cd(SRC): command = "{0} prospector {1} --profile {1}_style --pylint-config-file=pylintrc.ini --profile-path=.prospector".format( PIPENV, PROJECT_NAME).strip().replace(" ", " ") print(command) execute(*(command.split(" ")))
def upload_package(): """ Upload """ with safe_cd(SRC): if IS_GITHUB_CI: pass else: execute( *("{0} twine upload dist/*".format(PIPENV).strip().split(" ")))
def coverage(): """ Coverage, which is a bit redundant with nose test """ print("Coverage tests always re-run") with safe_cd(SRC): my_env = config_pythonpath() command = "{0} py.test {1} --cov={2} --cov-report html:coverage --cov-fail-under 40 --verbose".format( PIPENV, "test", PROJECT_NAME) execute_with_environment(command, my_env)
def coverage(): """ Do tests exercise enough code? """ print("Coverage tests always re-run") with safe_cd(SRC): my_env = config_pythonpath() command = "{0} py.test {1} --cov={2} --cov-report html:coverage --cov-fail-under 55 --verbose".format( PIPENV, "test", PROJECT_NAME ) execute_with_environment(command, my_env)
def check_setup_py(): # if # ValueError: ZIP does not support timestamps before 1980 # then run this to ID # find . -mtime +13700 -ls with safe_cd(SRC): if IS_TRAVIS: execute(PYTHON, *("setup.py check -r -s".split(" "))) else: execute(*("{0} {1} setup.py check -r -s".format( PIPENV, PYTHON).strip().split(" ")))
def runserver(): """Runs the server using gunicorn""" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "readonly.settings.local") with safe_cd(paths.project_paths.manage_root): project.venv_execute( 'gunicorn', '--reload', '-b', '0.0.0.0:8000', '--access-logfile', '-', 'readonly.wsgi', )
def upload_package(): """ Upload """ with safe_cd(SRC): if IS_GITHUB_CI: pass else: execute( *("{0} {1} setup.py upload".format(PIPENV, PYTHON).strip().split(" ")) )
def compile_mark_down(): """ Convert MD to RST """ with safe_cd(SRC): if IS_TRAVIS: command = "pandoc --from=markdown --to=rst --output=README.rst README.md".strip( ).split(" ") else: command = "{0} pandoc --from=markdown --to=rst --output=README.rst README.md".format( PIPENV).strip().split(" ") execute(*(command))
def compile_mark_down(): """ Convert MD to RST """ # print("Not compiling README.md because moderately complex MD makes pypi rst parser puke.") with safe_cd(SRC): if IS_TRAVIS: command = "pandoc --from=markdown --to=rst --output=README.rst README.md".strip( ).split(" ") else: command = "{0} pandoc --from=markdown --to=rst --output=README.rst README.md".format( PIPENV).strip().split(" ") execute(*(command))
def check_setup_py(): """ Setup.py checks package things including README.rst """ with safe_cd(SRC): if IS_GITHUB_CI: execute(PYTHON, *("setup.py check -r -s".split(" "))) else: execute( *( "{0} {1} setup.py check -r -s".format(PIPENV, PYTHON) .strip() .split(" ") ) )
def formatting(): with safe_cd(SRC): if sys.version_info < (3, 6): print("Black doesn't work on python 2") return command = "{0} black {1}".format(PIPENV, PROJECT_NAME).strip() print(command) result = execute_get_text(command) assert result changed = [] for line in result.split("\n"): if "reformatted " in line: file = line[len("reformatted "):].strip() changed.append(file) for change in changed: command = "git add {0}".format(change) print(command) execute(*(command.split(" ")))
def pin_dependencies(): """ Create requirement*.txt """ with safe_cd(SRC): def write_reqs(filename: str, command: str): result = execute_get_text(command) lines = result.split("\n") with open(filename, "w") as output_reqs: for i, line in enumerate(lines): if "Courtesy Notice:" not in line: output_reqs.writelines( [line + ("\n" if i != len(lines) - 1 else "")]) write_reqs("requirements.txt", "{0} lock --requirements".format("pipenv")) write_reqs("requirements-dev.txt", "{0} lock --requirements --dev".format("pipenv"))
def dead_code(): """ This also finds code you are working on today! """ with safe_cd(SRC): if IS_TRAVIS: command = "{0} vulture {1}".format(PYTHON, PROJECT_NAME).strip().split() else: command = "{0} vulture {1}".format(PIPENV, PROJECT_NAME).strip().split() output_file_name = "dead_code.txt" with open(output_file_name, "w") as outfile: env = config_pythonpath() subprocess.call(command, stdout=outfile, env=env) cutoff = 1000 print("High cutt off for dead code because not even out of beta") num_lines = sum(1 for line in open(output_file_name) if line) if num_lines > cutoff: print("Too many lines of dead code : {0}, max {1}".format(num_lines, cutoff)) exit(-1)
def compile_py(): """ Catch on the worst syntax errors """ with safe_cd(SRC): execute(PYTHON, "-m", "compileall", PROJECT_NAME)
def jiggle_version(): with safe_cd(SRC): command = "{0} jiggle_version here --module={1}".format( PIPENV, PROJECT_NAME).strip() execute(*(command.split(" ")))
def compile_py(): with safe_cd(SRC): execute(PYTHON, "-m", "compileall", PROJECT_NAME)
def clean(): with safe_cd(SRC): execute("rm", "-rf", ".mypy_cache", ".build_state", "dist", "build", PROJECT_NAME + ".egg-info", "dead_code.txt", "mypy_errors.txt", "detect-secrets-results.txt", "lint.txt")
def test_manage(*args, **kwargs): """Runs all tests through manage.py w/pynt args/kwargs""" with safe_cd(project_paths.manage_root): project.execute_manage('test', *args, **kwargs)
def clean_state(): with safe_cd(".build_state"): # wild cards don't expand by default for file in os.listdir("."): if file.startswith("last") and file.endswith(".txt"): execute("rm", "-f", str(file))
def rundocserver(): """Runs the sphinx-autobuild server""" with safe_cd('docs'): project.venv_execute('sphinx-autobuild', '.', '_build/html')
def test_venv(): """Runs all tests on venv""" with safe_cd('demo'): project.execute_manage('test', 'andablog') project.execute_manage('test')
def pwd_ls(dir="/tmp"): '''Run "pwd followed by "ls" on a given directory''' with safe_cd(dir): execute('pwd') execute("ls",dir)
def docs(*args, **kwargs): """Makes the docs w/pynt args/kwargs""" with safe_cd('docs'): project.venv_execute('sphinx-build', '-b', 'html', '.', '_build/html', *args, **kwargs)