Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce new command arg prepare. (second attempt) #38

Merged
merged 3 commits into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,14 @@ AppEnv itself is tested against Python 3.6+.

```
$ ./appenv --help
usage: appenv [-h] {update-lockfile,init,reset,python,run} ...
usage: appenv [-h] {update-lockfile,init,reset,prepare,python,run} ...

positional arguments:
{update-lockfile,init,reset,python,run}
{update-lockfile,init,reset,prepare,python,run}
update-lockfile Update the lock file.
init Create a new appenv project.
reset Reset the environment.
prepare Prepare the venv.
python Spawn the embedded Python interpreter REPL
run Run a script from the bin/ directory of the virtual env.

Expand Down
13 changes: 9 additions & 4 deletions src/appenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ def meta(self):
p = subparsers.add_parser("reset", help="Reset the environment.")
p.set_defaults(func=self.reset)

p = subparsers.add_parser('prepare', help='Prepare the venv.')
p.set_defaults(func=self.prepare)

p = subparsers.add_parser(
"python", help="Spawn the embedded Python interpreter REPL")
p.set_defaults(func=self.python)
Expand All @@ -280,7 +283,7 @@ def meta(self):
args.func(args, remaining)

def run(self, command, argv):
self._prepare()
self.prepare()
cmd = os.path.join(self.env_dir, 'bin', command)
argv = [cmd] + argv
os.environ['APPENV_BASEDIR'] = self.base
Expand Down Expand Up @@ -309,7 +312,7 @@ def _hash_requirements(self):
hash_content = f.read()
return hashlib.new("sha256", hash_content).hexdigest()

def _prepare(self):
def prepare(self, args=None, remaining=None):
# copy used requirements.txt into the target directory so we can use
# that to check later
# - when to clean up old versions? keep like one or two old revisions?
Expand All @@ -320,10 +323,12 @@ def _prepare(self):
self._assert_requirements_lock()

hash_content = []
requirements = open("requirements.lock", "rb").read()
with open("requirements.lock", "rb") as f:
requirements = f.read()
hash_content.append(os.fsencode(os.path.realpath(sys.executable)))
hash_content.append(requirements)
hash_content.append(open(__file__, "rb").read())
with open(__file__, "rb") as f:
hash_content.append(f.read())
env_hash = hashlib.new("sha256",
b"".join(hash_content)).hexdigest()[:8]
env_dir = os.path.join(self.appenv_dir, env_hash)
Expand Down
15 changes: 15 additions & 0 deletions tests/test_prepare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
import appenv
import io


def test_prepare_creates_envdir(workdir, monkeypatch):
monkeypatch.setattr('sys.stdin', io.StringIO('ducker\nducker<2.0.2\n\n'))
os.makedirs(os.path.join(workdir, 'ducker'))

env = appenv.AppEnv(os.path.join(workdir, 'ducker'))
env.init()
assert not os.path.exists(env.appenv_dir)
env.update_lockfile()
env.prepare()
assert os.path.exists(env.appenv_dir)
Loading