minor tooling refactor

This commit is contained in:
Rasmus Andersson 2018-09-03 14:19:38 -07:00
parent c66641b76b
commit fa601adc31
3 changed files with 73 additions and 67 deletions

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python #!/usr/bin/env python
from __future__ import print_function from __future__ import print_function, absolute_import
import sys, os import sys, os
# patch PYTHONPATH to include $BASEDIR/build/venv/python/site-packages from os.path import dirname, basename, abspath, relpath, join as pjoin
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir)) sys.path.append(abspath(pjoin(dirname(__file__), 'tools')))
VENVDIR = os.path.join(BASEDIR, 'build', 'venv') from common import BASEDIR, VENVDIR, getGitHash, getVersion
sys.path.append(os.path.join(VENVDIR, 'lib', 'python', 'site-packages'))
import argparse import argparse
import datetime import datetime
@ -20,32 +19,6 @@ from glyphsLib.interpolation import apply_instance_data
from mutatorMath.ufo.document import DesignSpaceDocumentReader from mutatorMath.ufo.document import DesignSpaceDocumentReader
BASEDIR = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
_gitHash = None
def getGitHash():
global _gitHash
if _gitHash is None:
_gitHash = ''
try:
_gitHash = subprocess.check_output(
['git', '-C', BASEDIR, 'rev-parse', '--short', 'HEAD'],
shell=False
).strip()
except:
pass
return _gitHash
_version = None
def getVersion():
global _version
if _version is None:
_version = open(os.path.join(BASEDIR, 'version.txt'), 'r').read().strip()
return _version
subfamily_re = re.compile(r'^\s*([^\s]+)(?:\s*italic|)\s*$', re.I | re.U) subfamily_re = re.compile(r'^\s*([^\s]+)(?:\s*italic|)\s*$', re.I | re.U)
@ -62,7 +35,7 @@ def mkdirs(path):
# setFontInfo patches font.info # setFontInfo patches font.info
# #
def setFontInfo(font, weight): def setFontInfo(font, weight, updateCreated=True):
# #
# For UFO3 names, see # For UFO3 names, see
# https://github.com/unified-font-object/ufo-spec/blob/gh-pages/versions/ # https://github.com/unified-font-object/ufo-spec/blob/gh-pages/versions/
@ -82,6 +55,7 @@ def setFontInfo(font, weight):
font.info.openTypeOS2WeightClass = weight font.info.openTypeOS2WeightClass = weight
# creation date & time (YYYY/MM/DD HH:MM:SS) # creation date & time (YYYY/MM/DD HH:MM:SS)
if updateCreated:
font.info.openTypeHeadCreated = now.strftime("%Y/%m/%d %H:%M:%S") font.info.openTypeHeadCreated = now.strftime("%Y/%m/%d %H:%M:%S")
# version # version
@ -129,17 +103,13 @@ def setFontInfo(font, weight):
class Main(object): class Main(object):
def __init__(self): def __init__(self):
self.tmpdir = os.path.join(BASEDIR,'build','tmp') self.tmpdir = pjoin(BASEDIR,'build','tmp')
def main(self, argv): def main(self, argv):
# make ^C instantly exit program # make ^C instantly exit program
signal.signal(signal.SIGINT, sighandler) signal.signal(signal.SIGINT, sighandler)
# update environment
os.environ['PATH'] = '%s:%s' % (
os.path.join(VENVDIR, 'bin'), os.environ['PATH'])
argparser = argparse.ArgumentParser( argparser = argparse.ArgumentParser(
description='', description='',
usage=''' usage='''
@ -202,7 +172,7 @@ class Main(object):
formats = [] formats = []
filename = args.output filename = args.output
if filename is None or filename == '': if filename is None or filename == '':
ufoname = os.path.basename(args.ufo) ufoname = basename(args.ufo)
name, ext = os.path.splitext(ufoname) name, ext = os.path.splitext(ufoname)
filename = name + '.otf' filename = name + '.otf'
logging.info('setting --output %r' % filename) logging.info('setting --output %r' % filename)
@ -231,8 +201,8 @@ class Main(object):
# run through ots-sanitize # run through ots-sanitize
# for filename in args.output: # for filename in args.output:
tmpfile = os.path.join(self.tmpdir, os.path.basename(filename)) tmpfile = pjoin(self.tmpdir, basename(filename))
mkdirs(os.path.dirname(filename)) mkdirs(dirname(filename))
success = True success = True
try: try:
otssan_res = subprocess.check_output( otssan_res = subprocess.check_output(
@ -272,18 +242,18 @@ class Main(object):
outdir = args.outdir outdir = args.outdir
if outdir is None: if outdir is None:
outdir = os.path.dirname(args.glyphsfile) outdir = dirname(args.glyphsfile)
# files # files
master_dir = outdir master_dir = outdir
glyphsfile = args.glyphsfile glyphsfile = args.glyphsfile
designspace_file = os.path.join(outdir, 'Inter-UI.designspace') designspace_file = pjoin(outdir, 'Inter-UI.designspace')
instance_dir = os.path.join(BASEDIR, 'build', 'ufo') instance_dir = pjoin(BASEDIR, 'build', 'ufo')
# load glyphs project file # load glyphs project file
print("generating %s from %s" % ( print("generating %s from %s" % (
os.path.relpath(designspace_file, os.getcwd()), relpath(designspace_file, os.getcwd()),
os.path.relpath(glyphsfile, os.getcwd()) relpath(glyphsfile, os.getcwd())
)) ))
font = glyphsLib.GSFont(glyphsfile) font = glyphsLib.GSFont(glyphsfile)
@ -291,7 +261,7 @@ class Main(object):
designspace = glyphsLib.to_designspace( designspace = glyphsLib.to_designspace(
font, font,
propagate_anchors=False, propagate_anchors=False,
instance_dir=os.path.relpath(instance_dir, master_dir) instance_dir=relpath(instance_dir, master_dir)
) )
# strip lib data # strip lib data
@ -310,7 +280,7 @@ class Main(object):
for source in designspace.sources: for source in designspace.sources:
# source : fontTools.designspaceLib.SourceDescriptor # source : fontTools.designspaceLib.SourceDescriptor
# source.font : defcon.objects.font.Font # source.font : defcon.objects.font.Font
ufo_path = os.path.join(master_dir, source.filename.replace('InterUI', 'Inter-UI')) ufo_path = pjoin(master_dir, source.filename.replace('InterUI', 'Inter-UI'))
# no need to also set the relative 'filename' attribute as that # no need to also set the relative 'filename' attribute as that
# will be auto-updated on writing the designspace document # will be auto-updated on writing the designspace document
@ -319,7 +289,7 @@ class Main(object):
# fixup font info # fixup font info
weight = int(source.location['Weight']) weight = int(source.location['Weight'])
setFontInfo(source.font, weight) setFontInfo(source.font, weight, updateCreated=False)
# cleanup lib # cleanup lib
lib = dict() lib = dict()
@ -334,7 +304,7 @@ class Main(object):
# write UFO file # write UFO file
source.path = ufo_path source.path = ufo_path
print("write %s" % os.path.relpath(ufo_path, os.getcwd())) print("write %s" % relpath(ufo_path, os.getcwd()))
source.font.save(ufo_path) source.font.save(ufo_path)
# patch instance names # patch instance names
@ -343,7 +313,7 @@ class Main(object):
instance.name = instance.styleName.lower().replace(' ', '') instance.name = instance.styleName.lower().replace(' ', '')
instance.filename = instance.filename.replace('InterUI', 'Inter-UI') instance.filename = instance.filename.replace('InterUI', 'Inter-UI')
print("write %s" % os.path.relpath(designspace_file, os.getcwd())) print("write %s" % relpath(designspace_file, os.getcwd()))
designspace.write(designspace_file) designspace.write(designspace_file)
@ -365,7 +335,7 @@ class Main(object):
# files # files
designspace_file = args.designspacefile designspace_file = args.designspacefile
instance_dir = os.path.join(BASEDIR, 'build', 'ufo') instance_dir = pjoin(BASEDIR, 'build', 'ufo')
# DesignSpaceDocumentReader generates UFOs # DesignSpaceDocumentReader generates UFOs
gen = DesignSpaceDocumentReader( gen = DesignSpaceDocumentReader(
@ -383,9 +353,9 @@ class Main(object):
instance_files = set() instance_files = set()
for instance in designspace.instances: for instance in designspace.instances:
if all_instances or instance.name in instances: if all_instances or instance.name in instances:
filebase = os.path.basename(instance.filename) filebase = basename(instance.filename)
relname = os.path.relpath( relname = relpath(
os.path.join(os.path.dirname(designspace_file), instance.filename), pjoin(dirname(designspace_file), instance.filename),
os.getcwd() os.getcwd()
) )
print('generating %s' % relname) print('generating %s' % relname)
@ -413,7 +383,7 @@ class Main(object):
font.info.italicAngle = italicAngle font.info.italicAngle = italicAngle
# update font info # update font info
weight = instance_weight[os.path.basename(font.path)] weight = instance_weight[basename(font.path)]
setFontInfo(font, weight) setFontInfo(font, weight)
font.save() font.save()

38
misc/tools/common.py Normal file
View file

@ -0,0 +1,38 @@
#!/usr/bin/env python
from __future__ import print_function
import sys, os
from os.path import dirname, abspath, join as pjoin
import subprocess
# patch PYTHONPATH to include $BASEDIR/build/venv/python/site-packages
BASEDIR = abspath(pjoin(dirname(__file__), os.pardir, os.pardir))
VENVDIR = pjoin(BASEDIR, 'build', 'venv')
sys.path.append(pjoin(VENVDIR, 'lib', 'python', 'site-packages'))
_gitHash = None
def getGitHash():
global _gitHash
if _gitHash is None:
_gitHash = ''
try:
_gitHash = subprocess.check_output(
['git', '-C', BASEDIR, 'rev-parse', '--short', 'HEAD'],
shell=False
).strip()
except:
pass
return _gitHash
_version = None
def getVersion():
global _version
if _version is None:
with open(pjoin(BASEDIR, 'version.txt'), 'r') as f:
_version = f.read().strip()
return _version
# update environment to include $VENVDIR/bin
os.environ['PATH'] = os.path.join(VENVDIR, 'bin') + ':' + os.environ['PATH']

View file

@ -4,21 +4,19 @@
# Updates the "?v=x" in docs/inter-ui.css # Updates the "?v=x" in docs/inter-ui.css
# #
from __future__ import print_function from __future__ import print_function
import os, sys, re
from collections import OrderedDict import os, sys
from ConfigParser import RawConfigParser from os.path import dirname, basename, abspath, relpath, join as pjoin
sys.path.append(abspath(pjoin(dirname(__file__), 'tools')))
from common import BASEDIR, getVersion
import re
def main(): def main():
rootDir = os.path.dirname(os.path.dirname(__file__)) version = getVersion()
config = RawConfigParser(dict_type=OrderedDict)
config.read(os.path.join(rootDir, 'src', 'fontbuild.cfg'))
version = config.get('main', 'version')
regex = re.compile(r'(url\("[^"]+?v=)([^"]+)("\))') regex = re.compile(r'(url\("[^"]+?v=)([^"]+)("\))')
cssFileName = pjoin(BASEDIR, 'docs', 'inter-ui.css')
cssFileName = os.path.join(rootDir, 'docs', 'inter-ui.css')
s = '' s = ''
with open(cssFileName, 'r') as f: with open(cssFileName, 'r') as f: