Skip to content

Commit

Permalink
Merge pull request #38 from sweh/patch-2
Browse files Browse the repository at this point in the history
Introduce new command arg `prepare`. (second attempt)
  • Loading branch information
elikoga authored Feb 21, 2024
2 parents 2338627 + 0be01c4 commit 6ab8f61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
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)

0 comments on commit 6ab8f61

Please sign in to comment.