Skip to content

Instantly share code, notes, and snippets.

@qnighy
Last active February 9, 2019 02:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qnighy/4619b0fae9ccb57ecf26 to your computer and use it in GitHub Desktop.
Save qnighy/4619b0fae9ccb57ecf26 to your computer and use it in GitHub Desktop.
Convert foo.synctex(.gz) from Cygwin to Windows
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import re
import subprocess
import sys
import time
import gzip
def cygpath_w(paths):
return subprocess.check_output(['cygpath', '-w'] + paths).splitlines(False)
def file_mtime(filename, default=0.0):
try:
return os.stat(filename).st_mtime
except OSError:
return default
def open_with_gz(filename, option):
if filename.endswith('.gz'):
return gzip.open(filename, option)
else:
return open(filename, option)
def main(synctex_files):
path_re = re.compile(r'\AInput:(?P<idx>\d+):(?P<pathname>.*)\Z')
while True:
for synctex_file in synctex_files:
synctex_timestamp_file = synctex_file + ".timestamp"
if not os.path.exists(synctex_file):
try:
os.remove(synctex_timestamp_file)
except OSError:
pass
elif file_mtime(synctex_timestamp_file) < file_mtime(synctex_file):
print("updating {}".format(synctex_file))
with open_with_gz(synctex_file, "rb") as f:
synctex_lines = [
line.rstrip('\n\r') for line in f.readlines()]
cygpath_queries = []
for line in synctex_lines:
m = path_re.match(line)
if m is not None:
idx = m.group('idx')
pathname = m.group('pathname')
cygpath_queries.append(pathname)
cygpath_result = {}
for (pathname, pathname_w) in \
zip(cygpath_queries, cygpath_w(cygpath_queries)):
cygpath_result[pathname] = pathname_w
with open_with_gz(synctex_file, "wb") as f:
for line in synctex_lines:
m = path_re.match(line)
if m is not None:
idx = m.group('idx')
pathname = m.group('pathname')
pathname_w = cygpath_result[pathname]
f.write("Input:{}:{}\n".format(
idx,
pathname_w))
else:
f.write(line + "\n")
print("updated {}".format(synctex_file))
with open(synctex_timestamp_file, "w"):
pass
time.sleep(5.0)
if __name__ == '__main__':
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment