def install(): """ Set up hugo. """ if hugo_dir.exists(): hugo_dir.rmtree(ignore_errors=True) Command( "wget", "https://github.com/gohugoio/hugo/releases/download/v0.50/hugo_0.50_Linux-64bit.tar.gz", ).in_dir(DIR.gen).run() DIR.gen.chdir() patoolib.extract_archive(DIR.gen / "hugo_0.50_Linux-64bit.tar.gz") DIR.gen.joinpath("hugo_0.50_Linux-64bit.tar").move(hugo_dir) DIR.gen.joinpath("hugo_0.50_Linux-64bit.tar.gz").remove() Command( "wget", "https://github.com/gohugoio/hugo/releases/download/v0.31.1/hugo_0.31.1_Linux-64bit.tar.gz", ).in_dir(DIR.gen).run() DIR.gen.chdir() patoolib.extract_archive(DIR.gen / "hugo_0.31.1_Linux-64bit.tar.gz") DIR.gen.joinpath("hugo_0.31.1_Linux-64bit.tar/").move(hugo_dir) DIR.gen.joinpath("hugo_0.31.1_Linux-64bit.tar.gz").remove()
def build(self): if self.incomplete(): self.buildpath.rmtree(ignore_errors=True) self.buildpath.mkdir() download_to = self.tmp / "postgresql-{}.tar.gz".format( self.version) utils.download_file( download_to, "https://ftp.postgresql.org/pub/source/v{0}/postgresql-{0}.tar.gz" .format(self.version), ) utils.extract_archive(download_to, self.buildpath) download_to.remove() for filepath in self.buildpath.joinpath("postgresql-{}".format( self.version)).listdir(): shutil.move(filepath, self.buildpath) self.buildpath.joinpath("postgresql-{}".format( self.version)).rmdir() print("Running ./configure --with-openssl --prefix={}".format( self.buildpath)) Command("./configure")("--with-openssl", "--prefix={}".format( self.buildpath)).in_dir(self.buildpath).run() print("Running make world") Command("make", "world").in_dir(self.buildpath).run() print("Running make install") Command("make")("install").in_dir(self.buildpath).run() print("Running make install for contrib") Command("make")("install").in_dir(self.buildpath / "contrib").run() self.verify() self.refingerprint()
def chromedriver_bin(self): if self.os_name == "linux": return Command(self.buildpath / "chromedriver_linux64" / "chromedriver") else: return Command(self.buildpath / "chromedriver_mac64" / "chromedriver")
def deploy(version): """ Deploy to pypi as specified version. """ NAME = "strictyaml" git = Command("git").in_dir(DIR.project) version_file = DIR.project.joinpath("VERSION") old_version = version_file.bytes().decode('utf8') if version_file.bytes().decode("utf8") != version: DIR.project.joinpath("VERSION").write_text(version) git("add", "VERSION").run() git("commit", "-m", "RELEASE: Version {0} -> {1}".format(old_version, version)).run() git("push").run() git("tag", "-a", version, "-m", "Version {0}".format(version)).run() git("push", "origin", version).run() else: git("push").run() # Set __version__ variable in __init__.py, build sdist and put it back initpy = DIR.project.joinpath(NAME, "__init__.py") original_initpy_contents = initpy.bytes().decode('utf8') initpy.write_text( original_initpy_contents.replace("DEVELOPMENT_VERSION", version)) python("setup.py", "sdist").in_dir(DIR.project).run() initpy.write_text(original_initpy_contents) # Upload to pypi python("-m", "twine", "upload", "dist/{0}-{1}.tar.gz".format(NAME, version)).in_dir(DIR.project).run()
def regression(): """ Run regression testing - lint and then run all tests. """ # HACK: Start using hitchbuildpy to get around this. Command("touch", DIR.project.joinpath("pathquery", "__init__.py").abspath()).run() storybook = _storybook({}).only_uninherited() #storybook.with_params(**{"python version": "2.7.10"})\ #.ordered_by_name().play() Command("touch", DIR.project.joinpath("pathquery", "__init__.py").abspath()).run() storybook.with_params(**{ "python version": "3.5.0" }).ordered_by_name().play() lint()
def runcommand(): """ hk runcommand -> "ls -l" """ # See https://hitchdev.com/commandlib/ # -- for more details on how to use commandlib Command("ls", "-l").in_dir(DIR.key).run()
def deploy(project_path, name, version): git = Command("git").in_dir(project_path) version_file = project_path.joinpath("VERSION") old_version = version_file.bytes().decode("utf8") if version_file.bytes().decode("utf8") != version: project_path.joinpath("VERSION").write_text(version) git("add", "VERSION").run() git("commit", "-m", "RELEASE: Version {0} -> {1}".format(old_version, version)).run() git("push").run() git("tag", "-a", version, "-m", "Version {0}".format(version)).run() git("push", "origin", version).run() else: git("push").run() # Set __version__ variable in __init__.py, build sdist and put it back initpy = project_path.joinpath(name, "__init__.py") original_initpy_contents = initpy.bytes().decode("utf8") initpy.write_text( original_initpy_contents.replace("DEVELOPMENT_VERSION", version)) python("setup.py", "sdist").in_dir(project_path).run() initpy.write_text(original_initpy_contents) # Upload to pypi python("-m", "twine", "upload", "dist/{0}-{1}.tar.gz".format(name, version)).in_dir(project_path).run()
def rerun(version="3.7.0"): """ Rerun last example code block with specified version of python. """ Command(DIR.gen.joinpath("py{0}".format(version), "bin", "python"))( DIR.gen.joinpath("state", "working", "examplepythoncode.py")).in_dir( DIR.gen.joinpath("state", "working")).run()
def bash(): """ Run bash """ from commandlib import Command Command("bash").run()
def download_file(downloaded_file_path, url, max_connections=2, max_concurrent=5): """Download file to specified location.""" file_path = Path(downloaded_file_path) assert file_path.isabs( ), "download file path must be absolute, not relative" if file_path.exists(): log("{} already downloaded".format(file_path)) return log("Downloading: {}\n".format(url)) aria2c = Command("aria2c") aria2c = aria2c("--max-connection-per-server={}".format(max_connections)) aria2c = aria2c("--max-concurrent-downloads={}".format(max_concurrent)) try: aria2c("--dir={}".format(file_path.dirname()), "--out={}.part".format(file_path.basename()), url).run() except CommandError: raise DownloadError( "Failed to download {}. Re-running may fix the problem.".format( url)) shutil.move(file_path + ".part", file_path)
def download_file(downloaded_file_name, url, max_connections=2, max_concurrent=5): """Download file to specified location.""" if os.path.exists(downloaded_file_name): return log("Downloading: {}\n".format(url)) aria2c = Command("aria2c") aria2c = aria2c("--max-connection-per-server={}".format(max_connections)) aria2c = aria2c("--max-concurrent-downloads={}".format(max_concurrent)) try: if os.path.isabs(downloaded_file_name): aria2c("--dir=/", "--out={}.part".format(downloaded_file_name), url).run() else: aria2c("--dir=.", "--out={}.part".format(downloaded_file_name), url).run() except CommandError: raise DownloadError( "Failed to download {}. Re-running may fix the problem.".format( url)) shutil.move(downloaded_file_name + ".part", downloaded_file_name)
def hvenvup(package, directory): """ Install a new version of a package in the hitch venv. """ pip = Command(DIR.gen.joinpath("hvenv", "bin", "pip")) pip("uninstall", package, "-y").run() pip("install", DIR.project.joinpath(directory).abspath()).run()
def regression(): """ Run regression testing - lint and then run all tests. """ # HACK: Start using hitchbuildpy to get around this. Command("touch", DIR.project.joinpath("strictyaml", "representation.py").abspath()).run() storybook = _storybook({}).only_uninherited() print( storybook.with_params(**{"python version": "2.7.10"}).ordered_by_name().play().report() ) Command("touch", DIR.project.joinpath("strictyaml", "representation.py").abspath()).run() print( storybook.with_params(**{"python version": "3.5.0"}).ordered_by_name().play().report() ) lint() doctest() doctest(version="2.7.10")
def push(): """ Push changes in hitchdev.github.com. """ git = Command("git").in_dir(DIR.project / ".." / "hitchdev.github.io") git("add", ".").run() git("commit", "-m", "Updates").run() git("push").run()
def _rsync(self, from_path, to_path): Command("rsync")( "--del", "-av", # Trailing slash so contents are moved not whole dir from_path + "/", to_path, ).run()
def hello(argument): """ Try running "hk hello world". """ # https://pathpy.readthedocs.io/en/stable/api.html DIR.gen.joinpath("hello.txt").write_text(argument) # https://hitchdev.com/commandlib/ Command("cat", "hello.txt").in_dir(DIR.gen).run()
def rerun(): """ Rerun last example code block with specified version of python. """ from commandlib import Command Command(DIR.gen.joinpath("py{0}".format("3.7.0"), "bin", "python"))(DIR.gen.joinpath( "working", "examplepythoncode.py")).in_dir( DIR.gen.joinpath("working")).run()
def rerun(): """ Rerun last example code block with specified version of python. """ from commandlib import Command version = _personal_settings().data['params']['python version'] Command(DIR.gen.joinpath("py{0}".format(version), "bin", "python"))(DIR.gen.joinpath( "working", "examplepythoncode.py")).in_dir( DIR.gen.joinpath("working")).run()
def ipython(): """ Run ipython in strictyaml virtualenv. """ DIR.gen.joinpath("example.py").write_text(("from strictyaml import *\n" "import IPython\n" "IPython.embed()\n")) from commandlib import Command version = _personal_settings().data['params']['python version'] Command(DIR.gen.joinpath("py{0}".format(version), "bin", "python"))(DIR.gen.joinpath("example.py")).run()
def buildprojectdocs(projectdir, projectname): """ Build documentation from a project. """ Command("hk", "docgen").in_dir(DIR.project.parent / projectdir).run() project_docs = DIR.project.parent / projectdir / "hitch" / "gen" / "docs" DIR.project.joinpath("content", projectname).rmtree(ignore_errors=True) for document in list(project_docs.walkfiles()): relative_path = document.replace(project_docs + '/', "") content_path = DIR.project.joinpath("content", projectname) write_path = content_path.joinpath(relative_path) if not write_path.dirname().exists(): write_path.dirname().makedirs() if write_path.exists(): write_path.remove() document.copy(write_path)
def prepdeploy(self, engine, version): """Prepare a deployment with new version, README and docs, but do not push.""" git = Command("git").in_dir(self._path.project) version_file = self._path.project.joinpath("VERSION") old_version = version_file.text().rstrip() from packaging.version import parse if not parse(version) > parse(): raise ToolkitError( "New version {} must be above old version {}".format( version, old_version)) version_file.write_text(version) self.readmegen(engine) self.docgen(engine) self.validate_readmegen(engine) self.validate_docgen(engine) git("add", ".").run() git("commit", "-m", "RELEASE: Version {0} -> {1}".format(old_version, version)).run() git("tag", "-a", version, "-m", "Version {0}".format(version)).run()
def ensure_built(self): if self.should_rebuild(): if self.buildpath.exists(): self.buildpath.rmtree() self.buildpath.mkdir() download_urls = self._download_urls[self.version] if self.os_name == "linux": chrome_download_url = download_urls["linux_chrome"] chromedriver_download_url = download_urls["linux_chromedriver"] elif self.os_name == "mac": chrome_download_url = download_urls["mac_chrome"] chromedriver_download_url = download_urls["mac_chromedriver"] download_to = self.tmp / "chrome-{}.zip".format(self.version) utils.download_file(download_to, chrome_download_url) if self.os_name == "mac": # patool has a bug on mac that breaks chromium Command("unzip", download_to).in_dir(self.buildpath).run() else: utils.extract_archive(download_to, self.buildpath) download_to.remove() chrome_bin = Path(self.chrome_bin) chrome_bin.chmod(chrome_bin.stat().st_mode | stat.S_IEXEC) # Install chromedriver download_to = self.tmp / "chromedriver-{}.zip".format(self.version) utils.download_file(download_to, chromedriver_download_url) utils.extract_archive(download_to, self.buildpath) download_to.remove() chromedriver_bin = Path(self.chromedriver_bin) chromedriver_bin.chmod(chromedriver_bin.stat().st_mode | stat.S_IEXEC) self.verify() self.refingerprint()
def pushdeploy(self): """Push a new deployment to pypi.""" git = Command("git").in_dir(self._path.project) version = self._path.project.joinpath("VERSION").text().rstrip() git("push", "--follow-tags").run() # Set __version__ variable in __init__.py, build sdist and put it back initpy = self._path.project.joinpath(self._project_name, "__init__.py") original_initpy_contents = initpy.bytes().decode("utf8") initpy.write_text( original_initpy_contents.replace("DEVELOPMENT_VERSION", version)) python("setup.py", "sdist").in_dir(self._path.project).run() initpy.write_text(original_initpy_contents) # Upload to pypi python( "-m", "twine", "upload", "dist/{0}-{1}.tar.gz".format(self._project_name, version), ).in_dir(self._path.project).run()
def set_up(self): """Set up your applications and the test environment.""" self.python_package = hitchpython.PythonPackage( self.preconditions.get('python_version', '3.5.0') ) self.python_package.build() # Uninstall and reinstall call([self.python_package.pip, "install", "ipython==1.2.1", ], stdout=PIPE) call([self.python_package.pip, "install", "pyzmq", ], stdout=PIPE) call([self.python_package.pip, "install", "flake8", ], stdout=PIPE) call([self.python_package.pip, "uninstall", "dumbyaml", "-y"], stdout=PIPE) #chdir(PROJECT_DIRECTORY) #check_call([self.python_package.python, "setup.py", "install"], stdout=PIPE) run(Command([self.python_package.python, "setup.py", "install"]).in_dir(PROJECT_DIRECTORY)) #print(Command([self.python_package.python, "setup.py", "install"]).arguments) self.services = hitchserve.ServiceBundle( PROJECT_DIRECTORY, startup_timeout=8.0, shutdown_timeout=1.0 ) self.services['IPython'] = hitchpython.IPythonKernelService(self.python_package) self.services.startup(interactive=False) self.ipython_kernel_filename = self.services['IPython'].wait_and_get_ipykernel_filename() self.ipython_step_library = hitchpython.IPythonStepLibrary() self.ipython_step_library.startup_connection(self.ipython_kernel_filename) self.run_command = self.ipython_step_library.run self.assert_true = self.ipython_step_library.assert_true self.assert_exception = self.ipython_step_library.assert_exception self.shutdown_connection = self.ipython_step_library.shutdown_connection self.run_command("import dumbyaml") self.run_command("import yaml")
def doctest(version="3.5.0"): Command(DIR.gen.joinpath("py{0}".format(version), "bin", "python"))( "-m", "doctest", "-v", DIR.project.joinpath("strictyaml", "utils.py") ).in_dir(DIR.project.joinpath("strictyaml")).run()
import threading import pdfkit from commandlib import Command if __name__ == "__main__": PORT = "5003" HOST = "127.0.0.1" flask = Command("flask", "run", "-p", PORT, "-h", HOST).with_env(FLASK_ENV="development", FLASK_APP="app/__init__.py") thread = threading.Thread(target=flask.run, daemon=True) thread.start() pdfkit.from_url( f"http://{HOST}:{PORT}/?for_pdf=True", "JACK_WARDELL_CV.pdf", options={"zoom": 0.95}, )
def initial_hk(self, args="", in_dir=""): if self._build: Command("hk", *shlex.split(args)).in_dir(self.path.state / in_dir).run()
def hugo(*args): """ Run Hugo """ Command(hugo_dir/"hugo").in_dir(DIR.project)(*args).run()
def ipy(): """ Run IPython in environment." """ Command(DIR.gen.joinpath("py3.5.0", "bin", "ipython")).run()
def tear_down(self): for vagrantfile in pathquery(self.path.state).named("Vagrantfile"): Command("vagrant", "destroy", "-f").in_dir(vagrantfile.abspath().dirname()).run()