Skip to content

Commit

Permalink
Determine the binary and requirements from the script name.
Browse files Browse the repository at this point in the history
  • Loading branch information
rblank committed Jan 10, 2025
1 parent e35ffd0 commit 1e40ce3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
27 changes: 21 additions & 6 deletions tdocv.py → [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,39 @@
import contextlib
import contextvars
import pathlib
import re
import subprocess
import shutil
import sys
import sysconfig
import time
import venv

# TODO: Use a naming convention to determine (package, cmd)
# TODO: Don't make upgrade.txt a requirements file, just metadata
# TODO: Garbage-collect based on the time of last use (touch requirements.txt)
# TODO: Identify existing venv through requirements
# TODO: Allow forcing requirements (file? env var?)
# TODO: Allow forcing the creation of a new venv

package = 't-doc-common'
command = 'tdoc'
keep_envs = 2
keep_envs_days = 3

executable_re = re.compile(r'^run-([^@]+)@(.+)\.py$')


def main(argv, stdin, stdout, stderr):
# Determine the command to run and the requirements for running it.
executable = pathlib.Path(argv[0]).name
if (m := executable_re.fullmatch(executable)) is not None:
command, requirements = m.group(1, 2)
elif len(argv) >= 3:
command, requirements = argv[1:3]
argv = argv[:1] + argv[3:]
else:
stderr.write("Not enough arguments\n\n"
f"Usage: {executable} COMMAND REQUIREMENTS [ARG ...]\n")
return 1

base = pathlib.Path.cwd()
builder = EnvBuilder(base, stderr)

Expand All @@ -30,7 +46,7 @@ def main(argv, stdin, stdout, stderr):
if not any(e.valid for e in envs):
stderr.write("Creating venv...\n")
env = builder.new()
env.create()
env.create(f'{requirements}\n')
envs.insert(0, env)
stderr.write("\n")
for env in envs:
Expand Down Expand Up @@ -115,8 +131,7 @@ def check_upgrade(self):
self.builder.out.write("\n")
if resp in ('y', 'yes', 'o', 'oui', 'j', 'ja'): return reqs

def create(self, reqs=None):
if reqs is None: reqs = f'{package}\n'
def create(self, reqs):
self.reqs = reqs
self.builder.root.mkdir(exist_ok=True)
token = self.env.set(self)
Expand Down
2 changes: 1 addition & 1 deletion serve.bat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@echo off
tdocv.py serve
run-tdoc@t-doc-common.py serve
2 changes: 1 addition & 1 deletion serve.desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Type=Application
GenericName=Start a local t-doc server
Comment=
Icon=document-preview
Exec=cd "$(dirname "%k")" && ./tdocv.py serve
Exec=cd "$(dirname "%k")" && ./run-tdoc@t-doc-common.py serve
Path=
Terminal=true
StartupNotify=false

0 comments on commit 1e40ce3

Please sign in to comment.