Skip to content

Commit

Permalink
feat: add activation, installation, and cleanup scripts for virtual e…
Browse files Browse the repository at this point in the history
…nvironment management
  • Loading branch information
zackees committed Jan 22, 2025
1 parent 7a3d326 commit 6ab3e95
Show file tree
Hide file tree
Showing 13 changed files with 226 additions and 32 deletions.
116 changes: 116 additions & 0 deletions activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Copyright (c) 2020-202x The virtualenv developers
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

# Get script path (only used if environment is relocatable).
if [ -n "${BASH_VERSION:+x}" ] ; then
SCRIPT_PATH="${BASH_SOURCE[0]}"
if [ "$SCRIPT_PATH" = "$0" ]; then
# Only bash has a reasonably robust check for source'dness.
echo "You must source this script: \$ source $0" >&2
exit 33
fi
elif [ -n "${ZSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${(%):-%x}"
elif [ -n "${KSH_VERSION:+x}" ] ; then
SCRIPT_PATH="${.sh.file}"
fi

deactivate () {
unset -f pydoc >/dev/null 2>&1 || true

# reset old environment variables
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
PATH="$_OLD_VIRTUAL_PATH"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# The hash command must be called to get it to forget past
# commands. Without forgetting past commands the $PATH changes
# we made may not be respected
hash -r 2>/dev/null

if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
PS1="$_OLD_VIRTUAL_PS1"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
unset VIRTUAL_ENV_PROMPT
if [ ! "${1-}" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV='C:\Users\niteris\dev\static-ffmpeg\.venv'
if ([ "$OSTYPE" = "cygwin" ] || [ "$OSTYPE" = "msys" ]) && $(command -v cygpath &> /dev/null) ; then
VIRTUAL_ENV=$(cygpath -u "$VIRTUAL_ENV")
fi
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/Scripts:$PATH"
export PATH

if [ "xstatic-ffmpeg" != x ] ; then
VIRTUAL_ENV_PROMPT="(static-ffmpeg) "
else
VIRTUAL_ENV_PROMPT="($(basename "$VIRTUAL_ENV")) "
fi
export VIRTUAL_ENV_PROMPT

# unset PYTHONHOME if set
if ! [ -z "${PYTHONHOME+_}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1-}"
PS1="${VIRTUAL_ENV_PROMPT}${PS1-}"
export PS1
fi

# Make sure to unalias pydoc if it's already there
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true

pydoc () {
python -m pydoc "$@"
}

# The hash command must be called to get it to forget past
# commands. Without forgetting past commands the $PATH changes
# we made may not be respected
hash -r 2>/dev/null
22 changes: 22 additions & 0 deletions clean
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash
set -x
rm -rf .tox
rm -rf build
rm -rf dist
rm -rf venv
rm -rf .venv
rm -rf .env_*
# remove all *.pyc files
find . -name "*.pyc" -exec rm -rf {} \;
# remove all *.egg files
find . -name "*.egg" -exec rm -rf {} \;
rm -rf build
rm -rf dist
rm -rf *.egg-info
rm -rf .eggs
rm -rf .tox
rm -rf .cache
rm -rf .pytest_cache
rm -rf .mypy_cache
rm -rf .coverage
rm -rf .iso_env
Empty file added downloaded_files/dashboard.lock
Empty file.
19 changes: 19 additions & 0 deletions install
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -e
uv venv --python 3.11
uv pip install -r requirements.testing.txt
uv pip install .
# This is needed to force the installation to finalize.
uv run python -c "import os; _ = os.getcwd()"
set +e

# if ./activate exists, remove it
if [ -f activate ]; then
rm activate
fi
# symlink activate to .venv/bin/activate on linux/mac and .venv/Scripts/activate on windows
if [[ "$OSTYPE" == "linux-gnu"* || "$OSTYPE" == "darwin"* ]]; then
ln -s .venv/bin/activate activate
else
ln -s .venv/Scripts/activate activate
fi
17 changes: 17 additions & 0 deletions lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
set -e

echo Running ruff static_ffmpeg
uv run ruff check --fix static_ffmpeg
echo Running ruff tests
uv run ruff check --fix tests
echo Running black static_ffmpeg tests
uv run black static_ffmpeg tests
echo Running isort static_ffmpeg tests
uv run isort --profile black static_ffmpeg tests
echo Running flake8 static_ffmpeg tests
uv run flake8 static_ffmpeg tests
echo Running mypy static_ffmpeg
uv run mypy static_ffmpeg tests
echo Linting complete!
exit 0
3 changes: 2 additions & 1 deletion requirements.testing.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flake8
mypy
pylint
pytest
pytest
ruff
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
requests
filelock
progress
2 changes: 2 additions & 0 deletions static_ffmpeg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
"""

from ._add_paths import add_paths

__all__ = ["add_paths"]
15 changes: 9 additions & 6 deletions static_ffmpeg/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import requests # type: ignore
from filelock import FileLock, Timeout
from progress.spinner import Spinner # type: ignore

TIMEOUT = 10 * 60 # Wait upto 10 minutes to validate install
# otherwise break the lock and install anyway.
Expand Down Expand Up @@ -52,12 +53,14 @@ def download_file(url, local_path):
with requests.get(url, stream=True, timeout=TIMEOUT) as req:
req.raise_for_status()
with open(local_path, "wb") as file_d:
for chunk in req.iter_content(chunk_size=8192 * 16):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
# if chunk:
sys.stdout.write(".")
file_d.write(chunk)
with Spinner() as spinner:
for chunk in req.iter_content(chunk_size=8192 * 16):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
# if chunk:
sys.stdout.write(".")
file_d.write(chunk)
spinner.next()
sys.stdout.write(f"\nDownload of {url} -> {local_path} completed.\n")
return local_path

Expand Down
4 changes: 4 additions & 0 deletions test
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

echo "Running unittests"
uv run pytest tests -v
49 changes: 28 additions & 21 deletions tests/test_bug9.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
Tests that https://github.com/zackees/static_ffmpeg/issues/9 is resolved. This bug did not allow spaces in the names.
"""


import os
import stat
import subprocess
import sys
import unittest

from static_ffmpeg import run

_HERE = os.path.dirname(os.path.abspath(__file__))
_TEST_FILE = os.path.join(_HERE, "bug 9.mp4")
_OUT_FILE = os.path.join(_HERE, "test_clip_out.mp4")
Expand All @@ -21,25 +16,37 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def test_platform_executable(self) -> None:
subprocess.call([
'static_ffmpeg', # call ffmpeg
'-ss', "0:00", # our clip starts here
'-i', _TEST_FILE, # this is the video to be converted
'-t', "1", #how long our clip is
'-threads', '4', # use 4 threads for the video conversion
'-c:v', 'libvpx-vp9', # c[odec]:v[ideo] - we use libvpx cause we want webm
'-c:a', 'libvorbis', # c[odec]:a[udio] - this is the one everyone else was using
'-b:v', '400k', # reccomended video bitrate
'-b:a', '192k', # reccomended audio bitrate
'-deadline', 'good', # setting for quality vs. speed (best, good, realtime (fastest)); boundry for quality vs. time set the following settings
'-qmin', '0', # quality minimum boundry. (lower means better)
'-qmax', '50', # quality maximum boundry (higher means worse)
_OUT_FILE # file to be written out
])
subprocess.call(
[
"static_ffmpeg", # call ffmpeg
"-ss",
"0:00", # our clip starts here
"-i",
_TEST_FILE, # this is the video to be converted
"-t",
"1", # how long our clip is
"-threads",
"4", # use 4 threads for the video conversion
"-c:v",
"libvpx-vp9", # c[odec]:v[ideo] - we use libvpx cause we want webm
"-c:a",
"libvorbis", # c[odec]:a[udio] - this is the one everyone else was using
"-b:v",
"400k", # reccomended video bitrate
"-b:a",
"192k", # reccomended audio bitrate
"-deadline",
"good", # setting for quality vs. speed (best, good, realtime (fastest)); boundry for quality vs. time set the following settings
"-qmin",
"0", # quality minimum boundry. (lower means better)
"-qmax",
"50", # quality maximum boundry (higher means worse)
_OUT_FILE, # file to be written out
]
)
if os.path.exists(_OUT_FILE):
os.remove(_OUT_FILE)



if __name__ == "__main__":
unittest.main()
2 changes: 2 additions & 0 deletions tests/test_static_add_paths_weak.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

from static_ffmpeg import _add_paths


class static_ffmpeg_add_paths_weak(unittest.TestCase):

def test_add_weak(self) -> None:
_add_paths._has = lambda name: True
installed: bool = _add_paths.add_paths(weak=True)
self.assertFalse(installed)


if __name__ == "__main__":
unittest.main()
8 changes: 4 additions & 4 deletions tests/test_static_ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import sys
import unittest

from static_ffmpeg import run, add_paths
from static_ffmpeg import add_paths, run


class static_ffmpegTester(unittest.TestCase):
Expand All @@ -20,11 +20,11 @@ def test_run_static_ffmpeg(self) -> None:

def test_run_static_ffprobe(self) -> None:
subprocess.check_output(["static_ffprobe", "-version"])

def test_run_no_shell(self) -> None:
ffmpeg_exe, ffprobe_exe = run.get_or_fetch_platform_executables_else_raise()
subprocess.check_output([ffmpeg_exe, '-version'])
subprocess.check_output([ffprobe_exe, '-version'])
subprocess.check_output([ffmpeg_exe, "-version"])
subprocess.check_output([ffprobe_exe, "-version"])

def test_add_paths(self) -> None:
add_paths()
Expand Down

0 comments on commit 6ab3e95

Please sign in to comment.