Python os.path.getsize() Examples

The following are code examples for showing how to use os.path.getsize(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: End-to-end-ASR-Pytorch   Author: Alexander-H-Liu   File: librispeech.py    MIT License 6 votes vote down vote up
def __init__(self, path, split, tokenizer, bucket_size, ascending=False):
        # Setup
        self.path = path
        self.bucket_size = bucket_size

        # List all wave files
        file_list = []
        for s in split:
            file_list += list(Path(join(path, s)).rglob("*.flac"))
        assert len(file_list) > 0, "No data found @ {}".format(path)

        # Read text
        text = Parallel(n_jobs=READ_FILE_THREADS)(
            delayed(read_text)(str(f)) for f in file_list)
        #text = Parallel(n_jobs=-1)(delayed(tokenizer.encode)(txt) for txt in text)
        text = [tokenizer.encode(txt) for txt in text]

        # Sort dataset by text length
        #file_len = Parallel(n_jobs=READ_FILE_THREADS)(delayed(getsize)(f) for f in file_list)
        self.file_list, self.text = zip(*[(f_name, txt)
                                          for f_name, txt in sorted(zip(file_list, text), reverse=not ascending, key=lambda x:len(x[1]))]) 
Example 2
Project: sql-editor   Author: struts2spring   File: TreePanel.py    MIT License 6 votes vote down vote up
def isSQLite3(self, fileName):
        ''' this is to check a valid SQLite file dropped.
        '''
        from os.path import isfile, getsize
    
        if not isfile(fileName):
            return False
        if getsize(fileName) < 100:  # SQLite database file header is 100 bytes
            return False
    
        with open(fileName, 'rb') as fd:
            header = fd.read(100)
    
        return header[:16] == b'SQLite format 3\x00'
#                 self.dirwin.addpath(filename)
#                 if Globals.pref.open_project_setting_dlg:
#                     wx.CallAfter(self.dirwin.OnSetProject)
#             else:
#                 Globals.mainframe.editctrl.new(filename)


#--------------------------------------------------------------------------- 
Example 3
Project: OnyxBackupVM   Author: OnyxfireInc   File: util.py    GNU General Public License v3.0 6 votes vote down vote up
def get_file_size(self, file):
		size = 0
		symbol = 'B'
		if exists(file):
			try:
				size = Decimal(float(getsize(file)))
				if size < 1024:
					symbol = 'B'
				elif (size / 1024) < 1024:
					size = size / 1024
					symbol = 'KB'
				elif (size / (1024 * 1024)) < 1024:
					size = size / (1024 * 1024)
					symbol = 'MB'
				else:
					size = size / (1024 * 1024 * 1024)
					symbol = 'GB'
			except OSError as e:
				self.logger.error('(!) Unable to get file size: {}'.format(e))
		else:
			self.logger.debug('(i) --> File does not exist: {}'.format(file))

		sizeString = '{}{}'.format(str(size.quantize(Decimal('0.00'))), symbol)
		return sizeString 
Example 4
Project: HtmlExtract-Python   Author: xinyi-Z   File: web_save_start.py    MIT License 6 votes vote down vote up
def judge_twice_url(self, save_path, save_type):
        '''
        根据指定的保存文件目录, 根据 title 和 mian.html文件大小来判断该网站是否存在跳页
        怀疑是否为跳转页面特征如下:
        1、title是否为 Page has moved
        2、页面htmml是否小于3KB
        '''
        page_path = pjoin(save_path, 'main.html')
        url_path = pjoin(save_path, 'url_file')
        with open(page_path, 'r') as f:
            content = f.read()
            title = get_title(content)
        if title == 'Page has moved' or getsize(page_path) <= 3072:
            with open(url_path, 'r') as f:
                url = f.read()
            redict_url = self.get_twice_page_url(content)
            # 保证是绝对路径
            if redict_url:
                if redict_url.find('http') != -1:
                    logger['logger_file_debug'].debug(
                        "redict_url: %s" % (redict_url, ))
                    self.re_download_twice_web(
                        redict_url, save_path, save_type) 
Example 5
Project: sublime-text-3-packages   Author: nickjj   File: gutter_color.py    MIT License 6 votes vote down vote up
def clear_cache(force = False):
  """
  If the folder exists, and has more than 5MB of icons in the cache, delete
  it to clear all the icons then recreate it.
  """
  from os.path import getsize, join, isfile, exists
  from os import makedirs, listdir
  from sublime import cache_path
  from shutil import rmtree

  # The icon cache path
  icon_path = join(cache_path(), "GutterColor")

  # The maximum amount of space to take up
  limit = 5242880 # 5 MB

  if exists(icon_path):
    size = sum(getsize(join(icon_path, f)) for f in listdir(icon_path) if isfile(join(icon_path, f)))
    if force or (size > limit): rmtree(icon_path)

  if not exists(icon_path): makedirs(icon_path) 
Example 6
Project: wrfxpy   Author: openwfm   File: hdf_source.py    MIT License 6 votes vote down vote up
def hdf_available_locally(self, path):
        """
        Check if an HDF file is available locally and if it's file size checks out.

        :param path: the HDF file path
        """
        info_path = path + '.size'
        if osp.exists(path) and osp.exists(info_path):
            content_size = int(open(info_path).read())
            return osp.getsize(path) = content_size
        else:
            return False

        def symlink_gribs(self, manifest, wps_dir):
            """
            Make symlinks in the form HDFFILE.XYZ to all manifest files into wps_dir.

            :param manifest: relative paths (wrt ingest_dir) to HDF files we want linked
            :param wps_dir: the WPS directory where we want the symlinks to appear
            :return:
            """
            for rel_path, hdf_name in zip(manifest, generate_hdf_names()):
                logging.info('Linking %s -> %s' % (osp.join(self.ingest_dir, rel_path), osp.join(wps_dir, hdf_name)))
                symlink_unless_exists(osp.join(self.ingest_dir, rel_path), osp.join(wps_dir, hdf_name)) 
Example 7
Project: wrfxpy   Author: openwfm   File: rtma_source.py    MIT License 6 votes vote down vote up
def _is_var_cached(path):
        """
        Check if the file for the variable is available in the ingest directory
        already and it's filesize is correct.

        :param path: the (absolute) path to the variable file
        :param var: the variable name
        :return: True if the file is available and verified (via file size), False otherwise
        """
        info_path = path + '.size'
        if osp.exists(path) and osp.exists(info_path):
            content_size = int(open(info_path).read())
            if content_size < min_content_size:
                logging.info('cached file %s size %s' % ( path, content_size))
                logging.warning('cached file size less than minimum %s, considered invalid' % min_content_size)

            return osp.getsize(path) == content_size and content_size >= min_content_size
        else:
            return False 
Example 8
Project: airflow   Author: apache   File: dynamodb_to_s3.py    Apache License 2.0 6 votes vote down vote up
def _scan_dynamodb_and_upload_to_s3(self, temp_file, scan_kwargs, table):
        while True:
            response = table.scan(**scan_kwargs)
            items = response['Items']
            for item in items:
                temp_file.write(self.process_func(item))

            if 'LastEvaluatedKey' not in response:
                # no more items to scan
                break

            last_evaluated_key = response['LastEvaluatedKey']
            scan_kwargs['ExclusiveStartKey'] = last_evaluated_key

            # Upload the file to S3 if reach file size limit
            if getsize(temp_file.name) >= self.file_size:
                _upload_file_to_s3(temp_file, self.s3_bucket_name,
                                   self.s3_key_prefix)
                temp_file.close()
                temp_file = NamedTemporaryFile()
        return temp_file 
Example 9
Project: ssbio   Author: SBRG   File: test_protein_seqprop.py    MIT License 6 votes vote down vote up
def test_write_gff_file(self, seqprop_with_i, tmpdir):
        """Test writing the features, and that features are now loaded from a file"""
        outpath = tmpdir.join('test_seqprop_with_i_write_gff_file.gff').strpath
        seqprop_with_i.write_gff_file(outfile=outpath, force_rerun=True)

        # Test that the file was written
        assert op.exists(outpath)
        assert op.getsize(outpath) > 0

        # Test that file paths are correct
        assert seqprop_with_i.feature_path == outpath
        assert seqprop_with_i.feature_file == 'test_seqprop_with_i_write_gff_file.gff'
        assert seqprop_with_i.feature_dir == tmpdir

        # Test that features cannot be changed
        with pytest.raises(ValueError):
            seqprop_with_i.features = ['NOFEATURES'] 
Example 10
Project: hacker-scripts   Author: restran   File: dir_compare.py    MIT License 6 votes vote down vote up
def traverse_dir(path):
    file_dict = {}
    dir_dict = {}
    count = 1
    for root, dirs, files in walk(path):
        for d in dirs:
            abs_p = join(root, d)
            dir_dict[abs_p] = 0
            print(abs_p)
            count += 1
            if count % 200 == 0:
                print('%s files scanned' % count)

        for f in files:
            abs_p = join(root, f)
            file_dict[abs_p] = getsize(abs_p)
            print(abs_p)
            count += 1
            if count % 200 == 0:
                print('%s files scanned' % count)

    return file_dict, dir_dict 
Example 11
Project: avtk   Author: senko   File: test_probe.py    MIT License 6 votes vote down vote up
def test_probe_image():
    path = asset_path('image', 'evil-frank.png')

    mi = MediaInfo(path)

    assert mi.format.size == getsize(path)
    assert 'png' in mi.format.name
    assert mi.format.duration is None

    assert len(mi.video_streams) == 1
    assert not mi.has_audio
    assert not mi.has_subtitles

    v = mi.video_streams[0]
    assert v.codec.name == 'png'
    assert v.frame_rate is None
    assert v.width == 320
    assert v.height == 180 
Example 12
Project: invenio-files-rest   Author: inveniosoftware   File: test_storage.py    MIT License 6 votes vote down vote up
def test_pyfs_checksum(get_md5):
    """Test fixity."""
    # Compute checksum of license file/
    with open('LICENSE', 'rb') as fp:
        data = fp.read()
        checksum = get_md5(data)

    counter = dict(size=0)

    def callback(total, size):
        counter['size'] = size

    # Now do it with storage interface
    s = PyFSFileStorage('LICENSE', size=getsize('LICENSE'))
    assert checksum == s.checksum(chunk_size=2, progress_callback=callback)
    assert counter['size'] == getsize('LICENSE')

    # No size provided, means progress callback isn't called
    counter['size'] = 0
    s = PyFSFileStorage('LICENSE')
    assert checksum == s.checksum(chunk_size=2, progress_callback=callback)
    assert counter['size'] == 0 
Example 13
Project: pydistcp   Author: yassineazzouz   File: utils.py    MIT License 6 votes vote down vote up
def from_local(cls, local_path, include_pattern="*", min_size=0):
    """Instantiate from a local path.
    :param local_path: Local path.
    """

    def _get_file_size(file_path):
        if fnmatch.fnmatch( osp.basename(file_path), include_pattern):
          try:
            return osp.getsize(file_path)
          except OSError, err:
            if 'No such file or directory' in str(err):
              # The files may have diappeared meanwhile
              # 0 is a valid file size
              return -1
            else:
              raise err
        else: 
Example 14
Project: cos-python-sdk-v4   Author: tencentyun   File: cos_common.py    Apache License 2.0 6 votes vote down vote up
def get_sha1_by_slice(file_name, slice_size):
        """ Get SHA array based on Qcloud Slice Upload Interface

        :param file_name: local file path
        :param slice_size: slice size in bit
        :return: sha array like [{“offset”:0, “datalen”:1024,”datasha”:”aaa”}, {}, {}]
        """
        from os import path

        with open(file_name, 'rb') as f:

            result = []
            file_size = path.getsize(file_name)
            sha1_obj = Sha1Hash()
            for current_offset in range(0, file_size, slice_size):

                data_length = min(slice_size, file_size - current_offset)
                sha1_obj.update(f.read(data_length))
                sha1_val = sha1_obj.inner_digest()
                result.append({"offset": current_offset, "datalen": data_length, "datasha": sha1_val})

            result[-1]['datasha'] = sha1_obj.hexdigest()
            return result 
Example 15
Project: password_pwncheck   Author: CboeSecurity   File: pwned-password-server.py    MIT License 5 votes vote down vote up
def __init__(self,filepath,debug=False):
        self.filepath = filepath
        self.fp = open(self.filepath,'r')

        first = self.fp.readline()
        self.linesize = len(first)
        self.nows_linesize = len(first.strip())
        self.filesize = getsize(self.filepath)
        self.filerows = self.filesize/self.linesize
        self.debug = debug 
Example 16
Project: password_pwncheck   Author: CboeSecurity   File: pwnpass.py    MIT License 5 votes vote down vote up
def __init__(self,filepath,debug=False):
        self.filepath = filepath
        self.fp = open(self.filepath,'r')

        first = self.fp.readline()
        self.linesize = len(first)
        self.nows_linesize = len(first.strip())
        self.filesize = getsize(self.filepath)
        self.filerows = self.filesize/self.linesize
        self.debug = debug 
Example 17
Project: signals   Author: Amsterdam   File: test_datawarehouse.py    Mozilla Public License 2.0 5 votes vote down vote up
def test_save_csv_files_datawarehouse(self, mocked_get_storage_backend):
        # Mocking the storage backend to local file system with tmp directory.
        # In this test case we don't want to make usage of the remote Object
        # Store.
        mocked_get_storage_backend.return_value = FileSystemStorage(
            location=self.file_backend_tmp_dir)

        # Creating a few objects in the database.
        for i in range(3):
            SignalFactory.create()

        datawarehouse.save_csv_files_datawarehouse()

        # Checking if we have files on the correct locations and that they
        # do have some content.
        signals_csv = path.join(self.file_backend_tmp_dir, 'signals.csv')
        locations_csv = path.join(self.file_backend_tmp_dir, 'locations.csv')
        reporters_csv = path.join(self.file_backend_tmp_dir, 'reporters.csv')
        categories_csv = path.join(self.file_backend_tmp_dir, 'categories.csv')
        statuses_csv = path.join(self.file_backend_tmp_dir, 'statuses.csv')
        self.assertTrue(path.exists(signals_csv))
        self.assertTrue(path.getsize(signals_csv))
        self.assertTrue(path.exists(locations_csv))
        self.assertTrue(path.getsize(locations_csv))
        self.assertTrue(path.exists(reporters_csv))
        self.assertTrue(path.getsize(reporters_csv))
        self.assertTrue(path.exists(categories_csv))
        self.assertTrue(path.getsize(categories_csv))
        self.assertTrue(path.exists(statuses_csv))
        self.assertTrue(path.getsize(statuses_csv)) 
Example 18
Project: sql-editor   Author: struts2spring   File: DataSourceExplorer.py    MIT License 5 votes vote down vote up
def isSQLite3(self, fileName):
        ''' this is to check a valid SQLite file dropped.
        '''
        from os.path import isfile, getsize

        if not isfile(fileName):
            return False
        if getsize(fileName) < 100:  # SQLite database file header is 100 bytes
            return False

        with open(fileName, 'rb') as fd:
            header = fd.read(100)

        return header[:16] == b'SQLite format 3\x00' 
Example 19
Project: jr-tools   Author: kingjr   File: base.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def download(self, f_server, f_client=None, overwrite=None):
        if self._is_offline():
            return
        # get default overwrite parameter
        if overwrite is None:
            overwrite = self.overwrite
        # default filenames
        f_server = self._strip_client_root(f_server)
        if f_client is None:
            f_client = op.join(self.client_root, f_server)
        # connect
        self.route.connect()
        # check that file exists online
        meta_data = self.route.metadata(f_server)
        # don't download if files are identical
        client_path = '/'.join(f_client.split('/')[:-1])
        if op.exists(f_client):
            if overwrite is False:
                print('%s already exists & no overwrite: skip' % f_client)
                return False
            elif overwrite == 'auto':
                fsize_client = op.getsize(f_client)
                if meta_data['bytes'] == fsize_client:
                    print('Identical files, download skipped %s' % f_client)
                    return False
        # create directory if doesn't exist already
        if ((not op.exists(client_path)) and (client_path is not None) and
                (client_path != '')):
            os.makedirs(client_path)
        # download
        self.route.download(f_server, f_client)
        print('Downloaded: %s > %s' % (f_server, f_client))
        return True 
Example 20
Project: jr-tools   Author: kingjr   File: base.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def _upload_file(self, f_client, f_server, overwrite, remove_on_upload):
        # connect
        self.route.connect()
        # check whether file exists online
        if overwrite is not True:
            metadata = self.route.metadata(f_server)
            # don't upload if files are identical
            if metadata['exist']:
                if overwrite is False:
                    print('File already exists & overwrite is False: skipped')
                    return False
                elif overwrite == 'auto':
                    fsize_client = op.getsize(f_client)
                    if metadata['bytes'] == fsize_client:
                        print('Identical files, upload skipped %s' % f_server)
                        return False
                else:
                    raise ValueError('overwrite must be bool or `auto`')
        # upload
        print('Uploading %s > %s' % (f_client,
                                     op.join(self.route.bucket, f_server)))
        self.route.upload(f_client, f_server)

        # remove_on_upload
        if remove_on_upload:
            os.remove(f_client)
        return True 
Example 21
Project: dephell   Author: dephell   File: _shutil.py    MIT License 5 votes vote down vote up
def get_path_size(path: Path) -> int:
    if not path.exists():
        return 0
    if path.is_file():
        return getsize(str(path))
    total = 0
    for subpath in path.glob('**/*'):
        if subpath.is_file():
            total += getsize(str(subpath))
    return total 
Example 22
Project: cassandra-dtest   Author: apache   File: sstablesplit_test.py    Apache License 2.0 5 votes vote down vote up
def _do_split(self, node, version):
        logger.debug("Run sstablesplit")
        time.sleep(5.0)
        node.stop()

        # default split size is 50MB
        splitmaxsize = 10
        expected_sstable_size = (10 * 1024 * 1024)
        keyspace = 'keyspace1'

        # get the initial sstables and their total size
        origsstables = node.get_sstables(keyspace, '')
        origsstable_size = sum([getsize(sstable) for sstable in origsstables])
        logger.debug("Original sstable and sizes before split: {}".format([(name, getsize(name)) for name in origsstables]))

        # calculate the expected number of sstables post-split
        expected_num_sstables = floor(origsstable_size / expected_sstable_size)

        # split the sstables
        result = node.run_sstablesplit(keyspace=keyspace, size=splitmaxsize,
                                       no_snapshot=True, debug=True)

        for (out, error, rc) in result:
            logger.debug("stdout: {}".format(out))
            logger.debug("stderr: {}".format(error))
            logger.debug("rc: {}".format(rc))

        # get the sstables post-split and their total size
        sstables = node.get_sstables(keyspace, '')
        logger.debug("Number of sstables after split: %s. expected %s" % (len(sstables), expected_num_sstables))
        assert expected_num_sstables <= len(sstables) + 1
        assert 1 <= len(sstables)

        # make sure none of the tables are bigger than the max expected size
        sstable_sizes = [getsize(sstable) for sstable in sstables]
        # add a bit extra for overhead
        assert max(sstable_sizes) <= expected_sstable_size + 512
        # make sure node can start with changed sstables
        node.start(wait_for_binary_proto=True) 
Example 23
Project: apio   Author: FPGAwars   File: test_init.py    GNU General Public License v2.0 5 votes vote down vote up
def validate_apio_ini(current_dir):
    path = join(current_dir, 'apio.ini')
    assert isfile(path) and getsize(path) > 0 
Example 24
Project: apio   Author: FPGAwars   File: test_init.py    GNU General Public License v2.0 5 votes vote down vote up
def validate_scons(apioproject_dir):
    path = join(apioproject_dir, 'SConstruct')
    assert isfile(path) and getsize(path) > 0 
Example 25
Project: apio   Author: FPGAwars   File: test_complete.py    GNU General Public License v2.0 5 votes vote down vote up
def validate_files_leds(apioproject_dir):
    path = join(apioproject_dir, 'leds.v')
    assert isfile(path) and getsize(path) > 0 
Example 26
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def repo_size(r):
    return getsize(r.git_dir) 
Example 27
Project: dockerizeme   Author: dockerizeme   File: snippet.py    Apache License 2.0 5 votes vote down vote up
def tempscreencap(format='png'):
    """Screencapture to a temporary filename with a given file extension
    Returns: String, temporary filename, if screenshot taken
    """
    ## create a temp filename
    tfile = NamedTemporaryFile(suffix='.' + format, delete=False)
    tpath = tfile.name
    # yield to user control and take the screenshot
    call(["screencapture", "-i", "-o", "-s", tpath])
    if getsize(tpath) == 0:
        raise RuntimeError("No screenshot was taken.")
    else:
        return tpath 
Example 28
Project: crawl-dataset   Author: e-lab   File: getImgs.py    ISC License 5 votes vote down vote up
def checkValid(savePath):
	si = getsize(savePath)
	print(si)
	if si == 2051:
		print('Not valid delete')
		call(['rm', '-rf', str(savePath)])

#Download img 
Example 29
Project: D-VAE   Author: muhanzhang   File: test_d3viz.py    MIT License 5 votes vote down vote up
def check(self, f, reference=None, verbose=False):
        tmp_dir = tempfile.mkdtemp()
        html_file = pt.join(tmp_dir, 'index.html')
        if verbose:
            print(html_file)
        d3v.d3viz(f, html_file)
        assert pt.getsize(html_file) > 0
        if reference:
            assert filecmp.cmp(html_file, reference) 
Example 30
Project: Library-Identification   Author: Riscure   File: library_identification.py    GNU General Public License v3.0 5 votes vote down vote up
def check_file(self):
        try:
            # Parse the ELF header
            elffile = ELFFile(open(self.filename, 'rb'))

            # Grab architecture (e.g. 'MIPS' from 'EM_MIPS')
            self.arch = elffile.header.e_machine.split('_')[1]

            # Grab ELF section info
            for section in elffile.iter_sections():
                self.elf_sections[section.name] = (section.header['sh_offset'],
                                                   section.header['sh_size'])

        except IOError:
            print("ERROR: Could not load the file '" + self.filename + "'.")
            exit(1)
        except ELFError:
            print("ERROR: '" + self.filename + "' is not a valid ELF object")
            exit(1)

        # Parse name and version from filename
        try:
            [self.name, self.version] = splitext(self.basename)[0].split('__')
        except:
            self.name = self.basename
            self.version = "unknown"

        # Get file size
        self.size = getsize(self.filename)


    # Grabs signature data from radare2 
Example 31
Project: analyzePF   Author: analyzeDFIR   File: prefetch.py    MIT License 5 votes vote down vote up
def get_metadata(self, simple_hash=True):
        '''
        Args:
            simple_hash: Boolean    => whether to only collect SHA256 hash or 
                                       MD5 and SHA1 as well
        Returns:
            Container<String, Any>
            Container of metadata about this prefetch file:
                file_name: prefetch file name
                file_path: full path on local system
                file_size: size of file on local system
                md5hash: MD5 hash of prefetch file
                sha1hash: SHA1 hash of prefetch file
                sha2hash: SHA256 hash of prefetch file
                modify_time: last modification time of prefetch file on local system
                access_time: last access time of prefetch file on local system
                create_time: create time of prefetch file on local system
        Preconditions:
            simple_hash is of type Boolean
        '''
        assert isinstance(simple_hash, bool), 'Simple_hash is of type Boolean'
        return Container(\
            file_name=path.basename(self._filepath),
            file_path=path.abspath(self._filepath),
            file_size=path.getsize(self._filepath),
            md5hash=self._hash_file('md5') if not simple_hash else None,
            sha1hash=self._hash_file('sha1') if not simple_hash else None,
            sha2hash=self._hash_file('sha256'),
            modify_time=datetime.fromtimestamp(path.getmtime(self._filepath), tzlocal()).astimezone(tzutc()),
            access_time=datetime.fromtimestamp(path.getatime(self._filepath), tzlocal()).astimezone(tzutc()),
            create_time=datetime.fromtimestamp(path.getctime(self._filepath), tzlocal()).astimezone(tzutc())\
        ) 
Example 32
Project: tortilla8   Author: aanunez   File: guacamole.py    GNU General Public License v3.0 5 votes vote down vote up
def load_rom(self, file_path):
        '''
        Loads a Chip-8 ROM from a file into the RAM.
        '''
        file_size = getsize(file_path)
        if file_size > MAX_ROM_SIZE:
            self.log("Rom file exceeds maximum rom size of " + str(MAX_ROM_SIZE) + \
                " bytes" , EmulationError._Fatal)
            return

        with open(file_path, "rb") as fh:
            self.ram[PROGRAM_BEGIN_ADDRESS:PROGRAM_BEGIN_ADDRESS + file_size] = \
                [int.from_bytes(fh.read(1), 'big') for i in range(file_size)]
            self.log("Rom file loaded" , EmulationError._Information) 
Example 33
Project: tortilla8   Author: aanunez   File: emulator.py    GNU General Public License v3.0 5 votes vote down vote up
def load_rom(self, file_path):
        '''
        Loads a Chip-8 ROM from a file into the RAM.
        '''
        file_size = getsize(file_path)
        if file_size > Emulator.MAX_ROM_SIZE:
            raise RomLoadError

        with open(file_path, "rb") as fh:
            self.ram[Emulator.PROGRAM_BEGIN_ADDRESS:Emulator.PROGRAM_BEGIN_ADDRESS + file_size] = \
                [int.from_bytes(fh.read(1), 'big') for i in range(file_size)] 
Example 34
Project: exopy   Author: Exopy   File: api_builder.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def shall_skip(module, opts):
    """Check if we want to skip this module."""
    # skip it if there is nothing (or just \n or \r\n) in the file
    if path.getsize(module) <= 2:
        return True
    # skip if it has a "private" name and this is selected
    filename = path.basename(module)
    if filename != '__init__.py' and filename.startswith('_') and \
       not opts.includeprivate:
        return True
    return False 
Example 35
Project: genielibs   Author: CiscoTestAutomation   File: ha.py    Apache License 2.0 5 votes vote down vote up
def check_disk_space(cls, device, disk, image):
        ''' Check if the ISSU state is in the expected state

            Args:
                device (`obj`): Device Object.
                disk (`str`): Memory location to check for space upto threshold:
                              - harddisk
                              - stby-harddisk:
                image (`str`): ISSU upgrade image full path

            Returns:
                None

            Raises:
                Exception: - Cannot parse 'dir <>' output
                           - Not enough space on disk

            Example:
                >>> check_disk_space(device=uut, disk='harddisk', image=image)
        '''

        # Acceptable states for ISSU to be in
        assert disk in ['harddisk:', 'stby-harddisk:']

        try:
            output = device.execute('dir {}'.format(disk))
        except Exception as e:
            raise Exception("Unable to execute 'dir {}'".format(disk))

        # 78704144384 bytes total (59693568000 bytes free)
        m = re.search('(?P<total>(\d+)) +bytes +total +\((?P<free>(\d+)) '
                      '+bytes +free\)', output)
        bytes_total = m.groupdict()['total']
        bytes_free = m.groupdict()['free']

        if getsize(image) > int(bytes_free):
            raise Exception("Not enough space on '{}' to copy ISSU image".\
                            format(disk))
        else:
            logger.info("Enough space on '{}' to copy ISSU image".format(disk)) 
Example 36
Project: genielibs   Author: CiscoTestAutomation   File: ha.py    Apache License 2.0 5 votes vote down vote up
def check_disk_space(cls, device, disk, image):
        ''' Check if the ISSU state is in the expected state

            Args:
                device (`obj`): Device Object.
                disk (`str`): Memory location to check for space upto threshold:
                              - harddisk
                              - stby-harddisk:
                image (`str`): ISSU upgrade image full path

            Returns:
                None

            Raises:
                Exception: - Cannot parse 'dir <>' output
                           - Not enough space on disk

            Example:
                >>> check_disk_space(device=uut, disk='harddisk', image=image)
        '''

        # Acceptable states for ISSU to be in
        assert disk in ['harddisk:', 'stby-harddisk:']

        try:
            output = device.execute('dir {}'.format(disk))
        except Exception as e:
            raise Exception("Unable to execute 'dir {}'".format(disk))

        # 78704144384 bytes total (59693568000 bytes free)
        m = re.search('(?P<total>(\d+)) +bytes +total +\((?P<free>(\d+)) '
                      '+bytes +free\)', output)
        bytes_total = m.groupdict()['total']
        bytes_free = m.groupdict()['free']

        if getsize(image) > int(bytes_free):
            raise Exception("Not enough space on '{}' to copy ISSU image".\
                            format(disk))
        else:
            logger.info("Enough space on '{}' to copy ISSU image".format(disk)) 
Example 37
Project: QCSuper   Author: P1sec   File: _utils.py    GNU General Public License v3.0 5 votes vote down vote up
def __call__(self, path):
        
        path = expanduser(path)
        
        if path == '/dev/stdout' and 'a' in self.mode:
            
            self.mode = self.mode.replace('a', 'w')
        
        if path == '-':
            
            if 'r' in self.mode:
                file_obj = stdin.buffer if 'b' in self.mode else stdin
            else:
                file_obj = fdopen(dup(stdout.fileno()), 'wb' if 'b' in self.mode else 'w')
                dup2(stderr.fileno(), stdout.fileno())
            return file_obj
        
        elif path[-3:] != '.gz':
            
            file_obj = open(path, self.mode)
        
        else:
            
            file_obj = gzip.open(path, {'r': 'rt', 'a': 'at'}.get(self.mode, self.mode))
        
        file_obj.appending_to_file = bool(exists(path) and getsize(path))
        
        return file_obj 
Example 38
Project: DCRM   Author: 82Flex   File: statistics.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def getdirsize(dir):
   size = 0
   for root, dirs, files in os.walk(dir):
      size += sum([getsize(join(root, name)) for name in files])
   return size 
Example 39
Project: wrfxpy   Author: openwfm   File: level0_source.py    MIT License 5 votes vote down vote up
def available_locally(self, path):
        """
        Check in a level0 file is available locally and if it's file size checks out.
        :param path: the level0 file path
        """
        info_path = path + '.size'
        if osp.exists(path) and osp.exists(info_path):
            content_size = int(open(info_path).read())
            return osp.getsize(path) == content_size
        else:
            return False 
Example 40
Project: wrfxpy   Author: openwfm   File: grib_source.py    MIT License 5 votes vote down vote up
def grib_available_locally(self, path):
        """
        Check if a GRIB2 file is available locally and if it's file size checks out.

        :param path: the GRIB2 file path
        """
        info_path = path + '.size' 
        if osp.exists(path) and osp.exists(info_path):
            content_size = int(open(info_path).read())
            return osp.getsize(path) == content_size
        else:
            return False 
Example 41
Project: wrfxpy   Author: openwfm   File: var_wisdom.py    MIT License 5 votes vote down vote up
def available_locally(path):
        """
        Check in a level0 file is available locally and if it's file size checks out.
        :param path: the level0 file path
        """
        info_path = path + '.size'
        if osp.exists(path) and osp.exists(info_path):
            content_size = int(open(info_path).read())
            return osp.getsize(path) == content_size
        else:
            return False 
Example 42
Project: enrichr   Author: pwwang   File: test.py    MIT License 5 votes vote down vote up
def testPlot(self, genes, description, plotfile):
		enrichr = Enrichr(cutoff = 1)
		enrichr.addList(genes, description)
		enrichr.enrich()
		enrichr.plot(plotfile)
		self.assertGreater(path.getsize(plotfile), 0) 
Example 43
Project: zttf   Author: zathras777   File: ttfile.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, filename):
        self.filename = filename
        self.faces = []

        if not exists(filename) or getsize(filename) == 0:
            raise IOError("The file '{}' does not exist or is empty".format(filename))

        with open(self.filename, 'rb') as fh:
            hdr = TTFCollectionHeader(fh)
            for off in hdr.offsets:
                self.faces.append(TTFont(filename, off)) 
Example 44
Project: ECCV_2018_pedestrian_detection_challenege   Author: miltonbd   File: score_pedestrian_detection.py    MIT License 5 votes vote down vote up
def check_size(submission_file):
    max_size = 60*1024*1024
    if osp.getsize(submission_file) > max_size:
        raise IOError #File size exceeds the specified maximum size, which is 60M for the server. 
Example 45
Project: ECCV_2018_pedestrian_detection_challenege   Author: miltonbd   File: multi_thread_score_pedestrian_detection.py    MIT License 5 votes vote down vote up
def check_size(submission_file):
    max_size = 60*1024*1024
    if osp.getsize(submission_file) > max_size:
        raise IOError #File size exceeds the specified maximum size, which is 60M for the server. 
Example 46
Project: ssbio   Author: SBRG   File: test_protein_seqprop.py    MIT License 5 votes vote down vote up
def test_write_fasta_file(self, seqprop_with_i, tmpdir, test_files_outputs, seq_record_example):
        """Test that everything behaves properly when writing the SeqProp to a FASTA file"""
        # Add dummy annotations to the SeqProp - check to see if they stay in the SeqProp even after Seq is written
        seqprop_with_i.letter_annotations.update({'test_la_key': 'X' * len(seqprop_with_i.seq)})
        seqprop_with_i.features.append(SeqFeature(FeatureLocation(1, 3)))

        # Write the Seq to a FASTA file
        outpath = tmpdir.join('test_seqprop_with_i_write_fasta_file.fasta').strpath
        seqprop_with_i.write_fasta_file(outfile=outpath, force_rerun=True)

        # Test that the file was written
        assert op.exists(outpath)
        assert op.getsize(outpath) > 0

        # Test that file paths are correct
        assert seqprop_with_i.sequence_path == outpath
        assert seqprop_with_i.sequence_file == 'test_seqprop_with_i_write_fasta_file.fasta'
        assert seqprop_with_i.sequence_dir == tmpdir

        # Once a file is written, the annotations should not be lost, even though the sequence now
            # loads from the written file as a Seq
        assert seqprop_with_i.description == seq_record_example.description
        assert seqprop_with_i.annotations == seq_record_example.annotations
        assert seqprop_with_i.letter_annotations == {'test_la_key': 'X' * len(seq_record_example.seq)}
        assert len(seqprop_with_i.features) == 1

        # Test that sequence cannot be changed
        with pytest.raises(ValueError):
            seqprop_with_i.seq = 'THISWILLNOTBETHESEQ'
        assert seqprop_with_i.seq == seq_record_example.seq 
Example 47
Project: sentinelsat   Author: sentinelsat   File: sentinel.py    GNU General Public License v3.0 5 votes vote down vote up
def _md5_compare(self, file_path, checksum, block_size=2 ** 13):
        """Compare a given MD5 checksum with one calculated from a file."""
        with closing(
            self._tqdm(desc="MD5 checksumming", total=getsize(file_path), unit="B", unit_scale=True)
        ) as progress:
            md5 = hashlib.md5()
            with open(file_path, "rb") as f:
                while True:
                    block_data = f.read(block_size)
                    if not block_data:
                        break
                    md5.update(block_data)
                    progress.update(len(block_data))
            return md5.hexdigest().lower() == checksum.lower() 
Example 48
Project: sentinelsat   Author: sentinelsat   File: sentinel.py    GNU General Public License v3.0 5 votes vote down vote up
def _download(self, url, path, session, file_size):
        headers = {}
        continuing = exists(path)
        if continuing:
            already_downloaded_bytes = getsize(path)
            headers = {"Range": "bytes={}-".format(already_downloaded_bytes)}
        else:
            already_downloaded_bytes = 0
        downloaded_bytes = 0
        with closing(
            session.get(url, stream=True, auth=session.auth, headers=headers, timeout=self.timeout)
        ) as r, closing(
            self._tqdm(
                desc="Downloading",
                total=file_size,
                unit="B",
                unit_scale=True,
                initial=already_downloaded_bytes,
            )
        ) as progress:
            _check_scihub_response(r, test_json=False)
            chunk_size = 2 ** 20  # download in 1 MB chunks
            mode = "ab" if continuing else "wb"
            with open(path, mode) as f:
                for chunk in r.iter_content(chunk_size=chunk_size):
                    if chunk:  # filter out keep-alive new chunks
                        f.write(chunk)
                        progress.update(len(chunk))
                        downloaded_bytes += len(chunk)
            # Return the number of bytes downloaded
            return downloaded_bytes 
Example 49
Project: bbotte.github.io   Author: bbotte   File: watch.py    Apache License 2.0 5 votes vote down vote up
def __init__(self, filename, counter):
        self.filename = path.abspath(filename)
        self.queue = Queue()
        self.check_chain = CheckerChain(self.queue, counter)
        self.observer = Observer()
        self.fd = None
        self.offset = 0
        if path.isfile(self.filename):
            self.fd = open(self.filename)
            self.offset = path.getsize(self.filename) 
Example 50
Project: bbotte.github.io   Author: bbotte   File: watch.py    Apache License 2.0 5 votes vote down vote up
def on_moved(self, event):
        if path.abspath(event.src_path) == self.filename:
            self.fd.close()
        if path.abspath(event.dest_path) == self.filename and path.isfile(self.filename):
            self.fd = open(self.filename)
            self.offset = path.getsize(self.filename)