Skip to content

Commit

Permalink
Refactoring.
Browse files Browse the repository at this point in the history
* Handling of required programs.
* Decoding is UTF-8 by default now.
  • Loading branch information
rsmith-nl committed Jul 30, 2019
1 parent 9b2e787 commit cccc8af
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 59 deletions.
13 changes: 10 additions & 3 deletions default_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2016-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2018-03-26T23:04:50+02:00
# Last modified: 2019-07-30T15:05:30+0200
# Last modified: 2019-07-30T15:51:39+0200
"""
Get a list of installed packages. For each package, determine if the options
are identical compared to the default options. If so, print out the package name.
Expand Down Expand Up @@ -45,7 +45,7 @@ def run(args): # {{{1
Standard output of the program, converted to UTF-8 string.
"""
comp = sp.run(args, stdout=sp.PIPE, stderr=sp.DEVNULL)
return comp.stdout.decode('utf-8')
return comp.stdout.decode()


def check(line): # {{{1
Expand Down Expand Up @@ -83,6 +83,13 @@ def main(argv): # {{{1
Arguments:
argv: command line arguments
"""
# Look for required programs.
try:
for prog in ('pkg', 'make'):
sp.run([prog], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
except FileNotFoundError:
print('ERROR: required program “{prog}” not found')
sys.exit(1)
data = run(['pkg', 'info', '-a', '-o'])
packagelines = data.splitlines()
print('# List of packages with default options.')
Expand All @@ -92,7 +99,7 @@ def main(argv): # {{{1
if result == Comparison.SAME:
print(pkg)
elif result == Comparison.UNKNOWN:
print('#', pkg, 'is unknown in the ports tree.')
print(f'# “{pkg}is unknown in the ports tree.')


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions dicom2jpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2016-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2016-02-13T10:51:55+01:00
# Last modified: 2019-07-27T21:07:13+0200
# Last modified: 2019-07-30T15:28:17+0200
"""
Convert DICOM files from an X-ray machine to JPEG format.
Expand Down Expand Up @@ -86,7 +86,7 @@ def main(argv):
)
logging.debug(f'command line arguments = {argv}')
logging.debug(f'parsed arguments = {args}')
# Check for requisites
# Check for required programs
try:
sp.run(['convert'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “convert”')
Expand Down
29 changes: 16 additions & 13 deletions find-pkg-upgrades.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2017-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2017-11-26T14:38:15+01:00
# Last modified: 2019-07-27T15:48:15+0200
# Last modified: 2019-07-30T15:49:57+0200
"""Find newer packages for FreeBSD."""

from enum import Enum
Expand Down Expand Up @@ -64,12 +64,8 @@ def run(args):
Returns:
Standard output of the program, converted to UTF-8 and split into lines.
"""
if not isinstance(args, (list, tuple)):
raise ValueError('args should be a list or tuple')
if not all(isinstance(x, str) for x in args):
raise ValueError('args should be a list or tuple of strings')
comp = sp.run(args, stdout=sp.PIPE, stderr=sp.DEVNULL)
return comp.stdout.decode('utf-8').splitlines()
return comp.stdout.decode().splitlines()


def uses_default_options(name, origin):
Expand Down Expand Up @@ -227,6 +223,14 @@ def main(argv):
extra = '(detected)'
else:
extra = '(override)'
# Look for required programs.
try:
for prog in ('pkg', 'make'):
sp.run([prog], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug(f'found “{prog}”')
except FileNotFoundError:
logging.error('required program “{prog}” not found')
sys.exit(1)
logging.info(f'FreeBSD processor architecture: {args.arch} {extra}')
logging.info('retrieving local and remote package lists')
# I'm using concurrent.futures here because especially get_remote_pkgs
Expand All @@ -236,16 +240,15 @@ def main(argv):
local = ex.submit(get_local_pkgs)
rd, ld = False, False
while not (rd and ld):
if remote.done() and not rd:
if (not rd) and remote.done():
rd = True
logging.info('finished retrieving remote packages.')
if local.done() and not ld:
remotepkg = remote.result()
logging.info(f'finished retrieving {len(remotepkg)} remote packages.')
if (not ld) and local.done():
ld = True
logging.info('finished retrieving local packages.')
localpkg = local.result()
logging.info('finished retrieving {len(localpkg)} local packages.')
time.sleep(0.25)
remotepkg = remote.result()
logging.debug('{len(remotepkg)} remote packages')
localpkg = local.result()
choose = {Check.UP_TO_DATE: [], Check.REBUILD_SOURCE: [],
Check.NOT_IN_REMOTE: [], Check.USE_PACKAGE: []}
with cf.ProcessPoolExecutor(max_workers=4) as chk:
Expand Down
32 changes: 20 additions & 12 deletions fix-pdftitle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2017-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2017-04-11T16:17:26+02:00
# Last modified: 2019-07-27T21:16:04+0200
# Last modified: 2019-07-30T15:32:22+0200
"""
Fix PDF file titles.
Expand Down Expand Up @@ -95,14 +95,14 @@ def set_title(path, fn, tempdir, newtitle):
os.chdir(orig)
if rv.returncode != 0:
os.remove(tempdir + os.sep + 'withmarks.pdf')
logging.error(f'could not change title of {path}; ghostscript returned {rv.returncode}')
logging.error(f'could not change title of {path}; ghostscript returned {rv.returncode}')
else:
try:
os.remove(path)
os.rename(tempdir + os.sep + 'withmarks.pdf', path)
logging.info(f'title of {path} changed')
logging.info(f'title of {path} changed')
except OSError as e:
logging.error(f'could not rename withmarks.pdf to {path}: {e}')
logging.error(f'could not rename withmarks.pdf to {path}: {e}')


def main(argv):
Expand All @@ -111,7 +111,7 @@ def main(argv):
'--log',
default='info',
choices=['debug', 'info', 'warning', 'error'],
help="logging level (defaults to 'info')"
help="logging level (defaults to info)"
)
parser.add_argument('-v', '--version', action='version', version=__version__)
parser.add_argument("files", metavar='file', nargs='+', help="one or more files to process")
Expand All @@ -121,28 +121,36 @@ def main(argv):
)
if args.log == 'debug':
logging.debug('using verbose logging')
# Look for required programs.
try:
for prog in ('pdfinfo', 'gs', 'qpdf'):
sp.run([prog], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug(f'found “{prog}”')
except FileNotFoundError:
logging.error('required program “{prog}” not found')
sys.exit(1)
tdir = tempfile.mkdtemp()
for path in args.files:
logging.debug(f'processing {path}')
logging.debug(f'processing {path}')
info = pdfinfo(path)
if len(info) == 0:
logging.error(f'skipping {path}; could not retrieve info dict')
logging.error(f'skipping {path}; could not retrieve info dict')
continue
fn = os.path.basename(path)
if info['Encrypted'].startswith('yes'):
logging.info(f'{path} is encrypted')
logging.info(f'{path} is encrypted')
rv = decrypt(path, fn, tdir)
if rv != 0:
logging.error(f'could not decrypt {path}; qpdf returned {rv}')
logging.error(f'could not decrypt {path}; qpdf returned {rv}')
continue
logging.debug(f'{path} decrypted')
logging.debug(f'{path} decrypted')
else:
logging.debug(f'{path} is not encrypted')
logging.debug(f'{path} is not encrypted')
newtitle = fn.replace('_', ' ')[:-4]
if info['Title'] != newtitle:
set_title(path, fn, tdir, newtitle)
else:
logging.debug(f'the title of {path} does not need to be changed')
logging.debug(f'the title of {path} does not need to be changed')
shutil.rmtree(tdir)


Expand Down
6 changes: 3 additions & 3 deletions foto4lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2011-2019 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2011-11-07T21:40:58+01:00
# Last modified: 2019-07-27T21:14:45+0200
# Last modified: 2019-07-30T15:30:46+0200
"""Shrink fotos to a size suitable for use in my logbook."""

from datetime import datetime
Expand Down Expand Up @@ -59,10 +59,10 @@ def main(argv):
if not args.path:
parser.print_help()
sys.exit(0)
# Check for requisites
# Check for required programs.
try:
sp.run(['convert'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “convert”')
logging.debug('found “convert”')
except FileNotFoundError:
logging.error('the program “convert” cannot be found')
sys.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions get-tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2017-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2017-09-10T12:15:13+02:00
# Last modified: 2019-07-27T20:36:08+0200
# Last modified: 2019-07-30T15:31:11+0200
"""Retrieve the numbered tracks from a dvd."""

import logging
Expand All @@ -28,7 +28,7 @@ def main(argv):
print('get-tracks version', __version__)
print('Example: get-tracks 3 4 5 retrieves tracks 3, 4 and 5')
exit(0)
# Check for requisites
# Check for required programs.
try:
sp.run(['tccat'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “tccat”')
Expand Down
4 changes: 2 additions & 2 deletions git-check-all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2012-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2012-09-02T17:45:51+02:00
# Last modified: 2019-07-27T21:11:22+0200
# Last modified: 2019-07-30T15:31:31+0200
"""
Run ``git gc`` on all the user's git repositories.
Expand Down Expand Up @@ -42,7 +42,7 @@ def main(argv):
)
logging.debug(f'Command line arguments = {argv}')
logging.debug(f'Parsed arguments = {args}')
# Check for requisites
# Check for required programs.
try:
sp.run(['git'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “git”')
Expand Down
6 changes: 3 additions & 3 deletions make-flac.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2012-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2012-12-22T00:12:03+01:00
# Last modified: 2019-07-27T21:12:18+0200
# Last modified: 2019-07-30T15:32:05+0200
"""
Encodes WAV files from cdparanoia (“trackNN.cdda.wav”) to FLAC format.
Expand Down Expand Up @@ -57,10 +57,10 @@ def main(argv):
)
logging.debug(f'command line arguments = {argv}')
logging.debug(f'parsed arguments = {args}')
# Check for requisites
# Check for required programs.
try:
sp.run(['flac'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “flac”')
logging.debug('found “flac”')
except FileNotFoundError:
logging.error('the program “flac” cannot be found')
sys.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions make-mp3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2012-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2012-12-22T01:26:10+01:00
# Last modified: 2019-07-27T21:11:52+0200
# Last modified: 2019-07-30T15:32:51+0200
"""
Encodes WAV files from cdparanoia (“trackNN.cdda.wav”) to MP3 format.
Expand Down Expand Up @@ -57,7 +57,7 @@ def main(argv):
)
logging.debug(f'command line arguments = {argv}')
logging.debug(f'parsed arguments = {args}')
# Check for requisites
# Check for required programs.
try:
sp.run(['lame'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “lame”')
Expand Down
6 changes: 3 additions & 3 deletions markphotos.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2011-2018 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2011-11-06T20:28:07+01:00
# Last modified: 2019-07-27T21:14:01+0200
# Last modified: 2019-07-30T15:39:33+0200
"""Script to add my copyright notice to photos."""

from os import utime
Expand Down Expand Up @@ -42,10 +42,10 @@ def main(argv):
)
logging.debug(f'command line arguments = {argv}')
logging.debug(f'parsed arguments = {args}')
# Check for requisites
# Check for required programs.
try:
sp.run(['exiftool'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “exiftool”')
logging.debug('found “exiftool”')
except FileNotFoundError:
logging.error('the “exiftool” program cannot be found')
sys.exit(1)
Expand Down
4 changes: 2 additions & 2 deletions pdfdiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Copyright © 2019 R.F. Smith <rsmith@xs4all.nl>
# SPDX-License-Identifier: MIT
# Created: 2019-07-11T00:22:30+0200
# Last modified: 2019-07-27T14:13:46+0200
# Last modified: 2019-07-30T15:51:58+0200
"""
Script to try and show a diff between two PDF files.
Expand All @@ -31,7 +31,7 @@ def pdftotext(path):
"""
args = ['pdftotext', '-layout', path, '-']
result = sp.run(args, stdout=sp.PIPE, stderr=sp.DEVNULL, check=True)
return result.stdout.decode('utf-8')
return result.stdout.decode()


def colordiff(txt):
Expand Down
11 changes: 5 additions & 6 deletions tifftopdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2012-2017 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2012-06-29T21:02:55+02:00
# Last modified: 2019-07-29T15:37:23+0200
# Last modified: 2019-07-30T15:36:53+0200
"""
Convert TIFF files to PDF format.
Expand Down Expand Up @@ -52,12 +52,11 @@ def main(argv):
logging.debug(f'parsed arguments = {args}')
# Check for requisites
try:
sp.run(['tiffinfo'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “tiffinfo”')
sp.run(['tiff2pdf'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “tiff2pdf”')
for prog in ('tiffinfo', 'tiff2pdf'):
sp.run([prog], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.debug(f'found “{prog}”')
except FileNotFoundError:
logging.error('a required program cannot be found')
logging.error(f'required program {prog}” not found')
sys.exit(1)
# Work starts here.
func = tiffconv
Expand Down
6 changes: 3 additions & 3 deletions vid2mkv.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Copyright © 2013-2017 R.F. Smith <rsmith@xs4all.nl>.
# SPDX-License-Identifier: MIT
# Created: 2013-11-16T18:41:21+01:00
# Last modified: 2019-07-27T21:10:22+0200
# Last modified: 2019-07-30T15:37:58+0200
"""Convert video files to Theora/Vorbis streams in a Matroska container."""

from functools import partial
Expand Down Expand Up @@ -47,10 +47,10 @@ def main(argv):
)
logging.debug(f'command line arguments = {args}')
logging.debug(f'parsed arguments = {args}')
# Check for requisites
# Check for rewuired programs.
try:
sp.run(['ffmpeg'], stdout=sp.DEVNULL, stderr=sp.DEVNULL)
logging.info('found “ffmpeg”')
logging.debug('found “ffmpeg”')
except FileNotFoundError:
logging.error('the “ffmpeg” program cannot be found')
sys.exit(1)
Expand Down
Loading

0 comments on commit cccc8af

Please sign in to comment.