| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
import fnmatch |
| 10 |
import os |
| 11 |
from pathlib import Path |
| 12 |
import re |
| 13 |
import itertools |
| 14 |
import shutil |
| 15 |
import sys |
| 16 |
import hashlib |
| 17 |
|
| 18 |
|
| 19 |
EXCLUDE = { |
| 20 |
"samtools": ( |
| 21 |
"test", "misc", |
| 22 |
"bgzip.c", |
| 23 |
"main.c", |
| 24 |
"calDepth.c", |
| 25 |
"bam2bed.c", |
| 26 |
"bam_tview.c", |
| 27 |
"bam_tview.h", |
| 28 |
"bam_tview_html.c", |
| 29 |
"bam_tview_curses.c", |
| 30 |
"bam2bcf.c", |
| 31 |
"bam2bcf.h", |
| 32 |
"vcf-miniview.c", |
| 33 |
), |
| 34 |
"bcftools": ( |
| 35 |
"test", "plugins", "peakfit.c", |
| 36 |
"peakfit.h", |
| 37 |
"polysomy.c"), |
| 38 |
"htslib": ( |
| 39 |
'annot-tsv.c', 'bgzip.c', 'htsfile.c', 'tabix.c', |
| 40 |
'hts_probe_cc.sh', |
| 41 |
"samples", "test", "tests"), |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
MAIN = { |
| 46 |
"samtools": "bamtk", |
| 47 |
"bcftools": "main" |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
C_VERSION = { |
| 52 |
"htslib": "HTS_VERSION_TEXT", |
| 53 |
"samtools": "SAMTOOLS_VERSION", |
| 54 |
"bcftools": "BCFTOOLS_VERSION" |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def locate(pattern, root=os.curdir, exclude=[], exclude_htslib=False): |
| 59 |
'''Locate all files matching supplied filename pattern (but not listed |
| 60 |
in exclude) in and below the supplied root directory. Omit any files under |
| 61 |
directories listed in exclude or (if exclude_htslib=True) matching /htslib-*/. |
| 62 |
''' |
| 63 |
for path, dirs, files in os.walk(os.path.abspath(root)): |
| 64 |
for filename in fnmatch.filter(files, pattern): |
| 65 |
if filename not in exclude: |
| 66 |
yield os.path.join(path, filename) |
| 67 |
|
| 68 |
for dirname in exclude: |
| 69 |
if dirname in dirs: dirs.remove(dirname) |
| 70 |
if exclude_htslib: |
| 71 |
for dirname in [d for d in dirs if re.match(r"htslib-", d)]: |
| 72 |
dirs.remove(dirname) |
| 73 |
|
| 74 |
|
| 75 |
def _update_pysam_files(cf, destdir): |
| 76 |
'''update pysam files applying redirection of ouput''' |
| 77 |
basename = os.path.basename(destdir) |
| 78 |
for filename in cf: |
| 79 |
if not filename: |
| 80 |
continue |
| 81 |
dest = filename + ".pysam.c" |
| 82 |
with open(filename, encoding="utf-8") as infile: |
| 83 |
lines = "".join(infile.readlines()) |
| 84 |
|
| 85 |
with open(dest, "w", encoding="utf-8") as outfile: |
| 86 |
outfile.write('#include "{}.pysam.h"\n\n'.format(basename)) |
| 87 |
subname, _ = os.path.splitext(os.path.basename(filename)) |
| 88 |
if subname in MAIN.get(basename, []): |
| 89 |
lines = re.sub(r"int main\(", "int {}_main(".format( |
| 90 |
basename), lines) |
| 91 |
else: |
| 92 |
lines = re.sub(r"int main\(", "int {}_{}_main(".format( |
| 93 |
basename, subname), lines) |
| 94 |
lines = re.sub(r"\b({}_stdout)\b".format(basename), r"\1_internal", lines) |
| 95 |
lines = re.sub(r"\bexit\(", "{}_exit(".format(basename), lines) |
| 96 |
lines = re.sub(r"\bstderr\b", "{}_stderr".format(basename), lines) |
| 97 |
lines = re.sub(r"\bstdout\b", "{}_stdout".format(basename), lines) |
| 98 |
lines = re.sub(r" printf\(", " fprintf({}_stdout, ".format(basename), lines) |
| 99 |
lines = re.sub(r"([^kf])puts\(", r"\1{}_puts(".format(basename), lines) |
| 100 |
lines = re.sub(r"putchar\(([^)]+)\)", |
| 101 |
r"fputc(\1, {}_stdout)".format(basename), lines) |
| 102 |
|
| 103 |
fn = os.path.basename(filename) |
| 104 |
|
| 105 |
SPECIFIC_SUBSTITUTIONS = { |
| 106 |
"bam_md.c": ( |
| 107 |
'sam_open_format("-", mode_w', |
| 108 |
'sam_open_format({}_stdout_fn, mode_w'.format(basename)), |
| 109 |
"phase.c": ( |
| 110 |
'putc("ACGT"[f->seq[j] == 1? (c&3, {}_stdout) : (c>>16&3)]);'.format(basename), |
| 111 |
'putc("ACGT"[f->seq[j] == 1? (c&3) : (c>>16&3)], {}_stdout);'.format(basename)), |
| 112 |
"cut_target.c": ( |
| 113 |
'putc(33 + (cns[j]>>8>>2, {}_stdout));'.format(basename), |
| 114 |
'putc(33 + (cns[j]>>8>>2), {}_stdout);'.format(basename)) |
| 115 |
} |
| 116 |
if fn in SPECIFIC_SUBSTITUTIONS: |
| 117 |
lines = lines.replace( |
| 118 |
SPECIFIC_SUBSTITUTIONS[fn][0], |
| 119 |
SPECIFIC_SUBSTITUTIONS[fn][1]) |
| 120 |
if fn == "bamtk.c": |
| 121 |
lines = re.sub(r'(#include "version.h")', r'\1\n#include "samtools_config_vars.h"', lines) |
| 122 |
lines = re.sub(r'(else if.*"tview")', r'//\1', lines) |
| 123 |
lines = re.sub(r'(if[ (]*fclose)', r'if (0) { //\1', lines) |
| 124 |
|
| 125 |
outfile.write(lines) |
| 126 |
|
| 127 |
with open(os.path.join("import", "pysam.h")) as inf, \ |
| 128 |
open(os.path.join(destdir, "{}.pysam.h".format(basename)), "w") as outf: |
| 129 |
outf.write(re.sub("@pysam@", basename, inf.read())) |
| 130 |
|
| 131 |
with open(os.path.join("import", "pysam.c")) as inf, \ |
| 132 |
open(os.path.join(destdir, "{}.pysam.c".format(basename)), "w") as outf: |
| 133 |
outf.write(re.sub("@pysam@", basename, inf.read())) |
| 134 |
|
| 135 |
|
| 136 |
if len(sys.argv) >= 1: |
| 137 |
if len(sys.argv) != 3: |
| 138 |
raise ValueError("import requires dest src") |
| 139 |
|
| 140 |
dest, srcdir = sys.argv[1:3] |
| 141 |
if dest not in EXCLUDE: |
| 142 |
raise ValueError("import expected one of %s" % |
| 143 |
",".join(EXCLUDE.keys())) |
| 144 |
exclude = EXCLUDE[dest] |
| 145 |
destdir = os.path.abspath(dest) |
| 146 |
srcdir = os.path.abspath(srcdir) |
| 147 |
if not os.path.exists(srcdir): |
| 148 |
raise IOError( |
| 149 |
"source directory `%s` does not exist." % srcdir) |
| 150 |
|
| 151 |
cfiles = locate("*.c", srcdir, exclude=exclude, exclude_htslib=True) |
| 152 |
hfiles = locate("*.h", srcdir, exclude=exclude, exclude_htslib=True) |
| 153 |
mfiles = itertools.chain(locate("README", srcdir), locate("LICENSE", srcdir), |
| 154 |
locate("version.sh", srcdir, exclude_htslib=True)) |
| 155 |
|
| 156 |
if dest == "htslib": |
| 157 |
|
| 158 |
mfiles = itertools.chain(mfiles, locate("Makefile", srcdir), |
| 159 |
locate("configure", srcdir), |
| 160 |
locate("config.*", srcdir), |
| 161 |
locate("*.[aims][cnk4h]", srcdir, exclude)) |
| 162 |
|
| 163 |
ncopied = 0 |
| 164 |
|
| 165 |
def _compareAndCopy(src, srcdir, destdir, exclude): |
| 166 |
|
| 167 |
d, f = os.path.split(src) |
| 168 |
common_prefix = os.path.commonprefix((d, srcdir)) |
| 169 |
subdir = re.sub(common_prefix, "", d)[1:] |
| 170 |
targetdir = os.path.join(destdir, subdir) |
| 171 |
if not os.path.exists(targetdir): |
| 172 |
os.makedirs(targetdir) |
| 173 |
old_file = os.path.join(targetdir, f) |
| 174 |
if os.path.exists(old_file): |
| 175 |
md5_old = hashlib.md5( |
| 176 |
"".join(open(old_file, "r", encoding="utf-8").readlines()).encode()).digest() |
| 177 |
md5_new = hashlib.md5( |
| 178 |
"".join(open(src, "r", encoding="utf-8").readlines()).encode()).digest() |
| 179 |
if md5_old != md5_new: |
| 180 |
raise ValueError( |
| 181 |
"incompatible files for %s and %s" % |
| 182 |
(old_file, src)) |
| 183 |
|
| 184 |
shutil.copy(src, targetdir) |
| 185 |
return old_file |
| 186 |
|
| 187 |
for src_file in hfiles: |
| 188 |
_compareAndCopy(src_file, srcdir, destdir, exclude) |
| 189 |
ncopied += 1 |
| 190 |
|
| 191 |
for src_file in mfiles: |
| 192 |
_compareAndCopy(src_file, srcdir, destdir, exclude) |
| 193 |
ncopied += 1 |
| 194 |
|
| 195 |
cf = [] |
| 196 |
for src_file in cfiles: |
| 197 |
cf.append(_compareAndCopy(src_file, |
| 198 |
srcdir, |
| 199 |
destdir, |
| 200 |
exclude)) |
| 201 |
ncopied += 1 |
| 202 |
|
| 203 |
sys.stdout.write( |
| 204 |
"installed latest source code from %s: " |
| 205 |
"%i files copied\n" % (srcdir, ncopied)) |
| 206 |
|
| 207 |
if dest in MAIN: |
| 208 |
|
| 209 |
sys.stdout.write("applying stderr redirection\n") |
| 210 |
|
| 211 |
_update_pysam_files(cf, destdir) |
| 212 |
|
| 213 |
def _getVersion(srcdir): |
| 214 |
with open(os.path.join(srcdir, "version.sh"), encoding="utf-8") as inf: |
| 215 |
for line in inf: |
| 216 |
m = re.match(r"VERSION=(\S+)", line) |
| 217 |
if m: return m.group(1) |
| 218 |
raise ValueError("no VERSION line in version.sh") |
| 219 |
|
| 220 |
def _update_version_file(key, value, filename): |
| 221 |
tmpfilename = filename + ".tmp" |
| 222 |
with open(filename, encoding="utf-8") as inf: |
| 223 |
with open(tmpfilename, "w", encoding="utf-8") as outf: |
| 224 |
for line in inf: |
| 225 |
if key in line: |
| 226 |
line = re.sub(r'"[^"]*"', '"{}"'.format(value), line) |
| 227 |
outf.write(line) |
| 228 |
os.rename(tmpfilename, filename) |
| 229 |
|
| 230 |
def _update_version_doc_file(dest, value, filename): |
| 231 |
tmpfilename = filename + ".tmp" |
| 232 |
with open(filename, encoding="utf-8") as inf: |
| 233 |
with open(tmpfilename, "w", encoding="utf-8") as outf: |
| 234 |
for line in inf: |
| 235 |
if " wraps " in line: |
| 236 |
|
| 237 |
line = re.sub(r'\.$', ',DOT', line) |
| 238 |
line = re.sub(r'{}-[^*,]*'.format(dest), |
| 239 |
'{}-{}'.format(dest, value), line) |
| 240 |
line = re.sub(',DOT', '.', line) |
| 241 |
outf.write(line) |
| 242 |
os.rename(tmpfilename, filename) |
| 243 |
|
| 244 |
version = _getVersion(srcdir) |
| 245 |
_update_version_file("__{}_version__".format(dest), version, "pysam/version.py") |
| 246 |
_update_version_file(C_VERSION[dest], version + " (pysam)", "pysam/version.h") |
| 247 |
_update_version_doc_file(dest, version, "README.rst") |
| 248 |
_update_version_doc_file(dest, version, "doc/index.rst") |
| 249 |
if dest in MAIN: |
| 250 |
path = Path("doc/conf.py") |
| 251 |
path.write_text(re.sub(rf"doc/[\d.]*/{dest}", rf"doc/{version}/{dest}", path.read_text())) |
| 252 |
|
| 253 |
sys.exit(0) |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|