def __init__(self, shell): super(FortranMagics, self).__init__(shell=shell) self._reloads = {} self._code_cache = {} self._lib_dir = os.path.join(get_ipython_cache_dir(), 'fortran') if not os.path.exists(self._lib_dir): os.makedirs(self._lib_dir)
def __init__(self): self._pid_file = os.path.join(config._config_dir, 'pid_list.txt') ## Perform a clean-up at the start (Note: this closes all previously started win32com Stata instances!) if os.path.isfile(self._pid_file): if not os.stat(self._pid_file).st_size == 0: with open(self._pid_file, 'r') as pid_text: still_open = self.get_stata_pid() pids = pid_text.read().split(',') count_close = 0 for x in pids: if int(x) in still_open: psutil.Process(int(x)).terminate() count_close += 1 if count_close > 0: print('Terminated %d unattached Stata session(s).' % count_close) else: os.remove(self._pid_file) time.sleep(1) stata_dir = os.path.join(get_ipython_cache_dir(), 'stata') for x in os.listdir(stata_dir): if re.match('^log_.*', x): try: os.remove(os.path.join(stata_dir, x)) except: pass
def test_get_ipython_cache_dir(): os.environ["HOME"] = HOME_TEST_DIR if os.name == "posix" and sys.platform != "darwin": # test default os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) os.environ.pop("XDG_CACHE_HOME", None) ipdir = path.get_ipython_cache_dir() nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), ipdir) nt.assert_true(os.path.isdir(ipdir)) # test env override os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR ipdir = path.get_ipython_cache_dir() nt.assert_true(os.path.isdir(ipdir)) nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) else: nt.assert_equal(path.get_ipython_cache_dir(), path.get_ipython_dir())
def test_get_ipython_cache_dir(): os.environ["HOME"] = HOME_TEST_DIR if os.name == 'posix' and sys.platform != 'darwin': # test default os.makedirs(os.path.join(HOME_TEST_DIR, ".cache")) os.environ.pop("XDG_CACHE_HOME", None) ipdir = path.get_ipython_cache_dir() nt.assert_equal(os.path.join(HOME_TEST_DIR, ".cache", "ipython"), ipdir) nt.assert_true(os.path.isdir(ipdir)) # test env override os.environ["XDG_CACHE_HOME"] = XDG_CACHE_DIR ipdir = path.get_ipython_cache_dir() nt.assert_true(os.path.isdir(ipdir)) nt.assert_equal(ipdir, os.path.join(XDG_CACHE_DIR, "ipython")) else: nt.assert_equal(path.get_ipython_cache_dir(), path.get_ipython_dir())
def __init__(self): self._config_dir = os.path.join(get_ipython_cache_dir(), 'stata', 'config') self._config_file = os.path.join(self._config_dir, 'configuration.ini') self._pid_file = os.path.join(self._config_dir, 'pid_list.txt') if py3: self.Config = configparser.ConfigParser() else: self.Config = ConfigParser.ConfigParser() if not os.path.exists(self._config_dir): os.makedirs(self._config_dir) if not os.path.isfile(self._config_file): with open(self._config_file, 'w') as configfile: self.Config.add_section('Stata configuration') self.Config.set('Stata configuration','installation',"C:\Program Files (x86)\Stata13\StataMP-64.exe") self.Config.write(configfile) self.Config.read(self._config_file) ## Perform a clean-up at the start (Note: this closes all previously started win32com Stata instances!) if os.path.isfile(self._pid_file): with open(self._pid_file, 'r') as pid_text: still_open = self.get_stata_pid() pids = pid_text.read().split(',') count_close = 0 for x in pids: if int(x) in still_open: psutil.Process(int(x)).terminate() count_close += 1 if count_close > 0: print('Terminated %d unattached Stata session(s).' % count_close) time.sleep(1) stata_dir = os.path.join(get_ipython_cache_dir(), 'stata') for x in os.listdir(stata_dir): if re.match('^log_.*', x): try: os.remove(os.path.join(stata_dir, x)) except: pass
def __init__(self, shell): super(iPyStataMagic, self).__init__(shell) self._lib_dir = os.path.join(get_ipython_cache_dir(), 'stata') self._pid_file = iPyStata._pid_file if not os.path.exists(self._lib_dir): os.makedirs(self._lib_dir) self.session_dict = {} self.do_dict = {} self.log_dict = {} self.pid_list = []
def fortran(self, line, cell): """Compile and import everything from a Fortran code cell, using f2py. The contents of the cell are written to a `.f90` file in the directory `IPYTHONDIR/fortran` using a filename with the hash of the code. This file is then compiled. The resulting module is imported and all of its symbols are injected into the user's namespace. Usage ===== Prepend ``%%fortran`` to your fortran code in a cell:: ``%%fortran ! put your code here. `` """ code = cell if cell.endswith('\n') else cell+'\n' lib_dir = os.path.join(get_ipython_cache_dir(), 'fortran') key = code, sys.version_info, sys.executable, f2py2e.f2py_version if not os.path.exists(lib_dir): os.makedirs(lib_dir) module_name = "_fortran_magic_" + \ hashlib.md5(str(key).encode('utf-8')).hexdigest() mod_ext = '.pyd' if 'win' in sys.platform.lower() else '.so' module_path = os.path.join(lib_dir, module_name + mod_ext) f90_file = os.path.join(lib_dir, module_name + '.f90') f90_file = py3compat.cast_bytes_py2(f90_file, encoding=sys.getfilesystemencoding()) with io.open(f90_file, 'w', encoding='utf-8') as f: f.write(code) cmd = "f2py -m %s -c %s" % (module_name, f90_file) # move to lib_dir temporally pwd = os.getcwdu() os.chdir(lib_dir) self.shell.getoutput(cmd) os.chdir(pwd) self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module)
def cython(self, line, cell): """Compile and import everything from a Cython code cell. The contents of the cell are written to a `.pyx` file in the directory `IPYTHONDIR/cython` using a filename with the hash of the code. This file is then cythonized and compiled. The resulting module is imported and all of its symbols are injected into the user's namespace. The usage is similar to that of `%%cython_pyximport` but you don't have to pass a module name:: %%cython def f(x): return 2.0*x To compile OpenMP codes, pass the required `--compile-args` and `--link-args`. For example with gcc:: %%cython --compile-args=-fopenmp --link-args=-fopenmp ... """ args = magic_arguments.parse_argstring(self.cython, line) code = cell if cell.endswith('\n') else cell + '\n' lib_dir = os.path.join(get_ipython_cache_dir(), 'cython') quiet = True key = code, sys.version_info, sys.executable, Cython.__version__ if not os.path.exists(lib_dir): os.makedirs(lib_dir) if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += time.time(), module_name = "_cython_magic_" + hashlib.md5( str(key).encode('utf-8')).hexdigest() module_path = os.path.join(lib_dir, module_name + self.so_ext) have_module = os.path.isfile(module_path) need_cythonize = not have_module if args.annotate: html_file = os.path.join(lib_dir, module_name + '.html') if not os.path.isfile(html_file): need_cythonize = True if need_cythonize: c_include_dirs = args.include if 'numpy' in code: import numpy c_include_dirs.append(numpy.get_include()) pyx_file = os.path.join(lib_dir, module_name + '.pyx') pyx_file = py3compat.cast_bytes_py2( pyx_file, encoding=sys.getfilesystemencoding()) with io.open(pyx_file, 'w', encoding='utf-8') as f: f.write(code) extension = Extension( name=module_name, sources=[pyx_file], include_dirs=c_include_dirs, library_dirs=args.library_dirs, extra_compile_args=args.compile_args, extra_link_args=args.link_args, libraries=args.lib, language='c++' if args.cplus else 'c', ) build_extension = self._get_build_extension() try: opts = dict( quiet=quiet, annotate=args.annotate, force=True, ) build_extension.extensions = cythonize([extension], **opts) except CompileError: return if not have_module: build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module) if args.annotate: try: with io.open(html_file, encoding='utf-8') as f: annotated_html = f.read() except IOError as e: # File could not be opened. Most likely the user has a version # of Cython before 0.15.1 (when `cythonize` learned the # `force` keyword argument) and has already compiled this # exact source without annotation. print( 'Cython completed successfully but the annotated ' 'source could not be read.', file=sys.stderr) print(e, file=sys.stderr) else: return display.HTML(self.clean_annotated_html(annotated_html))
def cython(self, line, cell): """Compile and import everything from a Cython code cell. The contents of the cell are written to a `.pyx` file in the directory `IPYTHONDIR/cython` using a filename with the hash of the code. This file is then cythonized and compiled. The resulting module is imported and all of its symbols are injected into the user's namespace. The usage is similar to that of `%%cython_pyximport` but you don't have to pass a module name:: %%cython def f(x): return 2.0*x To compile OpenMP codes, pass the required `--compile-args` and `--link-args`. For example with gcc:: %%cython --compile-args=-fopenmp --link-args=-fopenmp ... """ args = magic_arguments.parse_argstring(self.cython, line) code = cell if cell.endswith("\n") else cell + "\n" lib_dir = os.path.join(get_ipython_cache_dir(), "cython") quiet = True key = code, line, sys.version_info, sys.executable, cython_version if not os.path.exists(lib_dir): os.makedirs(lib_dir) if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += (time.time(),) if args.name: module_name = py3compat.unicode_to_str(args.name) else: module_name = "_cython_magic_" + hashlib.md5(str(key).encode("utf-8")).hexdigest() module_path = os.path.join(lib_dir, module_name + self.so_ext) have_module = os.path.isfile(module_path) need_cythonize = not have_module if args.annotate: html_file = os.path.join(lib_dir, module_name + ".html") if not os.path.isfile(html_file): need_cythonize = True if need_cythonize: c_include_dirs = args.include if "numpy" in code: import numpy c_include_dirs.append(numpy.get_include()) pyx_file = os.path.join(lib_dir, module_name + ".pyx") pyx_file = py3compat.cast_bytes_py2(pyx_file, encoding=sys.getfilesystemencoding()) with io.open(pyx_file, "w", encoding="utf-8") as f: f.write(code) extension = Extension( name=module_name, sources=[pyx_file], include_dirs=c_include_dirs, library_dirs=args.library_dirs, extra_compile_args=args.compile_args, extra_link_args=args.link_args, libraries=args.lib, language="c++" if args.cplus else "c", ) build_extension = self._get_build_extension() try: opts = dict(quiet=quiet, annotate=args.annotate, force=True) build_extension.extensions = cythonize([extension], **opts) except CompileError: return if not have_module: build_extension.build_temp = os.path.dirname(pyx_file) build_extension.build_lib = lib_dir build_extension.run() self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module) if args.annotate: try: with io.open(html_file, encoding="utf-8") as f: annotated_html = f.read() except IOError as e: # File could not be opened. Most likely the user has a version # of Cython before 0.15.1 (when `cythonize` learned the # `force` keyword argument) and has already compiled this # exact source without annotation. print("Cython completed successfully but the annotated " "source could not be read.", file=sys.stderr) print(e, file=sys.stderr) else: return display.HTML(self.clean_annotated_html(annotated_html))
def pyd(self, line, cell): args = magic_arguments.parse_argstring(self.pyd, line) code = 'import ppyd;\n\n\ extern(C) void PydMain()\n{\n \ registerAll!(Alias!(__traits(parent, PydMain)))();\n\ }\n\n'\ + cell code = code if code.endswith('\n') else code+'\n' key = code, line, sys.version_info, sys.executable try: args.dub_config = json.loads(args.dub_config) except: args.dub_config = json.loads(ast.literal_eval(args.dub_config)) pass try: args.dub_args = ast.literal_eval(args.dub_args) except: pass if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += (time.time(),) args.dub_args = '--force ' + args.dub_args if args.name: module_name = py3compat.unicode_to_str(args.name) else: module_name = "_pyd_magic_" + hashlib.md5(str(key).encode('utf-8')).hexdigest() lib_dir = os.path.join(get_ipython_cache_dir(), 'pyd', module_name) if not os.path.exists(lib_dir): os.makedirs(lib_dir) if os.name == 'nt': so_ext = '.dll' else: so_ext = '.so' #might have to go to dylib on OS X at some point??? module_path = os.path.join(lib_dir, 'lib' + module_name + so_ext) have_module = os.path.isfile(module_path) need_pydize = not have_module if need_pydize: d_include_dirs = args.include pyd_file = os.path.join(lib_dir, module_name + '.d') pyd_file = py3compat.cast_bytes_py2(pyd_file, encoding=sys.getfilesystemencoding()) with io.open(pyd_file, 'w', encoding='utf-8') as f: f.write(code) pyd_dub_file = os.path.join(lib_dir, 'dub.json') pyd_dub_file = py3compat.cast_bytes_py2(pyd_dub_file, encoding=sys.getfilesystemencoding()) pyd_dub_selections_file = os.path.join(lib_dir, 'dub.selections.json') pyd_dub_selections_file = py3compat.cast_bytes_py2(pyd_dub_selections_file, encoding=sys.getfilesystemencoding()) pyd_dub_json = json.loads('{}') pyd_dub_json['name'] = module_name pyd_dub_json['dependencies'] = { "pyd": args.pyd_version, "ppyd": ">=0.1.3" } pyd_dub_json['subConfigurations'] = { "pyd": "python{0}{1}".format(sys.version_info.major, sys.version_info.minor) } pyd_dub_json['sourceFiles'] = [pyd_file] pyd_dub_json['targetType'] = 'dynamicLibrary' pyd_dub_json['dflags'] = ['-fPIC'] pyd_dub_json['libs'] = ['phobos2'] pyd_dub_json['versions'] = ['PydPythonExtension'] with io.open(pyd_dub_file, 'w', encoding='utf-8') as f: f.write(unicode(json.dumps(pyd_dub_json)+'\n', encoding='utf-8')) try: os.remove(pyd_dub_selections_file) except: pass dub_desc = json.loads(subprocess.check_output(["dub", "describe", "--root=" + lib_dir], universal_newlines = True)) for pack in dub_desc['packages']: if pack['name'] == 'pyd': _infraDir = os.path.join(pack['path'], 'infrastructure') break if os.name == 'nt': boilerplatePath = os.path.join(_infraDir, 'd', 'python_dll_windows_boilerplate.d' ) else: boilerplatePath = os.path.join(_infraDir, 'd', 'python_so_linux_boilerplate.d' ) pyd_dub_json['sourceFiles'].append(boilerplatePath) if args.compiler == 'dmd': so_ctor_path = os.path.join(_infraDir, 'd', 'so_ctor.c') so_ctor_object_path = os.path.join(lib_dir, "so_ctor.o") subprocess.check_call(['cc', "-c", "-fPIC", "-o" + so_ctor_object_path, so_ctor_path]) pyd_dub_json['sourceFiles'].append(so_ctor_object_path) mainTemplate = os.path.join(_infraDir, 'd', 'pydmain_template.d') mainTemplate = py3compat.cast_bytes_py2(mainTemplate, encoding=sys.getfilesystemencoding()) mainTemplateOut = os.path.join(lib_dir, 'pydmain.d') mainTemplateOut = py3compat.cast_bytes_py2(mainTemplateOut, encoding=sys.getfilesystemencoding()) with io.open(mainTemplate, 'r', encoding='utf-8') as t, io.open(mainTemplateOut, 'w', encoding='utf-8') as m: m.write(t.read() % {'modulename' : module_name}) pyd_dub_json['sourceFiles'].append(mainTemplateOut) pyd_dub_json = ConfigDict(pyd_dub_json) pyd_dub_json.merge(args.dub_config) with io.open(pyd_dub_file, 'w', encoding='utf-8') as f: f.write(unicode(json.dumps(pyd_dub_json)+'\n', encoding='utf-8')) try: output = subprocess.check_output(["dub", "build", "--root=" + lib_dir] + args.dub_args.split(' '), universal_newlines=True, stderr=subprocess.STDOUT) except (subprocess.CalledProcessError) as e: print(e.output) raise e if args.print_compiler_output: print(output) if not have_module: self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module)
def __init__(self): self.tornado_dir = os.getcwd() stata_dir = os.path.join(get_ipython_cache_dir(), 'stata') print('IPyStata is loaded in batch mode.')
def __init__(self, shell): super(iPyStataMagic, self).__init__(shell) self._lib_dir = os.path.join(get_ipython_cache_dir(), 'stata') if not os.path.exists(self._lib_dir): os.makedirs(self._lib_dir)
import sys import os py3 = sys.version_info > (3, ) if not py3: import ConfigParser else: import configparser from IPython import version_info if version_info[0] < 4: from IPython.utils.path import get_ipython_cache_dir else: from IPython.paths import get_ipython_cache_dir _config_dir = os.path.join(get_ipython_cache_dir(), 'stata', 'config') _config_file = os.path.join(_config_dir, 'configuration.ini') if py3: Config = configparser.ConfigParser() else: Config = ConfigParser.ConfigParser() def make_config(): with open(_config_file, 'w') as configfile: try: Config.add_section('Stata configuration') except: pass Config.set('Stata configuration', 'installation',
def pyd(self, line, cell): args = magic_arguments.parse_argstring(self.pyd, line) code = 'import ppyd;\n\n\ extern(C) void PydMain()\n{\n \ registerAll!(Alias!(__traits(parent, PydMain)))();\n\ }\n\n'\ + cell code = code if code.endswith('\n') else code + '\n' key = code, line, sys.version_info, sys.executable try: args.dub_config = json.loads(args.dub_config) except: args.dub_config = json.loads(ast.literal_eval(args.dub_config)) pass try: args.dub_args = ast.literal_eval(args.dub_args) except: pass if args.force: # Force a new module name by adding the current time to the # key which is hashed to determine the module name. key += (time.time(), ) args.dub_args = '--force ' + args.dub_args if args.name: module_name = py3compat.unicode_to_str(args.name) else: module_name = "_pyd_magic_" + hashlib.md5( str(key).encode('utf-8')).hexdigest() lib_dir = os.path.join(get_ipython_cache_dir(), 'pyd', module_name) if not os.path.exists(lib_dir): os.makedirs(lib_dir) if os.name == 'nt': so_ext = '.dll' else: so_ext = '.so' #might have to go to dylib on OS X at some point??? module_path = os.path.join(lib_dir, 'lib' + module_name + so_ext) have_module = os.path.isfile(module_path) need_pydize = not have_module if need_pydize: d_include_dirs = args.include pyd_file = os.path.join(lib_dir, module_name + '.d') pyd_file = py3compat.cast_bytes_py2( pyd_file, encoding=sys.getfilesystemencoding()) with io.open(pyd_file, 'w', encoding='utf-8') as f: f.write(code) pyd_dub_file = os.path.join(lib_dir, 'dub.json') pyd_dub_file = py3compat.cast_bytes_py2( pyd_dub_file, encoding=sys.getfilesystemencoding()) pyd_dub_selections_file = os.path.join(lib_dir, 'dub.selections.json') pyd_dub_selections_file = py3compat.cast_bytes_py2( pyd_dub_selections_file, encoding=sys.getfilesystemencoding()) pyd_dub_json = json.loads('{}') pyd_dub_json['name'] = module_name pyd_dub_json['dependencies'] = { "pyd": args.pyd_version, "ppyd": ">=0.1.2" } pyd_dub_json['subConfigurations'] = { "pyd": "python{0}{1}".format(sys.version_info.major, sys.version_info.minor) } pyd_dub_json['sourceFiles'] = [pyd_file] pyd_dub_json['targetType'] = 'dynamicLibrary' pyd_dub_json['dflags'] = ['-fPIC'] pyd_dub_json['libs'] = ['phobos2'] pyd_dub_json['versions'] = ['PydPythonExtension'] with io.open(pyd_dub_file, 'w', encoding='utf-8') as f: f.write( unicode(json.dumps(pyd_dub_json) + '\n', encoding='utf-8')) try: os.remove(pyd_dub_selections_file) except: pass dub_desc = json.loads( subprocess.check_output( ["dub", "describe", "--root=" + lib_dir], universal_newlines=True)) for pack in dub_desc['packages']: if pack['name'] == 'pyd': _infraDir = os.path.join(pack['path'], 'infrastructure') break if os.name == 'nt': boilerplatePath = os.path.join( _infraDir, 'd', 'python_dll_windows_boilerplate.d') else: boilerplatePath = os.path.join( _infraDir, 'd', 'python_so_linux_boilerplate.d') pyd_dub_json['sourceFiles'].append(boilerplatePath) if args.compiler == 'dmd': so_ctor_path = os.path.join(_infraDir, 'd', 'so_ctor.c') so_ctor_object_path = os.path.join(lib_dir, "so_ctor.o") subprocess.check_call([ 'cc', "-c", "-fPIC", "-o" + so_ctor_object_path, so_ctor_path ]) pyd_dub_json['sourceFiles'].append(so_ctor_object_path) mainTemplate = os.path.join(_infraDir, 'd', 'pydmain_template.d') mainTemplate = py3compat.cast_bytes_py2( mainTemplate, encoding=sys.getfilesystemencoding()) mainTemplateOut = os.path.join(lib_dir, 'pydmain.d') mainTemplateOut = py3compat.cast_bytes_py2( mainTemplateOut, encoding=sys.getfilesystemencoding()) with io.open(mainTemplate, 'r', encoding='utf-8') as t, io.open( mainTemplateOut, 'w', encoding='utf-8') as m: m.write(t.read() % {'modulename': module_name}) pyd_dub_json['sourceFiles'].append(mainTemplateOut) pyd_dub_json = ConfigDict(pyd_dub_json) pyd_dub_json.merge(args.dub_config) with io.open(pyd_dub_file, 'w', encoding='utf-8') as f: f.write( unicode(json.dumps(pyd_dub_json) + '\n', encoding='utf-8')) try: output = subprocess.check_output( ["dub", "build", "--root=" + lib_dir] + args.dub_args.split(' '), universal_newlines=True, stderr=subprocess.STDOUT) except (subprocess.CalledProcessError) as e: print(e.output) raise e if args.print_compiler_output: print(output) if not have_module: self._code_cache[key] = module_name module = imp.load_dynamic(module_name, module_path) self._import_all(module)
import sys import os py3 = sys.version_info > (3,) if not py3: import ConfigParser else: import configparser from IPython import version_info if version_info[0] < 4: from IPython.utils.path import get_ipython_cache_dir else: from IPython.paths import get_ipython_cache_dir _config_dir = os.path.join(get_ipython_cache_dir(), 'stata', 'config') _config_file = os.path.join(_config_dir, 'configuration.ini') if py3: Config = configparser.ConfigParser() else: Config = ConfigParser.ConfigParser() def make_config(): with open(_config_file, 'w') as configfile: try: Config.add_section('Stata configuration') except: pass Config.set('Stata configuration', 'installation', "C:\Program Files (x86)\Stata13\StataMP-64.exe") Config.set('Stata configuration', 'force_batch_mode', "False")