def test_parse(self): f = FileDownloader() f.parse_args([]) self.assertFalse(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure']) self.assertTrue(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', 'bar', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertEqual(f.target, 'bar') f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert', '--insecure']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with self.assertRaisesRegexp( RuntimeError, "--cacert='nonexistant_file_name' does " "not refer to a valid file"): f.parse_args(['--cacert', 'nonexistant_file_name']) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--foo']) self.assertIn('error: unrecognized arguments: --foo', io.getvalue())
def test_parse(self): f = FileDownloader() f.parse_args([]) self.assertFalse(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure']) self.assertTrue(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertIsNone(f.target) f = FileDownloader() f.parse_args(['--insecure', 'bar', '--cacert', this_file()]) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertEqual(f.target, 'bar') f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--cacert', '--insecure']) self.assertIn('argument --cacert: expected one argument', io.getvalue()) f = FileDownloader() with self.assertRaisesRegexp( RuntimeError, "--cacert='nonexistant_file_name' does " "not refer to a valid file"): f.parse_args(['--cacert', 'nonexistant_file_name']) f = FileDownloader() with capture_output() as io: with self.assertRaises(SystemExit): f.parse_args(['--foo']) self.assertIn('error: unrecognized arguments: --foo', io.getvalue())
def test_init(self): f = FileDownloader() self.assertFalse(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f._fname) f = FileDownloader(True) self.assertTrue(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f._fname) f = FileDownloader(True, this_file()) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertIsNone(f._fname) with self.assertRaisesRegexp( RuntimeError, "cacert='nonexistant_file_name' does not " "refer to a valid file."): FileDownloader(True, 'nonexistant_file_name')
def test_init(self): f = FileDownloader() self.assertFalse(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f._fname) f = FileDownloader(True) self.assertTrue(f.insecure) self.assertIsNone(f.cacert) self.assertIsNone(f._fname) f = FileDownloader(True, this_file()) self.assertTrue(f.insecure) self.assertEqual(f.cacert, this_file()) self.assertIsNone(f._fname) with self.assertRaisesRegexp( RuntimeError, "cacert='nonexistant_file_name' does not " "refer to a valid file."): FileDownloader(True, 'nonexistant_file_name')
import pyomo.common.config as config from pyomo.common.log import LoggingIntercept from pyomo.common.fileutils import ( this_file, find_file, find_library, find_executable, ExecutableManager, _system, _path, _exeExt, _libExt, ) _this_file = this_file() class TestFileUtils(unittest.TestCase): def setUp(self): self.tmpdir = None self.basedir = os.path.abspath(os.path.curdir) self.config = config.PYOMO_CONFIG_DIR self.ld_library_path = os.environ.get('LD_LIBRARY_PATH', None) self.path = os.environ.get('PATH', None) def tearDown(self): config.PYOMO_CONFIG_DIR = self.config os.chdir(self.basedir) if self.tmpdir: shutil.rmtree(self.tmpdir)
import sys import tempfile from six import StringIO import pyutilib.th as unittest from pyutilib.subprocess import run import pyomo.common.config as config from pyomo.common.log import LoggingIntercept from pyomo.common.fileutils import ( this_file, this_file_dir, find_file, find_library, find_executable, PathManager, _system, _path, _exeExt, _libExt, _ExecutableData, ) _this_file = this_file() _this_file_dir = this_file_dir() class TestFileUtils(unittest.TestCase): def setUp(self): self.tmpdir = None self.basedir = os.path.abspath(os.path.curdir) self.config = config.PYOMO_CONFIG_DIR self.ld_library_path = os.environ.get('LD_LIBRARY_PATH', None) self.path = os.environ.get('PATH', None) def tearDown(self): config.PYOMO_CONFIG_DIR = self.config os.chdir(self.basedir) if self.tmpdir: shutil.rmtree(self.tmpdir)
def parse_data_commands(data=None, filename=None, debug=0, outputdir=None): global dat_lexer global dat_yaccer global dat_yaccer_tabfile if outputdir is None: # Try and write this into the module source... outputdir = os.path.dirname(getfile(currentframe())) _tabfile = os.path.join(outputdir, tabmodule + ".py") # Ideally, we would pollute a per-user configuration directory # first -- something like ~/.pyomo. if not os.access(outputdir, os.W_OK): _file = this_file() logger = logging.getLogger('pyomo.dataportal') if os.path.exists(_tabfile) and \ os.path.getmtime(_file) >= os.path.getmtime(_tabfile): logger.warning( "Potentially outdated DAT Parse Table found in source " "tree (%s), but you do not have write access to that " "directory, so we cannot update it. Please notify " "you system administrator to remove that file" % (_tabfile, )) if os.path.exists(_tabfile+'c') and \ os.path.getmtime(_file) >= os.path.getmtime(_tabfile+'c'): logger.warning( "Potentially outdated DAT Parse Table found in source " "tree (%s), but you do not have write access to that " "directory, so we cannot update it. Please notify " "you system administrator to remove that file" % (_tabfile + 'c', )) # Switch the directory for the tabmodule to the current directory outputdir = os.getcwd() # if the lexer/yaccer haven't been initialized, do so. if dat_lexer is None: # # Always remove the parser.out file, which is generated to # create debugging # _parser_out = os.path.join(outputdir, "parser.out") if os.path.exists(_parser_out): os.remove(_parser_out) _tabfile = dat_yaccer_tabfile = os.path.join(outputdir, tabmodule + ".py") if debug > 0 or \ ( os.path.exists(_tabfile) and os.path.getmtime(__file__) >= os.path.getmtime(_tabfile) ): # # Remove the parsetab.py* files. These apparently need to # be removed to ensure the creation of a parser.out file. # if os.path.exists(_tabfile): os.remove(_tabfile) if os.path.exists(_tabfile + "c"): os.remove(_tabfile + "c") for _mod in list(sys.modules.keys()): if _mod == tabmodule or _mod.endswith('.' + tabmodule): del sys.modules[_mod] dat_lexer = lex.lex() # tmpsyspath = sys.path sys.path.append(outputdir) dat_yaccer = yacc.yacc(debug=debug, tabmodule=tabmodule, outputdir=outputdir, optimize=True) sys.path = tmpsyspath # # Initialize parse object # dat_lexer.linepos = [] global _parse_info _parse_info = {} _parse_info[None] = [] # # Parse the file # if filename is not None: if data is not None: raise ValueError("parse_data_commands: cannot specify both " "data and filename arguments") with open(filename, 'r') as FILE: data = FILE.read() if data is None: return None dat_yaccer.parse(data, lexer=dat_lexer, debug=debug) return _parse_info