Skip to content

Commit

Permalink
Merge pull request #6 from lsst/u/timj/py
Browse files Browse the repository at this point in the history
Remove the py dependency and minor automated cleanup
  • Loading branch information
timj authored May 28, 2024
2 parents 4e1b26b + 280c51e commit 41996b7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 48 deletions.
26 changes: 9 additions & 17 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
name: lint

on:
- push
- pull_request
push:
branches:
- main
pull_request:

jobs:
lint:
call-workflow:
uses: lsst/rubin_workflows/.github/workflows/lint.yaml@main
ruff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.7

- name: Install
run: pip install -r <(curl https://raw.githubusercontent.com/lsst/linting/main/requirements.txt)

- name: Run linter
run: |
flake8 shebangtron
flake8
- uses: actions/checkout@v4
- uses: chartboost/ruff-action@v1
2 changes: 0 additions & 2 deletions requirements.txt

This file was deleted.

7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[flake8]
max-line-length = 110
max-doc-length = 79
ignore = W503, E203, E704, N802, N803, N806, N812, N815, N816
exclude = __init__.py
lex.py
yacc.py
82 changes: 53 additions & 29 deletions shebangtron
Original file line number Diff line number Diff line change
@@ -1,30 +1,56 @@
#!/usr/bin/env python

""" This script is intended to fixup the shebang paths on scripts installed via
"""This script is intended to fixup the shebang paths on scripts installed via
EUPS distrib "tarball" packages. The assumption is that an end-user will
invoke it as a manual post-install step on OSX systems with SIP installed.
"""

from __future__ import absolute_import, print_function

import io
import mmap
import os.path
import re
import shutil
import stat
import sys
from subprocess import check_output
from distutils.spawn import find_executable

NON_SCRIPT_EXT = (
'.pyc', '.pyo', '.h', '.a', '.c', '.cc', '.txt', '.html',
'.xml', '.png', '.jpg', '.gif', '.class',
'.o', '.dylib', '.so', '.os', '.version', '.m4', '.table',
'.pdf', '.tex', '.dox', '.tag', '.conf', '.cfg', '.fits', '.fz',
'.js', '.yaml', '.md', '.css', '.rst',
".pyc",
".pyo",
".h",
".a",
".c",
".cc",
".txt",
".html",
".xml",
".png",
".jpg",
".gif",
".class",
".o",
".dylib",
".so",
".os",
".version",
".m4",
".table",
".pdf",
".tex",
".dox",
".tag",
".conf",
".cfg",
".fits",
".fz",
".js",
".yaml",
".md",
".css",
".rst",
)

SHEBANG_PAT = re.compile(br'^#!.+$', re.M)
SHEBANG_PAT = re.compile(rb"^#!.+$", re.M)


def fix_shebang(path, build_python):
Expand All @@ -46,39 +72,39 @@ def fix_shebang(path, build_python):
if not os.access(path, os.R_OK | os.W_OK):
return

with io.open(path, mode='rb') as fi:
with open(path, mode="rb") as fi:
data = fi.read(1000)

if not data.startswith(b'#!'):
if not data.startswith(b"#!"):
return

m = SHEBANG_PAT.match(data)
if not m:
return

# skip scripts that use #!/usr/bin/env
if b'/usr/bin/env' in m.group():
if b"/usr/bin/env" in m.group():
return
if b'python' not in m.group():
if b"python" not in m.group():
return

fi.seek(0)
mm = mmap.mmap(fi.fileno(), 0, prot=mmap.PROT_READ)
data = mm[:]

encoding = 'utf8'
encoding = "utf8"

# remove the conda prefix logic and set the path to the python interp
# explicity
py_exec = (build_python)
new_data = SHEBANG_PAT.sub(b'#!' + py_exec.encode(encoding), data, count=1)
py_exec = build_python
new_data = SHEBANG_PAT.sub(b"#!" + py_exec.encode(encoding), data, count=1)
if new_data == data:
return
print("updating shebang:", path)

# save original file mode
mode = os.stat(path).st_mode
with io.open(path, 'w', encoding=encoding) as fo:
with open(path, "w", encoding=encoding) as fo:
fo.write(new_data.decode(encoding))
# restore file mode
os.chmod(path, mode)
Expand All @@ -87,36 +113,34 @@ def fix_shebang(path, build_python):
# /post.py

try:
flavor = os.environ['SHTRON_EUPS_FLAVOR']
flavor = os.environ["SHTRON_EUPS_FLAVOR"]
except KeyError:
# py >= 3.3 has shutil.which
eups = find_executable('eups')
eups = shutil.which("eups")
if not eups:
raise RuntimeError('unable to find eups command')
raise RuntimeError("unable to find eups command")

flavor = check_output([eups, "flavor"])
if not flavor:
raise RuntimeError('eups flavor sub-command may be broken')
raise RuntimeError("eups flavor sub-command may be broken")

flavor = flavor.decode('utf-8')
flavor = flavor.decode("utf-8")

# remove newline from eups output
flavor = flavor.rstrip()

try:
eups_path = os.environ['EUPS_PATH']
eups_path = os.environ["EUPS_PATH"]
except KeyError:
raise RuntimeError('required environment variable EUPS_PATH is missing')
raise RuntimeError("required environment variable EUPS_PATH is missing")

prod_install_path = os.path.join(eups_path, flavor)
# sanity check
if not os.path.isdir(prod_install_path):
raise RuntimeError(f'EUPS product install path "{prod_install_path}"'
'is missing')
raise RuntimeError(f'EUPS product install path "{prod_install_path}"' "is missing")

# fully qualified path to new shebang interp
try:
py = os.environ['SHTRON_PYTHON']
py = os.environ["SHTRON_PYTHON"]
except KeyError:
py = sys.executable

Expand Down

0 comments on commit 41996b7

Please sign in to comment.