pydu/docs/system.rst

159 lines
5.7 KiB
ReStructuredText
Raw Normal View History

2017-12-23 06:17:39 +00:00
System
------
2017-12-04 14:45:17 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.makedirs(path, mode=0o755, ignore_errors=False, exist_ok=False)
2017-12-04 14:45:17 +00:00
2017-12-06 15:58:06 +00:00
Based on ``os.makedirs``,create a leaf directory and all intermediate ones.
``mode`` default is ``0o755``. When make an exists path, if exist_ok is false,
``makedirs`` will raise an ``Exception``. If ``ignore_errors`` which will ignore
all errors raised by ``os.makedirs``.
2017-12-04 14:45:17 +00:00
2017-12-23 06:17:39 +00:00
>>> from pydu.system import makedirs
2017-12-05 15:38:21 +00:00
>>> makedirs('test1/test2')
>>> makedirs('test1',exist_ok=True)
>>> makedirs('test1')
2017-12-04 14:45:17 +00:00
Traceback (most recent call last):
2017-12-06 16:02:29 +00:00
... OSError: Create dir: test1 error.
2017-12-04 14:45:17 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.remove(path, mode=0o755, ignore_errors=False, onerror)
2017-12-06 15:58:06 +00:00
2017-12-05 15:38:21 +00:00
Remove a file or directory.
2017-12-09 15:57:40 +00:00
If ``ignore_errors`` is set, errors are ignored; otherwise, if `onerror`
2017-12-09 15:50:44 +00:00
is set, it is called to handle the error with arguments (`func` ,
`path` , `exc_info` ) where func is platform and implementation dependent;
`path` is the argument to that function that caused it to fail; and
`exc_info` is a tuple returned by `sys.exc_info()`. If `ignore_errors`
is `False` and `onerror` is None, an exception is raised.
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
>>> from pydu.system import makedirs
>>> from pydu.system import remove
>>> from pydu.system import open_file
2017-12-05 15:38:21 +00:00
>>> makedirs('test1')
>>> remove('test1')
>>> open_file('test.txt')
>>> remove('test.txt')
>>> remove('test',ignore_errors=True)
>>> remove('test')
Traceback (most recent call last):
2017-12-06 16:02:29 +00:00
... OSError: Remove path: test error
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.removes(paths, mode=0o755, ignore_errors=False, onerror)
2017-12-05 15:38:21 +00:00
2017-12-06 15:58:06 +00:00
Remove a list of file and/or directory.Other parameters same as `remove`.
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
>>> from pydu.system import makedirs
>>> from pydu.system import remove
>>> from pydu.system import open_file
2017-12-05 15:38:21 +00:00
>>> makedirs('test1')
>>> makedirs('test2')
>>> open_file('test.txt')
>>> removes(['test.txt','test1','test2'])
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.open_file(path, mode='wb+', buffer_size=-1, ignore_errors=False):
2017-12-05 15:38:21 +00:00
2017-12-06 15:58:06 +00:00
Open a file, defualt mode ``wb+``. If path not exists, it will be created
automatically. If ``ignore_errors`` is set, errors are ignored.
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
>>> from pydu.system import open_file
2017-12-05 15:38:21 +00:00
>>> open_file('test.txt')
>>> ls
test.txt
>>> open_file('test1.txt',mode='r')
Traceback (most recent call last):
2017-12-06 16:02:29 +00:00
... OSError: Open file: test1.txt error
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.copy(src, dst, ignore_errors=False, follow_symlinks=True):
2017-12-05 15:38:21 +00:00
Copy data and mode bits (`cp src dst`).Both the source and destination
may be a directory.When `copy` a directory,which contains a symlink,if
the optional symlinks flag is true, symbolic links in the source tree
result in symbolic links in the destination tree; if it is false, the
contents of the files pointed to by symbolic links are copied.When copy
a file,if follow_symlinks is false and src is a symbolic link, a new
symlink will be created instead of copying the file it points to,else
the contents of the file pointed to by symbolic links is copied.
2017-12-23 06:17:39 +00:00
>>> from pydu.system import copy,symlink
>>> from pydu.system import makedirs,open_fle
2017-12-05 15:38:21 +00:00
>>> open_fle('test/test.txt')
>>> symlink('test/test.txt','test/test.link')
>>> copy('test/test.link','test/test_copy1.link')
2017-12-06 13:42:05 +00:00
>>> copy('test/test.link','test/test_copy2.link',follow_symlink=False)
2017-12-05 15:38:21 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.touch(path):
2017-12-10 14:39:39 +00:00
Open a file as write,and then close it.
2017-12-23 06:17:39 +00:00
>>> from pydu.system import touch
>>> touch('test.txt')
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.symlink(src, dst, overwrite=False, ignore_errors=False)
2017-12-14 12:01:15 +00:00
``symlink`` only work on `Unix-like` system, it create a symbolic link pointing
to source named link_name.If dist is exist and overwrite is true,a new
symlink will be created.
2017-12-23 06:17:39 +00:00
>>> from pydu.system import symlink
>>> symlink('test.txt','test.link')
2017-12-14 12:01:15 +00:00
.. note:: ``symlink`` can only be used on ``unix-like`` system.
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.link(src, dst, overwrite=False, ignore_errors=False):
2017-12-06 15:58:06 +00:00
``link`` only work on `Unix-like` system, it create a hard link pointing to
source named link_name.If dist is exist and overwrite is true,a
2017-12-09 15:50:44 +00:00
new link will be created.
2017-12-23 06:17:39 +00:00
>>> from pydu.system import link
>>> link('test.txt','test.link')
2017-12-14 12:01:15 +00:00
.. note:: ``link`` can only be used on ``unix-like`` system.
2017-12-19 00:19:13 +00:00
2017-12-23 06:17:39 +00:00
.. py:function:: pydu.system.which(cmd, mode=os.F_OK | os.X_OK, path=None):
2017-12-19 00:19:13 +00:00
Given a command, mode, and a PATH string, return the path which
conforms to the given mode on the PATH, or None if there is no such
file.
``mode`` defaults to os.F_OK | os.X_OK. ``path`` defaults to the result
of os.environ.get("PATH"), or can be overridden with a custom search
path.
`which` is `shutil.which` in Python 3.
2017-12-23 06:17:39 +00:00
>>> from pydu.system import which
2017-12-19 00:19:13 +00:00
>>> which('echo')
/bin/echo
2017-12-23 06:22:54 +00:00
.. py:function:: pydu.cmd.chmod(path, mode, recursive=False)
Change permissions to the given mode.
If ``recursive`` is True perform recursively.
>>> from pydu.system import chmod
>>> chmod('/opt/sometest', 0o744)
>>> oct(os.stat('/opt/sometest').st_mode)[-3:]
'744'
.. note:: Although Windows supports ``chmod``, you can only set the files
read-only flag with it (via the stat.S_IWRITE and stat.S_IREAD constants
or a corresponding integer value). All other bits are ignored.
2017-12-23 06:22:54 +00:00
.. py:function:: pydu.cmd.chcp(code)
Context manager which sets the active code page number.
It could also be used as function.
>>> from pydu.cmd import chcp
>>> chcp(437)
2017-12-23 10:12:53 +00:00
<active code page number: 437>
2017-12-23 06:22:54 +00:00
>>> with chcp(437):
... pass
>>>
.. note:: ``chcp`` can only be used on ``Windows`` system.