Python os.html() Examples

The following are code examples for showing how to use os.html(). They are extracted from open source Python projects. You can vote up the examples you like or vote down the ones you don't like. You can also save this page to your account.

Example 1
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 6 votes vote down vote up
def run(command):
    # TODO: replace this with fork()
    #       (https://docs.python.org/2/library/os.html#os.fork)
    pid = 0
    if pid == 0:
        # This is the child, we'll try to do some containment here
        try:
            contain(command)
        except Exception:
            traceback.print_exc()
            os._exit(1)  # something went wrong in contain()

    # This is the parent, pid contains the PID of the forked process
    # wait for the forked child and fetch the exit status
    _, status = os.waitpid(pid, 0)
    print('{} exited with status {}'.format(pid, status)) 
Example 2
Project: respeaker_virtualenv   Author: respeaker   File: common.py    (license) View Source Project 6 votes vote down vote up
def _find_files(root_dir, should_include):
    """
    Return a list of paths to all modules below the given directory.

    Arguments:

      should_include: a function that accepts a file path and returns True or False.

    """
    paths = []  # Return value.

    is_module = lambda path: path.endswith(".py")

    # os.walk() is new in Python 2.3
    #   http://docs.python.org/library/os.html#os.walk
    for dir_path, dir_names, file_names in os.walk(root_dir):
        new_paths = [os.path.join(dir_path, file_name) for file_name in file_names]
        new_paths = filter(is_module, new_paths)
        new_paths = filter(should_include, new_paths)
        paths.extend(new_paths)

    return paths 
Example 3
Project: jinjabread   Author: Inveracity   File: utils.py    (license) View Source Project 6 votes vote down vote up
def gen_mac(prefix='AC:DE:48'):
    '''
    Generates a MAC address with the defined OUI prefix.

    Common prefixes:

     - ``00:16:3E`` -- Xen
     - ``00:18:51`` -- OpenVZ
     - ``00:50:56`` -- VMware (manually generated)
     - ``52:54:00`` -- QEMU/KVM
     - ``AC:DE:48`` -- PRIVATE

    References:

     - http://standards.ieee.org/develop/regauth/oui/oui.txt
     - https://www.wireshark.org/tools/oui-lookup.html
     - https://en.wikipedia.org/wiki/MAC_address
    '''
    return '{0}:{1:02X}:{2:02X}:{3:02X}'.format(prefix,
                                                random.randint(0, 0xff),
                                                random.randint(0, 0xff),
                                                random.randint(0, 0xff)) 
Example 4
Project: jinjabread   Author: Inveracity   File: utils.py    (license) View Source Project 6 votes vote down vote up
def date_format(date=None, format="%Y-%m-%d"):
    '''
    Converts date into a time-based string

    date
      any datetime, time string representation...

    format
       :ref:`strftime<http://docs.python.org/2/library/datetime.html#datetime.datetime.strftime>` format

    >>> import datetime
    >>> src = datetime.datetime(2002, 12, 25, 12, 00, 00, 00)
    >>> date_format(src)
    '2002-12-25'
    >>> src = '2002/12/25'
    >>> date_format(src)
    '2002-12-25'
    >>> src = 1040814000
    >>> date_format(src)
    '2002-12-25'
    >>> src = '1040814000'
    >>> date_format(src)
    '2002-12-25'
    '''
    return date_cast(date).strftime(format) 
Example 5
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def unversioned_sys_platform():
	"""
	Return the unversioned platform name.
	Some Python platform names contain versions, that depend on
	the build environment, e.g. linux2, freebsd6, etc.
	This returns the name without the version number. Exceptions are
	os2 and win32, which are returned verbatim.

	:rtype: string
	:return: Unversioned platform name
	"""
	s = sys.platform
	if s.startswith('java'):
		# The real OS is hidden under the JVM.
		from java.lang import System
		s = System.getProperty('os.name')
		# see http://lopica.sourceforge.net/os.html for a list of possible values
		if s == 'Mac OS X':
			return 'darwin'
		elif s.startswith('Windows '):
			return 'win32'
		elif s == 'OS/2':
			return 'os2'
		elif s == 'HP-UX':
			return 'hp-ux'
		elif s in ('SunOS', 'Solaris'):
			return 'sunos'
		else: s = s.lower()

	# powerpc == darwin for our purposes
	if s == 'powerpc':
		return 'darwin'
	if s == 'win32' or s == 'os2':
		return s
	if s == 'cli' and os.name == 'nt':
		# ironpython is only on windows as far as we know
		return 'win32'
	return re.split('\d+$', s)[0] 
Example 6
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def unversioned_sys_platform():
	"""
	Return the unversioned platform name.
	Some Python platform names contain versions, that depend on
	the build environment, e.g. linux2, freebsd6, etc.
	This returns the name without the version number. Exceptions are
	os2 and win32, which are returned verbatim.

	:rtype: string
	:return: Unversioned platform name
	"""
	s = sys.platform
	if s.startswith('java'):
		# The real OS is hidden under the JVM.
		from java.lang import System
		s = System.getProperty('os.name')
		# see http://lopica.sourceforge.net/os.html for a list of possible values
		if s == 'Mac OS X':
			return 'darwin'
		elif s.startswith('Windows '):
			return 'win32'
		elif s == 'OS/2':
			return 'os2'
		elif s == 'HP-UX':
			return 'hp-ux'
		elif s in ('SunOS', 'Solaris'):
			return 'sunos'
		else: s = s.lower()

	# powerpc == darwin for our purposes
	if s == 'powerpc':
		return 'darwin'
	if s == 'win32' or s == 'os2':
		return s
	if s == 'cli' and os.name == 'nt':
		# ironpython is only on windows as far as we know
		return 'win32'
	return re.split('\d+$', s)[0] 
Example 7
Project: SoCFoundationFlow   Author: mattaw   File: Utils.py    (Apache License 2.0) View Source Project 5 votes vote down vote up
def unversioned_sys_platform():
	"""
	Return the unversioned platform name.
	Some Python platform names contain versions, that depend on
	the build environment, e.g. linux2, freebsd6, etc.
	This returns the name without the version number. Exceptions are
	os2 and win32, which are returned verbatim.

	:rtype: string
	:return: Unversioned platform name
	"""
	s = sys.platform
	if s.startswith('java'):
		# The real OS is hidden under the JVM.
		from java.lang import System
		s = System.getProperty('os.name')
		# see http://lopica.sourceforge.net/os.html for a list of possible values
		if s == 'Mac OS X':
			return 'darwin'
		elif s.startswith('Windows '):
			return 'win32'
		elif s == 'OS/2':
			return 'os2'
		elif s == 'HP-UX':
			return 'hp-ux'
		elif s in ('SunOS', 'Solaris'):
			return 'sunos'
		else: s = s.lower()

	# powerpc == darwin for our purposes
	if s == 'powerpc':
		return 'darwin'
	if s == 'win32' or s == 'os2':
		return s
	return re.split('\d+$', s)[0] 
Example 8
Project: mongodb_consistent_backup   Author: Percona-Lab   File: Oplog.py    (license) View Source Project 5 votes vote down vote up
def fsync(self):
        if self._oplog:
            # https://docs.python.org/2/library/os.html#os.fsync
            self._oplog.flush()
            self._last_flush_time  = time()
            self._writes_unflushed = 0
            return os.fsync(self._oplog.fileno()) 
Example 9
Project: rubber-docker   Author: Fewbytes   File: rd.py    (license) View Source Project 5 votes vote down vote up
def contain(command):
    # TODO: exec command, note the difference between the exec flavours
    #       https://docs.python.org/2/library/os.html#os.execv
    # NOTE: command is an array (the first element is path/file, and the entire
    #       array is exec's args)

    os._exit(0)  # TODO: remove this after adding exec 
Example 10
Project: cheatsheet   Author: martialblog   File: test_main.py    (license) View Source Project 5 votes vote down vote up
def setUp(self):
        """
        Defining the exitcodes

        Expected Exitcodes:
        On Unix, the return value is the exit status of the
        process encoded in the format specified for wait()
        https://docs.python.org/3.5/library/os.html#os.system
        """

        self.exit_0 = 0 << 8
        self.exit_1 = 1 << 8
        self.exit_2 = 2 << 8 
Example 11
Project: jak   Author: dispel   File: helpers.py    (license) View Source Project 5 votes vote down vote up
def generate_256bit_key():
    """Generate a pseudo-random secure ready-for-crypto-use key.

    Generate it straight using urandom. Proving randomness is impossible, and a good source
    is a hotly debated subject. As always, opinions are welcome but please inform
    yourself first and be prepared to cite a source.

    Further Reading:
    https://docs.python.org/3.5/library/os.html#os.urandom
    https://docs.python.org/2.7/library/os.html#os.urandom
    https://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers/
    http://www.2uo.de/myths-about-urandom/
    https://github.com/dlitz/pycrypto/blob/master/lib/Crypto/Random/__init__.py
    """
    return binascii.hexlify(os.urandom(32)) 
Example 12
Project: respeaker_virtualenv   Author: respeaker   File: common.py    (license) View Source Project 5 votes vote down vote up
def html_escape(u):
    """
    An html escape function that behaves the same in both Python 2 and 3.

    This function is needed because single quotes are escaped in Python 3
    (to '&#x27;'), but not in Python 2.

    The global defaults.TAG_ESCAPE can be set to this function in the
    setUp() and tearDown() of unittest test cases, for example, for
    consistent test results.

    """
    u = _DEFAULT_TAG_ESCAPE(u)
    return u.replace("'", '&#x27;') 
Example 13
Project: driveboardapp   Author: nortd   File: compat.py    (license) View Source Project 5 votes vote down vote up
def system():
    # On some Windows installation (Python 2.4) platform.system() is
    # broken and incorrectly returns 'Microsoft' instead of 'Windows'.
    # http://mail.python.org/pipermail/patches/2007-June/022947.html
    syst = platform.system()
    if syst == 'Microsoft':
        return 'Windows'
    return syst 
Example 14
Project: driveboardapp   Author: nortd   File: compat.py    (license) View Source Project 5 votes vote down vote up
def machine():
    """
    Return machine suffix to use in directory name when looking
    for bootloader.

    PyInstaller is reported to work even on ARM architecture. For that
    case functions system() and architecture() are not enough.
    Path to bootloader has to be composed from system(), architecture()
    and machine() like:
        'Linux-32bit-arm'
    """
    mach = platform.machine()
    if mach.startswith('arm'):
        return 'arm'
    else:
        # Assume x86/x86_64 machine.
        return None


# Set and get environment variables does not handle unicode strings correctly
# on Windows.

# Acting on os.environ instead of using getenv()/setenv()/unsetenv(),
# as suggested in <http://docs.python.org/library/os.html#os.environ>:
# "Calling putenv() directly does not change os.environ, so it's
# better to modify os.environ." (Same for unsetenv.) 
Example 15
Project: sff   Author: nevarezs   File: listcontacts.py    (license) View Source Project 5 votes vote down vote up
def run(self):
        """Run module's code."""
        contacts = self._list_contacts()

        # Output
        title = "Contacts"
        header = None
        output_format = self.get_option_value("OUTPUT_FORMAT").lower()
        if (output_format == "stdout"):
            print title
            self._print_table(contacts)
        elif (output_format == "html"):
            if (not os.path.isdir(self.output_dir)):
                os.mkdir(self.output_dir)
            output_prefix = self.get_option_value("OUTPUT_FILE_NAME_PREFIX")
            file_full_path = self.output_dir + "/" + output_prefix + ".html"
            html.create_document_from_row_list(title,
                header,
                contacts,
                file_full_path)
            print "Output saved to: " + file_full_path
        elif (output_format == "pdf"):
            if (not os.path.isdir(self.output_dir)):
                os.mkdir(self.output_dir)
            output_prefix = self.get_option_value("OUTPUT_FILE_NAME_PREFIX")
            file_full_path = self.output_dir + "/" + output_prefix + ".pdf"
            pdf.create_document_from_row_list(title,
                header,
                contacts,
                file_full_path)
            print "Output saved to: " + file_full_path
        else:
            print "Unsupported OUTPUT_FORMAT"

    # ***************************************************************
    # HELPER methods
    # *************************************************************** 
Example 16
Project: sff   Author: nevarezs   File: listconversations.py    (license) View Source Project 5 votes vote down vote up
def run(self):
        """Run module's code."""
        conversations = self._get_conversation_list()

        # Output
        title = "Conversation List"
        header = None
        output_format = self.get_option_value("OUTPUT_FORMAT").lower()
        if (output_format == "stdout"):
            print title
            self._print_table(conversations)
        elif (output_format == "html"):
            if (not os.path.isdir(self.output_dir)):
                os.mkdir(self.output_dir)
            output_prefix = self.get_option_value("OUTPUT_FILE_NAME_PREFIX")
            file_full_path = self.output_dir + "/" + output_prefix + ".html"
            html.create_document_from_row_list(title,
                header,
                conversations,
                file_full_path)
            print "Output saved to: " + file_full_path
        elif (output_format == "pdf"):
            if (not os.path.isdir(self.output_dir)):
                os.mkdir(self.output_dir)
            output_prefix = self.get_option_value("OUTPUT_FILE_NAME_PREFIX")
            file_full_path = self.output_dir + "/" + output_prefix + ".pdf"
            pdf.create_document_from_row_list(title,
                header,
                conversations,
                file_full_path)
            print "Output saved to: " + file_full_path
        else:
            print "Unsupported OUTPUT_FORMAT"

    # ***************************************************************
    # HELPER methods
    # *************************************************************** 
Example 17
Project: cmdchallenge-site   Author: jarv   File: utils.py    (license) View Source Project 5 votes vote down vote up
def get_paths(root, exclude_patterns, include_patterns, has_exceptions=False):
    paths = []

    for parent, dirs, files in os.walk(root, topdown=True, followlinks=False):
        parent = os.path.relpath(parent, root)
        if parent == '.':
            parent = ''

        # Remove excluded patterns from the list of directories to traverse
        # by mutating the dirs we're iterating over.
        # This looks strange, but is considered the correct way to skip
        # traversal. See https://docs.python.org/2/library/os.html#os.walk
        dirs[:] = [d for d in dirs if
                   should_check_directory(os.path.join(parent, d),
                                          exclude_patterns, include_patterns)]

        for path in dirs:
            if should_include(os.path.join(parent, path),
                              exclude_patterns, include_patterns):
                paths.append(os.path.join(parent, path))

        for path in files:
            if should_include(os.path.join(parent, path),
                              exclude_patterns, include_patterns):
                paths.append(os.path.join(parent, path))

    return paths 
Example 18
Project: OVERWATCH   Author: raymondEhlers   File: fileToHTTP.py    (license) View Source Project 5 votes vote down vote up
def FileWithLocalFilename(filename, writeFile = False):
    with tempfile.NamedTemporaryFile() as f:
        try:
            with FileInMemory(filename) as (success, status, fileInMemory):
                if success:
                    print("Writing to temporary file")
                    print("success: {}, status: {}".format(success, status))
                    f.write(fileInMemory.read())
                    f.flush()
                    #f.write("Hello")
                    # Return to start of file so the read is seamless
                    f.seek(0)
                    # May be required to fully flush, although flush() seems sufficient for now
                    # See: https://docs.python.org/2/library/os.html#os.fsync
                    #os.fsync(f.fileno())
                    #print("f.read(): {}".format(f.read()))
                    #f.seek(0)

                    yield f.name
                    #print("Post yield")
                    #f.seek(0, os.SEEK_END)
                    #print("f length in with def: {}".format(f.tell()))
                else:
                    #yield (False, status, fileInMemory)
                    yield False
                print("Successfully completed FileWithLocalFilename")
        except IOError as e:
            # Just need an exception so that else is valid.
            print("IOError: {}".format(e))
        else:
            # Only do this if there are no exceptions above
            print("Potentially writing file")
            if writeFile:
                (success, status, returnValue) = putFile(filename = filename, file = f)
                print("Wrote file. success: {}, status: {}, returnValue: {}".format(success, status, returnValue))
        finally:
            print("Finally exiting from FileWithLocalFilename") 
Example 19
Project: mac-package-build   Author: persepolisdm   File: compat.py    (license) View Source Project 5 votes vote down vote up
def system():
    # On some Windows installation (Python 2.4) platform.system() is
    # broken and incorrectly returns 'Microsoft' instead of 'Windows'.
    # http://mail.python.org/pipermail/patches/2007-June/022947.html
    syst = platform.system()
    if syst == 'Microsoft':
        return 'Windows'
    return syst 
Example 20
Project: mac-package-build   Author: persepolisdm   File: compat.py    (license) View Source Project 5 votes vote down vote up
def machine():
    """
    Return machine suffix to use in directory name when looking
    for bootloader.

    PyInstaller is reported to work even on ARM architecture. For that
    case functions system() and architecture() are not enough.
    Path to bootloader has to be composed from system(), architecture()
    and machine() like:
        'Linux-32bit-arm'
    """
    mach = platform.machine()
    if mach.startswith('arm'):
        return 'arm'
    else:
        # Assume x86/x86_64 machine.
        return None


# Set and get environment variables does not handle unicode strings correctly
# on Windows.

# Acting on os.environ instead of using getenv()/setenv()/unsetenv(),
# as suggested in <http://docs.python.org/library/os.html#os.environ>:
# "Calling putenv() directly does not change os.environ, so it's
# better to modify os.environ." (Same for unsetenv.) 
Example 21
Project: DawnlightSearch   Author: chg-hou   File: update_db_module.py    (license) View Source Project 4 votes vote down vote up
def update_uuid(self):
        self.mutex.lock()

        # https://docs.python.org/2/library/sqlite3.html
        try:
            cur = MainCon.cur
            cur.execute('''
                SELECT uuid FROM `UUID` ;
                ''')
            uuid_in_db = cur.fetchall()
            self.mutex.unlock()
            uuids = [x[0] for x in uuid_in_db]
            logger.debug('SystemDevices.refresh_state')
            SystemDevices.refresh_state()
            logger.debug('SystemDevices.refresh_state END')
            deviceDict = SystemDevices.deviceDict
            uuid_list = []
            for dev_id, dev in deviceDict.items():
                uuid = dev['uuid']
                fstype = dev['fstype']
                label = dev['label']
                major_dnum, minor_dnum = dev_id
                uuid_list.append(uuid)
                if not uuid in uuids:
                    cur.execute('''INSERT INTO UUID (included, uuid, alias, fstype,name,label,major_dnum,minor_dnum, path)
                                 VALUES (?, ?,   ?,  ?,   ?,    ?,    ?,   ?, ?)''',
                                (False, dev['uuid'], dev['mountpoint'], dev['fstype'], dev['name'], dev['label'],
                                 major_dnum, minor_dnum, dev['mountpoint']))
                else:
                    cur.execute('''UPDATE  UUID SET fstype=?,name=?,label=?,major_dnum=?,minor_dnum=?,path=?
                        WHERE uuid=?''',
                                (dev['fstype'], dev['name'], dev['label'],
                                 major_dnum, minor_dnum, dev['mountpoint'],
                                 dev['uuid'])
                                )
            MainCon.con.commit()

        except Exception as e:
            self.show_statusbar_warning_msg_SIGNAL.emit(str(e))
            logger.error(str(e))

        self.mutex.unlock()
        logger.debug('init_table')
        for uuid in uuid_list:
            self.init_table(uuid, clear_table=False)
        logger.debug('init_table END')
        try:
            MainCon.con.commit()
        except Exception as e:
            logger.error(str(e))
            # self.con.commit()  # commit 
Example 22
Project: deb-python-dcos   Author: openstack   File: test_service.py    (license) View Source Project 4 votes vote down vote up
def test_log_follow():
    package_install('chronos', deploy=True)

    args = ['dcos', 'service', 'log', 'chronos', '--follow']

    if sys.platform == 'win32':
        proc = subprocess.Popen(
            args,
            creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
    else:
        # disable stdout/stderr buffering:
        # https://docs.python.org/3/using/cmdline.html#cmdoption-u
        my_env = os.environ.copy()
        my_env['PYTHONUNBUFFERED'] = 'x'

        # os.setsid is only available for Unix:
        # https://docs.python.org/2/library/os.html#os.setsid
        proc = subprocess.Popen(
            args,
            preexec_fn=os.setsid,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            env=my_env)

    time.sleep(10)

    proc.poll()
    assert proc.returncode is None

    if sys.platform == 'win32':
        os.kill(proc.pid, signal.CTRL_BREAK_EVENT)
    else:
        # using Unix-only commands os.killpg + os.getgid
        # https://docs.python.org/2/library/os.html#os.killpg
        # https://docs.python.org/2/library/os.html#os.getpgid
        os.killpg(os.getpgid(proc.pid), 15)

    stdout = proc.stdout.read()
    stderr = proc.stderr.read()

    print('STDOUT: {}'.format(stdout))
    print('STDERR: {}'.format(stderr))
    assert len(stdout.decode('utf-8').split('\n')) > 3

    assert_lines(['dcos', 'service', 'log', 'chronos', '--lines=4'], 4)

    exec_command(['dcos', 'package', 'uninstall', 'chronos', '--yes']) 
Example 23
Project: ClockworkVMs   Author: csd-dev-tools   File: buildlib.py    (license) View Source Project 4 votes vote down vote up
def chmodR(self, perm, target, writemode):
        '''
        Recursively apply chmod to a directory

        @author: Eric Ball
        @param perm: Permissions to be applied. For information on available
                     permissions/modes, see os.chmod documentation at
                     https://docs.python.org/2/library/os.html#os.chmod
        @param target: Target directory
        @param writemode: [a]ppend or [o]verwrite
        '''
        try:
            if not os.path.isdir(target):
                raise TypeError(target)
            else:
                try:
                    if writemode[0] == "a":
                        for root, dirs, files in os.walk(target):
                            # Change permissions for root directory
                            currentPerm = os.stat(root)[0]
                            newPerm = currentPerm | perm
                            os.chmod(root, newPerm)
                            # Change permissions for child directories
                            for mydir in dirs:
                                currentPerm = os.stat(os.path.join(root, mydir))[0]
                                newPerm = currentPerm | perm
                                os.chmod(os.path.join(root, mydir), newPerm)
                            # Change permissions for all files
                            for myfile in files:
                                currentPerm = os.stat(os.path.join(root,
                                                                   myfile))[0]
                                newPerm = currentPerm | perm
                                os.chmod(os.path.join(root, myfile), newPerm)
                    elif writemode[0] == "o":
                        for root, dirs, files in os.walk(target):
                            # Change permissions for root directory
                            os.chmod(root, perm)
                            # Change permissions for child directories
                            for mydir in dirs:
                                os.chmod(os.path.join(root, mydir), perm)
                            # Change permissions for all files
                            for myfile in files:
                                os.chmod(os.path.join(root, myfile), perm)
                    else:
                        raise NameError(writemode)
                except NameError:
                    raise
        except TypeError:
            print "Error: Cannot chmodR target, must be a directory"
            raise
        except NameError:
            print "Error: Invalid writemode specified. Please use [a]ppend " + \
                "or [o]verwrite"
            raise
        except Exception:
            raise