def test_preprocess(): code = """\ A #if True always True another line #endif B #if False never see this #endif C #if x == 0 x = 0 #else x != 0 #endif D #if x != 0 x != 0 #endif E """ res = """\ A always True another line B C x != 0 D x != 0 E """ assert preprocess(code, dict(x=1)) == res
def make_nsi(info, dir_path): "Creates the tmp/main.nsi from the template file" data = read_nsi_tmpl() name = info['name'] dists0 = common.DISTS[0][:-8] py_name, py_version, unused_build = dists0.rsplit('-', 2) if py_name != 'python': sys.exit("Error: a Python package needs to be part of the " "specifications") arch = int(info['platform'].split('-')[1]) license_path = abspath(info.get('license_file', join(NSIS_DIR, 'placeholder_license.txt'))) data = preprocess(data, ns_platform(info['platform'])) data = data.replace('__NAME__', str_esc(name)) data = data.replace('__VERSION__', info['version']) data = data.replace('__COMPANY__', str_esc(info.get('company', 'Unknown, Inc.'))) data = data.replace('__ARCH__', str_esc('%d-bit' % arch)) data = data.replace('__PY_VER__', py_version[:3]) data = data.replace('__PYVERSION__', str_esc(py_version)) data = data.replace('__PYVERSION_JUSTDIGITS__', str_esc(''.join(py_version.split('.')))) data = data.replace('__OUTFILE__', str_esc(info['_outpath'])) data = data.replace('__LICENSEFILE__', str_esc(license_path)) for placeholder, fn in [('__HEADERIMAGE__', 'header.bmp'), ('__WELCOMEIMAGE__', 'welcome.bmp'), ('__ICONFILE__', 'icon.ico')]: data = data.replace(placeholder, str_esc(join(dir_path, fn))) # these are unescaped (and unquoted) data = data.replace('@NAME@', name) data = data.replace('@NSIS_DIR@', NSIS_DIR) data = data.replace('@BITS@', str(arch)) pkg_commands = [] for fn in common.DISTS: path = join(common.REPO_DIR, fn) pkg_commands.append('# --> %s <--' % fn) pkg_commands.append('File %s' % str_esc(path)) pkg_commands.append('untgz::extract "-d" "$INSTDIR" ' '"-zbz2" "$INSTDIR\pkgs\%s"' % fn) pkg_commands.append('ExecWait \'"$INSTDIR\pythonw.exe" ' '"$INSTDIR\Lib\_nsis.py" postpkg\'') pkg_commands.append('') data = data.replace('@PKG_COMMANDS@', '\n '.join(pkg_commands)) nsi_path = join(dir_path, 'main.nsi') with open(nsi_path, 'w') as fo: fo.write(data) # Copy all the NSIS header files (*.nsh) for fn in os.listdir(NSIS_DIR): if fn.endswith('.nsh'): shutil.copy(join(NSIS_DIR, fn), join(dir_path, fn)) print('Created %s file' % nsi_path) return nsi_path
def make_nsi(info, dir_path): "Creates the tmp/main.nsi from the template file" name = info['name'] download_dir = info['_download_dir'] dists = info['_dists'] py_name, py_version, unused_build = dists[0].rsplit('-', 2) assert py_name == 'python' arch = int(info['_platform'].split('-')[1]) # these appear as __<key>__ in the template, and get escaped replace = { 'NAME': name, 'VERSION': info['version'], 'COMPANY': info.get('company', 'Unknown, Inc.'), 'ARCH': '%d-bit' % arch, 'PY_VER': py_version[:3], 'PYVERSION': py_version, 'PYVERSION_JUSTDIGITS': ''.join(py_version.split('.')), 'OUTFILE': info['_outpath'], 'LICENSEFILE': abspath(info.get('license_file', join(NSIS_DIR, 'placeholder_license.txt'))), 'DEFAULT_PREFIX': info.get( 'default_prefix', join('%LOCALAPPDATA%', 'Continuum', name.lower()) ), } for key, fn in [('HEADERIMAGE', 'header.bmp'), ('WELCOMEIMAGE', 'welcome.bmp'), ('ICONFILE', 'icon.ico')]: replace[key] = join(dir_path, fn) for key in replace: replace[key] = str_esc(replace[key]) data = read_nsi_tmpl() data = preprocess(data, ns_platform(info['_platform'])) data = fill_template(data, replace) # these are unescaped (and unquoted) cmds = '\n '.join(pkg_commands(download_dir, dists, py_version)) for key, value in [('@NAME@', name), ('@NSIS_DIR@', NSIS_DIR), ('@BITS@', str(arch)), ('@PKG_COMMANDS@', cmds)]: data = data.replace(key, value) nsi_path = join(dir_path, 'main.nsi') with open(nsi_path, 'w') as fo: fo.write(data) # Copy all the NSIS header files (*.nsh) for fn in os.listdir(NSIS_DIR): if fn.endswith('.nsh'): shutil.copy(join(NSIS_DIR, fn), join(dir_path, fn)) print('Created %s file' % nsi_path) return nsi_path
def make_nsi(info, dir_path): "Creates the tmp/main.nsi from the template file" name = info['name'] download_dir = info['_download_dir'] dists = info['_dists'] py_name, py_version, unused_build = dists[0].rsplit('-', 2) assert py_name == 'python' arch = int(info['_platform'].split('-')[1]) # these appear as __<key>__ in the template, and get escaped replace = { 'NAME': name, 'VERSION': info['version'], 'COMPANY': info.get('company', 'Unknown, Inc.'), 'ARCH': '%d-bit' % arch, 'PY_VER': py_version[:3], 'PYVERSION': py_version, 'PYVERSION_JUSTDIGITS': ''.join(py_version.split('.')), 'OUTFILE': info['_outpath'], 'LICENSEFILE': abspath(info.get('license_file', join(NSIS_DIR, 'placeholder_license.txt'))), 'DEFAULT_PREFIX': expandvars(info.get( 'default_prefix', join('%LOCALAPPDATA%', 'Continuum', name.lower()) )), } for key, fn in [('HEADERIMAGE', 'header.bmp'), ('WELCOMEIMAGE', 'welcome.bmp'), ('ICONFILE', 'icon.ico')]: replace[key] = join(dir_path, fn) for key in replace: replace[key] = str_esc(replace[key]) data = read_nsi_tmpl() data = preprocess(data, ns_platform(info['_platform'])) data = fill_template(data, replace) # these are unescaped (and unquoted) cmds = '\n '.join(pkg_commands(download_dir, dists, py_version)) for key, value in [('@NAME@', name), ('@NSIS_DIR@', NSIS_DIR), ('@BITS@', str(arch)), ('@PKG_COMMANDS@', cmds)]: data = data.replace(key, value) nsi_path = join(dir_path, 'main.nsi') with open(nsi_path, 'w') as fo: fo.write(data) # Copy all the NSIS header files (*.nsh) for fn in os.listdir(NSIS_DIR): if fn.endswith('.nsh'): shutil.copy(join(NSIS_DIR, fn), join(dir_path, fn)) print('Created %s file' % nsi_path) return nsi_path
def get_header(tarball, info): name = info['name'] dists = [fn[:-8] for fn in info['_dists']] dist0 = dists[0] assert name_dist(dist0) == 'python' has_license = bool('license_file' in info) ppd = ns_platform(info['_platform']) ppd['keep_pkgs'] = bool(info.get('keep_pkgs')) ppd['has_license'] = has_license for key in 'pre_install', 'post_install': ppd['has_%s' % key] = bool(key in info) ppd['add_to_path_default'] = info.get('add_to_path_default', None) install_lines = ['install_dist %s' % d for d in dists] install_lines.extend(add_condarc(info)) # Needs to happen first -- can be templated replace = { 'NAME': name, 'name': name.lower(), 'VERSION': info['version'], 'PLAT': info['_platform'], 'DIST0': dist0, 'DEFAULT_PREFIX': info.get('default_prefix', '$HOME/%s' % name.lower()), 'MD5': md5_file(tarball), 'INSTALL_COMMANDS': '\n'.join(install_lines), 'pycache': '__pycache__', } if has_license: replace['LICENSE'] = read_ascii_only(info['license_file']) data = read_header_template() data = preprocess(data, ppd) data = fill_template(data, replace) n = data.count('\n') data = data.replace('@LINES@', str(n + 1)) # note that this replacement does not change the size of the header, # which would result into an inconsistency n = len(data) + getsize(tarball) data = data.replace('@SIZE_BYTES@', '%12d' % n) assert len(data) + getsize(tarball) == n return data
def get_header(tarball, info): name = info['name'] dists = [fn[:-8] for fn in info['_dists']] dist0 = dists[0] assert name_dist(dist0) == 'python' has_license = bool('license_file' in info) ppd = ns_platform(info['_platform']) ppd['keep_pkgs'] = bool(info.get('keep_pkgs')) ppd['has_license'] = has_license for key in 'pre_install', 'post_install': ppd['has_%s' % key] = bool(key in info) install_lines = ['install_dist %s' % d for d in dists] install_lines.extend(add_condarc(info)) # Needs to happen first -- can be templated replace = { 'NAME': name, 'name': name.lower(), 'VERSION': info['version'], 'PLAT': info['_platform'], 'DIST0': dist0, 'DEFAULT_PREFIX': info.get('default_prefix', '$HOME/%s' % name.lower()), 'MD5': md5_file(tarball), 'INSTALL_COMMANDS': '\n'.join(install_lines), 'pycache': '__pycache__', } if has_license: replace['LICENSE'] = read_ascii_only(info['license_file']) data = read_header_template() data = preprocess(data, ppd) data = fill_template(data, replace) n = data.count('\n') data = data.replace('@LINES@', str(n + 1)) # note that this replacement does not change the size of the header, # which would result into an inconsistency n = len(data) + getsize(tarball) data = data.replace('@SIZE_BYTES@', '%12d' % n) assert len(data) + getsize(tarball) == n return data
def get_header(tarball, info): data = read_header_template() name = info['name'] dists0 = common.DISTS[0][:-8] py_name, py_version, unused_build = dists0.rsplit('-', 2) if py_name != 'python': sys.exit("Error: a Python package needs to be part of the " "specifications") data = preprocess(data, ns_platform(info['platform'])) # Needs to happen first -- can be templated data = data.replace('__NAME__', name) data = data.replace('__name__', name.lower()) data = data.replace('__VERSION__', info['version']) data = data.replace('__DEFAULT_PREFIX__', info.get('default_prefix', '$HOME/' + name.lower())) data = data.replace('__PLAT__', info['platform']) data = data.replace('__DIST0__', dists0) data = data.replace('__PY_VER__', py_version[:3]) has_license = bool('license_file' in info) data = data.replace('__HAS_LICENSE__', str(int(has_license))) if has_license: data = data.replace('__LICENSE__', read_ascii_only(info['license_file'])) lines = ['install_dist %s' % (fn[:-8],) for fn in common.DISTS] if 'conda_default_channels' in info: add_condarc(lines, info) data = data.replace('__INSTALL_COMMANDS__', '\n'.join(lines)) data = data.replace('__MD5__', md5_file(tarball)) n = data.count('\n') data = data.replace('__LINES__', str(n + 1)) # note that this replacement does not change the size of the header, # which would result into an inconsistency n = len(data) + getsize(tarball) data = data.replace('___BYTES___', '%11d' % n) return data