Ejemplo n.º 1
0
    def OnExportSurface(self, pubsub_evt):
        filename, filetype = pubsub_evt.data
        ftype_prefix = {
            const.FILETYPE_STL: '.stl',
            const.FILETYPE_VTP: '.vtp',
            const.FILETYPE_PLY: '.ply',
            const.FILETYPE_STL_ASCII: '.stl',
        }
        if filetype in ftype_prefix:
            temp_file = tempfile.mktemp(suffix=ftype_prefix[filetype])

            if _has_win32api:
                utl.touch(temp_file)
                _temp_file = temp_file
                temp_file = win32api.GetShortPathName(temp_file)
                os.remove(_temp_file)

            temp_file = temp_file.decode(const.FS_ENCODE)
            self._export_surface(temp_file, filetype)

            shutil.move(temp_file, filename)
Ejemplo n.º 2
0
			def file_created(path):
				if os.path.exists(recordfile_name):
					installed_files = []
					if os.path.exists(recordfile_name):
						recordfile = open(recordfile_name, "r")
						installed_files.extend(line.rstrip("\n") for line in 
											   recordfile)
						recordfile.close()
					try:
						path.decode("ASCII")
					except (UnicodeDecodeError, UnicodeEncodeError):
						# the contents of the record file used by distutils 
						# must be ASCII GetShortPathName allows us to avoid 
						# any issues with encoding because it returns the 
						# short path as 7-bit string (while still being a 
						# valid path)
						path = win32api.GetShortPathName(path)
					installed_files.append(path)
					recordfile = open(recordfile_name, "w")
					recordfile.write("\n".join(installed_files))
					recordfile.close()
Ejemplo n.º 3
0
def shortPath(path):
    """
    Get absolute 'short' path in Unicode, converts to 8.3 windows filenames.
    """
    path_ = os.path.abspath(path)
    if not Windows():
        if not isinstance(path_, unicode):
            return unicode(path_)
        return path_
    if not os.path.exists(path_):
        if os.path.isdir(os.path.dirname(path_)):
            res = shortPath(os.path.dirname(path_))
            return unicode(os.path.join(res, os.path.basename(path_)))
        return unicode(path_)
    try:
        import win32api
        spath = win32api.GetShortPathName(path_)
        return unicode(spath)
    except:
        lg.exc()
        return unicode(path_)
Ejemplo n.º 4
0
def Extract(filename, folder):
    if _has_win32api:
        folder = win32api.GetShortPathName(folder)
    folder = decode(folder, const.FS_ENCODE)

    tar = tarfile.open(filename, "r")
    idir = decode(os.path.split(tar.getnames()[0])[0], 'utf8')
    os.mkdir(os.path.join(folder, idir))
    filelist = []
    for t in tar.getmembers():
        fsrc = tar.extractfile(t)
        fname = os.path.join(folder, decode(t.name, 'utf-8'))
        fdst = open(fname, 'wb')
        shutil.copyfileobj(fsrc, fdst)
        filelist.append(fname)
        fsrc.close()
        fdst.close()
        del fsrc
        del fdst
    tar.close()
    return filelist
Ejemplo n.º 5
0
def getShortName(filename):
    '''
    Get the Short Name of the specified file. This function gets the short path, but maintains the full resolution base filename.

    This is a workaround for an issue that Farsite has with the command file containing blanks in the file paths. We can't go to
    full 8.3 syntax, because this confuses dealing with Shapefiles. We were having issues with the 3rd Ignition Points file
    always gave us grief, no matter what the name.

    This is required because:
    1) The command file wont support quotes around file paths that contains spaces. So we need a way to remove spaces, thus Short Names
    2) Unfortunately Short Names aren't prefix consistent. So the short name prefix of FileName.shp isnt neccessarily the same as Filename.dbf.
    Ex:
        IT69BF~1.DBF It0001-Ts2028-ignitionPtsFile.dbf
        IT3ED5~1.PRJ It0001-Ts2028-ignitionPtsFile.prj

    :param filename: The filename we want to get the short path for
    :return: The absolute path of the file, with the directory of the file expressing in short format, but the base filename itself in full "resolution"
    '''

    file_dir = os.path.dirname(filename)
    return os.path.join(win32api.GetShortPathName(file_dir),os.path.basename(filename))
Ejemplo n.º 6
0
def getcwd():
    """
    Wrap os.getcwd()

    On Windows return ShortPathName (8.3 filename) that contain only ascii
    characters.
    """
    cwd = os.getcwd()
    # TODO os.getcwd should work properly with py3 on windows.
    if is_win:
        try:
            unicode(cwd)
        except UnicodeDecodeError:
            # Do conversion to ShortPathName really only in case 'cwd' is not
            # ascii only - conversion to unicode type cause this unicode error.
            try:
                import win32api
                cwd = win32api.GetShortPathName(cwd)
            except ImportError:
                pass
    return cwd
Ejemplo n.º 7
0
def remove_folder(path):
	"""Recursively delete a directory tree."""
	if os.name == "nt" and win32api_available:
		path = win32api.GetShortPathName(path)
	names = os.listdir(path)
	for name in names:
		fullname = os.path.join(path, name)
		if os.path.isdir(fullname):
			remove_folder(fullname)
		else:
			try:
				os.remove(fullname)
			except OSError:
				try:
					os.remove("\\\\?\\" + fullname)
				except OSError:  # it's a dir?
					remove_folder(fullname)
	try:
		os.rmdir(path)
	except OSError:
		os.rmdir("\\\\?\\" + path)
Ejemplo n.º 8
0
def create(scripts, debug, verbosity, workdir, ascii=0):
    infos = []  # (path, module, klasses)
    for script in scripts:
        infos.append(analscriptname(script))
    if not os.path.exists(workdir):
        os.makedirs(workdir)
    outfnm = 'drive%s.py' % infos[0][1]
    outfnm = os.path.join(workdir, outfnm)
    outf = open(outfnm, 'w')
    klassspecs = []
    modimports = []
    flags = 'debug=%s, quiet=%s' % (debug, verbosity == 0)
    paths = []
    for path, module, klasses in infos:
        if path:
            paths.append(path)
        modimports.append("import %s" % (module, ))
        for klass in klasses:
            klassspecs.append("%s.%s" % (module, klass))
    for i in range(len(paths)):
        path = paths[i]
        paths[i] = win32api.GetShortPathName(os.path.normpath(path))
    modimports = string.join(modimports, '\n')
    klassspecs = string.join(klassspecs, ', ')
    d = {
        'modules': modimports,
        'klasses': klassspecs,
    }
    outf.write(tmplt % d)
    outf.close()
    print "**********************************"
    print "Driver script %s created" % outfnm
    specfnm = Makespec.main([outfnm],
                            console=debug,
                            debug=debug,
                            workdir=workdir,
                            pathex=paths,
                            comserver=1,
                            ascii=ascii)
    print "Spec file %s created" % specfnm
Ejemplo n.º 9
0
    def check(self, run):
        if not run.run_ok:
            log.error("Cannot check failed run %s" % (run))
            return False

        args = run.get_tmp_files([self.file1, self.gold])

        if os.name != "nt":
        
            x = Run({}, "numdiff.py", [(x, AT_OPAQUE) for x in (self.options + args)])
            if not x.run():
                log.info("numdiff.py %s '%s' '%s'" % tuple([" ".join(self.options)] + args))
                return False

            run.check_ok = True   
        else:

            # Here we go through bash for windows because numdiff isn't on windows.

            # To get a properly cased filed path (required for bash)
            # to install for python2.7:
            # pip install pypiwin32
            import win32api
            args = [win32api.GetLongPathName(win32api.GetShortPathName(x)) for x in args]

            # Now replace drive to use bash on windows format. e.g. c: gets replaced to /mnt/c/
            # and \ gets replaced with /
            def windows_drive_to_bash(p):
                drive = p[0].lower()
                return "/mnt/" + drive + p[2:]
                
            args = [windows_drive_to_bash(x.replace("\\", "/")) for x in args]
            
            x = Run({}, "numdiff.bat", [(x, AT_OPAQUE) for x in (self.options + args)])
            if not x.run():
                log.info("numdiff.bat  %s '%s' '%s'" % tuple([" ".join(self.options)] + args))
                return False

            run.check_ok = True
        return True
Ejemplo n.º 10
0
 def threadPycheckerRun(self):
     result=''
     rc=-1
     try:
         options = self.greppattern
         files= ' '.join(self.flist)
         # Recently MarkH has failed to run pychecker without it having 
         # been explicitly installed - so we assume it is and locate it
         # from its default location.
         # Step1 - get python.exe
         py = os.path.join(sys.prefix, 'python.exe')
         if not os.path.isfile(py):
             if "64 bit" in sys.version:
                 py = os.path.join(sys.prefix, 'PCBuild', 'amd64', 'python.exe')
             else:
                 py = os.path.join(sys.prefix, 'PCBuild', 'python.exe')
         try:
             py = win32api.GetShortPathName(py)
         except win32api.error:
             py = ""
         # Find checker.py
         from distutils.sysconfig import get_python_lib
         pychecker = os.path.join(get_python_lib(), 'pychecker', 'checker.py')
         if not os.path.isfile(py):
             result = "Can't find python.exe!\n"
         elif not os.path.isfile(pychecker):
             result = "Can't find checker.py - please install pychecker " \
                      "(or run 'setup.py install' if you have the source version)\n"
         else:
             cmd='%s "%s" %s %s 2>&1' % (py, pychecker, options,files)
             ##fin,fout,ferr=os.popen3(cmd)
             ##result=ferr.read()+fout.read()
             result=os.popen(cmd).read()
             ##rc=f.close()
         self.GetFirstView().Append(result)
     finally:
         self.result=result
         print '== Pychecker run finished =='
         self.GetFirstView().Append('\n'+'== Pychecker run finished ==')
         self.SetModifiedFlag(0)
Ejemplo n.º 11
0
def move_ensure_casing(source_path, target_path):
    """Moves a file from source_path to target_path.
    If the move would result just in the name changing the case apply workarounds
    for Linux and Windows to ensure the case change is applied on case-insensitive
    file systems. Otherwise use shutil.move to move the file.
    """
    source_path = os.path.normpath(source_path)
    target_path = os.path.normpath(target_path)
    if source_path == target_path:
        return
    # Special handling is only required if both paths refer to the same file
    # but the file name differs in casing.
    # Also macOS does allow renaming only the casing and does not need special
    # handling.
    if not IS_MACOS and samefile_different_casing(source_path, target_path):
        if IS_LINUX:
            # On Linux always force a double move
            _move_force_rename(source_path, target_path)
            return
        elif IS_WIN and win32api:
            # Windows supports case renaming for NTFS and SMB shares, but not
            # on FAT32 or exFAT file systems. Perform a normal move first,
            # then check the result.
            shutil.move(source_path, target_path)
            try:
                # Get the path in the actual casing as stored on disk
                actual_path = win32api.GetLongPathNameW(
                    win32api.GetShortPathName(target_path))
                if samefile_different_casing(target_path, actual_path):
                    _move_force_rename(source_path, target_path)
            except pywintypes.error:
                pass
            return
    # Just perform a normal move
    try:
        shutil.move(source_path, target_path)
    except shutil.SameFileError:
        # Sometimes different paths refer to the same file (e.g. network path / local path on Windows)
        pass
Ejemplo n.º 12
0
def _get_win_folder_with_pywin32(csidl_name):
    from win32com.shell import shellcon, shell
    dir = shell.SHGetFolderPath(0, getattr(shellcon, csidl_name), 0, 0)
    # Try to make this a unicode path because SHGetFolderPath does
    # not return unicode strings when there is unicode data in the
    # path.
    try:
        dir = unicode(dir)
        has_high_char = False
        for c in dir:
            if ord(c) > 255:
                has_high_char = True
                break
        if has_high_char:
            try:
                import win32api
                dir = win32api.GetShortPathName(dir)
            except ImportError:
                pass
    except UnicodeError:
        pass
    return dir
Ejemplo n.º 13
0
 def testShortLongPathNames(self):
     try:
         me = __file__
     except NameError:
         me = sys.argv[0]
     fname = os.path.abspath(me).lower()
     short_name = win32api.GetShortPathName(fname).lower()
     long_name = win32api.GetLongPathName(short_name).lower()
     self.assertTrue(
         long_name == fname,
         "Expected long name ('%s') to be original name ('%s')" % (long_name, fname),
     )
     self.assertEqual(long_name, win32api.GetLongPathNameW(short_name).lower())
     long_name = win32api.GetLongPathNameW(short_name).lower()
     self.assertTrue(
         type(long_name) == str,
         "GetLongPathNameW returned type '%s'" % (type(long_name),),
     )
     self.assertTrue(
         long_name == fname,
         "Expected long name ('%s') to be original name ('%s')" % (long_name, fname),
     )
Ejemplo n.º 14
0
def getcwd():
    """
    Wrap os.getcwd()

    On Windows return ShortPathName (8.3 filename) that contain only ascii
    characters.
    """
    cwd = os.getcwd()
    # os.getcwd works properly with Python 3 on Windows.
    # We need this workaround only for Python 2 on Windows.
    if is_win and is_py2:
        try:
            unicode(cwd)
        except UnicodeDecodeError:
            # Do conversion to ShortPathName really only in case 'cwd' is not
            # ascii only - conversion to unicode type cause this unicode error.
            try:
                import win32api
                cwd = win32api.GetShortPathName(cwd)
            except ImportError:
                pass
    return cwd
Ejemplo n.º 15
0
    def genheatmap(fname, filetype=None, move=True):

        Common.initFolder(RSInferencer.dname)
        loadcmd = {None: "load", "std": "read_std"}

        # write script
        cmd = "iplot2_pdf"
        f = tempfile.SpooledTemporaryFile(mode="wt")
        if platform.system() == "Windows":
            import win32api

            fname = win32api.GetShortPathName(fname)
        f.write("%s %s\n" % (loadcmd[filetype], fname))
        f.write("%s\n" % cmd)
        f.write("quit\n")
        f.seek(0)

        # invoke psuade
        out, error = Common.invokePsuade(f)
        f.close()

        # process error
        if error:
            return None

        # check output file
        mfile = "matlabiplt2pdf.m"
        if os.path.exists(mfile):
            if move:
                mfile_ = RSInferencer.dname + os.path.sep + mfile
                os.rename(mfile, mfile_)
                mfile = mfile_
        else:
            error = "RSInference: %s does not exist." % mfile
            Common.showError(error, out)
            return None

        return mfile
Ejemplo n.º 16
0
def main(argv):

    global sqlServerInstance, sqlDbName, sqlUser, sqlPassword, sqlScriptListfilePath, origOutputFilepath, pathToNewSqlDir, newOutputFilepath, sqlCmdDirPath, IsDummyRun

    try:
        opts, args = getopt.getopt(argv, "dhw",
                                   ["dummyrun", "help", "warnings"])
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    if (len(args) != 8):
        usage()
        sys.exit(3)
    #assign the args to variables:
    sqlServerInstance = args[0]
    sqlDbName = args[1]
    sqlUser = args[2]
    sqlScriptListfilePath = args[3]
    origOutputFilepath = args[4]
    pathToNewSqlDir = args[5]
    newOutputFilepath = args[6]
    sqlCmdDirPath = args[7]

    #convert sqlCmdDirPath to short file names:
    #printOut("looking for sqlcmd.exe at " + sqlCmdDirPath + "\n")
    sqlCmdDirPath = win32api.GetShortPathName(sqlCmdDirPath)

    for opt, arg in opts:
        if opt in ("-d", "--dummyrun"):
            IsDummyRun = True
        elif opt in ("-h", "--help"):
            usage()
            sys.exit()
        elif opt in ("-w", "--warnings"):
            setLogVerbosity(LOG_WARNINGS)

    prompt = "Please enter the password for server: " + sqlServerInstance + " database: " + sqlDbName + " user: "******" "
    sqlPassword = getpass.getpass(prompt)
Ejemplo n.º 17
0
    def genheatmap(fname, filetype=None, move=True):

        Common.initFolder(RSInferencer.dname)
        loadcmd = {None: 'load', 'std': 'read_std'}

        # write script
        cmd = 'iplot2_pdf'
        f = tempfile.SpooledTemporaryFile()
        if platform.system() == 'Windows':
            import win32api
            fname = win32api.GetShortPathName(fname)
        f.write('%s %s\n' % (loadcmd[filetype], fname))
        f.write('%s\n' % cmd)
        f.write('quit\n')
        f.seek(0)

        # invoke psuade
        out, error = Common.invokePsuade(f)
        f.close()

        # process error
        if error:
            return None

        # check output file
        mfile = 'matlabiplt2pdf.m'
        if os.path.exists(mfile):
            if move:
                mfile_ = RSInferencer.dname + os.path.sep + mfile
                os.rename(mfile, mfile_)
                mfile = mfile_
        else:
            error = 'RSInference: %s does not exist.' % mfile
            Common.showError(error, out)
            return None

        return mfile
Ejemplo n.º 18
0
 def run_test(script, cmdline_rest=""):
     dirname, scriptname = os.path.split(script)
     # some tests prefer to be run from their directory.
     cwd = os.getcwd()
     os.chdir(dirname)
     try:
         executable = win32api.GetShortPathName(sys.executable)
         cmd = '%s "%s" %s' % (sys.executable, scriptname, cmdline_rest)
         print script
         stdin, stdout, stderr = os.popen3(cmd)
         stdin.close()
         while 1:
             char = stderr.read(1)
             if not char:
                 break
             sys.stdout.write(char)
         for line in stdout.readlines():
             print line
         stdout.close()
         result = stderr.close()
         if result is not None:
             print "****** %s failed: %s" % (script, result)
     finally:
         os.chdir(cwd)
Ejemplo n.º 19
0
    def run(self, config, year, *args, **kwargs):
        """Runs the travel model, using appropriate info from config. 
        """
        tm_config = config["travel_model_configuration"]
        tm_data_dir = tm_config["directory"]
        self.prepare_for_run(tm_config, year)

        year_dir = tm_config[year]  #'CoreEA0511202006\\urbansim\\2001'
        dir_part1, dir_part2 = os.path.split(year_dir)
        while dir_part1:
            dir_part1, dir_part2 = os.path.split(dir_part1)
        project_year_dir = os.path.join(
            tm_data_dir, dir_part2)  #C:/SEMCOG_baseline/CoreEA0511202006

        logger.log_status('Start travel model from directory %s for year %d' %
                          (project_year_dir, year))
        #for macroname, ui_db_file in tm_config['macro']['run_semcog_travel_model'].iteritems():
        #pass
        macroname, ui_db_file = tm_config['macro']['run_semcog_travel_model']

        loops = 1
        logger.log_status('Running travel model ...')
        tcwcmd = win32api.GetShortPathName(tm_config['transcad_binary'])

        os.system('start /B "start TransCAD" %s' %
                  tcwcmd)  #start TransCAD in background
        time.sleep(1)
        #os.system("%s -a %s -ai '%s'" % (tcwcmd, ui_db_file, macroname))
        run_transcad_macro(macroname, ui_db_file, loops)

        try:
            pass
            ##win32process.TerminateProcess(self.hProcess, 0)
        except:
            logger.log_warning(
                "The code has problem to terminate the TransCAD it started.")
Ejemplo n.º 20
0
    def fix_path(self, in_some_path_to_fix):
        """  On Windows: to overcome cUrl inability to handle path with unicode chars, we try to calculate the windows
                short path (DOS style 8.3 chars). The function that does that, win32api.GetShortPathName,
                does not work for paths that do not yet exist so we need to also create the folder.
                However if the creation requires admin permissions - it could fail -
                in which case we revert to using the long path.
        """

        fixed_path = PurePath(in_some_path_to_fix)
        if 'Win' in utils.get_current_os_names():
            # to overcome cUrl inability to handle path with unicode chars, we try to calculate the windows
            # short path (DOS style 8.3 chars). The function that does that, win32api.GetShortPathName,
            # does not work for paths that do not yet exist so we need to also create the folder.
            # However if the creation requires admin permissions - it could fail -
            # in which case we revert to using the long path.
            import win32api
            fixed_path_parent = str(fixed_path.parent)
            fixed_path_name = str(fixed_path.name)
            if fixed_path_parent not in self.short_win_paths_cache:
                try:
                    os.makedirs(fixed_path_parent, exist_ok=True)
                    short_parent_path = win32api.GetShortPathName(
                        fixed_path_parent)
                    self.short_win_paths_cache[
                        fixed_path_parent] = short_parent_path
                except Exception as e:  # failed to mkdir or get the short path? never mind, just use the full path
                    self.short_win_paths_cache[
                        fixed_path_parent] = fixed_path_parent
                    log.warning(
                        f"""warning creating short path failed for {fixed_path}, {e}, using long path"""
                    )

            short_file_path = os.path.join(
                self.short_win_paths_cache[fixed_path_parent], fixed_path_name)
            fixed_path = short_file_path.replace("\\", "\\\\")
        return fixed_path
Ejemplo n.º 21
0
 def testShortUnicodeNames(self):
     try:
         me = __file__
     except NameError:
         me = sys.argv[0]
     fname = os.path.abspath(me).lower()
     # passing unicode should cause GetShortPathNameW to be called.
     short_name = win32api.GetShortPathName(str(fname)).lower()
     self.assertTrue(isinstance(short_name, str))
     long_name = win32api.GetLongPathName(short_name).lower()
     self.assertTrue(
         long_name == fname,
         "Expected long name ('%s') to be original name ('%s')" % (long_name, fname),
     )
     self.assertEqual(long_name, win32api.GetLongPathNameW(short_name).lower())
     long_name = win32api.GetLongPathNameW(short_name).lower()
     self.assertTrue(
         type(long_name) == str,
         "GetLongPathNameW returned type '%s'" % (type(long_name),),
     )
     self.assertTrue(
         long_name == fname,
         "Expected long name ('%s') to be original name ('%s')" % (long_name, fname),
     )
Ejemplo n.º 22
0
 def __savelists__(self):        
     _dellist  = MpGlobal.installPath+"sync_dellist.bat"
     _cpylist = MpGlobal.installPath+"sync_copylist.bat"
     
     _filelist = MpGlobal.installPath+"sync_filelist.txt"
     
     # sorting the lists makes it easier to manually compare them
     k = lambda record: record[1]
     self.listc.sort(key = k)
     self.listd.sort()
     
     #self.filelist.sort()
     #wf = open("./test_externalfilelist.txt","w")
     #for item in self.filelist:
     #    wf.write( "%s\n"%item )
     #wf.close()
     
     wf = open(_dellist,"w")
     wf.write( "@echo off\n" )
     for item in self.listd:
         wf.write( "DEL \"%s\"\n"%item )
     wf.close()
     
     wf = open(_cpylist,"w")
     wf.write( "@echo off\n" )
     for item in self.listdir:
         wf.write( "md \"%s\"\n"%item )
     for item in self.listc:
         
         s0 = "\"%s\""%win32api.GetShortPathName(item[0].replace('/','\\'))
         s1 = "\"%s\""%item[1]
         
         wf.write( "copy %-70s %s\n"%(s0,s1) )
         #wf.write( "%s\n"%(item[0]) )
         #wf.write( "%s\n"%(item[1]) )
     wf.close()
Ejemplo n.º 23
0
    def filterdata(fname, **kwargs):

        # read data
        data = LocalExecutionModule.readSampleFromPsuadeFile(fname)

        # process keyworded arguments
        filterVar = None
        vmin = None
        vmax = None
        for key in kwargs:
            k = key.lower()
            if k == 'input':
                filterVar = kwargs[key]
                cmd = 'ifilter'

                # Get only input variables that are variable
                types = data.getInputTypes()
                inVarNames = SampleData.getInputNames(data)
                varNames = []
                for i in range(len(types)):
                    if types[i] == Model.VARIABLE:
                        varNames.append(inVarNames[i])

            elif k == 'output':
                filterVar = kwargs[key]
                cmd = 'ofilter'
                varNames = SampleData.getOutputNames(data)
            elif k == 'vmin':
                vmin = kwargs[key]
            elif k == 'vmax':
                vmax = kwargs[key]

        if filterVar is None:
            error = 'DataProcessor: In function filterData(), the filter variable is not specified.'
            Common.showError(error)
            return None
        if (vmin is None) | (vmax is None) | (vmin >= vmax):
            error = 'DataProcessor: filterData() requires a valid [min, max] range to filter the variable "%s".' % filterVar
            Common.showError(error)
            return None

        # check if the filter variable exists 
        if filterVar not in varNames:
            error = 'DataProcessor: In function filterData(), %s does not contain the filter variable "%s".' % (fname, filterVar)
            Common.showError(error)
            outfile = fname
            return outfile

        # get the output index to filter variable
        filterIndex = varNames.index(filterVar) + 1    # psuade is 1-indexed

        # write script
        outfile = Common.getLocalFileName(DataProcessor.dname, fname, '.filtered')
        f = tempfile.SpooledTemporaryFile(mode="wt")
        if platform.system() == 'Windows':
            import win32api
            fname = win32api.GetShortPathName(fname)        
        f.write('load %s\n' % fname)
        f.write('%s\n' % cmd)            # invoke ifilter or ofilter
        if (cmd == 'ifilter' and data.getNumInputs() > 1) or (cmd == 'ofilter' and data.getNumOutputs() > 1):
            f.write('%d\n' % filterIndex)    # select the filter variable
        f.write('%f\n' % vmin)           # extract points within range [vmin, vmax]
        f.write('%f\n' % vmax)
        if platform.system() == 'Windows':
            head, tail = os.path.split(outfile)
            head = win32api.GetShortPathName(head)
            outfile = os.path.join(head, tail)
        f.write('write %s\n' % outfile) 
        f.write('n\n')                   # write all outputs
        f.write('quit\n')
        f.seek(0)

        # invoke psuade
        out, error = Common.invokePsuade(f)
        f.close()

        # Process error
        if error:
            return None

        if not os.path.exists(outfile):
            error = 'DataProcessor: %s does not exist.' % outfile
            Common.showError(error, out)
            return None

        return outfile
Ejemplo n.º 24
0
 def real_path(path):
     """Get the real capitalisation of a path on Windows."""
     if not os.path.exists(path):
         return path
     return win32api.GetLongPathNameW(win32api.GetShortPathName(path))
Ejemplo n.º 25
0
    def getMetadata(self):

        if self.dir.endswith(".zip"):
            zpfile = zipfile.ZipFile(self.dir)
            metadatafile = fnmatch.filter(zpfile.namelist(), "*/GRANULE/*/MTD_TL.xml")

            if len(metadatafile) != 1 and self.tileID is not None:
                metadatafile = fnmatch.filter(zpfile.namelist(), "*/GRANULE/*/*%s.xml" % self.tileID)

            if len(metadatafile) != 1:
                raise RuntimeError("No metadata file found!")

            # Open metadata file
            metaF = zpfile.open(metadatafile[0])
            metainfo = eTree.parse(metaF)
            rootElement = metainfo.getroot()

            # Get IDs
            self.ID = [child.text for child in rootElement.iter("TILE_ID")][0]
            tileIDindex = self.ID.rfind("_T")+1
            self.tileID = self.ID[tileIDindex:tileIDindex+6]

            # Get date
            datestring = [child.text for child in rootElement.iter("SENSING_TIME")][0]
            self.cloudy = [float(child.text) for child in rootElement.iter("CLOUDY_PIXEL_PERCENTAGE")][0]
            self.degraded = [float(child.text) for child in rootElement.iter("DEGRADED_MSI_DATA_PERCENTAGE")][0]

            self.date = dt.strptime(datestring[:10], "%Y-%m-%d")

            metaF.close()
            zpfile.close()

        else:
            metadataMatches = []
            if self.tileID is not None:
                searchPattern = "*" + self.tileID + "*.xml"
            else:
                searchPattern = "*.xml"

            for (root, dirnames, filenames) in os.walk(self.dir):
                for d in fnmatch.filter(dirnames, "GRANULE"):
                    self.granuleDir = os.path.join(root, d)
            for d in os.listdir(self.granuleDir):
                for f in fnmatch.filter(os.listdir(os.path.join(self.granuleDir, d)), searchPattern):
                    metadataMatches.append(os.path.join(self.granuleDir, d, f))

            if len(metadataMatches) != 1:
                raise RuntimeError("No metadata file found!")
            else:
                metadatafile = metadataMatches[0]

            self.metadatafile = os.path.join(win32api.GetShortPathName(os.path.dirname(metadatafile)), os.path.basename(metadatafile))
            try:
                metaF = open(self.metadatafile)
            except IOError as e:
                if len(self.metadatafile) > 256:
                    raise IOError("File path to metadata file is too long. Move scene to a different location to shorten the file path.")
                else:
                    raise IOError("Meta data file could not be read.")

            metainfo = eTree.parse(metaF)
            rootElement = metainfo.getroot()

            datestring = [child.text for child in rootElement.iter("SENSING_TIME")][0]
            self.cloudy = [float(child.text) for child in rootElement.iter("CLOUDY_PIXEL_PERCENTAGE")][0]
            self.degraded = [float(child.text) for child in rootElement.iter("DEGRADED_MSI_DATA_PERCENTAGE")][0]
            self.date = dt.strptime(datestring[:10], "%Y-%m-%d")
            self.ID = [child.text for child in rootElement.iter("TILE_ID")][0]
            tileIDindex = self.ID.rfind("_T") + 1
            self.tileID = self.ID[tileIDindex:tileIDindex + 6]

            metaF.close()
Ejemplo n.º 26
0
import win32api

mypathraw = raw_input("Enter directory:")
if ( len(mypathraw) < 1 ) : mypath = r'g:\working'
else : mypath = mypathraw

#pprint.pprint(os.listdir(mypath))
#print mypath

for dirname, dirnames, filenames in os.walk(mypath):
	print "DIRNAME: "
	print dirname
	print "DIRNAMES: " 
	print dirnames
	print "FILENAMES: "
	#pprint.pprint(filenames)
	for fname in filenames:
		#fnameshort = win32api.GetShortPathName(fname)
		fname2 = os.path.join(dirname, fname)
		#print fname2
		fnameshort = win32api.GetShortPathName(fname2)
		print fname2		
		#newfname = re.findall('.*Fantasy op.17 - (.*)', fname)
		newfname = 'Schumann - ' + fname
		print newfname
		newfname2 = os.path.join(dirname, newfname)
		#newfname2short = win32api.GetShortPathName(newfname2)
		if os.path.isfile(fnameshort):
			print "OK OK OK OK OK"
			os.rename(fnameshort, newfname2)
Ejemplo n.º 27
0
def ReExecuteElevated(flags):
  from win32com.shell.shell import ShellExecuteEx
  from win32com.shell import shellcon
  import win32process, win32event
  import winxpgui # we've already checked we are running XP above
  import tempfile

  if not flags['quiet']:
    print("Requesting elevation and retrying...")
  new_params = " ".join(['"' + a + '"' for a in sys.argv])
  # If we aren't already in unattended mode, we want our sub-process to
  # be.
  if not flags['unattended']:
    new_params += " --unattended"
  # specifying the parent means the dialog is centered over our window,
  # which is a good usability clue.
  # hwnd is unlikely on the command-line, but flags may come from elsewhere
  hwnd = flags.get('hwnd', None)
  if hwnd is None:
    try:
      hwnd = winxpgui.GetConsoleWindow()
    except winxpgui.error:
      hwnd = 0
  # Redirect output so we give the user some clue what went wrong.  This
  # also means we need to use COMSPEC.  However, the "current directory"
  # appears to end up ignored - so we execute things via a temp batch file.
  tempbase = tempfile.mktemp("pycomserverreg")
  outfile = tempbase + ".out"
  batfile = tempbase + ".bat"
  try:
    batf = open(batfile, "w")
    try:
      cwd = os.getcwd()
      print("@echo off", file=batf)
      # nothing is 'inherited' by the elevated process, including the
      # environment.  I wonder if we need to set more?
      print("set PYTHONPATH=%s" % os.environ.get('PYTHONPATH', ''), file=batf)
      # may be on a different drive - select that before attempting to CD.
      print(os.path.splitdrive(cwd)[0], file=batf)
      print('cd "%s"' % os.getcwd(), file=batf)
      print('%s %s > "%s" 2>&1' % (win32api.GetShortPathName(sys.executable), new_params, outfile), file=batf)
    finally:
      batf.close()
    executable = os.environ.get('COMSPEC', 'cmd.exe')
    rc = ShellExecuteEx(hwnd=hwnd,
                        fMask=shellcon.SEE_MASK_NOCLOSEPROCESS,
                        lpVerb="runas",
                        lpFile=executable,
                        lpParameters='/C "%s"' % batfile,
                        nShow=win32con.SW_SHOW)
    hproc = rc['hProcess']
    win32event.WaitForSingleObject(hproc, win32event.INFINITE)
    exit_code = win32process.GetExitCodeProcess(hproc)
    outf = open(outfile)
    try:
      output = outf.read()
    finally:
      outf.close()

    if exit_code:
      # Even if quiet you get to see this message.
      print("Error: registration failed (exit code %s)." % exit_code)
    # if we are quiet then the output if likely to already be nearly
    # empty, so always print it.
    print(output, end=' ')
  finally:
    for f in (outfile, batfile):
      try:
        os.unlink(f)
      except os.error as exc:
        print("Failed to remove tempfile '%s': %s" % (f, exc))
Ejemplo n.º 28
0
def RegisterServer(clsid, 
                   pythonInstString=None, 
                   desc=None,
                   progID=None, verProgID=None,
                   defIcon=None,
                   threadingModel="both",
                   policy=None,
                   catids=[], other={},
                   addPyComCat=None,
                   dispatcher = None,
                   clsctx = None,
                   addnPath = None,
                  ):
  """Registers a Python object as a COM Server.  This enters almost all necessary
     information in the system registry, allowing COM to use the object.

     clsid -- The (unique) CLSID of the server.
     pythonInstString -- A string holding the instance name that will be created
                   whenever COM requests a new object.
     desc -- The description of the COM object.
     progID -- The user name of this object (eg, Word.Document)
     verProgId -- The user name of this version's implementation (eg Word.6.Document)
     defIcon -- The default icon for the object.
     threadingModel -- The threading model this object supports.
     policy -- The policy to use when creating this object.
     catids -- A list of category ID's this object belongs in.
     other -- A dictionary of extra items to be registered.
     addPyComCat -- A flag indicating if the object should be added to the list
              of Python servers installed on the machine.  If None (the default)
              then it will be registered when running from python source, but
              not registered if running in a frozen environment.
     dispatcher -- The dispatcher to use when creating this object.
     clsctx -- One of the CLSCTX_* constants.
     addnPath -- An additional path the COM framework will add to sys.path
                 before attempting to create the object.
  """


  ### backwards-compat check
  ### Certain policies do not require a "class name", just the policy itself.
  if not pythonInstString and not policy:
    raise TypeError('You must specify either the Python Class or Python Policy which implement the COM object.')

  keyNameRoot = "CLSID\\%s" % str(clsid)
  _set_string(keyNameRoot, desc)

  # Also register as an "Application" so DCOM etc all see us.
  _set_string("AppID\\%s" % clsid, progID)
  # Depending on contexts requested, register the specified server type.
  # Set default clsctx.
  if not clsctx:
    clsctx = pythoncom.CLSCTX_INPROC_SERVER | pythoncom.CLSCTX_LOCAL_SERVER
  # And if we are frozen, ignore the ones that don't make sense in this
  # context.
  if pythoncom.frozen:
    assert sys.frozen, "pythoncom is frozen, but sys.frozen is not set - don't know the context!"
    if sys.frozen == "dll":
      clsctx = clsctx & pythoncom.CLSCTX_INPROC_SERVER
    else:
      clsctx = clsctx & pythoncom.CLSCTX_LOCAL_SERVER
  # Now setup based on the clsctx left over.
  if clsctx & pythoncom.CLSCTX_INPROC_SERVER:
    # get the module to use for registration.
    # nod to Gordon's installer - if sys.frozen and sys.frozendllhandle
    # exist, then we are being registered via a DLL - use this DLL as the
    # file name.
    if pythoncom.frozen:
      if hasattr(sys, "frozendllhandle"):
        dllName = win32api.GetModuleFileName(sys.frozendllhandle)
      else:
        raise RuntimeError("We appear to have a frozen DLL, but I don't know the DLL to use")
    else:
      # Normal case - running from .py file, so register pythoncom's DLL.
      # Although now we prefer a 'loader' DLL if it exists to avoid some
      # manifest issues (the 'loader' DLL has a manifest, but pythoncom does not)
      pythoncom_dir = os.path.dirname(pythoncom.__file__)
      if pythoncom.__file__.find("_d") < 0:
        suffix = ""
      else:
        suffix = "_d"
      loadername = "pythoncomloader%d%d%s.dll" % (sys.version_info[0], sys.version_info[1], suffix)
      if os.path.isfile(os.path.join(pythoncom_dir, loadername)):
        dllName = loadername
      else:
        # just use pythoncom.
        dllName = os.path.basename(pythoncom.__file__)

    _set_subkeys(keyNameRoot + "\\InprocServer32",
                 { None : dllName,
                   "ThreadingModel" : threadingModel,
                   })
  else: # Remove any old InProcServer32 registrations
    _remove_key(keyNameRoot + "\\InprocServer32")

  if clsctx & pythoncom.CLSCTX_LOCAL_SERVER:
    if pythoncom.frozen:
      # If we are frozen, we write "{exe} /Automate", just
      # like "normal" .EXEs do
      exeName = win32api.GetShortPathName(sys.executable)
      command = '%s /Automate' % (exeName,)
    else:
      # Running from .py sources - we need to write
      # 'python.exe win32com\server\localserver.py {clsid}"
      exeName = _find_localserver_exe(1)
      exeName = win32api.GetShortPathName(exeName)
      pyfile = _find_localserver_module()
      command = '%s "%s" %s' % (exeName, pyfile, str(clsid))
    _set_string(keyNameRoot + '\\LocalServer32', command)
  else: # Remove any old LocalServer32 registrations
    _remove_key(keyNameRoot + "\\LocalServer32")

  if pythonInstString:
    _set_string(keyNameRoot + '\\PythonCOM', pythonInstString)
  else:
    _remove_key(keyNameRoot + '\\PythonCOM')
  if policy:
    _set_string(keyNameRoot + '\\PythonCOMPolicy', policy)
  else:
    _remove_key(keyNameRoot + '\\PythonCOMPolicy')

  if dispatcher:
    _set_string(keyNameRoot + '\\PythonCOMDispatcher', dispatcher)
  else:
    _remove_key(keyNameRoot + '\\PythonCOMDispatcher')

  if defIcon:
    _set_string(keyNameRoot + '\\DefaultIcon', defIcon)
  else:
    _remove_key(keyNameRoot + '\\DefaultIcon')

  if addnPath:
    _set_string(keyNameRoot + "\\PythonCOMPath", addnPath)
  else:
    _remove_key(keyNameRoot + "\\PythonCOMPath")

  if addPyComCat is None:
    addPyComCat = pythoncom.frozen == 0
  if addPyComCat:
    catids = catids + [ CATID_PythonCOMServer ]

  # Set up the implemented categories
  if catids:
    regCat = _cat_registrar()
    regCat.RegisterClassImplCategories(clsid, catids)

  # set up any other reg values they might have
  if other:
    for key, value in other.items():
      _set_string(keyNameRoot + '\\' + key, value)

  if progID:
    # set the progID as the most specific that was given to us
    if verProgID:
      _set_string(keyNameRoot + '\\ProgID', verProgID)
    else:
      _set_string(keyNameRoot + '\\ProgID', progID)

    # Set up the root entries - version independent.
    if desc:
      _set_string(progID, desc)
    _set_string(progID + '\\CLSID', str(clsid))

    # Set up the root entries - version dependent.
    if verProgID:
      # point from independent to the current version
      _set_string(progID + '\\CurVer', verProgID)

      # point to the version-independent one
      _set_string(keyNameRoot + '\\VersionIndependentProgID', progID)

      # set up the versioned progID
      if desc:
        _set_string(verProgID, desc)
      _set_string(verProgID + '\\CLSID', str(clsid))
Ejemplo n.º 29
0
# submodules can use the absolute path. This is required e.g. if the
# current directorey changes prior to loading the hooks.
PACKAGEPATH = os.path.abspath(os.path.dirname(__file__))

HOMEPATH = os.path.dirname(PACKAGEPATH)
if is_win and is_py2:
    # This ensures for Python 2 that PyInstaller will work on Windows
    # with paths containing foreign characters.
    try:
        unicode(HOMEPATH)
    except UnicodeDecodeError:
        # Do conversion to ShortPathName really only in case HOMEPATH is not
        # ascii only - conversion to unicode type cause this unicode error.
        try:
            import win32api
            HOMEPATH = win32api.GetShortPathName(HOMEPATH)
        except ImportError:
            pass

# Update __version__ as necessary.
if os.path.exists(os.path.join(HOMEPATH, 'setup.py')):
    # PyInstaller is run directly from source without installation or
    # __version__ is called from 'setup.py' ...
    if compat.getenv('PYINSTALLER_DO_RELEASE') == '1':
        # Suppress the git revision when doing a release.
        pass
    elif 'sdist' not in sys.argv:
        # and 'setup.py' was not called with 'sdist' argument.
        # For creating source tarball we do not want git revision
        # in the filename.
        try:
Ejemplo n.º 30
0
    def createPsuadeInFile(data, filename, includePDF=True):
        outf = open(filename, 'w')
        outf.write('PSUADE\n')

        #Write inputs
        outf.write('INPUT\n')
        outf.write('   dimension = %d\n' % data.getNumInputs())
        names = data.getInputNames()
        mins = data.getInputMins()
        maxs = data.getInputMaxs()
        indices = list(range(data.getNumInputs()))
        for i, name, minimum, maximum in zip(indices, names, mins, maxs):
            outf.write('   variable %d %s = %e %e\n' %
                       (i + 1, name, minimum, maximum))
        distributions = data.getInputDistributions()
        for i, dist in zip(indices, distributions):
            distType = dist.getDistributionType()
            distParams = dist.getParameterValues()
            if distType == Distribution.SAMPLE:
                outf.write('   PDF %d %c' %
                           (i + 1, Distribution.getPsuadeName(distType)))
                if distParams[0] is not None:
                    filename = distParams[0]
                    if platform.system() == 'Windows':
                        import win32api
                        filename = win32api.GetShortPathName(filename)
                    outf.write(' %s' % filename)
                if distParams[1] is not None:
                    outf.write(' %d' % distParams[1])
                outf.write('\n')
            elif includePDF:  # write out PDF info for all non-uniform PDF types
                if distType != Distribution.UNIFORM:
                    outf.write('   PDF %d %c' %
                               (i + 1, Distribution.getPsuadeName(distType)))
                    if distParams[0] is not None:
                        outf.write(' %e' % distParams[0])
                    if distParams[1] is not None:
                        outf.write(' %e' % distParams[1])
                    outf.write('\n')
        outf.write('END\n')

        #Write outputs
        outf.write('OUTPUT\n')
        if data.getNumOutputs() == 0:
            outf.write('   dimension = 1\n')
            names = ['ghostOuput']
            indices = list(range(1))
            for i, name in zip(indices, names):
                outf.write('   variable %d %s\n' % (i + 1, name))
        else:
            outf.write('   dimension = %d\n' % data.getNumOutputs())
            names = data.getOutputNames()
            indices = list(range(data.getNumOutputs()))
            for i, name in zip(indices, names):
                outf.write('   variable %d %s\n' % (i + 1, name))

        outf.write('END\n')

        #Write Method
        outf.write('METHOD\n')
        outf.write('   sampling = %s\n' %
                   SamplingMethods.getPsuadeName(data.getSampleMethod()))
        outf.write('   num_samples = %d\n' % data.getNumSamples())
        if data.getSampleMethod() != SamplingMethods.GMOAT:
            outf.write('   randomize\n')
        outf.write('END\n')

        #Write Application
        outf.write('APPLICATION\n')
        driverString = data.getDriverName()
        if driverString is None:
            driverString = 'NONE'
        elif platform.system() == 'Windows':
            if os.path.exists(driverString):
                import win32api
                driverString = win32api.GetShortPathName(driverString)
            else:
                driverString = 'NONE'
        driverString = 'NONE'

        outf.write('   driver = %s\n' % driverString)
        outf.write('   save_frequency = 1\n')
        outf.write('END\n')

        #Write Analysis
        outf.write('ANALYSIS\n')
        outf.write('   diagnostics 2\n')
        outf.write('END\n')

        outf.write('END\n')
        outf.close()